Take a GDAL SpatialReference system and add its information to the `spatial_ref_sys` table of the spatial backend. Doing this enables database-level spatial transformations for the backend. Thus, this utility is useful for adding spatial reference systems not included by default wit
(
srs, auth_name="EPSG", auth_srid=None, ref_sys_name=None, database=None
)
| 3 | |
| 4 | |
| 5 | def add_srs_entry( |
| 6 | srs, auth_name="EPSG", auth_srid=None, ref_sys_name=None, database=None |
| 7 | ): |
| 8 | """ |
| 9 | Take a GDAL SpatialReference system and add its information to the |
| 10 | `spatial_ref_sys` table of the spatial backend. Doing this enables |
| 11 | database-level spatial transformations for the backend. Thus, this utility |
| 12 | is useful for adding spatial reference systems not included by default with |
| 13 | the backend: |
| 14 | |
| 15 | >>> from django.contrib.gis.utils import add_srs_entry |
| 16 | >>> add_srs_entry(3857) |
| 17 | |
| 18 | Keyword Arguments: |
| 19 | auth_name: |
| 20 | This keyword may be customized with the value of the `auth_name` field. |
| 21 | Defaults to 'EPSG'. |
| 22 | |
| 23 | auth_srid: |
| 24 | This keyword may be customized with the value of the `auth_srid` field. |
| 25 | Defaults to the SRID determined by GDAL. |
| 26 | |
| 27 | ref_sys_name: |
| 28 | For SpatiaLite users only, sets the value of the `ref_sys_name` field. |
| 29 | Defaults to the name determined by GDAL. |
| 30 | |
| 31 | database: |
| 32 | The name of the database connection to use; the default is the value |
| 33 | of `django.db.DEFAULT_DB_ALIAS` (at the time of this writing, its value |
| 34 | is 'default'). |
| 35 | """ |
| 36 | database = database or DEFAULT_DB_ALIAS |
| 37 | connection = connections[database] |
| 38 | |
| 39 | if not hasattr(connection.ops, "spatial_version"): |
| 40 | raise Exception("The `add_srs_entry` utility only works with spatial backends.") |
| 41 | if not connection.features.supports_add_srs_entry: |
| 42 | raise Exception("This utility does not support your database backend.") |
| 43 | SpatialRefSys = connection.ops.spatial_ref_sys() |
| 44 | |
| 45 | # If argument is not a `SpatialReference` instance, use it as parameter |
| 46 | # to construct a `SpatialReference` instance. |
| 47 | if not isinstance(srs, SpatialReference): |
| 48 | srs = SpatialReference(srs) |
| 49 | |
| 50 | if srs.srid is None: |
| 51 | raise Exception( |
| 52 | "Spatial reference requires an SRID to be " |
| 53 | "compatible with the spatial backend." |
| 54 | ) |
| 55 | |
| 56 | # Initializing the keyword arguments dictionary for both PostGIS |
| 57 | # and SpatiaLite. |
| 58 | kwargs = { |
| 59 | "srid": srs.srid, |
| 60 | "auth_name": auth_name, |
| 61 | "auth_srid": auth_srid or srs.srid, |
| 62 | "proj4text": srs.proj4, |