2009-12-22 23:18:51 +08:00
|
|
|
from django.db import connection
|
2012-06-17 17:39:02 +08:00
|
|
|
from django.contrib.gis.gdal import HAS_GDAL
|
|
|
|
from django.contrib.gis.tests.utils import (no_mysql, oracle, postgis,
|
|
|
|
spatialite, HAS_SPATIALREFSYS, SpatialRefSys)
|
2013-03-09 05:41:16 +08:00
|
|
|
from django.utils import six
|
2010-10-11 20:55:17 +08:00
|
|
|
from django.utils import unittest
|
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
test_srs = ({'srid' : 4326,
|
|
|
|
'auth_name' : ('EPSG', True),
|
|
|
|
'auth_srid' : 4326,
|
2012-09-13 22:17:32 +08:00
|
|
|
# Only the beginning, because there are differences depending on installed libs
|
|
|
|
'srtext' : 'GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84"',
|
2013-03-09 05:41:16 +08:00
|
|
|
# +ellps=WGS84 has been removed in the 4326 proj string in proj-4.8
|
|
|
|
'proj4_re' : r'\+proj=longlat (\+ellps=WGS84 )?\+datum=WGS84 \+no_defs ',
|
2010-10-11 20:55:17 +08:00
|
|
|
'spheroid' : 'WGS 84', 'name' : 'WGS 84',
|
2009-03-31 06:15:41 +08:00
|
|
|
'geographic' : True, 'projected' : False, 'spatialite' : True,
|
2008-08-06 02:13:06 +08:00
|
|
|
'ellipsoid' : (6378137.0, 6356752.3, 298.257223563), # From proj's "cs2cs -le" and Wikipedia (semi-minor only)
|
|
|
|
'eprec' : (1, 1, 9),
|
|
|
|
},
|
|
|
|
{'srid' : 32140,
|
|
|
|
'auth_name' : ('EPSG', False),
|
|
|
|
'auth_srid' : 32140,
|
2012-09-13 22:17:32 +08:00
|
|
|
'srtext' : 'PROJCS["NAD83 / Texas South Central",GEOGCS["NAD83",DATUM["North_American_Datum_1983",SPHEROID["GRS 1980"',
|
2013-03-09 05:41:16 +08:00
|
|
|
'proj4_re' : r'\+proj=lcc \+lat_1=30.28333333333333 \+lat_2=28.38333333333333 \+lat_0=27.83333333333333 '
|
|
|
|
r'\+lon_0=-99 \+x_0=600000 \+y_0=4000000 \+ellps=GRS80 '
|
|
|
|
r'(\+datum=NAD83 |\+towgs84=0,0,0,0,0,0,0)?\+units=m \+no_defs ',
|
2008-08-06 02:13:06 +08:00
|
|
|
'spheroid' : 'GRS 1980', 'name' : 'NAD83 / Texas South Central',
|
2009-03-31 06:15:41 +08:00
|
|
|
'geographic' : False, 'projected' : True, 'spatialite' : False,
|
2008-08-06 02:13:06 +08:00
|
|
|
'ellipsoid' : (6378137.0, 6356752.31414, 298.257222101), # From proj's "cs2cs -le" and Wikipedia (semi-minor only)
|
|
|
|
'eprec' : (1, 5, 10),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2012-06-15 03:44:08 +08:00
|
|
|
@unittest.skipUnless(HAS_GDAL and HAS_SPATIALREFSYS,
|
|
|
|
"SpatialRefSysTest needs gdal support and a spatial database")
|
2008-08-06 02:13:06 +08:00
|
|
|
class SpatialRefSysTest(unittest.TestCase):
|
|
|
|
|
|
|
|
@no_mysql
|
|
|
|
def test01_retrieve(self):
|
|
|
|
"Testing retrieval of SpatialRefSys model objects."
|
|
|
|
for sd in test_srs:
|
|
|
|
srs = SpatialRefSys.objects.get(srid=sd['srid'])
|
|
|
|
self.assertEqual(sd['srid'], srs.srid)
|
|
|
|
|
|
|
|
# Some of the authority names are borked on Oracle, e.g., SRID=32140.
|
|
|
|
# also, Oracle Spatial seems to add extraneous info to fields, hence the
|
|
|
|
# the testing with the 'startswith' flag.
|
|
|
|
auth_name, oracle_flag = sd['auth_name']
|
|
|
|
if postgis or (oracle and oracle_flag):
|
|
|
|
self.assertEqual(True, srs.auth_name.startswith(auth_name))
|
2010-10-11 20:55:17 +08:00
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
self.assertEqual(sd['auth_srid'], srs.auth_srid)
|
|
|
|
|
|
|
|
# No proj.4 and different srtext on oracle backends :(
|
|
|
|
if postgis:
|
2012-09-13 22:17:32 +08:00
|
|
|
self.assertTrue(srs.wkt.startswith(sd['srtext']))
|
2013-03-09 05:41:16 +08:00
|
|
|
six.assertRegex(self, srs.proj4text, sd['proj4_re'])
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
@no_mysql
|
|
|
|
def test02_osr(self):
|
|
|
|
"Testing getting OSR objects from SpatialRefSys model objects."
|
|
|
|
for sd in test_srs:
|
|
|
|
sr = SpatialRefSys.objects.get(srid=sd['srid'])
|
|
|
|
self.assertEqual(True, sr.spheroid.startswith(sd['spheroid']))
|
|
|
|
self.assertEqual(sd['geographic'], sr.geographic)
|
|
|
|
self.assertEqual(sd['projected'], sr.projected)
|
2009-03-31 06:15:41 +08:00
|
|
|
|
|
|
|
if not (spatialite and not sd['spatialite']):
|
|
|
|
# Can't get 'NAD83 / Texas South Central' from PROJ.4 string
|
|
|
|
# on SpatiaLite
|
|
|
|
self.assertEqual(True, sr.name.startswith(sd['name']))
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
# Testing the SpatialReference object directly.
|
2009-03-31 06:15:41 +08:00
|
|
|
if postgis or spatialite:
|
2008-08-06 02:13:06 +08:00
|
|
|
srs = sr.srs
|
2013-03-09 05:41:16 +08:00
|
|
|
six.assertRegex(self, srs.proj4, sd['proj4_re'])
|
2009-03-31 06:15:41 +08:00
|
|
|
# No `srtext` field in the `spatial_ref_sys` table in SpatiaLite
|
|
|
|
if not spatialite:
|
2012-09-13 22:17:32 +08:00
|
|
|
self.assertTrue(srs.wkt.startswith(sd['srtext']))
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
@no_mysql
|
|
|
|
def test03_ellipsoid(self):
|
|
|
|
"Testing the ellipsoid property."
|
|
|
|
for sd in test_srs:
|
|
|
|
# Getting the ellipsoid and precision parameters.
|
|
|
|
ellps1 = sd['ellipsoid']
|
|
|
|
prec = sd['eprec']
|
|
|
|
|
|
|
|
# Getting our spatial reference and its ellipsoid
|
|
|
|
srs = SpatialRefSys.objects.get(srid=sd['srid'])
|
|
|
|
ellps2 = srs.ellipsoid
|
|
|
|
|
|
|
|
for i in range(3):
|
|
|
|
param1 = ellps1[i]
|
|
|
|
param2 = ellps2[i]
|
|
|
|
self.assertAlmostEqual(ellps1[i], ellps2[i], prec[i])
|
|
|
|
|
|
|
|
def suite():
|
|
|
|
s = unittest.TestSuite()
|
|
|
|
s.addTest(unittest.makeSuite(SpatialRefSysTest))
|
|
|
|
return s
|
|
|
|
|
|
|
|
def run(verbosity=2):
|
|
|
|
unittest.TextTestRunner(verbosity=verbosity).run(suite())
|