test_spatialrefsys.py 5.0 KB

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