app/django/contrib/gis/tests/test_spatialrefsys.py
changeset 323 ff1a9aa48cfd
equal deleted inserted replaced
322:6641e941ef1e 323:ff1a9aa48cfd
       
     1 import unittest
       
     2 from django.contrib.gis.tests.utils import mysql, no_mysql, oracle, postgis
       
     3 if not mysql:
       
     4     from django.contrib.gis.models import SpatialRefSys
       
     5 
       
     6 test_srs = ({'srid' : 4326,
       
     7              'auth_name' : ('EPSG', True),
       
     8              'auth_srid' : 4326,
       
     9              '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"]]',
       
    10              'proj4' : '+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs ',
       
    11              'spheroid' : 'WGS 84', 'name' : 'WGS 84', 
       
    12              'geographic' : True, 'projected' : False,
       
    13              'ellipsoid' : (6378137.0, 6356752.3, 298.257223563), # From proj's "cs2cs -le" and Wikipedia (semi-minor only)
       
    14              'eprec' : (1, 1, 9),
       
    15              },
       
    16             {'srid' : 32140,
       
    17              'auth_name' : ('EPSG', False),
       
    18              'auth_srid' : 32140,
       
    19              '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"]]',
       
    20              '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 ',
       
    21              'spheroid' : 'GRS 1980', 'name' : 'NAD83 / Texas South Central',
       
    22              'geographic' : False, 'projected' : True,
       
    23              'ellipsoid' : (6378137.0, 6356752.31414, 298.257222101), # From proj's "cs2cs -le" and Wikipedia (semi-minor only)
       
    24              'eprec' : (1, 5, 10),
       
    25              },
       
    26             )
       
    27 
       
    28 class SpatialRefSysTest(unittest.TestCase):
       
    29 
       
    30     @no_mysql
       
    31     def test01_retrieve(self):
       
    32         "Testing retrieval of SpatialRefSys model objects."
       
    33         for sd in test_srs:
       
    34             srs = SpatialRefSys.objects.get(srid=sd['srid'])
       
    35             self.assertEqual(sd['srid'], srs.srid)
       
    36 
       
    37             # Some of the authority names are borked on Oracle, e.g., SRID=32140.
       
    38             #  also, Oracle Spatial seems to add extraneous info to fields, hence the
       
    39             #  the testing with the 'startswith' flag.
       
    40             auth_name, oracle_flag = sd['auth_name']
       
    41             if postgis or (oracle and oracle_flag):
       
    42                 self.assertEqual(True, srs.auth_name.startswith(auth_name))
       
    43                 
       
    44             self.assertEqual(sd['auth_srid'], srs.auth_srid)
       
    45 
       
    46             # No proj.4 and different srtext on oracle backends :(
       
    47             if postgis:
       
    48                 self.assertEqual(sd['srtext'], srs.wkt)
       
    49                 self.assertEqual(sd['proj4'], srs.proj4text)
       
    50 
       
    51     @no_mysql
       
    52     def test02_osr(self):
       
    53         "Testing getting OSR objects from SpatialRefSys model objects."
       
    54         for sd in test_srs:
       
    55             sr = SpatialRefSys.objects.get(srid=sd['srid'])
       
    56             self.assertEqual(True, sr.spheroid.startswith(sd['spheroid']))
       
    57             self.assertEqual(sd['geographic'], sr.geographic)
       
    58             self.assertEqual(sd['projected'], sr.projected)
       
    59             self.assertEqual(True, sr.name.startswith(sd['name']))
       
    60 
       
    61             # Testing the SpatialReference object directly.
       
    62             if postgis:
       
    63                 srs = sr.srs
       
    64                 self.assertEqual(sd['proj4'], srs.proj4)
       
    65                 self.assertEqual(sd['srtext'], srs.wkt)
       
    66 
       
    67     @no_mysql
       
    68     def test03_ellipsoid(self):
       
    69         "Testing the ellipsoid property."
       
    70         for sd in test_srs:
       
    71             # Getting the ellipsoid and precision parameters.
       
    72             ellps1 = sd['ellipsoid']
       
    73             prec = sd['eprec']
       
    74 
       
    75             # Getting our spatial reference and its ellipsoid
       
    76             srs = SpatialRefSys.objects.get(srid=sd['srid'])
       
    77             ellps2 = srs.ellipsoid
       
    78 
       
    79             for i in range(3):
       
    80                 param1 = ellps1[i]
       
    81                 param2 = ellps2[i]
       
    82                 self.assertAlmostEqual(ellps1[i], ellps2[i], prec[i])
       
    83 
       
    84 def suite():
       
    85     s = unittest.TestSuite()
       
    86     s.addTest(unittest.makeSuite(SpatialRefSysTest))
       
    87     return s
       
    88 
       
    89 def run(verbosity=2):
       
    90     unittest.TextTestRunner(verbosity=verbosity).run(suite())