<s> import os <EOL> import shutil <EOL> import zipfile <EOL> import tempfile <EOL> from django . contrib . gis import forms <EOL> from django . contrib . gis import gdal <EOL> from django . contrib . gis . gdal . srs import SpatialReference , SRSException <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from greenwich . geometry import Envelope <EOL> from spillway . compat import json <EOL> class CommaSepFloatField ( forms . FloatField ) : <EOL> """<STR_LIT>""" <EOL> default_error_messages = { <EOL> '<STR_LIT>' : _ ( u'<STR_LIT>' ) , <EOL> '<STR_LIT>' : _ ( u'<STR_LIT>' ) , <EOL> '<STR_LIT>' : _ ( u'<STR_LIT>' ) , <EOL> } <EOL> def to_python ( self , value ) : <EOL> "<STR_LIT>" <EOL> if not value : <EOL> return [ ] <EOL> return map ( super ( CommaSepFloatField , self ) . to_python , value . split ( '<STR_LIT:U+002C>' ) ) <EOL> def run_validators ( self , values ) : <EOL> """<STR_LIT>""" <EOL> for val in values : <EOL> super ( CommaSepFloatField , self ) . run_validators ( val ) <EOL> class BoundingBoxField ( CommaSepFloatField ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , srid = <NUM_LIT> , * args , ** kwargs ) : <EOL> super ( BoundingBoxField , self ) . __init__ ( * args , ** kwargs ) <EOL> self . srid = srid <EOL> def to_python ( self , value ) : <EOL> """<STR_LIT>""" <EOL> value = super ( BoundingBoxField , self ) . to_python ( value ) <EOL> try : <EOL> bbox = gdal . OGRGeometry . from_bbox ( value ) . geos <EOL> except ( ValueError , AttributeError ) : <EOL> return [ ] <EOL> bbox . srid = self . srid <EOL> return bbox <EOL> class GeometryFileField ( forms . FileField ) : <EOL> """<STR_LIT>""" <EOL> def to_python ( self , value ) : <EOL> value = super ( GeometryFileField , self ) . to_python ( value ) <EOL> if value is None : <EOL> return value <EOL> filename = value . name <EOL> tmpdir = None <EOL> if zipfile . is_zipfile ( value ) : <EOL> tmpdir = tempfile . mkdtemp ( ) <EOL> zf = zipfile . ZipFile ( value ) <EOL> for item in zf . namelist ( ) : <EOL> filename = os . path . join ( tmpdir , os . path . basename ( item ) ) <EOL> with open ( filename , '<STR_LIT:wb>' ) as f : <EOL> f . write ( zf . read ( item ) ) <EOL> if filename . endswith ( '<STR_LIT>' ) : <EOL> break <EOL> try : <EOL> geoms = gdal . DataSource ( filename ) [ <NUM_LIT:0> ] . get_geoms ( ) <EOL> geom = reduce ( lambda g1 , g2 : g1 . union ( g2 ) , geoms ) <EOL> if not geom . srs : <EOL> raise gdal . OGRException ( '<STR_LIT>' ) <EOL> except ( gdal . OGRException , gdal . OGRIndexError ) : <EOL> geom = None <EOL> finally : <EOL> if tmpdir and os . path . isdir ( tmpdir ) : <EOL> shutil . rmtree ( tmpdir ) <EOL> return geom <EOL> class OGRGeometryField ( forms . GeometryField ) : <EOL> """<STR_LIT>""" <EOL> def to_python ( self , value ) : <EOL> if value in self . empty_values : <EOL> return None <EOL> if '<STR_LIT>' in value : <EOL> d = json . loads ( value ) <EOL> value = json . dumps ( d . get ( '<STR_LIT>' ) ) <EOL> elif list ( value ) . count ( '<STR_LIT:U+002C>' ) == <NUM_LIT:3> : <EOL> value = Envelope ( value . split ( '<STR_LIT:U+002C>' ) ) . polygon . ExportToWkt ( ) <EOL> try : <EOL> geom = gdal . OGRGeometry ( value ) <EOL> except ( gdal . OGRException , TypeError , ValueError ) : <EOL> raise forms . ValidationError ( self . error_messages [ '<STR_LIT>' ] ) <EOL> if not geom . srs : <EOL> geom . srid = self . srid or self . widget . map_srid <EOL> return geom <EOL> class SpatialReferenceField ( forms . IntegerField ) : <EOL> """<STR_LIT>""" <EOL> def to_python ( self , value ) : <EOL> value = super ( SpatialReferenceField , self ) . to_python ( value ) <EOL> try : <EOL> return SpatialReference ( value ) <EOL> except ( SRSException , TypeError ) : <EOL> return None </s>
