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