PageRenderTime 41ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://code.google.com/p/mango-py/
Python | 131 lines | 111 code | 4 blank | 16 comment | 5 complexity | 5d74e33e4f41efe5161f6e2b1a7114db MD5 | raw file
Possible License(s): BSD-3-Clause
  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
  6. class GeoModelAdmin(ModelAdmin):
  7. """
  8. The administration options class for Geographic models. Map settings
  9. may be overloaded from their defaults to create custom maps.
  10. """
  11. # The default map settings that may be overloaded -- still subject
  12. # to API changes.
  13. default_lon = 0
  14. default_lat = 0
  15. default_zoom = 4
  16. display_wkt = False
  17. display_srid = False
  18. extra_js = []
  19. num_zoom = 18
  20. max_zoom = False
  21. min_zoom = False
  22. units = False
  23. max_resolution = False
  24. max_extent = False
  25. modifiable = True
  26. mouse_position = True
  27. scale_text = True
  28. layerswitcher = True
  29. scrollable = True
  30. map_width = 600
  31. map_height = 400
  32. map_srid = 4326
  33. map_template = 'gis/admin/openlayers.html'
  34. openlayers_url = 'http://openlayers.org/api/2.10/OpenLayers.js'
  35. point_zoom = num_zoom - 6
  36. wms_url = 'http://labs.metacarta.com/wms/vmap0'
  37. wms_layer = 'basic'
  38. wms_name = 'OpenLayers WMS'
  39. debug = False
  40. widget = OpenLayersWidget
  41. def _media(self):
  42. "Injects OpenLayers JavaScript into the admin."
  43. media = super(GeoModelAdmin, self)._media()
  44. media.add_js([self.openlayers_url])
  45. media.add_js(self.extra_js)
  46. return media
  47. media = property(_media)
  48. def formfield_for_dbfield(self, db_field, **kwargs):
  49. """
  50. Overloaded from ModelAdmin so that an OpenLayersWidget is used
  51. for viewing/editing GeometryFields.
  52. """
  53. if isinstance(db_field, models.GeometryField):
  54. request = kwargs.pop('request', None)
  55. # Setting the widget with the newly defined widget.
  56. kwargs['widget'] = self.get_map_widget(db_field)
  57. return db_field.formfield(**kwargs)
  58. else:
  59. return super(GeoModelAdmin, self).formfield_for_dbfield(db_field, **kwargs)
  60. def get_map_widget(self, db_field):
  61. """
  62. Returns a subclass of the OpenLayersWidget (or whatever was specified
  63. in the `widget` attribute) using the settings from the attributes set
  64. in this class.
  65. """
  66. is_collection = db_field.geom_type in ('MULTIPOINT', 'MULTILINESTRING', 'MULTIPOLYGON', 'GEOMETRYCOLLECTION')
  67. if is_collection:
  68. if db_field.geom_type == 'GEOMETRYCOLLECTION': collection_type = 'Any'
  69. else: collection_type = OGRGeomType(db_field.geom_type.replace('MULTI', ''))
  70. else:
  71. collection_type = 'None'
  72. class OLMap(self.widget):
  73. template = self.map_template
  74. geom_type = db_field.geom_type
  75. params = {'default_lon' : self.default_lon,
  76. 'default_lat' : self.default_lat,
  77. 'default_zoom' : self.default_zoom,
  78. 'display_wkt' : self.debug or self.display_wkt,
  79. 'geom_type' : OGRGeomType(db_field.geom_type),
  80. 'field_name' : db_field.name,
  81. 'is_collection' : is_collection,
  82. 'scrollable' : self.scrollable,
  83. 'layerswitcher' : self.layerswitcher,
  84. 'collection_type' : collection_type,
  85. 'is_linestring' : db_field.geom_type in ('LINESTRING', 'MULTILINESTRING'),
  86. 'is_polygon' : db_field.geom_type in ('POLYGON', 'MULTIPOLYGON'),
  87. 'is_point' : db_field.geom_type in ('POINT', 'MULTIPOINT'),
  88. 'num_zoom' : self.num_zoom,
  89. 'max_zoom' : self.max_zoom,
  90. 'min_zoom' : self.min_zoom,
  91. 'units' : self.units, #likely shoud get from object
  92. 'max_resolution' : self.max_resolution,
  93. 'max_extent' : self.max_extent,
  94. 'modifiable' : self.modifiable,
  95. 'mouse_position' : self.mouse_position,
  96. 'scale_text' : self.scale_text,
  97. 'map_width' : self.map_width,
  98. 'map_height' : self.map_height,
  99. 'point_zoom' : self.point_zoom,
  100. 'srid' : self.map_srid,
  101. 'display_srid' : self.display_srid,
  102. 'wms_url' : self.wms_url,
  103. 'wms_layer' : self.wms_layer,
  104. 'wms_name' : self.wms_name,
  105. 'debug' : self.debug,
  106. }
  107. return OLMap
  108. from django.contrib.gis import gdal
  109. if gdal.HAS_GDAL:
  110. # Use the official spherical mercator projection SRID on versions
  111. # of GDAL that support it; otherwise, fallback to 900913.
  112. if gdal.GDAL_VERSION >= (1, 7):
  113. spherical_mercator_srid = 3857
  114. else:
  115. spherical_mercator_srid = 900913
  116. class OSMGeoAdmin(GeoModelAdmin):
  117. map_template = 'gis/admin/osm.html'
  118. extra_js = ['http://www.openstreetmap.org/openlayers/OpenStreetMap.js']
  119. num_zoom = 20
  120. map_srid = spherical_mercator_srid
  121. max_extent = '-20037508,-20037508,20037508,20037508'
  122. max_resolution = '156543.0339'
  123. point_zoom = num_zoom - 6
  124. units = 'm'