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.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. from django.contrib.gis.gdal import SpatialReference
  2. from django.db import DEFAULT_DB_ALIAS, connections
  3. def add_srs_entry(srs, auth_name='EPSG', auth_srid=None, ref_sys_name=None,
  4. database=None):
  5. """
  6. Take a GDAL SpatialReference system and add its information to the
  7. `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. database = database or DEFAULT_DB_ALIAS
  29. connection = connections[database]
  30. if not hasattr(connection.ops, 'spatial_version'):
  31. raise Exception('The `add_srs_entry` utility only works '
  32. 'with spatial backends.')
  33. if not connection.features.supports_add_srs_entry:
  34. raise Exception('This utility does not support your database backend.')
  35. SpatialRefSys = connection.ops.spatial_ref_sys()
  36. # If argument is not a `SpatialReference` instance, use it as parameter
  37. # to construct a `SpatialReference` instance.
  38. if not isinstance(srs, SpatialReference):
  39. srs = SpatialReference(srs)
  40. if srs.srid is None:
  41. raise Exception('Spatial reference requires an SRID to be '
  42. 'compatible with the spatial backend.')
  43. # Initializing the keyword arguments dictionary for both PostGIS
  44. # and SpatiaLite.
  45. kwargs = {
  46. 'srid': srs.srid,
  47. 'auth_name': auth_name,
  48. 'auth_srid': auth_srid or srs.srid,
  49. 'proj4text': srs.proj4,
  50. }
  51. # Backend-specific fields for the SpatialRefSys model.
  52. srs_field_names = {f.name for f in SpatialRefSys._meta.get_fields()}
  53. if 'srtext' in srs_field_names:
  54. kwargs['srtext'] = srs.wkt
  55. if 'ref_sys_name' in srs_field_names:
  56. # SpatiaLite specific
  57. kwargs['ref_sys_name'] = ref_sys_name or srs.name
  58. # Creating the spatial_ref_sys model.
  59. try:
  60. # Try getting via SRID only, because using all kwargs may
  61. # differ from exact wkt/proj in database.
  62. SpatialRefSys.objects.using(database).get(srid=srs.srid)
  63. except SpatialRefSys.DoesNotExist:
  64. SpatialRefSys.objects.using(database).create(**kwargs)