2010-01-15 01:43:46 +08:00
|
|
|
from django.contrib.gis.gdal import SpatialReference
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.db import DEFAULT_DB_ALIAS, connections
|
2010-01-15 01:43:46 +08:00
|
|
|
|
2013-11-03 04:12:09 +08:00
|
|
|
|
2010-01-15 01:43:46 +08:00
|
|
|
def add_srs_entry(srs, auth_name='EPSG', auth_srid=None, ref_sys_name=None,
|
2011-09-11 01:19:05 +08:00
|
|
|
database=None):
|
2008-08-06 02:13:06 +08:00
|
|
|
"""
|
2017-01-25 04:31:57 +08:00
|
|
|
Take a GDAL SpatialReference system and add its information to the
|
|
|
|
`spatial_ref_sys` table of the spatial backend. Doing this enables
|
2010-01-15 01:43:46 +08:00
|
|
|
database-level spatial transformations for the backend. Thus, this utility
|
|
|
|
is useful for adding spatial reference systems not included by default with
|
2014-04-18 03:10:41 +08:00
|
|
|
the backend:
|
2009-03-31 06:54:24 +08:00
|
|
|
|
2010-01-15 01:43:46 +08:00
|
|
|
>>> from django.contrib.gis.utils import add_srs_entry
|
2014-04-18 03:10:41 +08:00
|
|
|
>>> add_srs_entry(3857)
|
2008-08-06 02:13:06 +08:00
|
|
|
|
2009-03-31 06:54:24 +08:00
|
|
|
Keyword Arguments:
|
2010-01-15 01:43:46 +08:00
|
|
|
auth_name:
|
|
|
|
This keyword may be customized with the value of the `auth_name` field.
|
|
|
|
Defaults to 'EPSG'.
|
2009-03-31 06:54:24 +08:00
|
|
|
|
2010-01-15 01:43:46 +08:00
|
|
|
auth_srid:
|
|
|
|
This keyword may be customized with the value of the `auth_srid` field.
|
|
|
|
Defaults to the SRID determined by GDAL.
|
2009-03-31 06:54:24 +08:00
|
|
|
|
2010-01-15 01:43:46 +08:00
|
|
|
ref_sys_name:
|
2013-01-29 23:45:40 +08:00
|
|
|
For SpatiaLite users only, sets the value of the `ref_sys_name` field.
|
2010-01-15 01:43:46 +08:00
|
|
|
Defaults to the name determined by GDAL.
|
2008-08-06 02:13:06 +08:00
|
|
|
|
2010-01-15 01:43:46 +08:00
|
|
|
database:
|
|
|
|
The name of the database connection to use; the default is the value
|
2014-01-30 16:37:15 +08:00
|
|
|
of `django.db.DEFAULT_DB_ALIAS` (at the time of this writing, its value
|
2010-01-15 01:43:46 +08:00
|
|
|
is 'default').
|
|
|
|
"""
|
2018-01-04 07:52:12 +08:00
|
|
|
database = database or DEFAULT_DB_ALIAS
|
2010-01-15 01:43:46 +08:00
|
|
|
connection = connections[database]
|
2011-09-11 01:19:05 +08:00
|
|
|
|
2010-01-15 01:43:46 +08:00
|
|
|
if not hasattr(connection.ops, 'spatial_version'):
|
|
|
|
raise Exception('The `add_srs_entry` utility only works '
|
|
|
|
'with spatial backends.')
|
2014-09-18 03:55:34 +08:00
|
|
|
if not connection.features.supports_add_srs_entry:
|
|
|
|
raise Exception('This utility does not support your database backend.')
|
2010-01-15 01:43:46 +08:00
|
|
|
SpatialRefSys = connection.ops.spatial_ref_sys()
|
2009-03-31 06:54:24 +08:00
|
|
|
|
2010-01-15 01:43:46 +08:00
|
|
|
# If argument is not a `SpatialReference` instance, use it as parameter
|
|
|
|
# to construct a `SpatialReference` instance.
|
2008-08-06 02:13:06 +08:00
|
|
|
if not isinstance(srs, SpatialReference):
|
|
|
|
srs = SpatialReference(srs)
|
|
|
|
|
|
|
|
if srs.srid is None:
|
2010-01-15 01:43:46 +08:00
|
|
|
raise Exception('Spatial reference requires an SRID to be '
|
|
|
|
'compatible with the spatial backend.')
|
2009-03-31 06:54:24 +08:00
|
|
|
|
2010-01-15 01:43:46 +08:00
|
|
|
# Initializing the keyword arguments dictionary for both PostGIS
|
|
|
|
# and SpatiaLite.
|
2019-01-03 07:18:19 +08:00
|
|
|
kwargs = {
|
|
|
|
'srid': srs.srid,
|
|
|
|
'auth_name': auth_name,
|
|
|
|
'auth_srid': auth_srid or srs.srid,
|
|
|
|
'proj4text': srs.proj4,
|
|
|
|
}
|
2010-01-15 01:43:46 +08:00
|
|
|
# Backend-specific fields for the SpatialRefSys model.
|
2015-01-07 08:16:35 +08:00
|
|
|
srs_field_names = {f.name for f in SpatialRefSys._meta.get_fields()}
|
2014-01-30 16:49:02 +08:00
|
|
|
if 'srtext' in srs_field_names:
|
2010-01-15 01:43:46 +08:00
|
|
|
kwargs['srtext'] = srs.wkt
|
2014-01-30 16:49:02 +08:00
|
|
|
if 'ref_sys_name' in srs_field_names:
|
2016-08-10 00:46:14 +08:00
|
|
|
# SpatiaLite specific
|
2010-01-15 01:43:46 +08:00
|
|
|
kwargs['ref_sys_name'] = ref_sys_name or srs.name
|
2009-03-31 06:54:24 +08:00
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
# Creating the spatial_ref_sys model.
|
2010-01-15 01:43:46 +08:00
|
|
|
try:
|
|
|
|
# Try getting via SRID only, because using all kwargs may
|
|
|
|
# differ from exact wkt/proj in database.
|
2013-09-08 23:05:16 +08:00
|
|
|
SpatialRefSys.objects.using(database).get(srid=srs.srid)
|
2010-01-15 01:43:46 +08:00
|
|
|
except SpatialRefSys.DoesNotExist:
|
2013-09-08 23:05:16 +08:00
|
|
|
SpatialRefSys.objects.using(database).create(**kwargs)
|