widgets.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import logging
  2. from django.forms.widgets import Textarea
  3. from django.template import loader, Context
  4. from django.utils import six
  5. from django.utils import translation
  6. from django.contrib.gis.gdal import OGRException
  7. from django.contrib.gis.geos import GEOSGeometry, GEOSException
  8. # Creating a template context that contains Django settings
  9. # values needed by admin map templates.
  10. geo_context = Context({'LANGUAGE_BIDI': translation.get_language_bidi()})
  11. logger = logging.getLogger('django.contrib.gis')
  12. class OpenLayersWidget(Textarea):
  13. """
  14. Renders an OpenLayers map using the WKT of the geometry.
  15. """
  16. def render(self, name, value, attrs=None):
  17. # Update the template parameters with any attributes passed in.
  18. if attrs:
  19. self.params.update(attrs)
  20. # Defaulting the WKT value to a blank string -- this
  21. # will be tested in the JavaScript and the appropriate
  22. # interface will be constructed.
  23. self.params['wkt'] = ''
  24. # If a string reaches here (via a validation error on another
  25. # field) then just reconstruct the Geometry.
  26. if isinstance(value, six.string_types):
  27. try:
  28. value = GEOSGeometry(value)
  29. except (GEOSException, ValueError) as err:
  30. logger.error(
  31. "Error creating geometry from value '%s' (%s)" % (
  32. value, err)
  33. )
  34. value = None
  35. if (value and value.geom_type.upper() != self.geom_type and
  36. self.geom_type != 'GEOMETRY'):
  37. value = None
  38. # Constructing the dictionary of the map options.
  39. self.params['map_options'] = self.map_options()
  40. # Constructing the JavaScript module name using the name of
  41. # the GeometryField (passed in via the `attrs` keyword).
  42. # Use the 'name' attr for the field name (rather than 'field')
  43. self.params['name'] = name
  44. # note: we must switch out dashes for underscores since js
  45. # functions are created using the module variable
  46. js_safe_name = self.params['name'].replace('-', '_')
  47. self.params['module'] = 'geodjango_%s' % js_safe_name
  48. if value:
  49. # Transforming the geometry to the projection used on the
  50. # OpenLayers map.
  51. srid = self.params['srid']
  52. if value.srid != srid:
  53. try:
  54. ogr = value.ogr
  55. ogr.transform(srid)
  56. wkt = ogr.wkt
  57. except OGRException as err:
  58. logger.error(
  59. "Error transforming geometry from srid '%s' to srid '%s' (%s)" % (
  60. value.srid, srid, err)
  61. )
  62. wkt = ''
  63. else:
  64. wkt = value.wkt
  65. # Setting the parameter WKT with that of the transformed
  66. # geometry.
  67. self.params['wkt'] = wkt
  68. return loader.render_to_string(self.template, self.params,
  69. context_instance=geo_context)
  70. def map_options(self):
  71. "Builds the map options hash for the OpenLayers template."
  72. # JavaScript construction utilities for the Bounds and Projection.
  73. def ol_bounds(extent):
  74. return 'new OpenLayers.Bounds(%s)' % str(extent)
  75. def ol_projection(srid):
  76. return 'new OpenLayers.Projection("EPSG:%s")' % srid
  77. # An array of the parameter name, the name of their OpenLayers
  78. # counterpart, and the type of variable they are.
  79. map_types = [('srid', 'projection', 'srid'),
  80. ('display_srid', 'displayProjection', 'srid'),
  81. ('units', 'units', str),
  82. ('max_resolution', 'maxResolution', float),
  83. ('max_extent', 'maxExtent', 'bounds'),
  84. ('num_zoom', 'numZoomLevels', int),
  85. ('max_zoom', 'maxZoomLevels', int),
  86. ('min_zoom', 'minZoomLevel', int),
  87. ]
  88. # Building the map options hash.
  89. map_options = {}
  90. for param_name, js_name, option_type in map_types:
  91. if self.params.get(param_name, False):
  92. if option_type == 'srid':
  93. value = ol_projection(self.params[param_name])
  94. elif option_type == 'bounds':
  95. value = ol_bounds(self.params[param_name])
  96. elif option_type in (float, int):
  97. value = self.params[param_name]
  98. elif option_type in (str,):
  99. value = '"%s"' % self.params[param_name]
  100. else:
  101. raise TypeError
  102. map_options[js_name] = value
  103. return map_options