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.
 
 
 
 

81 lines
3.0 KiB

  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. 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 not connection.features.supports_add_srs_entry:
  35. raise Exception('This utility does not support your database backend.')
  36. SpatialRefSys = connection.ops.spatial_ref_sys()
  37. # If argument is not a `SpatialReference` instance, use it as parameter
  38. # to construct a `SpatialReference` instance.
  39. if not isinstance(srs, SpatialReference):
  40. srs = SpatialReference(srs)
  41. if srs.srid is None:
  42. raise Exception('Spatial reference requires an SRID to be '
  43. 'compatible with the spatial backend.')
  44. # Initializing the keyword arguments dictionary for both PostGIS
  45. # and SpatiaLite.
  46. kwargs = {'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)
  65. # Alias is for backwards-compatibility purposes.
  66. add_postgis_srs = add_srs_entry