<s> from zipfile import ZipFile <EOL> from cStringIO import StringIO <EOL> from django . contrib . gis . geos import Polygon <EOL> from lxml import etree <EOL> from . import Driver <EOL> from osgeo import osr <EOL> import re <EOL> from PIL import Image <EOL> class KmzDriver ( Driver ) : <EOL> @ classmethod <EOL> def supports_multiple_layers ( cls ) : <EOL> return False <EOL> @ classmethod <EOL> def supports_configuration ( cls ) : <EOL> return False <EOL> def supports_point_query ( cls ) : <EOL> return False <EOL> def supports_related ( cls ) : <EOL> return False <EOL> def ready_data_resource ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> slug , srs = super ( KmzDriver , self ) . ready_data_resource ( ** kwargs ) <EOL> return slug , srs , { <EOL> '<STR_LIT:type>' : '<STR_LIT>' , <EOL> "<STR_LIT:file>" : self . get_filename ( '<STR_LIT>' ) <EOL> } <EOL> def compute_fields ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> super ( KmzDriver , self ) . compute_fields ( ** kwargs ) <EOL> srs = osr . SpatialReference ( ) <EOL> srs . ImportFromEPSG ( <NUM_LIT> ) <EOL> self . resource . spatial_metadata . native_srs = srs . ExportToProj4 ( ) <EOL> self . resource . spatial_metadata . bounding_box = Polygon . from_bbox ( ( - <NUM_LIT> , - <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) ) <EOL> self . resource . spatial_metadata . native_bounding_box = Polygon . from_bbox ( ( - <NUM_LIT> , - <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) ) <EOL> self . resource . spatial_metadata . three_d = False <EOL> self . resource . spatial_metadata . save ( ) <EOL> self . resource . save ( ) <EOL> def open_stream ( self , filename ) : <EOL> archive = ZipFile ( self . get_filename ( '<STR_LIT>' ) ) <EOL> return archive . open ( filename ) <EOL> def features ( self ) : <EOL> """<STR_LIT>""" <EOL> archive = ZipFile ( self . get_filename ( '<STR_LIT>' ) ) <EOL> with archive . open ( '<STR_LIT>' ) as kml : <EOL> kks = kml . read ( ) <EOL> kks = re . sub ( r'<STR_LIT>' , r'<STR_LIT>' , kks ) <EOL> kks = re . sub ( r'<STR_LIT>' , r'<STR_LIT>' , kks ) <EOL> return kks <EOL> def ground_overlays ( self ) : <EOL> ground_overlays = [ ] <EOL> archive = ZipFile ( self . get_filename ( '<STR_LIT>' ) ) <EOL> on = False <EOL> with archive . open ( '<STR_LIT>' ) as kml : <EOL> kml = StringIO ( kml . read ( ) . replace ( '<STR_LIT>' , "<STR_LIT>" ) . replace ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> for event , elt in etree . iterparse ( kml , events = [ '<STR_LIT:start>' , '<STR_LIT:end>' ] , remove_comments = True , remove_blank_text = True ) : <EOL> print event , elt . tag <EOL> if event == '<STR_LIT:start>' : <EOL> if elt . tag == '<STR_LIT>' : <EOL> ground_overlays . append ( { } ) <EOL> on = True <EOL> elif on and elt . tag in { '<STR_LIT:name>' , '<STR_LIT>' } : <EOL> ground_overlays [ - <NUM_LIT:1> ] [ elt . tag ] = elt . text <EOL> elif on and elt . tag in { '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' } : <EOL> ground_overlays [ - <NUM_LIT:1> ] [ elt . tag ] = float ( elt . text ) <EOL> on = on and not ( event == '<STR_LIT:end>' and elt . tag == '<STR_LIT>' ) <EOL> for i , ol in enumerate ( ground_overlays ) : <EOL> ground_overlays [ i ] [ '<STR_LIT:width>' ] , ground_overlays [ i ] [ '<STR_LIT>' ] = Image . open ( StringIO ( self . open_stream ( ol [ '<STR_LIT>' ] ) . read ( ) ) ) . size <EOL> return ground_overlays <EOL> def summary ( self , * args , ** kwargs ) : <EOL> return [ ] <EOL> driver = KmzDriver </s>
