test_spatialrefsys.py 4.8 KB

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