<s> from __future__ import unicode_literals <EOL> from django import forms <EOL> from django . contrib . gis . geos import GEOSException , GEOSGeometry <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from . widgets import OpenLayersWidget <EOL> class GeometryField ( forms . Field ) : <EOL> """<STR_LIT>""" <EOL> widget = OpenLayersWidget <EOL> geom_type = '<STR_LIT>' <EOL> default_error_messages = { <EOL> '<STR_LIT>' : _ ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : _ ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : _ ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> } <EOL> def __init__ ( self , ** kwargs ) : <EOL> self . srid = kwargs . pop ( '<STR_LIT>' , None ) <EOL> self . geom_type = kwargs . pop ( '<STR_LIT>' , self . geom_type ) <EOL> super ( GeometryField , self ) . __init__ ( ** kwargs ) <EOL> self . widget . attrs [ '<STR_LIT>' ] = self . geom_type <EOL> def to_python ( self , value ) : <EOL> """<STR_LIT>""" <EOL> if value in self . empty_values : <EOL> return None <EOL> if not isinstance ( value , GEOSGeometry ) : <EOL> try : <EOL> value = GEOSGeometry ( value ) <EOL> except ( GEOSException , ValueError , TypeError ) : <EOL> raise forms . ValidationError ( self . error_messages [ '<STR_LIT>' ] , code = '<STR_LIT>' ) <EOL> if not value . srid : <EOL> try : <EOL> value . srid = self . widget . map_srid <EOL> except AttributeError : <EOL> if self . srid : <EOL> value . srid = self . srid <EOL> return value <EOL> def clean ( self , value ) : <EOL> """<STR_LIT>""" <EOL> geom = super ( GeometryField , self ) . clean ( value ) <EOL> if geom is None : <EOL> return geom <EOL> if str ( geom . geom_type ) . upper ( ) != self . geom_type and not self . geom_type == '<STR_LIT>' : <EOL> raise forms . ValidationError ( self . error_messages [ '<STR_LIT>' ] , code = '<STR_LIT>' ) <EOL> if self . srid and self . srid != - <NUM_LIT:1> and self . srid != geom . srid : <EOL> try : <EOL> geom . transform ( self . srid ) <EOL> except GEOSException : <EOL> raise forms . ValidationError ( <EOL> self . error_messages [ '<STR_LIT>' ] , code = '<STR_LIT>' ) <EOL> return geom <EOL> def has_changed ( self , initial , data ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> data = self . to_python ( data ) <EOL> initial = self . to_python ( initial ) <EOL> except forms . ValidationError : <EOL> return True <EOL> if initial and data : <EOL> data . transform ( initial . srid ) <EOL> return not initial . equals_exact ( data , tolerance = <NUM_LIT> ) <EOL> else : <EOL> return bool ( initial ) != bool ( data ) <EOL> class GeometryCollectionField ( GeometryField ) : <EOL> geom_type = '<STR_LIT>' <EOL> class PointField ( GeometryField ) : <EOL> geom_type = '<STR_LIT>' <EOL> class MultiPointField ( GeometryField ) : <EOL> geom_type = '<STR_LIT>' <EOL> class LineStringField ( GeometryField ) : <EOL> geom_type = '<STR_LIT>' <EOL> class MultiLineStringField ( GeometryField ) : <EOL> geom_type = '<STR_LIT>' <EOL> class PolygonField ( GeometryField ) : <EOL> geom_type = '<STR_LIT>' <EOL> class MultiPolygonField ( GeometryField ) : <EOL> geom_type = '<STR_LIT>' </s>
