KMZMiddleware

 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
from cStringIO import StringIO
import zipfile

class KMZMiddleware(object):
    """
    Middleware for serving KML data or converting it to KMZ.
    """
    def process_response(self, request, response):
        if request.path in ('/kmz/', '/kmz/restricted/', ):
            kmz = StringIO()
            f = zipfile.ZipFile(kmz, 'w', zipfile.ZIP_DEFLATED)
            f.writestr('doc.kml', response.content)
            f.close()
            response.content = kmz.getvalue()
            kmz.close()
            response['Content-Type']        = 'application/vnd.google-earth.kmz'
            response['Content-Disposition'] = 'attachment; filename=foo.kmz'
            response['Content-Description'] = 'some description'
            response['Content-Length']      = str(len(response.content))
        if request.path in ('/kml/', '/kml/restricted/', ):
            response['Content-Type']        = 'application/vnd.google-earth.kml+xml'
            response['Content-Disposition'] = 'attachment; filename=foo.kml'
            response['Content-Description'] = 'some description'
            response['Content-Length']      = str(len(response.content))
            
        return response

More like this

  1. KMLMiddleware by takinbo 2 years, 3 months ago
  2. Forcing Right-Hand Rule (Counter Clockwise) Polygons in GeoDjango for Google Earth / KML by gabejackson 3 years, 3 months ago
  3. JSON encoding middleware by kcarnold 4 years, 11 months ago
  4. SSL Decorator by pjs 4 years, 2 months ago
  5. Better Static Image Serving With Fallback by menendez 3 years, 2 months ago

Comments

scottj (on April 17, 2008):
  • KMZ isn't just zipped KML. A good KMZ file is a zipped file that decompress to a file called 'doc.kml', which is a KML file.

So, in other words, KMZ is just zipped KML.

#

giovabal (on April 18, 2008):

to scottj: no it isn't try to zip KML data to a file: you won't get a working KMZ

#

(Forgotten your password?)