- February 19, 2011
- google-maps kml kmz google-earth
- 0 (after 0 ratings)
This is a slight modification from the original version at http://djangosnippets.org/snippets/709/ and is a middleware designed to output the right headers for generated KML or KMZ content. In the case of KMZ content, it handles the compression of the original KML content into the KMZ format.
This version applies the processing to only requests ending with a .kml or .kmz in the URL. For instance, a request with the URL http://example.com/kml/foo.kml or http://example.com/foo.kmz will get processed by this middleware.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
from cStringIO import StringIO import zipfile class KMLMiddleware(object): """ Middleware for serving KML data and optionally converting it to KMZ if the right extension is used. """ def process_response(self, request, response): request_file = request.path.split("/")[-1] if request_file.lower().endswith(".kmz"): kmz = StringIO() f = zipfile.ZipFile(kmz, 'w', zipfile.ZIP_DEFLATED) save_file_name = request_file[:request_file.lower().rfind(".kmz")] # strips off the .kmz extension f.writestr('%s.kml' % save_file_name, response.content) f.close() response.content = kmz.getvalue() kmz.close() response['Content-Type'] = 'application/vnd.google-earth.kmz' response['Content-Disposition'] = 'attachment; filename=%s.kmz' % save_file_name response['Content-Length'] = str(len(response.content)) if request_file.lower().endswith(".kml"): save_file_name = request_file[:request_file.lower().rfind(".kml")] # strips off the .kmz extension response['Content-Type'] = 'application/vnd.google-earth.kml+xml' response['Content-Disposition'] = 'attachment; filename=%s.kml' % save_file_name response['Content-Length'] = str(len(response.content)) return response
More like this
- Automatically setup raw_id_fields ForeignKey & OneToOneField by agusmakmun 8 months ago
- Crispy Form by sourabhsinha396 8 months, 3 weeks ago
- ReadOnlySelect by mkoistinen 9 months ago
- Verify events sent to your webhook endpoints by santos22 10 months ago
- Django Language Middleware by agusmakmun 10 months, 2 weeks ago