<s> """<STR_LIT>""" <EOL> from sys import stderr <EOL> from math import floor , log <EOL> from os import unlink , close , write , chmod , makedirs <EOL> from os . path import basename , exists , isdir , join <EOL> from httplib import HTTPConnection <EOL> from urlparse import urlparse <EOL> from tempfile import mkstemp <EOL> from zipfile import ZipFile <EOL> from hashlib import md5 <EOL> from TileStache . Geography import SphericalMercator <EOL> from osgeo import gdal , osr <EOL> ideal_zoom = <NUM_LIT> <EOL> osr . UseExceptions ( ) <EOL> sref = osr . SpatialReference ( ) <EOL> sref . ImportFromProj4 ( '<STR_LIT>' ) <EOL> def region ( lat , lon ) : <EOL> """<STR_LIT>""" <EOL> if <NUM_LIT> <= lat and lat < <NUM_LIT:50> and - <NUM_LIT> <= lon and lon < - <NUM_LIT> : <EOL> return <NUM_LIT:1> <EOL> if <NUM_LIT> <= lat and lat < <NUM_LIT:50> and - <NUM_LIT> <= lon and lon < - <NUM_LIT> : <EOL> return <NUM_LIT:2> <EOL> if <NUM_LIT> <= lat and lat < <NUM_LIT:50> and - <NUM_LIT> <= lon and lon < - <NUM_LIT> : <EOL> return <NUM_LIT:3> <EOL> if <NUM_LIT> <= lat and lat < <NUM_LIT> and - <NUM_LIT> <= lon and lon < - <NUM_LIT:100> : <EOL> return <NUM_LIT:4> <EOL> if <NUM_LIT> <= lat and lat < <NUM_LIT> and - <NUM_LIT:100> <= lon and lon < - <NUM_LIT> : <EOL> return <NUM_LIT:5> <EOL> if <NUM_LIT> <= lat and lat < <NUM_LIT> and - <NUM_LIT> <= lon and lon < - <NUM_LIT:64> : <EOL> return <NUM_LIT:6> <EOL> if - <NUM_LIT:15> <= lat and lat < <NUM_LIT> and ( ( <NUM_LIT> <= lon and lon < <NUM_LIT> ) or ( - <NUM_LIT> <= lon and lon < - <NUM_LIT> ) ) : <EOL> return <NUM_LIT:7> <EOL> raise ValueError ( '<STR_LIT>' % ( lat , lon ) ) <EOL> def quads ( minlon , minlat , maxlon , maxlat ) : <EOL> """<STR_LIT>""" <EOL> lon = floor ( minlon ) <EOL> while lon <= maxlon : <EOL> lat = floor ( minlat ) <EOL> while lat <= maxlat : <EOL> yield lon , lat <EOL> lat += <NUM_LIT:1> <EOL> lon += <NUM_LIT:1> <EOL> def datasource ( lat , lon , source_dir ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> reg = region ( lat , lon ) <EOL> except ValueError : <EOL> return None <EOL> fmt = '<STR_LIT>' <EOL> url = fmt % ( reg , abs ( lat ) , abs ( lon ) ) <EOL> s , host , path , p , q , f = urlparse ( url ) <EOL> dem_dir = md5 ( url ) . hexdigest ( ) [ : <NUM_LIT:3> ] <EOL> dem_dir = join ( source_dir , dem_dir ) <EOL> dem_path = join ( dem_dir , basename ( path ) [ : - <NUM_LIT:4> ] ) <EOL> dem_none = dem_path [ : - <NUM_LIT:4> ] + '<STR_LIT>' <EOL> if exists ( dem_path ) : <EOL> return gdal . Open ( dem_path , gdal . GA_ReadOnly ) <EOL> if exists ( dem_none ) : <EOL> return None <EOL> if not exists ( dem_dir ) : <EOL> makedirs ( dem_dir ) <EOL> chmod ( dem_dir , <NUM_LIT:0> <NUM_LIT> ) <EOL> assert isdir ( dem_dir ) <EOL> print >> stderr , '<STR_LIT>' , url , '<STR_LIT>' <EOL> conn = HTTPConnection ( host , <NUM_LIT> ) <EOL> conn . request ( '<STR_LIT:GET>' , path ) <EOL> resp = conn . getresponse ( ) <EOL> if resp . status == <NUM_LIT> : <EOL> print >> open ( dem_none , '<STR_LIT:w>' ) , url <EOL> return None <EOL> assert resp . status == <NUM_LIT:200> , ( resp . status , resp . read ( ) ) <EOL> try : <EOL> handle , zip_path = mkstemp ( prefix = '<STR_LIT>' , suffix = '<STR_LIT>' ) <EOL> write ( handle , resp . read ( ) ) <EOL> close ( handle ) <EOL> zipfile = ZipFile ( zip_path , '<STR_LIT:r>' ) <EOL> dem_file = open ( dem_path , '<STR_LIT:w>' ) <EOL> dem_file . write ( zipfile . read ( zipfile . namelist ( ) [ <NUM_LIT:0> ] ) ) <EOL> dem_file . close ( ) <EOL> chmod ( dem_path , <NUM_LIT:0> <NUM_LIT> ) <EOL> finally : <EOL> unlink ( zip_path ) <EOL> return gdal . Open ( dem_path , gdal . GA_ReadOnly ) <EOL> def datasources ( minlon , minlat , maxlon , maxlat , source_dir ) : <EOL> """<STR_LIT>""" <EOL> lonlats = quads ( minlon , minlat , maxlon , maxlat ) <EOL> sources = [ datasource ( lat , lon , source_dir ) for ( lon , lat ) in lonlats ] <EOL> return [ ds for ds in sources if ds ] </s>
