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