1
from django.conf import settings
2
from django.contrib.admin import ModelAdmin
3
from django.contrib.gis.admin.widgets import OpenLayersWidget
4
from django.contrib.gis.gdal import OGRGeomType
5
from django.contrib.gis.db import models
7
class GeoModelAdmin(ModelAdmin):
9
The administration options class for Geographic models. Map settings
10
may be overloaded from their defaults to create custom maps.
12
# The default map settings that may be overloaded -- still subject
24
max_resolution = False
31
admin_media_prefix = settings.ADMIN_MEDIA_PREFIX
35
map_template = 'gis/admin/openlayers.html'
36
openlayers_url = 'http://openlayers.org/api/2.6/OpenLayers.js'
37
wms_url = 'http://labs.metacarta.com/wms/vmap0'
39
wms_name = 'OpenLayers WMS'
41
widget = OpenLayersWidget
44
"Injects OpenLayers JavaScript into the admin."
45
media = super(GeoModelAdmin, self)._media()
46
media.add_js([self.openlayers_url])
47
media.add_js(self.extra_js)
49
media = property(_media)
51
def formfield_for_dbfield(self, db_field, **kwargs):
53
Overloaded from ModelAdmin so that an OpenLayersWidget is used
54
for viewing/editing GeometryFields.
56
if isinstance(db_field, models.GeometryField):
57
# Setting the widget with the newly defined widget.
58
kwargs['widget'] = self.get_map_widget(db_field)
59
return db_field.formfield(**kwargs)
61
return super(GeoModelAdmin, self).formfield_for_dbfield(db_field, **kwargs)
63
def get_map_widget(self, db_field):
65
Returns a subclass of the OpenLayersWidget (or whatever was specified
66
in the `widget` attribute) using the settings from the attributes set
69
is_collection = db_field._geom in ('MULTIPOINT', 'MULTILINESTRING', 'MULTIPOLYGON', 'GEOMETRYCOLLECTION')
71
if db_field._geom == 'GEOMETRYCOLLECTION': collection_type = 'Any'
72
else: collection_type = OGRGeomType(db_field._geom.replace('MULTI', ''))
74
collection_type = 'None'
76
class OLMap(self.widget):
77
template = self.map_template
78
geom_type = db_field._geom
79
params = {'admin_media_prefix' : self.admin_media_prefix,
80
'default_lon' : self.default_lon,
81
'default_lat' : self.default_lat,
82
'default_zoom' : self.default_zoom,
83
'display_wkt' : self.debug or self.display_wkt,
84
'geom_type' : OGRGeomType(db_field._geom),
85
'field_name' : db_field.name,
86
'is_collection' : is_collection,
87
'scrollable' : self.scrollable,
88
'layerswitcher' : self.layerswitcher,
89
'collection_type' : collection_type,
90
'is_linestring' : db_field._geom in ('LINESTRING', 'MULTILINESTRING'),
91
'is_polygon' : db_field._geom in ('POLYGON', 'MULTIPOLYGON'),
92
'is_point' : db_field._geom in ('POINT', 'MULTIPOINT'),
93
'num_zoom' : self.num_zoom,
94
'max_zoom' : self.max_zoom,
95
'min_zoom' : self.min_zoom,
96
'units' : self.units, #likely shoud get from object
97
'max_resolution' : self.max_resolution,
98
'max_extent' : self.max_extent,
99
'modifiable' : self.modifiable,
100
'mouse_position' : self.mouse_position,
101
'scale_text' : self.scale_text,
102
'map_width' : self.map_width,
103
'map_height' : self.map_height,
104
'srid' : self.map_srid,
105
'display_srid' : self.display_srid,
106
'wms_url' : self.wms_url,
107
'wms_layer' : self.wms_layer,
108
'wms_name' : self.wms_name,
109
'debug' : self.debug,
113
# Using the Beta OSM in the admin requires the following:
114
# (1) The Google Maps Mercator projection needs to be added
115
# to your `spatial_ref_sys` table. You'll need at least GDAL 1.5:
116
# >>> from django.contrib.gis.gdal import SpatialReference
117
# >>> from django.contrib.gis.utils import add_postgis_srs
118
# >>> add_postgis_srs(SpatialReference(900913)) # Adding the Google Projection
119
from django.contrib.gis import gdal
121
class OSMGeoAdmin(GeoModelAdmin):
122
map_template = 'gis/admin/osm.html'
123
extra_js = ['http://openstreetmap.org/openlayers/OpenStreetMap.js']
126
max_extent = '-20037508,-20037508,20037508,20037508'
127
max_resolution = 156543.0339