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

https://code.google.com/p/mango-py/ · Python · 115 lines · 89 code · 16 blank · 10 comment · 21 complexity · 77ff8bf38938ca9045dac55ea6562767 MD5 · raw file

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