srs.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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=None):
  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:
  11. >>> from django.contrib.gis.utils import add_srs_entry
  12. >>> add_srs_entry(3857)
  13. Keyword Arguments:
  14. auth_name:
  15. This keyword may be customized with the value of the `auth_name` field.
  16. Defaults to 'EPSG'.
  17. auth_srid:
  18. This keyword may be customized with the value of the `auth_srid` field.
  19. Defaults to the SRID determined by GDAL.
  20. ref_sys_name:
  21. For SpatiaLite users only, sets the value of the `ref_sys_name` field.
  22. Defaults to the name determined by GDAL.
  23. database:
  24. The name of the database connection to use; the default is the value
  25. of `django.db.DEFAULT_DB_ALIAS` (at the time of this writing, its value
  26. is 'default').
  27. """
  28. if not database:
  29. database = DEFAULT_DB_ALIAS
  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. srs_field_names = SpatialRefSys._meta.get_all_field_names()
  54. if 'srtext' in srs_field_names:
  55. kwargs['srtext'] = srs.wkt
  56. if 'ref_sys_name' in srs_field_names:
  57. # Spatialite specific
  58. kwargs['ref_sys_name'] = ref_sys_name or srs.name
  59. # Creating the spatial_ref_sys model.
  60. try:
  61. # Try getting via SRID only, because using all kwargs may
  62. # differ from exact wkt/proj in database.
  63. SpatialRefSys.objects.using(database).get(srid=srs.srid)
  64. except SpatialRefSys.DoesNotExist:
  65. SpatialRefSys.objects.using(database).create(**kwargs)
  66. # Alias is for backwards-compatibility purposes.
  67. add_postgis_srs = add_srs_entry