web/lib/django/contrib/gis/tests/test_spatialrefsys.py
changeset 0 0d40e90630ef
child 29 cc9b7e14412b
equal deleted inserted replaced
-1:000000000000 0:0d40e90630ef
       
     1 import unittest
       
     2 from django.contrib.gis.db.backend import SpatialBackend
       
     3 from django.contrib.gis.tests.utils import mysql, no_mysql, oracle, postgis, spatialite
       
     4 if not mysql:
       
     5     from django.contrib.gis.models import SpatialRefSys
       
     6 
       
     7 test_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 
       
    31 if SpatialBackend.postgis:
       
    32     major, minor1, minor2 = SpatialBackend.version
       
    33     POSTGIS_14 = major >=1 and minor1 >= 4
       
    34 
       
    35 class SpatialRefSysTest(unittest.TestCase):
       
    36 
       
    37     @no_mysql
       
    38     def test01_retrieve(self):
       
    39         "Testing retrieval of SpatialRefSys model objects."
       
    40         for sd in test_srs:
       
    41             srs = SpatialRefSys.objects.get(srid=sd['srid'])
       
    42             self.assertEqual(sd['srid'], srs.srid)
       
    43 
       
    44             # Some of the authority names are borked on Oracle, e.g., SRID=32140.
       
    45             #  also, Oracle Spatial seems to add extraneous info to fields, hence the
       
    46             #  the testing with the 'startswith' flag.
       
    47             auth_name, oracle_flag = sd['auth_name']
       
    48             if postgis or (oracle and oracle_flag):
       
    49                 self.assertEqual(True, srs.auth_name.startswith(auth_name))
       
    50                 
       
    51             self.assertEqual(sd['auth_srid'], srs.auth_srid)
       
    52 
       
    53             # No proj.4 and different srtext on oracle backends :(
       
    54             if postgis:
       
    55                 if POSTGIS_14:
       
    56                     srtext = sd['srtext14']
       
    57                 else:
       
    58                     srtext = sd['srtext']
       
    59                 self.assertEqual(srtext, srs.wkt)
       
    60                 self.assertEqual(sd['proj4'], srs.proj4text)
       
    61 
       
    62     @no_mysql
       
    63     def test02_osr(self):
       
    64         "Testing getting OSR objects from SpatialRefSys model objects."
       
    65         for sd in test_srs:
       
    66             sr = SpatialRefSys.objects.get(srid=sd['srid'])
       
    67             self.assertEqual(True, sr.spheroid.startswith(sd['spheroid']))
       
    68             self.assertEqual(sd['geographic'], sr.geographic)
       
    69             self.assertEqual(sd['projected'], sr.projected)
       
    70 
       
    71             if not (spatialite and not sd['spatialite']):
       
    72                 # Can't get 'NAD83 / Texas South Central' from PROJ.4 string
       
    73                 # on SpatiaLite
       
    74                 self.assertEqual(True, sr.name.startswith(sd['name']))
       
    75 
       
    76             # Testing the SpatialReference object directly.
       
    77             if postgis or spatialite:
       
    78                 srs = sr.srs
       
    79                 self.assertEqual(sd['proj4'], srs.proj4)
       
    80                 # No `srtext` field in the `spatial_ref_sys` table in SpatiaLite
       
    81                 if not spatialite:
       
    82                     if POSTGIS_14:
       
    83                         srtext = sd['srtext14']
       
    84                     else:
       
    85                         srtext = sd['srtext']
       
    86                     self.assertEqual(srtext, srs.wkt)
       
    87 
       
    88     @no_mysql
       
    89     def test03_ellipsoid(self):
       
    90         "Testing the ellipsoid property."
       
    91         for sd in test_srs:
       
    92             # Getting the ellipsoid and precision parameters.
       
    93             ellps1 = sd['ellipsoid']
       
    94             prec = sd['eprec']
       
    95 
       
    96             # Getting our spatial reference and its ellipsoid
       
    97             srs = SpatialRefSys.objects.get(srid=sd['srid'])
       
    98             ellps2 = srs.ellipsoid
       
    99 
       
   100             for i in range(3):
       
   101                 param1 = ellps1[i]
       
   102                 param2 = ellps2[i]
       
   103                 self.assertAlmostEqual(ellps1[i], ellps2[i], prec[i])
       
   104 
       
   105 def suite():
       
   106     s = unittest.TestSuite()
       
   107     s.addTest(unittest.makeSuite(SpatialRefSysTest))
       
   108     return s
       
   109 
       
   110 def run(verbosity=2):
       
   111     unittest.TextTestRunner(verbosity=verbosity).run(suite())