PageRenderTime 9ms CodeModel.GetById 1ms RepoModel.GetById 1ms app.codeStats 0ms

/django/contrib/gis/admin/widgets.py

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