<s> """<STR_LIT>""" <EOL> from sys import stderr <EOL> from math import floor , ceil , log <EOL> from os import unlink , close , write , makedirs , chmod <EOL> from os . path import basename , exists , isdir , join <EOL> from httplib import HTTPConnection <EOL> from urlparse import urlparse <EOL> from StringIO import StringIO <EOL> from gzip import GzipFile <EOL> from hashlib import md5 <EOL> from TileStache . Geography import SphericalMercator <EOL> from osgeo import gdal , osr <EOL> ideal_zoom = <NUM_LIT:7> <EOL> osr . UseExceptions ( ) <EOL> sref = osr . SpatialReference ( ) <EOL> sref . ImportFromProj4 ( '<STR_LIT>' ) <EOL> def quads ( minlon , minlat , maxlon , maxlat ) : <EOL> """<STR_LIT>""" <EOL> lon = floor ( minlon ) <EOL> while lon <= maxlon : <EOL> lat = ceil ( maxlat ) <EOL> while lat >= minlat : <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> fmt = '<STR_LIT>' <EOL> url = fmt % ( abs ( lat ) , abs ( lon ) ) <EOL> s , host , path , p , q , f = urlparse ( url ) <EOL> local_dir = md5 ( url ) . hexdigest ( ) [ : <NUM_LIT:3> ] <EOL> local_dir = join ( source_dir , local_dir ) <EOL> local_base = join ( local_dir , basename ( path ) [ : - <NUM_LIT:7> ] ) <EOL> local_path = local_base + '<STR_LIT>' <EOL> local_none = local_base + '<STR_LIT>' <EOL> if exists ( local_path ) : <EOL> return gdal . Open ( local_path , gdal . GA_ReadOnly ) <EOL> if exists ( local_none ) : <EOL> return None <EOL> if not exists ( local_dir ) : <EOL> makedirs ( local_dir ) <EOL> chmod ( local_dir , <NUM_LIT:0> <NUM_LIT> ) <EOL> assert isdir ( local_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 in range ( <NUM_LIT> , <NUM_LIT> ) : <EOL> print >> open ( local_none , '<STR_LIT:w>' ) , url <EOL> return None <EOL> assert resp . status == <NUM_LIT:200> , ( resp . status , resp . read ( ) ) <EOL> body = StringIO ( resp . read ( ) ) <EOL> file = GzipFile ( fileobj = body , mode = '<STR_LIT:r>' ) <EOL> open ( local_path , mode = '<STR_LIT:w>' ) . write ( file . read ( ) ) <EOL> return gdal . Open ( local_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>
