PageRenderTime 112ms CodeModel.GetById 40ms app.highlight 28ms RepoModel.GetById 33ms app.codeStats 7ms

/django/contrib/gis/gdal/tests/test_srs.py

https://code.google.com/p/mango-py/
Python | 165 lines | 142 code | 20 blank | 3 comment | 39 complexity | e157aaacf7c2d223d0a7ea4f4bf24184 MD5 | raw file
  1from django.contrib.gis.gdal import SpatialReference, CoordTransform, OGRException, SRSException
  2from django.utils import unittest
  3
  4
  5class TestSRS:
  6    def __init__(self, wkt, **kwargs):
  7        self.wkt = wkt
  8        for key, value in kwargs.items():
  9            setattr(self, key, value)
 10
 11# Some Spatial Reference examples
 12srlist = (TestSRS('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"]]',
 13                  proj='+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs ',
 14                  epsg=4326, projected=False, geographic=True, local=False,
 15                  lin_name='unknown', ang_name='degree', lin_units=1.0, ang_units=0.0174532925199,
 16                  auth={'GEOGCS' : ('EPSG', '4326'), 'spheroid' : ('EPSG', '7030')},
 17                  attr=(('DATUM', 'WGS_1984'), (('SPHEROID', 1), '6378137'),('primem|authority', 'EPSG'),),
 18                  ),
 19          TestSRS('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                  proj=None, epsg=32140, projected=True, geographic=False, local=False,
 21                  lin_name='metre', ang_name='degree', lin_units=1.0, ang_units=0.0174532925199,
 22                  auth={'PROJCS' : ('EPSG', '32140'), 'spheroid' : ('EPSG', '7019'), 'unit' : ('EPSG', '9001'),},
 23                  attr=(('DATUM', 'North_American_Datum_1983'),(('SPHEROID', 2), '298.257222101'),('PROJECTION','Lambert_Conformal_Conic_2SP'),),
 24                  ),
 25          TestSRS('PROJCS["NAD_1983_StatePlane_Texas_South_Central_FIPS_4204_Feet",GEOGCS["GCS_North_American_1983",DATUM["North_American_Datum_1983",SPHEROID["GRS_1980",6378137.0,298.257222101]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]],PROJECTION["Lambert_Conformal_Conic_2SP"],PARAMETER["False_Easting",1968500.0],PARAMETER["False_Northing",13123333.33333333],PARAMETER["Central_Meridian",-99.0],PARAMETER["Standard_Parallel_1",28.38333333333333],PARAMETER["Standard_Parallel_2",30.28333333333334],PARAMETER["Latitude_Of_Origin",27.83333333333333],UNIT["Foot_US",0.3048006096012192]]',
 26                  proj=None, epsg=None, projected=True, geographic=False, local=False,
 27                  lin_name='Foot_US', ang_name='Degree', lin_units=0.3048006096012192, ang_units=0.0174532925199,
 28                  auth={'PROJCS' : (None, None),},
 29                  attr=(('PROJCS|GeOgCs|spheroid', 'GRS_1980'),(('projcs', 9), 'UNIT'), (('projcs', 11), None),),
 30                  ),
 31          # This is really ESRI format, not WKT -- but the import should work the same
 32          TestSRS('LOCAL_CS["Non-Earth (Meter)",LOCAL_DATUM["Local Datum",0],UNIT["Meter",1.0],AXIS["X",EAST],AXIS["Y",NORTH]]',
 33                  esri=True, proj=None, epsg=None, projected=False, geographic=False, local=True,
 34                  lin_name='Meter', ang_name='degree', lin_units=1.0, ang_units=0.0174532925199,
 35                  attr=(('LOCAL_DATUM', 'Local Datum'), ('unit', 'Meter')),
 36                  ),
 37          )
 38
 39# Well-Known Names
 40well_known = (TestSRS('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"]]', wk='WGS84', name='WGS 84', attrs=(('GEOGCS|AUTHORITY', 1, '4326'), ('SPHEROID', 'WGS 84'))),
 41              TestSRS('GEOGCS["WGS 72",DATUM["WGS_1972",SPHEROID["WGS 72",6378135,298.26,AUTHORITY["EPSG","7043"]],AUTHORITY["EPSG","6322"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.01745329251994328,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4322"]]', wk='WGS72', name='WGS 72', attrs=(('GEOGCS|AUTHORITY', 1, '4322'), ('SPHEROID', 'WGS 72'))),
 42              TestSRS('GEOGCS["NAD27",DATUM["North_American_Datum_1927",SPHEROID["Clarke 1866",6378206.4,294.9786982138982,AUTHORITY["EPSG","7008"]],AUTHORITY["EPSG","6267"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.01745329251994328,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4267"]]', wk='NAD27', name='NAD27', attrs=(('GEOGCS|AUTHORITY', 1, '4267'), ('SPHEROID', 'Clarke 1866'))),
 43              TestSRS('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"]]', wk='NAD83', name='NAD83', attrs=(('GEOGCS|AUTHORITY', 1, '4269'), ('SPHEROID', 'GRS 1980'))),
 44              TestSRS('PROJCS["NZGD49 / Karamea Circuit",GEOGCS["NZGD49",DATUM["New_Zealand_Geodetic_Datum_1949",SPHEROID["International 1924",6378388,297,AUTHORITY["EPSG","7022"]],TOWGS84[59.47,-5.04,187.44,0.47,-0.1,1.024,-4.5993],AUTHORITY["EPSG","6272"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.01745329251994328,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4272"]],PROJECTION["Transverse_Mercator"],PARAMETER["latitude_of_origin",-41.28991152777778],PARAMETER["central_meridian",172.1090281944444],PARAMETER["scale_factor",1],PARAMETER["false_easting",300000],PARAMETER["false_northing",700000],UNIT["metre",1,AUTHORITY["EPSG","9001"]],AUTHORITY["EPSG","27216"]]', wk='EPSG:27216', name='NZGD49 / Karamea Circuit', attrs=(('PROJECTION','Transverse_Mercator'), ('SPHEROID', 'International 1924'))),
 45              )
 46
 47bad_srlist = ('Foobar', 'OOJCS["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"]]',)
 48
 49class SpatialRefTest(unittest.TestCase):
 50
 51    def test01_wkt(self):
 52        "Testing initialization on valid OGC WKT."
 53        for s in srlist:
 54            srs = SpatialReference(s.wkt)
 55
 56    def test02_bad_wkt(self):
 57        "Testing initialization on invalid WKT."
 58        for bad in bad_srlist:
 59            try:
 60                srs = SpatialReference(bad)
 61                srs.validate()
 62            except (SRSException, OGRException):
 63                pass
 64            else:
 65                self.fail('Should not have initialized on bad WKT "%s"!')
 66
 67    def test03_get_wkt(self):
 68        "Testing getting the WKT."
 69        for s in srlist:
 70            srs = SpatialReference(s.wkt)
 71            self.assertEqual(s.wkt, srs.wkt)
 72
 73    def test04_proj(self):
 74        "Test PROJ.4 import and export."
 75        for s in srlist:
 76            if s.proj:
 77                srs1 = SpatialReference(s.wkt)
 78                srs2 = SpatialReference(s.proj)
 79                self.assertEqual(srs1.proj, srs2.proj)
 80
 81    def test05_epsg(self):
 82        "Test EPSG import."
 83        for s in srlist:
 84            if s.epsg:
 85                srs1 = SpatialReference(s.wkt)
 86                srs2 = SpatialReference(s.epsg)
 87                srs3 = SpatialReference(str(s.epsg))
 88                srs4 = SpatialReference('EPSG:%d' % s.epsg)
 89                for srs in (srs1, srs2, srs3, srs4):
 90                    for attr, expected in s.attr:
 91                        self.assertEqual(expected, srs[attr])
 92
 93    def test07_boolean_props(self):
 94        "Testing the boolean properties."
 95        for s in srlist:
 96            srs = SpatialReference(s.wkt)
 97            self.assertEqual(s.projected, srs.projected)
 98            self.assertEqual(s.geographic, srs.geographic)
 99
100    def test08_angular_linear(self):
101        "Testing the linear and angular units routines."
102        for s in srlist:
103            srs = SpatialReference(s.wkt)
104            self.assertEqual(s.ang_name, srs.angular_name)
105            self.assertEqual(s.lin_name, srs.linear_name)
106            self.assertAlmostEqual(s.ang_units, srs.angular_units, 9)
107            self.assertAlmostEqual(s.lin_units, srs.linear_units, 9)
108
109    def test09_authority(self):
110        "Testing the authority name & code routines."
111        for s in srlist:
112            if hasattr(s, 'auth'):
113                srs = SpatialReference(s.wkt)
114                for target, tup in s.auth.items():
115                    self.assertEqual(tup[0], srs.auth_name(target))
116                    self.assertEqual(tup[1], srs.auth_code(target))
117
118    def test10_attributes(self):
119        "Testing the attribute retrieval routines."
120        for s in srlist:
121            srs = SpatialReference(s.wkt)
122            for tup in s.attr:
123                att = tup[0] # Attribute to test
124                exp = tup[1] # Expected result
125                self.assertEqual(exp, srs[att])
126
127    def test11_wellknown(self):
128        "Testing Well Known Names of Spatial References."
129        for s in well_known:
130            srs = SpatialReference(s.wk)
131            self.assertEqual(s.name, srs.name)
132            for tup in s.attrs:
133                if len(tup) == 2:
134                    key = tup[0]
135                    exp = tup[1]
136                elif len(tup) == 3:
137                    key = tup[:2]
138                    exp = tup[2]
139                self.assertEqual(srs[key], exp)
140
141    def test12_coordtransform(self):
142        "Testing initialization of a CoordTransform."
143        target = SpatialReference('WGS84')
144        for s in srlist:
145            if s.proj:
146                ct = CoordTransform(SpatialReference(s.wkt), target)
147
148    def test13_attr_value(self):
149        "Testing the attr_value() method."
150        s1 = SpatialReference('WGS84')
151        self.assertRaises(TypeError, s1.__getitem__, 0)
152        self.assertRaises(TypeError, s1.__getitem__, ('GEOGCS', 'foo'))
153        self.assertEqual('WGS 84', s1['GEOGCS'])
154        self.assertEqual('WGS_1984', s1['DATUM'])
155        self.assertEqual('EPSG', s1['AUTHORITY'])
156        self.assertEqual(4326, int(s1['AUTHORITY', 1]))
157        self.assertEqual(None, s1['FOOBAR'])
158
159def suite():
160    s = unittest.TestSuite()
161    s.addTest(unittest.makeSuite(SpatialRefTest))
162    return s
163
164def run(verbosity=2):
165    unittest.TextTestRunner(verbosity=verbosity).run(suite())