2010-01-15 01:43:46 +08:00
|
|
|
from django.contrib.gis.gdal import SpatialReference
|
2014-01-30 16:37:15 +08:00
|
|
|
from django.db import connections, DEFAULT_DB_ALIAS
|
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
|
|
|
"""
|
2010-01-15 01:43:46 +08:00
|
|
|
This function takes a GDAL SpatialReference system and adds 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 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').
|
|
|
|
"""
|
2011-09-11 01:19:05 +08:00
|
|
|
if not database:
|
|
|
|
database = 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.')
|
|
|
|
if connection.ops.oracle or connection.ops.mysql:
|
|
|
|
raise Exception('This utility does not support the '
|
|
|
|
'Oracle or MySQL spatial backends.')
|
|
|
|
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.
|
2013-10-27 09:27:42 +08:00
|
|
|
kwargs = {'srid': srs.srid,
|
|
|
|
'auth_name': auth_name,
|
|
|
|
'auth_srid': auth_srid or srs.srid,
|
|
|
|
'proj4text': srs.proj4,
|
2009-03-31 06:54:24 +08:00
|
|
|
}
|
|
|
|
|
2010-01-15 01:43:46 +08:00
|
|
|
# Backend-specific fields for the SpatialRefSys model.
|
2014-01-30 16:49:02 +08:00
|
|
|
srs_field_names = SpatialRefSys._meta.get_all_field_names()
|
|
|
|
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:
|
|
|
|
# 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)
|
2010-01-15 01:43:46 +08:00
|
|
|
|
|
|
|
# Alias is for backwards-compatibility purposes.
|
|
|
|
add_postgis_srs = add_srs_entry
|