PageRenderTime 46ms CodeModel.GetById 12ms app.highlight 27ms RepoModel.GetById 1ms app.codeStats 1ms

/django/contrib/gis/admin/options.py

https://code.google.com/p/mango-py/
Python | 131 lines | 111 code | 4 blank | 16 comment | 4 complexity | 5d74e33e4f41efe5161f6e2b1a7114db MD5 | raw file
  1from django.conf import settings
  2from django.contrib.admin import ModelAdmin
  3from django.contrib.gis.admin.widgets import OpenLayersWidget
  4from django.contrib.gis.gdal import OGRGeomType
  5from django.contrib.gis.db import models
  6
  7class GeoModelAdmin(ModelAdmin):
  8    """
  9    The administration options class for Geographic models. Map settings
 10    may be overloaded from their defaults to create custom maps.
 11    """
 12    # The default map settings that may be overloaded -- still subject
 13    # to API changes.
 14    default_lon = 0
 15    default_lat = 0
 16    default_zoom = 4
 17    display_wkt = False
 18    display_srid = False
 19    extra_js = []
 20    num_zoom = 18
 21    max_zoom = False
 22    min_zoom = False
 23    units = False
 24    max_resolution = False
 25    max_extent = False
 26    modifiable = True
 27    mouse_position = True
 28    scale_text = True
 29    layerswitcher = True
 30    scrollable = True
 31    map_width = 600
 32    map_height = 400
 33    map_srid = 4326
 34    map_template = 'gis/admin/openlayers.html'
 35    openlayers_url = 'http://openlayers.org/api/2.10/OpenLayers.js'
 36    point_zoom = num_zoom - 6
 37    wms_url = 'http://labs.metacarta.com/wms/vmap0'
 38    wms_layer = 'basic'
 39    wms_name = 'OpenLayers WMS'
 40    debug = False
 41    widget = OpenLayersWidget
 42
 43    def _media(self):
 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)
 48        return media
 49    media = property(_media)
 50
 51    def formfield_for_dbfield(self, db_field, **kwargs):
 52        """
 53        Overloaded from ModelAdmin so that an OpenLayersWidget is used
 54        for viewing/editing GeometryFields.
 55        """
 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)
 61        else:
 62            return super(GeoModelAdmin, self).formfield_for_dbfield(db_field, **kwargs)
 63
 64    def get_map_widget(self, db_field):
 65        """
 66        Returns a subclass of the OpenLayersWidget (or whatever was specified
 67        in the `widget` attribute) using the settings from the attributes set
 68        in this class.
 69        """
 70        is_collection = db_field.geom_type in ('MULTIPOINT', 'MULTILINESTRING', 'MULTIPOLYGON', 'GEOMETRYCOLLECTION')
 71        if is_collection:
 72            if db_field.geom_type == 'GEOMETRYCOLLECTION': collection_type = 'Any'
 73            else: collection_type = OGRGeomType(db_field.geom_type.replace('MULTI', ''))
 74        else:
 75            collection_type = 'None'
 76
 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,
111                      }
112        return OLMap
113
114from django.contrib.gis import gdal
115if gdal.HAS_GDAL:
116    # Use the official spherical mercator projection SRID on versions
117    # of GDAL that support it; otherwise, fallback to 900913.
118    if gdal.GDAL_VERSION >= (1, 7):
119        spherical_mercator_srid = 3857
120    else:
121        spherical_mercator_srid = 900913
122
123    class OSMGeoAdmin(GeoModelAdmin):
124        map_template = 'gis/admin/osm.html'
125        extra_js = ['http://www.openstreetmap.org/openlayers/OpenStreetMap.js']
126        num_zoom = 20
127        map_srid = spherical_mercator_srid
128        max_extent = '-20037508,-20037508,20037508,20037508'
129        max_resolution = '156543.0339'
130        point_zoom = num_zoom - 6
131        units = 'm'