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
34 map_template
= 'gis/admin/openlayers.html'
35 openlayers_url
= 'http://openlayers.org/api/2.8/OpenLayers.js'
36 point_zoom
= num_zoom
- 6
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 request
= kwargs
.pop('request', None)
58 # Setting the widget with the newly defined widget.
59 kwargs
['widget'] = self
.get_map_widget(db_field
)
60 return db_field
.formfield(**kwargs
)
62 return super(GeoModelAdmin
, self
).formfield_for_dbfield(db_field
, **kwargs
)
64 def get_map_widget(self
, db_field
):
66 Returns a subclass of the OpenLayersWidget (or whatever was specified
67 in the `widget` attribute) using the settings from the attributes set
70 is_collection
= db_field
.geom_type
in ('MULTIPOINT', 'MULTILINESTRING', 'MULTIPOLYGON', 'GEOMETRYCOLLECTION')
72 if db_field
.geom_type
== 'GEOMETRYCOLLECTION': collection_type
= 'Any'
73 else: collection_type
= OGRGeomType(db_field
.geom_type
.replace('MULTI', ''))
75 collection_type
= 'None'
77 class OLMap(self
.widget
):
78 template
= self
.map_template
79 geom_type
= db_field
.geom_type
80 params
= {'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_type
),
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_type
in ('LINESTRING', 'MULTILINESTRING'),
91 'is_polygon' : db_field
.geom_type
in ('POLYGON', 'MULTIPOLYGON'),
92 'is_point' : db_field
.geom_type
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 'point_zoom' : self
.point_zoom
,
105 'srid' : self
.map_srid
,
106 'display_srid' : self
.display_srid
,
107 'wms_url' : self
.wms_url
,
108 'wms_layer' : self
.wms_layer
,
109 'wms_name' : self
.wms_name
,
110 'debug' : self
.debug
,
114 from django
.contrib
.gis
import gdal
116 class OSMGeoAdmin(GeoModelAdmin
):
117 map_template
= 'gis/admin/osm.html'
118 extra_js
= ['http://openstreetmap.org/openlayers/OpenStreetMap.js']
121 max_extent
= '-20037508,-20037508,20037508,20037508'
122 max_resolution
= 156543.0339
123 point_zoom
= num_zoom
- 6