2008-08-06 02:13:06 +08:00
|
|
|
from django.contrib.admin import ModelAdmin
|
|
|
|
from django.contrib.gis.admin.widgets import OpenLayersWidget
|
|
|
|
from django.contrib.gis.gdal import OGRGeomType
|
|
|
|
from django.contrib.gis.db import models
|
|
|
|
|
|
|
|
class GeoModelAdmin(ModelAdmin):
|
|
|
|
"""
|
|
|
|
The administration options class for Geographic models. Map settings
|
|
|
|
may be overloaded from their defaults to create custom maps.
|
|
|
|
"""
|
|
|
|
# The default map settings that may be overloaded -- still subject
|
|
|
|
# to API changes.
|
|
|
|
default_lon = 0
|
|
|
|
default_lat = 0
|
|
|
|
default_zoom = 4
|
|
|
|
display_wkt = False
|
|
|
|
display_srid = False
|
|
|
|
extra_js = []
|
|
|
|
num_zoom = 18
|
|
|
|
max_zoom = False
|
|
|
|
min_zoom = False
|
|
|
|
units = False
|
|
|
|
max_resolution = False
|
|
|
|
max_extent = False
|
|
|
|
modifiable = True
|
|
|
|
mouse_position = True
|
|
|
|
scale_text = True
|
|
|
|
layerswitcher = True
|
|
|
|
scrollable = True
|
|
|
|
map_width = 600
|
|
|
|
map_height = 400
|
|
|
|
map_srid = 4326
|
|
|
|
map_template = 'gis/admin/openlayers.html'
|
2010-11-03 03:31:14 +08:00
|
|
|
openlayers_url = 'http://openlayers.org/api/2.10/OpenLayers.js'
|
2009-04-26 14:01:59 +08:00
|
|
|
point_zoom = num_zoom - 6
|
2008-08-06 02:13:06 +08:00
|
|
|
wms_url = 'http://labs.metacarta.com/wms/vmap0'
|
|
|
|
wms_layer = 'basic'
|
|
|
|
wms_name = 'OpenLayers WMS'
|
|
|
|
debug = False
|
|
|
|
widget = OpenLayersWidget
|
|
|
|
|
2011-09-10 11:04:30 +08:00
|
|
|
@property
|
|
|
|
def media(self):
|
2008-08-06 02:13:06 +08:00
|
|
|
"Injects OpenLayers JavaScript into the admin."
|
2011-09-10 11:04:30 +08:00
|
|
|
media = super(GeoModelAdmin, self).media
|
2008-08-06 02:13:06 +08:00
|
|
|
media.add_js([self.openlayers_url])
|
|
|
|
media.add_js(self.extra_js)
|
|
|
|
return media
|
|
|
|
|
|
|
|
def formfield_for_dbfield(self, db_field, **kwargs):
|
|
|
|
"""
|
|
|
|
Overloaded from ModelAdmin so that an OpenLayersWidget is used
|
|
|
|
for viewing/editing GeometryFields.
|
|
|
|
"""
|
|
|
|
if isinstance(db_field, models.GeometryField):
|
2009-01-17 04:05:39 +08:00
|
|
|
request = kwargs.pop('request', None)
|
2008-08-06 02:13:06 +08:00
|
|
|
# Setting the widget with the newly defined widget.
|
|
|
|
kwargs['widget'] = self.get_map_widget(db_field)
|
|
|
|
return db_field.formfield(**kwargs)
|
|
|
|
else:
|
|
|
|
return super(GeoModelAdmin, self).formfield_for_dbfield(db_field, **kwargs)
|
|
|
|
|
|
|
|
def get_map_widget(self, db_field):
|
|
|
|
"""
|
|
|
|
Returns a subclass of the OpenLayersWidget (or whatever was specified
|
2010-04-17 00:34:42 +08:00
|
|
|
in the `widget` attribute) using the settings from the attributes set
|
2008-08-06 02:13:06 +08:00
|
|
|
in this class.
|
|
|
|
"""
|
2009-04-25 04:34:48 +08:00
|
|
|
is_collection = db_field.geom_type in ('MULTIPOINT', 'MULTILINESTRING', 'MULTIPOLYGON', 'GEOMETRYCOLLECTION')
|
2008-08-06 02:13:06 +08:00
|
|
|
if is_collection:
|
2009-04-25 04:34:48 +08:00
|
|
|
if db_field.geom_type == 'GEOMETRYCOLLECTION': collection_type = 'Any'
|
|
|
|
else: collection_type = OGRGeomType(db_field.geom_type.replace('MULTI', ''))
|
2008-08-06 02:13:06 +08:00
|
|
|
else:
|
|
|
|
collection_type = 'None'
|
|
|
|
|
|
|
|
class OLMap(self.widget):
|
|
|
|
template = self.map_template
|
2009-04-25 04:34:48 +08:00
|
|
|
geom_type = db_field.geom_type
|
2009-01-13 10:08:26 +08:00
|
|
|
params = {'default_lon' : self.default_lon,
|
2008-08-06 02:13:06 +08:00
|
|
|
'default_lat' : self.default_lat,
|
|
|
|
'default_zoom' : self.default_zoom,
|
|
|
|
'display_wkt' : self.debug or self.display_wkt,
|
2009-04-25 04:34:48 +08:00
|
|
|
'geom_type' : OGRGeomType(db_field.geom_type),
|
2008-08-06 02:13:06 +08:00
|
|
|
'field_name' : db_field.name,
|
|
|
|
'is_collection' : is_collection,
|
|
|
|
'scrollable' : self.scrollable,
|
|
|
|
'layerswitcher' : self.layerswitcher,
|
|
|
|
'collection_type' : collection_type,
|
2009-04-25 04:34:48 +08:00
|
|
|
'is_linestring' : db_field.geom_type in ('LINESTRING', 'MULTILINESTRING'),
|
|
|
|
'is_polygon' : db_field.geom_type in ('POLYGON', 'MULTIPOLYGON'),
|
|
|
|
'is_point' : db_field.geom_type in ('POINT', 'MULTIPOINT'),
|
2008-08-06 02:13:06 +08:00
|
|
|
'num_zoom' : self.num_zoom,
|
|
|
|
'max_zoom' : self.max_zoom,
|
|
|
|
'min_zoom' : self.min_zoom,
|
|
|
|
'units' : self.units, #likely shoud get from object
|
|
|
|
'max_resolution' : self.max_resolution,
|
|
|
|
'max_extent' : self.max_extent,
|
|
|
|
'modifiable' : self.modifiable,
|
|
|
|
'mouse_position' : self.mouse_position,
|
|
|
|
'scale_text' : self.scale_text,
|
|
|
|
'map_width' : self.map_width,
|
|
|
|
'map_height' : self.map_height,
|
2009-04-26 14:01:59 +08:00
|
|
|
'point_zoom' : self.point_zoom,
|
2008-08-06 02:13:06 +08:00
|
|
|
'srid' : self.map_srid,
|
|
|
|
'display_srid' : self.display_srid,
|
|
|
|
'wms_url' : self.wms_url,
|
|
|
|
'wms_layer' : self.wms_layer,
|
|
|
|
'wms_name' : self.wms_name,
|
|
|
|
'debug' : self.debug,
|
|
|
|
}
|
|
|
|
return OLMap
|
|
|
|
|
|
|
|
from django.contrib.gis import gdal
|
|
|
|
if gdal.HAS_GDAL:
|
2011-03-16 10:58:35 +08:00
|
|
|
# Use the official spherical mercator projection SRID on versions
|
|
|
|
# of GDAL that support it; otherwise, fallback to 900913.
|
|
|
|
if gdal.GDAL_VERSION >= (1, 7):
|
|
|
|
spherical_mercator_srid = 3857
|
|
|
|
else:
|
|
|
|
spherical_mercator_srid = 900913
|
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
class OSMGeoAdmin(GeoModelAdmin):
|
|
|
|
map_template = 'gis/admin/osm.html'
|
2010-11-03 03:31:14 +08:00
|
|
|
extra_js = ['http://www.openstreetmap.org/openlayers/OpenStreetMap.js']
|
2008-08-06 02:13:06 +08:00
|
|
|
num_zoom = 20
|
2011-03-16 10:58:35 +08:00
|
|
|
map_srid = spherical_mercator_srid
|
2008-08-06 02:13:06 +08:00
|
|
|
max_extent = '-20037508,-20037508,20037508,20037508'
|
2010-10-24 17:39:47 +08:00
|
|
|
max_resolution = '156543.0339'
|
2009-04-26 14:01:59 +08:00
|
|
|
point_zoom = num_zoom - 6
|
2008-08-06 02:13:06 +08:00
|
|
|
units = 'm'
|