PageRenderTime 50ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/django/contrib/gis/utils/srs.py

https://code.google.com/p/mango-py/
Python | 77 lines | 71 code | 1 blank | 5 comment | 0 complexity | bbef03d1a2042116540864b016ad5f67 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. from django.contrib.gis.gdal import SpatialReference
  2. from django.db import connections, DEFAULT_DB_ALIAS
  3. def add_srs_entry(srs, auth_name='EPSG', auth_srid=None, ref_sys_name=None,
  4. database=DEFAULT_DB_ALIAS):
  5. """
  6. This function takes a GDAL SpatialReference system and adds its information
  7. to the `spatial_ref_sys` table of the spatial backend. Doing this enables
  8. database-level spatial transformations for the backend. Thus, this utility
  9. is useful for adding spatial reference systems not included by default with
  10. the backend -- for example, the so-called "Google Maps Mercator Projection"
  11. is excluded in PostGIS 1.3 and below, and the following adds it to the
  12. `spatial_ref_sys` table:
  13. >>> from django.contrib.gis.utils import add_srs_entry
  14. >>> add_srs_entry(900913)
  15. Keyword Arguments:
  16. auth_name:
  17. This keyword may be customized with the value of the `auth_name` field.
  18. Defaults to 'EPSG'.
  19. auth_srid:
  20. This keyword may be customized with the value of the `auth_srid` field.
  21. Defaults to the SRID determined by GDAL.
  22. ref_sys_name:
  23. For SpatiaLite users only, sets the value of the the `ref_sys_name` field.
  24. Defaults to the name determined by GDAL.
  25. database:
  26. The name of the database connection to use; the default is the value
  27. of `django.db.DEFAULT_DB_ALIAS` (at the time of this writing, it's value
  28. is 'default').
  29. """
  30. connection = connections[database]
  31. if not hasattr(connection.ops, 'spatial_version'):
  32. raise Exception('The `add_srs_entry` utility only works '
  33. 'with spatial backends.')
  34. if connection.ops.oracle or connection.ops.mysql:
  35. raise Exception('This utility does not support the '
  36. 'Oracle or MySQL spatial backends.')
  37. SpatialRefSys = connection.ops.spatial_ref_sys()
  38. # If argument is not a `SpatialReference` instance, use it as parameter
  39. # to construct a `SpatialReference` instance.
  40. if not isinstance(srs, SpatialReference):
  41. srs = SpatialReference(srs)
  42. if srs.srid is None:
  43. raise Exception('Spatial reference requires an SRID to be '
  44. 'compatible with the spatial backend.')
  45. # Initializing the keyword arguments dictionary for both PostGIS
  46. # and SpatiaLite.
  47. kwargs = {'srid' : srs.srid,
  48. 'auth_name' : auth_name,
  49. 'auth_srid' : auth_srid or srs.srid,
  50. 'proj4text' : srs.proj4,
  51. }
  52. # Backend-specific fields for the SpatialRefSys model.
  53. if connection.ops.postgis:
  54. kwargs['srtext'] = srs.wkt
  55. if connection.ops.spatialite:
  56. kwargs['ref_sys_name'] = ref_sys_name or srs.name
  57. # Creating the spatial_ref_sys model.
  58. try:
  59. # Try getting via SRID only, because using all kwargs may
  60. # differ from exact wkt/proj in database.
  61. sr = SpatialRefSys.objects.get(srid=srs.srid)
  62. except SpatialRefSys.DoesNotExist:
  63. sr = SpatialRefSys.objects.create(**kwargs)
  64. # Alias is for backwards-compatibility purposes.
  65. add_postgis_srs = add_srs_entry