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.

shortcuts.py 1.0KB

12345678910111213141516171819202122232425262728293031323334353637383940
  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. "Return compressed KMZ from the given KML string."
  13. kmz = BytesIO()
  14. with zipfile.ZipFile(kmz, 'a', zipfile.ZIP_DEFLATED) as zf:
  15. zf.writestr('doc.kml', kml.encode(settings.DEFAULT_CHARSET))
  16. kmz.seek(0)
  17. return kmz.read()
  18. def render_to_kml(*args, **kwargs):
  19. "Render the response as KML (using the correct MIME type)."
  20. return HttpResponse(
  21. loader.render_to_string(*args, **kwargs),
  22. content_type='application/vnd.google-earth.kml+xml',
  23. )
  24. def render_to_kmz(*args, **kwargs):
  25. """
  26. Compress the KML content and return as KMZ (using the correct
  27. MIME type).
  28. """
  29. return HttpResponse(
  30. compress_kml(loader.render_to_string(*args, **kwargs)),
  31. content_type='application/vnd.google-earth.kmz',
  32. )