2011-07-13 17:35:51 +08:00
|
|
|
import zipfile
|
2012-05-06 01:47:03 +08:00
|
|
|
from io import BytesIO
|
2011-07-13 17:35:51 +08:00
|
|
|
|
2009-09-13 02:35:08 +08:00
|
|
|
from django.conf import settings
|
2008-08-06 02:13:06 +08:00
|
|
|
from django.http import HttpResponse
|
|
|
|
from django.template import loader
|
|
|
|
|
2015-02-09 18:14:48 +08:00
|
|
|
# NumPy supported?
|
|
|
|
try:
|
|
|
|
import numpy
|
|
|
|
except ImportError:
|
|
|
|
numpy = False
|
|
|
|
|
2013-11-03 01:18:46 +08:00
|
|
|
|
2008-08-24 03:22:23 +08:00
|
|
|
def compress_kml(kml):
|
2017-01-25 04:31:57 +08:00
|
|
|
"Return compressed KMZ from the given KML string."
|
2012-05-06 01:47:03 +08:00
|
|
|
kmz = BytesIO()
|
2016-08-05 07:45:14 +08:00
|
|
|
with zipfile.ZipFile(kmz, 'a', zipfile.ZIP_DEFLATED) as zf:
|
|
|
|
zf.writestr('doc.kml', kml.encode(settings.DEFAULT_CHARSET))
|
2008-08-24 03:22:23 +08:00
|
|
|
kmz.seek(0)
|
|
|
|
return kmz.read()
|
|
|
|
|
2013-11-03 01:18:46 +08:00
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
def render_to_kml(*args, **kwargs):
|
2017-01-25 04:31:57 +08:00
|
|
|
"Render the response as KML (using the correct MIME type)."
|
2016-03-29 06:33:29 +08:00
|
|
|
return HttpResponse(
|
|
|
|
loader.render_to_string(*args, **kwargs),
|
|
|
|
content_type='application/vnd.google-earth.kml+xml',
|
|
|
|
)
|
2008-08-06 02:13:06 +08:00
|
|
|
|
2013-11-03 01:18:46 +08:00
|
|
|
|
2008-08-24 03:22:23 +08:00
|
|
|
def render_to_kmz(*args, **kwargs):
|
|
|
|
"""
|
2017-01-25 04:31:57 +08:00
|
|
|
Compress the KML content and return as KMZ (using the correct
|
2008-08-24 03:22:23 +08:00
|
|
|
MIME type).
|
|
|
|
"""
|
2016-03-29 06:33:29 +08:00
|
|
|
return HttpResponse(
|
|
|
|
compress_kml(loader.render_to_string(*args, **kwargs)),
|
|
|
|
content_type='application/vnd.google-earth.kmz',
|
|
|
|
)
|