PageRenderTime 45ms CodeModel.GetById 36ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/django/contrib/gis/tests/test_spatialrefsys.py

https://code.google.com/p/mango-py/
Python | 115 lines | 89 code | 16 blank | 10 comment | 24 complexity | 77ff8bf38938ca9045dac55ea6562767 MD5 | raw file
  1from django.db import connection
  2from django.contrib.gis.gdal import GDAL_VERSION
  3from django.contrib.gis.tests.utils import mysql, no_mysql, oracle, postgis, spatialite
  4from django.utils import unittest
  5
  6
  7test_srs = ({'srid' : 4326,
  8             'auth_name' : ('EPSG', True),
  9             'auth_srid' : 4326,
 10             'srtext' : 'GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],TOWGS84[0,0,0,0,0,0,0],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.01745329251994328,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4326"]]',
 11             'srtext14' : 'GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.01745329251994328,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4326"]]',
 12             'proj4' : '+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs ',
 13             'spheroid' : 'WGS 84', 'name' : 'WGS 84',
 14             'geographic' : True, 'projected' : False, 'spatialite' : True,
 15             'ellipsoid' : (6378137.0, 6356752.3, 298.257223563), # From proj's "cs2cs -le" and Wikipedia (semi-minor only)
 16             'eprec' : (1, 1, 9),
 17             },
 18            {'srid' : 32140,
 19             'auth_name' : ('EPSG', False),
 20             'auth_srid' : 32140,
 21             'srtext' : 'PROJCS["NAD83 / Texas South Central",GEOGCS["NAD83",DATUM["North_American_Datum_1983",SPHEROID["GRS 1980",6378137,298.257222101,AUTHORITY["EPSG","7019"]],AUTHORITY["EPSG","6269"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.01745329251994328,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4269"]],PROJECTION["Lambert_Conformal_Conic_2SP"],PARAMETER["standard_parallel_1",30.28333333333333],PARAMETER["standard_parallel_2",28.38333333333333],PARAMETER["latitude_of_origin",27.83333333333333],PARAMETER["central_meridian",-99],PARAMETER["false_easting",600000],PARAMETER["false_northing",4000000],UNIT["metre",1,AUTHORITY["EPSG","9001"]],AUTHORITY["EPSG","32140"]]',
 22             'srtext14': 'PROJCS["NAD83 / Texas South Central",GEOGCS["NAD83",DATUM["North_American_Datum_1983",SPHEROID["GRS 1980",6378137,298.257222101,AUTHORITY["EPSG","7019"]],AUTHORITY["EPSG","6269"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.01745329251994328,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4269"]],UNIT["metre",1,AUTHORITY["EPSG","9001"]],PROJECTION["Lambert_Conformal_Conic_2SP"],PARAMETER["standard_parallel_1",30.28333333333333],PARAMETER["standard_parallel_2",28.38333333333333],PARAMETER["latitude_of_origin",27.83333333333333],PARAMETER["central_meridian",-99],PARAMETER["false_easting",600000],PARAMETER["false_northing",4000000],AUTHORITY["EPSG","32140"],AXIS["X",EAST],AXIS["Y",NORTH]]',
 23             'proj4' : '+proj=lcc +lat_1=30.28333333333333 +lat_2=28.38333333333333 +lat_0=27.83333333333333 +lon_0=-99 +x_0=600000 +y_0=4000000 +ellps=GRS80 +datum=NAD83 +units=m +no_defs ',
 24             'spheroid' : 'GRS 1980', 'name' : 'NAD83 / Texas South Central',
 25             'geographic' : False, 'projected' : True, 'spatialite' : False,
 26             'ellipsoid' : (6378137.0, 6356752.31414, 298.257222101), # From proj's "cs2cs -le" and Wikipedia (semi-minor only)
 27             'eprec' : (1, 5, 10),
 28             },
 29            )
 30
 31if oracle:
 32    from django.contrib.gis.db.backends.oracle.models import SpatialRefSys
 33elif postgis:
 34    from django.contrib.gis.db.backends.postgis.models import SpatialRefSys
 35elif spatialite:
 36    from django.contrib.gis.db.backends.spatialite.models import SpatialRefSys
 37
 38class SpatialRefSysTest(unittest.TestCase):
 39
 40    @no_mysql
 41    def test01_retrieve(self):
 42        "Testing retrieval of SpatialRefSys model objects."
 43        for sd in test_srs:
 44            srs = SpatialRefSys.objects.get(srid=sd['srid'])
 45            self.assertEqual(sd['srid'], srs.srid)
 46
 47            # Some of the authority names are borked on Oracle, e.g., SRID=32140.
 48            #  also, Oracle Spatial seems to add extraneous info to fields, hence the
 49            #  the testing with the 'startswith' flag.
 50            auth_name, oracle_flag = sd['auth_name']
 51            if postgis or (oracle and oracle_flag):
 52                self.assertEqual(True, srs.auth_name.startswith(auth_name))
 53
 54            self.assertEqual(sd['auth_srid'], srs.auth_srid)
 55
 56            # No proj.4 and different srtext on oracle backends :(
 57            if postgis:
 58                if connection.ops.spatial_version >= (1, 4, 0):
 59                    srtext = sd['srtext14']
 60                else:
 61                    srtext = sd['srtext']
 62                self.assertEqual(srtext, srs.wkt)
 63                self.assertEqual(sd['proj4'], srs.proj4text)
 64
 65    @no_mysql
 66    def test02_osr(self):
 67        "Testing getting OSR objects from SpatialRefSys model objects."
 68        for sd in test_srs:
 69            sr = SpatialRefSys.objects.get(srid=sd['srid'])
 70            self.assertEqual(True, sr.spheroid.startswith(sd['spheroid']))
 71            self.assertEqual(sd['geographic'], sr.geographic)
 72            self.assertEqual(sd['projected'], sr.projected)
 73
 74            if not (spatialite and not sd['spatialite']):
 75                # Can't get 'NAD83 / Texas South Central' from PROJ.4 string
 76                # on SpatiaLite
 77                self.assertEqual(True, sr.name.startswith(sd['name']))
 78
 79            # Testing the SpatialReference object directly.
 80            if postgis or spatialite:
 81                srs = sr.srs
 82                if GDAL_VERSION <= (1, 8):
 83                    self.assertEqual(sd['proj4'], srs.proj4)
 84                # No `srtext` field in the `spatial_ref_sys` table in SpatiaLite
 85                if not spatialite:
 86                    if connection.ops.spatial_version >= (1, 4, 0):
 87                        srtext = sd['srtext14']
 88                    else:
 89                        srtext = sd['srtext']
 90                    self.assertEqual(srtext, srs.wkt)
 91
 92    @no_mysql
 93    def test03_ellipsoid(self):
 94        "Testing the ellipsoid property."
 95        for sd in test_srs:
 96            # Getting the ellipsoid and precision parameters.
 97            ellps1 = sd['ellipsoid']
 98            prec = sd['eprec']
 99
100            # Getting our spatial reference and its ellipsoid
101            srs = SpatialRefSys.objects.get(srid=sd['srid'])
102            ellps2 = srs.ellipsoid
103
104            for i in range(3):
105                param1 = ellps1[i]
106                param2 = ellps2[i]
107                self.assertAlmostEqual(ellps1[i], ellps2[i], prec[i])
108
109def suite():
110    s = unittest.TestSuite()
111    s.addTest(unittest.makeSuite(SpatialRefSysTest))
112    return s
113
114def run(verbosity=2):
115    unittest.TextTestRunner(verbosity=verbosity).run(suite())