2012-06-08 00:08:47 +08:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2013-12-24 19:25:17 +08:00
|
|
|
from django.apps import apps
|
2008-08-26 08:46:30 +08:00
|
|
|
from django.contrib.gis.db.models.fields import GeometryField
|
2015-01-30 03:12:08 +08:00
|
|
|
from django.contrib.gis.db.models.functions import AsKML, Transform
|
2014-03-23 01:31:52 +08:00
|
|
|
from django.contrib.gis.shortcuts import render_to_kml, render_to_kmz
|
2015-01-02 23:14:23 +08:00
|
|
|
from django.core.exceptions import FieldDoesNotExist
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.db import DEFAULT_DB_ALIAS, connections
|
|
|
|
from django.http import Http404
|
2008-08-24 03:22:23 +08:00
|
|
|
|
2013-11-03 04:12:09 +08:00
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
def kml(request, label, model, field_name=None, compress=False, using=DEFAULT_DB_ALIAS):
|
2008-08-24 03:22:23 +08:00
|
|
|
"""
|
|
|
|
This view generates KML for the given app label, model, and field name.
|
|
|
|
|
2016-03-12 01:53:12 +08:00
|
|
|
The field name must be that of a geographic field.
|
2008-08-24 03:22:23 +08:00
|
|
|
"""
|
|
|
|
placemarks = []
|
2013-12-28 21:55:54 +08:00
|
|
|
try:
|
|
|
|
klass = apps.get_model(label, model)
|
|
|
|
except LookupError:
|
2008-08-26 08:46:30 +08:00
|
|
|
raise Http404('You must supply a valid app label and module name. Got "%s.%s"' % (label, model))
|
|
|
|
|
|
|
|
if field_name:
|
|
|
|
try:
|
2015-01-07 08:16:35 +08:00
|
|
|
field = klass._meta.get_field(field_name)
|
2013-09-30 23:55:14 +08:00
|
|
|
if not isinstance(field, GeometryField):
|
|
|
|
raise FieldDoesNotExist
|
|
|
|
except FieldDoesNotExist:
|
2008-08-26 08:46:30 +08:00
|
|
|
raise Http404('Invalid geometry field.')
|
2008-08-24 03:22:23 +08:00
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
connection = connections[using]
|
|
|
|
|
2015-01-30 03:12:08 +08:00
|
|
|
if connection.features.has_AsKML_function:
|
2015-01-17 19:41:18 +08:00
|
|
|
# Database will take care of transformation.
|
2015-01-30 03:12:08 +08:00
|
|
|
placemarks = klass._default_manager.using(using).annotate(kml=AsKML(field_name))
|
2008-08-24 03:22:23 +08:00
|
|
|
else:
|
2015-01-17 19:41:18 +08:00
|
|
|
# If the database offers no KML method, we use the `kml`
|
2008-08-24 03:22:23 +08:00
|
|
|
# attribute of the lazy geometry instead.
|
|
|
|
placemarks = []
|
2015-01-30 03:12:08 +08:00
|
|
|
if connection.features.has_Transform_function:
|
|
|
|
qs = klass._default_manager.using(using).annotate(
|
|
|
|
**{'%s_4326' % field_name: Transform(field_name, 4326)})
|
|
|
|
field_name += '_4326'
|
2008-08-24 03:22:23 +08:00
|
|
|
else:
|
2009-12-22 23:18:51 +08:00
|
|
|
qs = klass._default_manager.using(using).all()
|
2008-08-24 03:22:23 +08:00
|
|
|
for mod in qs:
|
2010-09-27 08:20:42 +08:00
|
|
|
mod.kml = getattr(mod, field_name).kml
|
2008-08-24 03:22:23 +08:00
|
|
|
placemarks.append(mod)
|
|
|
|
|
|
|
|
# Getting the render function and rendering to the correct.
|
|
|
|
if compress:
|
|
|
|
render = render_to_kmz
|
|
|
|
else:
|
|
|
|
render = render_to_kml
|
2013-10-27 09:27:42 +08:00
|
|
|
return render('gis/kml/placemarks.kml', {'places': placemarks})
|
2008-08-24 03:22:23 +08:00
|
|
|
|
2013-11-03 04:12:09 +08:00
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
def kmz(request, label, model, field_name=None, using=DEFAULT_DB_ALIAS):
|
2008-08-24 03:22:23 +08:00
|
|
|
"""
|
|
|
|
This view returns KMZ for the given app label, model, and field name.
|
|
|
|
"""
|
2009-12-22 23:18:51 +08:00
|
|
|
return kml(request, label, model, field_name, compress=True, using=using)
|