| 1 |
from django.contrib.gis.gdal import ( |
| 2 |
GDAL_VERSION, |
| 3 |
AxisOrder, |
| 4 |
CoordTransform, |
| 5 |
GDALException, |
| 6 |
SpatialReference, |
| 7 |
SRSException, |
| 8 |
) |
| 9 |
from django.contrib.gis.geos import GEOSGeometry |
| 10 |
from django.test import SimpleTestCase |
| 11 |
|
| 12 |
|
| 13 |
class TestSRS: |
| 14 |
def __init__(self, wkt, **kwargs): |
| 15 |
self.wkt = wkt |
| 16 |
for key, value in kwargs.items(): |
| 17 |
setattr(self, key, value) |
| 18 |
|
| 19 |
|
| 20 |
# Some Spatial Reference examples |
| 21 |
srlist = ( |
| 22 |
TestSRS( |
| 23 |
'GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,' |
| 24 |
'AUTHORITY["EPSG","7030"]],TOWGS84[0,0,0,0,0,0,0],AUTHORITY["EPSG","6326"]],' |
| 25 |
'PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",' |
| 26 |
'0.0174532925199433,AUTHORITY["EPSG","9122"]],AXIS["Latitude",NORTH],' |
| 27 |
'AXIS["Longitude",EAST],AUTHORITY["EPSG","4326"]]', |
| 28 |
epsg=4326, |
| 29 |
projected=False, |
| 30 |
geographic=True, |
| 31 |
local=False, |
| 32 |
lin_name="unknown", |
| 33 |
ang_name="degree", |
| 34 |
lin_units=1.0, |
| 35 |
ang_units=0.0174532925199, |
| 36 |
auth={ |
| 37 |
None: ("EPSG", "4326"), # Top-level authority. |
| 38 |
"GEOGCS": ("EPSG", "4326"), |
| 39 |
"spheroid": ("EPSG", "7030"), |
| 40 |
}, |
| 41 |
attr=( |
| 42 |
("DATUM", "WGS_1984"), |
| 43 |
(("SPHEROID", 1), "6378137"), |
| 44 |
("primem|authority", "EPSG"), |
| 45 |
), |
| 46 |
), |
| 47 |
TestSRS( |
| 48 |
'PROJCS["NAD83 / Texas South Central",' |
| 49 |
'GEOGCS["NAD83",DATUM["North_American_Datum_1983",' |
| 50 |
'SPHEROID["GRS 1980",6378137,298.257222101,AUTHORITY["EPSG","7019"]],' |
| 51 |
'AUTHORITY["EPSG","6269"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],' |
| 52 |
'UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],' |
| 53 |
'AUTHORITY["EPSG","4269"]],PROJECTION["Lambert_Conformal_Conic_2SP"],' |
| 54 |
'PARAMETER["standard_parallel_1",30.2833333333333],' |
| 55 |
'PARAMETER["standard_parallel_2",28.3833333333333],' |
| 56 |
'PARAMETER["latitude_of_origin",27.8333333333333],' |
| 57 |
'PARAMETER["central_meridian",-99],PARAMETER["false_easting",600000],' |
| 58 |
'PARAMETER["false_northing",4000000],UNIT["metre",1,AUTHORITY["EPSG","9001"]],' |
| 59 |
'AXIS["Easting",EAST],AXIS["Northing",NORTH],AUTHORITY["EPSG","32140"]]', |
| 60 |
epsg=32140, |
| 61 |
projected=True, |
| 62 |
geographic=False, |
| 63 |
local=False, |
| 64 |
lin_name="metre", |
| 65 |
ang_name="degree", |
| 66 |
lin_units=1.0, |
| 67 |
ang_units=0.0174532925199, |
| 68 |
auth={ |
| 69 |
None: ("EPSG", "32140"), # Top-level authority. |
| 70 |
"PROJCS": ("EPSG", "32140"), |
| 71 |
"spheroid": ("EPSG", "7019"), |
| 72 |
"unit": ("EPSG", "9001"), |
| 73 |
}, |
| 74 |
attr=( |
| 75 |
("DATUM", "North_American_Datum_1983"), |
| 76 |
(("SPHEROID", 2), "298.257222101"), |
| 77 |
("PROJECTION", "Lambert_Conformal_Conic_2SP"), |
| 78 |
), |
| 79 |
), |
| 80 |
TestSRS( |
| 81 |
'PROJCS["NAD83 / Texas South Central (ftUS)",' |
| 82 |
'GEOGCS["NAD83",DATUM["North_American_Datum_1983",' |
| 83 |
'SPHEROID["GRS 1980",6378137,298.257222101,AUTHORITY["EPSG","7019"]],' |
| 84 |
'AUTHORITY["EPSG","6269"]],' |
| 85 |
'PRIMEM["Greenwich",0],' |
| 86 |
'UNIT["Degree",0.0174532925199433]],PROJECTION["Lambert_Conformal_Conic_2SP"],' |
| 87 |
'PARAMETER["false_easting",1968500],' |
| 88 |
'PARAMETER["false_northing",13123333.3333333],' |
| 89 |
'PARAMETER["central_meridian",-99],' |
| 90 |
'PARAMETER["standard_parallel_1",28.3833333333333],' |
| 91 |
'PARAMETER["standard_parallel_2",30.2833333333333],' |
| 92 |
'PARAMETER["latitude_of_origin",27.8333333333333],' |
| 93 |
'UNIT["US survey foot",0.304800609601219],AXIS["Easting",EAST],' |
| 94 |
'AXIS["Northing",NORTH]]', |
| 95 |
epsg=None, |
| 96 |
projected=True, |
| 97 |
geographic=False, |
| 98 |
local=False, |
| 99 |
lin_name="US survey foot", |
| 100 |
ang_name="Degree", |
| 101 |
lin_units=0.3048006096012192, |
| 102 |
ang_units=0.0174532925199, |
| 103 |
auth={ |
| 104 |
None: (None, None), # Top-level authority. |
| 105 |
"PROJCS": (None, None), |
| 106 |
}, |
| 107 |
attr=( |
| 108 |
("PROJCS|GeOgCs|spheroid", "GRS 1980"), |
| 109 |
(("projcs", 9), "UNIT"), |
| 110 |
(("projcs", 11), "AXIS"), |
| 111 |
), |
| 112 |
), |
| 113 |
# This is really ESRI format, not WKT -- but the import should work the same |
| 114 |
TestSRS( |
| 115 |
'LOCAL_CS["Non-Earth (Meter)",LOCAL_DATUM["Local Datum",32767],' |
| 116 |
'UNIT["Meter",1],AXIS["X",EAST],AXIS["Y",NORTH]]', |
| 117 |
esri=True, |
| 118 |
epsg=None, |
| 119 |
projected=False, |
| 120 |
geographic=False, |
| 121 |
local=True, |
| 122 |
lin_name="Meter", |
| 123 |
ang_name="degree", |
| 124 |
lin_units=1.0, |
| 125 |
ang_units=0.0174532925199, |
| 126 |
attr=(("LOCAL_DATUM", "Local Datum"),), |
| 127 |
), |
| 128 |
) |
| 129 |
|
| 130 |
# Well-Known Names |
| 131 |
well_known = ( |
| 132 |
TestSRS( |
| 133 |
'GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,' |
| 134 |
'AUTHORITY["EPSG","7030"]],TOWGS84[0,0,0,0,0,0,0],AUTHORITY["EPSG","6326"]],' |
| 135 |
'PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],' |
| 136 |
'UNIT["degree",0.01745329251994328,' |
| 137 |
'AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4326"]]', |
| 138 |
wk="WGS84", |
| 139 |
name="WGS 84", |
| 140 |
attrs=(("GEOGCS|AUTHORITY", 1, "4326"), ("SPHEROID", "WGS 84")), |
| 141 |
), |
| 142 |
TestSRS( |
| 143 |
'GEOGCS["WGS 72",DATUM["WGS_1972",SPHEROID["WGS 72",6378135,298.26,' |
| 144 |
'AUTHORITY["EPSG","7043"]],AUTHORITY["EPSG","6322"]],' |
| 145 |
'PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],' |
| 146 |
'UNIT["degree",0.01745329251994328,AUTHORITY["EPSG","9122"]],' |
| 147 |
'AUTHORITY["EPSG","4322"]]', |
| 148 |
wk="WGS72", |
| 149 |
name="WGS 72", |
| 150 |
attrs=(("GEOGCS|AUTHORITY", 1, "4322"), ("SPHEROID", "WGS 72")), |
| 151 |
), |
| 152 |
TestSRS( |
| 153 |
'GEOGCS["NAD27",DATUM["North_American_Datum_1927",' |
| 154 |
'SPHEROID["Clarke 1866",6378206.4,294.9786982138982,' |
| 155 |
'AUTHORITY["EPSG","7008"]],AUTHORITY["EPSG","6267"]],' |
| 156 |
'PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],' |
| 157 |
'UNIT["degree",0.01745329251994328,AUTHORITY["EPSG","9122"]],' |
| 158 |
'AUTHORITY["EPSG","4267"]]', |
| 159 |
wk="NAD27", |
| 160 |
name="NAD27", |
| 161 |
attrs=(("GEOGCS|AUTHORITY", 1, "4267"), ("SPHEROID", "Clarke 1866")), |
| 162 |
), |
| 163 |
TestSRS( |
| 164 |
'GEOGCS["NAD83",DATUM["North_American_Datum_1983",' |
| 165 |
'SPHEROID["GRS 1980",6378137,298.257222101,' |
| 166 |
'AUTHORITY["EPSG","7019"]],AUTHORITY["EPSG","6269"]],' |
| 167 |
'PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],' |
| 168 |
'UNIT["degree",0.01745329251994328,AUTHORITY["EPSG","9122"]],' |
| 169 |
'AUTHORITY["EPSG","4269"]]', |
| 170 |
wk="NAD83", |
| 171 |
name="NAD83", |
| 172 |
attrs=(("GEOGCS|AUTHORITY", 1, "4269"), ("SPHEROID", "GRS 1980")), |
| 173 |
), |
| 174 |
TestSRS( |
| 175 |
'PROJCS["NZGD49 / Karamea Circuit",GEOGCS["NZGD49",' |
| 176 |
'DATUM["New_Zealand_Geodetic_Datum_1949",' |
| 177 |
'SPHEROID["International 1924",6378388,297,' |
| 178 |
'AUTHORITY["EPSG","7022"]],' |
| 179 |
"TOWGS84[59.47,-5.04,187.44,0.47,-0.1,1.024,-4.5993]," |
| 180 |
'AUTHORITY["EPSG","6272"]],PRIMEM["Greenwich",0,' |
| 181 |
'AUTHORITY["EPSG","8901"]],UNIT["degree",0.01745329251994328,' |
| 182 |
'AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4272"]],' |
| 183 |
'PROJECTION["Transverse_Mercator"],' |
| 184 |
'PARAMETER["latitude_of_origin",-41.28991152777778],' |
| 185 |
'PARAMETER["central_meridian",172.1090281944444],' |
| 186 |
'PARAMETER["scale_factor",1],PARAMETER["false_easting",300000],' |
| 187 |
'PARAMETER["false_northing",700000],' |
| 188 |
'UNIT["metre",1,AUTHORITY["EPSG","9001"]],AUTHORITY["EPSG","27216"]]', |
| 189 |
wk="EPSG:27216", |
| 190 |
name="NZGD49 / Karamea Circuit", |
| 191 |
attrs=( |
| 192 |
("PROJECTION", "Transverse_Mercator"), |
| 193 |
("SPHEROID", "International 1924"), |
| 194 |
), |
| 195 |
), |
| 196 |
) |
| 197 |
|
| 198 |
bad_srlist = ( |
| 199 |
"Foobar", |
| 200 |
'OOJCS["NAD83 / Texas South Central",GEOGCS["NAD83",' |
| 201 |
'DATUM["North_American_Datum_1983",' |
| 202 |
'SPHEROID["GRS 1980",6378137,298.257222101,AUTHORITY["EPSG","7019"]],' |
| 203 |
'AUTHORITY["EPSG","6269"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],' |
| 204 |
'UNIT["degree",0.01745329251994328,AUTHORITY["EPSG","9122"]],' |
| 205 |
'AUTHORITY["EPSG","4269"]],PROJECTION["Lambert_Conformal_Conic_2SP"],' |
| 206 |
'PARAMETER["standard_parallel_1",30.28333333333333],' |
| 207 |
'PARAMETER["standard_parallel_2",28.38333333333333],' |
| 208 |
'PARAMETER["latitude_of_origin",27.83333333333333],' |
| 209 |
'PARAMETER["central_meridian",-99],PARAMETER["false_easting",600000],' |
| 210 |
'PARAMETER["false_northing",4000000],UNIT["metre",1,' |
| 211 |
'AUTHORITY["EPSG","9001"]],AUTHORITY["EPSG","32140"]]', |
| 212 |
) |
| 213 |
|
| 214 |
|
| 215 |
class SpatialRefTest(SimpleTestCase): |
| 216 |
def test01_wkt(self): |
| 217 |
"Testing initialization on valid OGC WKT." |
| 218 |
for s in srlist: |
| 219 |
SpatialReference(s.wkt) |
| 220 |
|
| 221 |
def test02_bad_wkt(self): |
| 222 |
"Testing initialization on invalid WKT." |
| 223 |
for bad in bad_srlist: |
| 224 |
try: |
| 225 |
srs = SpatialReference(bad) |
| 226 |
srs.validate() |
| 227 |
except (SRSException, GDALException): |
| 228 |
pass |
| 229 |
else: |
| 230 |
self.fail('Should not have initialized on bad WKT "%s"!') |
| 231 |
|
| 232 |
def test03_get_wkt(self): |
| 233 |
"Testing getting the WKT." |
| 234 |
for s in srlist: |
| 235 |
srs = SpatialReference(s.wkt) |
| 236 |
# GDAL 3 strips UNIT part in the last occurrence. |
| 237 |
self.assertEqual( |
| 238 |
s.wkt.replace(',UNIT["Meter",1]', ""), |
| 239 |
srs.wkt.replace(',UNIT["Meter",1]', ""), |
| 240 |
) |
| 241 |
|
| 242 |
def test04_proj(self): |
| 243 |
"""PROJ import and export.""" |
| 244 |
proj_parts = [ |
| 245 |
"+proj=longlat", |
| 246 |
"+ellps=WGS84", |
| 247 |
"+towgs84=0,0,0,0,0,0,0", |
| 248 |
"+datum=WGS84", |
| 249 |
"+no_defs", |
| 250 |
] |
| 251 |
srs1 = SpatialReference(srlist[0].wkt) |
| 252 |
srs2 = SpatialReference("+proj=longlat +datum=WGS84 +no_defs") |
| 253 |
self.assertTrue(all(part in proj_parts for part in srs1.proj.split())) |
| 254 |
self.assertTrue(all(part in proj_parts for part in srs2.proj.split())) |
| 255 |
|
| 256 |
def test05_epsg(self): |
| 257 |
"Test EPSG import." |
| 258 |
for s in srlist: |
| 259 |
if s.epsg: |
| 260 |
srs1 = SpatialReference(s.wkt) |
| 261 |
srs2 = SpatialReference(s.epsg) |
| 262 |
srs3 = SpatialReference(str(s.epsg)) |
| 263 |
srs4 = SpatialReference("EPSG:%d" % s.epsg) |
| 264 |
for srs in (srs1, srs2, srs3, srs4): |
| 265 |
for attr, expected in s.attr: |
| 266 |
self.assertEqual(expected, srs[attr]) |
| 267 |
|
| 268 |
def test07_boolean_props(self): |
| 269 |
"Testing the boolean properties." |
| 270 |
for s in srlist: |
| 271 |
srs = SpatialReference(s.wkt) |
| 272 |
self.assertEqual(s.projected, srs.projected) |
| 273 |
self.assertEqual(s.geographic, srs.geographic) |
| 274 |
|
| 275 |
def test08_angular_linear(self): |
| 276 |
"Testing the linear and angular units routines." |
| 277 |
for s in srlist: |
| 278 |
srs = SpatialReference(s.wkt) |
| 279 |
self.assertEqual(s.ang_name, srs.angular_name) |
| 280 |
self.assertEqual(s.lin_name, srs.linear_name) |
| 281 |
self.assertAlmostEqual(s.ang_units, srs.angular_units, 9) |
| 282 |
self.assertAlmostEqual(s.lin_units, srs.linear_units, 9) |
| 283 |
|
| 284 |
def test09_authority(self): |
| 285 |
"Testing the authority name & code routines." |
| 286 |
for s in srlist: |
| 287 |
if hasattr(s, "auth"): |
| 288 |
srs = SpatialReference(s.wkt) |
| 289 |
for target, tup in s.auth.items(): |
| 290 |
self.assertEqual(tup[0], srs.auth_name(target)) |
| 291 |
self.assertEqual(tup[1], srs.auth_code(target)) |
| 292 |
|
| 293 |
def test10_attributes(self): |
| 294 |
"Testing the attribute retrieval routines." |
| 295 |
for s in srlist: |
| 296 |
srs = SpatialReference(s.wkt) |
| 297 |
for tup in s.attr: |
| 298 |
att = tup[0] # Attribute to test |
| 299 |
exp = tup[1] # Expected result |
| 300 |
self.assertEqual(exp, srs[att]) |
| 301 |
|
| 302 |
def test11_wellknown(self): |
| 303 |
"Testing Well Known Names of Spatial References." |
| 304 |
for s in well_known: |
| 305 |
srs = SpatialReference(s.wk) |
| 306 |
self.assertEqual(s.name, srs.name) |
| 307 |
for tup in s.attrs: |
| 308 |
if len(tup) == 2: |
| 309 |
key = tup[0] |
| 310 |
exp = tup[1] |
| 311 |
elif len(tup) == 3: |
| 312 |
key = tup[:2] |
| 313 |
exp = tup[2] |
| 314 |
self.assertEqual(srs[key], exp) |
| 315 |
|
| 316 |
def test12_coordtransform(self): |
| 317 |
"Testing initialization of a CoordTransform." |
| 318 |
target = SpatialReference("WGS84") |
| 319 |
CoordTransform(SpatialReference(srlist[0].wkt), target) |
| 320 |
|
| 321 |
def test13_attr_value(self): |
| 322 |
"Testing the attr_value() method." |
| 323 |
s1 = SpatialReference("WGS84") |
| 324 |
with self.assertRaises(TypeError): |
| 325 |
s1.__getitem__(0) |
| 326 |
with self.assertRaises(TypeError): |
| 327 |
s1.__getitem__(("GEOGCS", "foo")) |
| 328 |
self.assertEqual("WGS 84", s1["GEOGCS"]) |
| 329 |
self.assertEqual("WGS_1984", s1["DATUM"]) |
| 330 |
self.assertEqual("EPSG", s1["AUTHORITY"]) |
| 331 |
self.assertEqual(4326, int(s1["AUTHORITY", 1])) |
| 332 |
self.assertIsNone(s1["FOOBAR"]) |
| 333 |
|
| 334 |
def test_unicode(self): |
| 335 |
wkt = ( |
| 336 |
'PROJCS["DHDN / Soldner 39 Langschoß",' |
| 337 |
'GEOGCS["DHDN",DATUM["Deutsches_Hauptdreiecksnetz",' |
| 338 |
'SPHEROID["Bessel 1841",6377397.155,299.1528128,AUTHORITY["EPSG","7004"]],' |
| 339 |
'AUTHORITY["EPSG","6314"]],' |
| 340 |
'PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],' |
| 341 |
'UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],' |
| 342 |
'AUTHORITY["EPSG","4314"]],PROJECTION["Cassini_Soldner"],' |
| 343 |
'PARAMETER["latitude_of_origin",50.66738711],' |
| 344 |
'PARAMETER["central_meridian",6.28935703],' |
| 345 |
'PARAMETER["false_easting",0],PARAMETER["false_northing",0],' |
| 346 |
'UNIT["metre",1,AUTHORITY["EPSG","9001"]],AXIS["X",NORTH],AXIS["Y",EAST],' |
| 347 |
'AUTHORITY["mj10777.de","187939"]]' |
| 348 |
) |
| 349 |
srs = SpatialReference(wkt) |
| 350 |
srs_list = [srs, srs.clone()] |
| 351 |
srs.import_wkt(wkt) |
| 352 |
|
| 353 |
for srs in srs_list: |
| 354 |
self.assertEqual(srs.name, "DHDN / Soldner 39 Langschoß") |
| 355 |
self.assertEqual(srs.wkt, wkt) |
| 356 |
self.assertIn("Langschoß", srs.pretty_wkt) |
| 357 |
if GDAL_VERSION < (3, 9): |
| 358 |
self.assertIn("Langschoß", srs.xml) |
| 359 |
|
| 360 |
def test_axis_order(self): |
| 361 |
wgs84_trad = SpatialReference(4326, axis_order=AxisOrder.TRADITIONAL) |
| 362 |
wgs84_auth = SpatialReference(4326, axis_order=AxisOrder.AUTHORITY) |
| 363 |
# Coordinate interpretation may depend on the srs axis predicate. |
| 364 |
pt = GEOSGeometry("POINT (992385.4472045 481455.4944650)", 2774) |
| 365 |
pt_trad = pt.transform(wgs84_trad, clone=True) |
| 366 |
self.assertAlmostEqual(pt_trad.x, -104.609, 3) |
| 367 |
self.assertAlmostEqual(pt_trad.y, 38.255, 3) |
| 368 |
pt_auth = pt.transform(wgs84_auth, clone=True) |
| 369 |
self.assertAlmostEqual(pt_auth.x, 38.255, 3) |
| 370 |
self.assertAlmostEqual(pt_auth.y, -104.609, 3) |
| 371 |
# clone() preserves the axis order. |
| 372 |
pt_auth = pt.transform(wgs84_auth.clone(), clone=True) |
| 373 |
self.assertAlmostEqual(pt_auth.x, 38.255, 3) |
| 374 |
self.assertAlmostEqual(pt_auth.y, -104.609, 3) |
| 375 |
|
| 376 |
def test_axis_order_invalid(self): |
| 377 |
msg = "SpatialReference.axis_order must be an AxisOrder instance." |
| 378 |
with self.assertRaisesMessage(ValueError, msg): |
| 379 |
SpatialReference(4326, axis_order="other") |
| 380 |
|
| 381 |
def test_esri(self): |
| 382 |
srs = SpatialReference("NAD83") |
| 383 |
pre_esri_wkt = srs.wkt |
| 384 |
srs.to_esri() |
| 385 |
self.assertNotEqual(srs.wkt, pre_esri_wkt) |
| 386 |
self.assertIn('DATUM["D_North_American_1983"', srs.wkt) |
| 387 |
srs.from_esri() |
| 388 |
self.assertIn('DATUM["North_American_Datum_1983"', srs.wkt) |
| 389 |
|
| 390 |
def test_srid(self): |
| 391 |
"""The srid property returns top-level authority code.""" |
| 392 |
for s in srlist: |
| 393 |
if hasattr(s, "epsg"): |
| 394 |
srs = SpatialReference(s.wkt) |
| 395 |
self.assertEqual(srs.srid, s.epsg) |