test_spatialrefsys.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import re
  2. import unittest
  3. from django.test import skipUnlessDBFeature
  4. from django.utils import six
  5. from .utils import SpatialRefSys, oracle, postgis, spatialite
  6. test_srs = ({
  7. 'srid': 4326,
  8. 'auth_name': ('EPSG', True),
  9. 'auth_srid': 4326,
  10. # Only the beginning, because there are differences depending on installed libs
  11. 'srtext': 'GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84"',
  12. # +ellps=WGS84 has been removed in the 4326 proj string in proj-4.8
  13. 'proj4_re': r'\+proj=longlat (\+ellps=WGS84 )?(\+datum=WGS84 |\+towgs84=0,0,0,0,0,0,0 )\+no_defs ',
  14. 'spheroid': 'WGS 84', 'name': 'WGS 84',
  15. 'geographic': True, 'projected': False, 'spatialite': True,
  16. # From proj's "cs2cs -le" and Wikipedia (semi-minor only)
  17. 'ellipsoid': (6378137.0, 6356752.3, 298.257223563),
  18. 'eprec': (1, 1, 9),
  19. 'wkt': re.sub('[\s+]', '', """
  20. GEOGCS["WGS 84",
  21. DATUM["WGS_1984",
  22. SPHEROID["WGS 84",6378137,298.257223563,
  23. AUTHORITY["EPSG","7030"]],
  24. AUTHORITY["EPSG","6326"]],
  25. PRIMEM["Greenwich",0,
  26. AUTHORITY["EPSG","8901"]],
  27. UNIT["degree",0.01745329251994328,
  28. AUTHORITY["EPSG","9122"]],
  29. AUTHORITY["EPSG","4326"]]
  30. """)
  31. }, {
  32. 'srid': 32140,
  33. 'auth_name': ('EPSG', False),
  34. 'auth_srid': 32140,
  35. 'srtext': (
  36. 'PROJCS["NAD83 / Texas South Central",GEOGCS["NAD83",'
  37. 'DATUM["North_American_Datum_1983",SPHEROID["GRS 1980"'
  38. ),
  39. 'proj4_re': r'\+proj=lcc \+lat_1=30.28333333333333 \+lat_2=28.38333333333333 \+lat_0=27.83333333333333 '
  40. r'\+lon_0=-99 \+x_0=600000 \+y_0=4000000 (\+ellps=GRS80 )?'
  41. r'(\+datum=NAD83 |\+towgs84=0,0,0,0,0,0,0 )?\+units=m \+no_defs ',
  42. 'spheroid': 'GRS 1980', 'name': 'NAD83 / Texas South Central',
  43. 'geographic': False, 'projected': True, 'spatialite': False,
  44. # From proj's "cs2cs -le" and Wikipedia (semi-minor only)
  45. 'ellipsoid': (6378137.0, 6356752.31414, 298.257222101),
  46. 'eprec': (1, 5, 10),
  47. })
  48. @skipUnlessDBFeature("has_spatialrefsys_table")
  49. class SpatialRefSysTest(unittest.TestCase):
  50. def test_get_units(self):
  51. epsg_4326 = next(f for f in test_srs if f['srid'] == 4326)
  52. unit, unit_name = SpatialRefSys().get_units(epsg_4326['wkt'])
  53. self.assertEqual(unit_name, 'degree')
  54. self.assertAlmostEqual(unit, 0.01745329251994328)
  55. def test_retrieve(self):
  56. """
  57. Test retrieval of SpatialRefSys model objects.
  58. """
  59. for sd in test_srs:
  60. srs = SpatialRefSys.objects.get(srid=sd['srid'])
  61. self.assertEqual(sd['srid'], srs.srid)
  62. # Some of the authority names are borked on Oracle, e.g., SRID=32140.
  63. # also, Oracle Spatial seems to add extraneous info to fields, hence the
  64. # the testing with the 'startswith' flag.
  65. auth_name, oracle_flag = sd['auth_name']
  66. if postgis or (oracle and oracle_flag):
  67. self.assertTrue(srs.auth_name.startswith(auth_name))
  68. self.assertEqual(sd['auth_srid'], srs.auth_srid)
  69. # No proj.4 and different srtext on oracle backends :(
  70. if postgis:
  71. self.assertTrue(srs.wkt.startswith(sd['srtext']))
  72. six.assertRegex(self, srs.proj4text, sd['proj4_re'])
  73. def test_osr(self):
  74. """
  75. Test getting OSR objects from SpatialRefSys model objects.
  76. """
  77. for sd in test_srs:
  78. sr = SpatialRefSys.objects.get(srid=sd['srid'])
  79. self.assertTrue(sr.spheroid.startswith(sd['spheroid']))
  80. self.assertEqual(sd['geographic'], sr.geographic)
  81. self.assertEqual(sd['projected'], sr.projected)
  82. if not (spatialite and not sd['spatialite']):
  83. # Can't get 'NAD83 / Texas South Central' from PROJ.4 string
  84. # on SpatiaLite
  85. self.assertTrue(sr.name.startswith(sd['name']))
  86. # Testing the SpatialReference object directly.
  87. if postgis or spatialite:
  88. srs = sr.srs
  89. six.assertRegex(self, srs.proj4, sd['proj4_re'])
  90. self.assertTrue(srs.wkt.startswith(sd['srtext']))
  91. def test_ellipsoid(self):
  92. """
  93. Test the ellipsoid property.
  94. """
  95. for sd in test_srs:
  96. # Getting the ellipsoid and precision parameters.
  97. ellps1 = sd['ellipsoid']
  98. prec = sd['eprec']
  99. # Getting our spatial reference and its ellipsoid
  100. srs = SpatialRefSys.objects.get(srid=sd['srid'])
  101. ellps2 = srs.ellipsoid
  102. for i in range(3):
  103. self.assertAlmostEqual(ellps1[i], ellps2[i], prec[i])
  104. @skipUnlessDBFeature('supports_add_srs_entry')
  105. def test_add_entry(self):
  106. """
  107. Test adding a new entry in the SpatialRefSys model using the
  108. add_srs_entry utility.
  109. """
  110. from django.contrib.gis.utils import add_srs_entry
  111. add_srs_entry(3857)
  112. self.assertTrue(
  113. SpatialRefSys.objects.filter(srid=3857).exists()
  114. )
  115. srs = SpatialRefSys.objects.get(srid=3857)
  116. self.assertTrue(
  117. SpatialRefSys.get_spheroid(srs.wkt).startswith('SPHEROID[')
  118. )