def get_mirrored_kml(url): logger.info("Downloading %s.", url) with urlopen(urllib2.Request(url)) as response: headers = response.info() last_modified = headers.getdate('Last-Modified') # check the content type content_type = headers['Content-Type'].strip() if content_type not in KML_MIRROR_TYPES: raise Exception('Unsupported Content-Type') # prevent humongous downloads content_length = int(headers['Content-Length'].strip()) if content_length >= KML_MIRROR_MAX_CONTENT_LENGTH: raise Exception('KML_MIRROR_MAX_CONTENT_LENGTH exceeded') # perform the download content = response.read(KML_MIRROR_MAX_CONTENT_LENGTH) # recompress KML to KMZ if content_type == MIME_KML: logger.info("Compressing %s to KMZ.", url) content = compress_kml(content) content_type = MIME_KMZ return content, content_type
def handle(self, *args, **options): print "Getting all trees..." trees = models.Tree.objects.filter(present=True) print "Writing %i trees to CSV" % trees.count() queryset_to_excel_file(trees, "All_Trees",force_csv=True) print "Getting kml strings..." trees = trees.kml() output = file('All_Trees.kmz','wb') print "Writing %i trees to KML" % trees.count() output.write(compress_kml(loader.render_to_string("treemap/kml_output.kml", {'trees': trees,'root_url':settings.ROOT_URL}))) output.close()
def handle(self, *args, **options): print "Getting all trees..." trees = models.Tree.objects.filter(present=True) print "Writing %i trees to CSV" % trees.count() queryset_to_excel_file(trees, "All_Trees", force_csv=True) print "Getting kml strings..." trees = trees.kml() output = file('All_Trees.kmz', 'wb') print "Writing %i trees to KML" % trees.count() output.write( compress_kml( loader.render_to_string("treemap/kml_output.kml", { 'trees': trees, 'root_url': settings.ROOT_URL }))) output.close()
def get_mirrored_kml(url): ''' Mirror external data located at given url, so it can be served with our own webserver. The downloaded data is cached for 24 hours. Returns a tuple (content, content_type). ''' logger.info("Downloading %s.", url) with urlopen(urllib2.Request(url)) as response: headers = response.info() last_modified = headers.getdate('Last-Modified') # check the content type content_type = headers['Content-Type'].strip() # sometimes servers like to add ';mode=networklink' to the content-type if ';' in content_type: content_type = content_type.split(';')[0] # ensure only known types are proxy'd if content_type not in KML_MIRROR_TYPES: raise Exception('Unsupported Content-Type') # prevent humongous downloads # some servers (Geoserver...) won't even send this header if 'Content-Length' in headers: content_length = int(headers['Content-Length'].strip()) if content_length >= KML_MIRROR_MAX_CONTENT_LENGTH: raise Exception('KML_MIRROR_MAX_CONTENT_LENGTH exceeded') # perform the download content = response.read(KML_MIRROR_MAX_CONTENT_LENGTH) # when len(content) == KML_MIRROR_MAX_CONTENT_LENGTH, # this probably means there's more data available if len(content) >= KML_MIRROR_MAX_CONTENT_LENGTH: raise Exception('KML_MIRROR_MAX_CONTENT_LENGTH exceeded') # recompress KML to KMZ if content_type == MIME_KML: logger.info("Compressing %s to KMZ.", url) content = compress_kml(content) content_type = MIME_KMZ return content, content_type
def render(self, *args, **kwargs): kmldata = super(KMZRenderer, self).render(*args, **kwargs) return compress_kml(kmldata)