You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

options.py 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. from django.contrib.admin import ModelAdmin
  2. from django.contrib.gis.admin.widgets import OpenLayersWidget
  3. from django.contrib.gis.db import models
  4. from django.contrib.gis.gdal import OGRGeomType
  5. from django.forms import Media
  6. spherical_mercator_srid = 3857
  7. class 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 = 'https://cdnjs.cloudflare.com/ajax/libs/openlayers/2.13.1/OpenLayers.js'
  36. point_zoom = num_zoom - 6
  37. wms_url = 'http://vmap0.tiles.osgeo.org/wms/vmap0'
  38. wms_layer = 'basic'
  39. wms_name = 'OpenLayers WMS'
  40. wms_options = {'format': 'image/jpeg'}
  41. debug = False
  42. widget = OpenLayersWidget
  43. @property
  44. def media(self):
  45. "Injects OpenLayers JavaScript into the admin."
  46. return super().media + Media(js=[self.openlayers_url] + self.extra_js)
  47. def formfield_for_dbfield(self, db_field, request, **kwargs):
  48. """
  49. Overloaded from ModelAdmin so that an OpenLayersWidget is used
  50. for viewing/editing 2D GeometryFields (OpenLayers 2 does not support
  51. 3D editing).
  52. """
  53. if isinstance(db_field, models.GeometryField) and db_field.dim < 3:
  54. # Setting the widget with the newly defined widget.
  55. kwargs['widget'] = self.get_map_widget(db_field)
  56. return db_field.formfield(**kwargs)
  57. else:
  58. return super().formfield_for_dbfield(db_field, request, **kwargs)
  59. def get_map_widget(self, db_field):
  60. """
  61. Return a subclass of the OpenLayersWidget (or whatever was specified
  62. in the `widget` attribute) using the settings from the attributes set
  63. in this class.
  64. """
  65. is_collection = db_field.geom_type in ('MULTIPOINT', 'MULTILINESTRING', 'MULTIPOLYGON', 'GEOMETRYCOLLECTION')
  66. if is_collection:
  67. if db_field.geom_type == 'GEOMETRYCOLLECTION':
  68. collection_type = 'Any'
  69. else:
  70. collection_type = OGRGeomType(db_field.geom_type.replace('MULTI', ''))
  71. else:
  72. collection_type = 'None'
  73. class OLMap(self.widget):
  74. template_name = self.map_template
  75. geom_type = db_field.geom_type
  76. wms_options = ''
  77. if self.wms_options:
  78. wms_options = ["%s: '%s'" % pair for pair in self.wms_options.items()]
  79. wms_options = ', %s' % ', '.join(wms_options)
  80. params = {
  81. 'default_lon': self.default_lon,
  82. 'default_lat': self.default_lat,
  83. 'default_zoom': self.default_zoom,
  84. 'display_wkt': self.debug or self.display_wkt,
  85. 'geom_type': OGRGeomType(db_field.geom_type),
  86. 'field_name': db_field.name,
  87. 'is_collection': is_collection,
  88. 'scrollable': self.scrollable,
  89. 'layerswitcher': self.layerswitcher,
  90. 'collection_type': collection_type,
  91. 'is_generic': db_field.geom_type == 'GEOMETRY',
  92. 'is_linestring': db_field.geom_type in ('LINESTRING', 'MULTILINESTRING'),
  93. 'is_polygon': db_field.geom_type in ('POLYGON', 'MULTIPOLYGON'),
  94. 'is_point': db_field.geom_type in ('POINT', 'MULTIPOINT'),
  95. 'num_zoom': self.num_zoom,
  96. 'max_zoom': self.max_zoom,
  97. 'min_zoom': self.min_zoom,
  98. 'units': self.units, # likely should get from object
  99. 'max_resolution': self.max_resolution,
  100. 'max_extent': self.max_extent,
  101. 'modifiable': self.modifiable,
  102. 'mouse_position': self.mouse_position,
  103. 'scale_text': self.scale_text,
  104. 'map_width': self.map_width,
  105. 'map_height': self.map_height,
  106. 'point_zoom': self.point_zoom,
  107. 'srid': self.map_srid,
  108. 'display_srid': self.display_srid,
  109. 'wms_url': self.wms_url,
  110. 'wms_layer': self.wms_layer,
  111. 'wms_name': self.wms_name,
  112. 'wms_options': wms_options,
  113. 'debug': self.debug,
  114. }
  115. return OLMap
  116. class OSMGeoAdmin(GeoModelAdmin):
  117. map_template = 'gis/admin/osm.html'
  118. num_zoom = 20
  119. map_srid = spherical_mercator_srid
  120. max_extent = '-20037508,-20037508,20037508,20037508'
  121. max_resolution = '156543.0339'
  122. point_zoom = num_zoom - 6
  123. units = 'm'