You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

44 lines
1.2 KiB

  1. import zipfile
  2. from io import BytesIO
  3. from django.conf import settings
  4. from django.http import HttpResponse
  5. from django.template import loader
  6. # NumPy supported?
  7. try:
  8. import numpy
  9. except ImportError:
  10. numpy = False
  11. def compress_kml(kml):
  12. "Returns compressed KMZ from the given KML string."
  13. kmz = BytesIO()
  14. zf = zipfile.ZipFile(kmz, 'a', zipfile.ZIP_DEFLATED)
  15. zf.writestr('doc.kml', kml.encode(settings.DEFAULT_CHARSET))
  16. zf.close()
  17. kmz.seek(0)
  18. return kmz.read()
  19. def render_to_kml(*args, **kwargs):
  20. "Renders the response as KML (using the correct MIME type)."
  21. return HttpResponse(loader.render_to_string(*args, **kwargs),
  22. content_type='application/vnd.google-earth.kml+xml')
  23. def render_to_kmz(*args, **kwargs):
  24. """
  25. Compresses the KML content and returns as KMZ (using the correct
  26. MIME type).
  27. """
  28. return HttpResponse(compress_kml(loader.render_to_string(*args, **kwargs)),
  29. content_type='application/vnd.google-earth.kmz')
  30. def render_to_text(*args, **kwargs):
  31. "Renders the response using the MIME type for plain text."
  32. return HttpResponse(loader.render_to_string(*args, **kwargs),
  33. content_type='text/plain')