| 1 |
import json |
| 2 |
import pickle |
| 3 |
|
| 4 |
from django.contrib.gis.gdal import ( |
| 5 |
CoordTransform, |
| 6 |
GDALException, |
| 7 |
OGRGeometry, |
| 8 |
OGRGeomType, |
| 9 |
SpatialReference, |
| 10 |
) |
| 11 |
from django.contrib.gis.gdal.geometries import CircularString, CurvePolygon |
| 12 |
from django.contrib.gis.geos import GEOSException |
| 13 |
from django.template import Context |
| 14 |
from django.template.engine import Engine |
| 15 |
from django.test import SimpleTestCase |
| 16 |
from django.utils.deprecation import RemovedInDjango60Warning |
| 17 |
|
| 18 |
from ..test_data import TestDataMixin |
| 19 |
|
| 20 |
|
| 21 |
class OGRGeomTest(SimpleTestCase, TestDataMixin): |
| 22 |
"This tests the OGR Geometry." |
| 23 |
|
| 24 |
def test_geomtype(self): |
| 25 |
"Testing OGRGeomType object." |
| 26 |
|
| 27 |
# OGRGeomType should initialize on all these inputs. |
| 28 |
OGRGeomType(1) |
| 29 |
OGRGeomType(7) |
| 30 |
OGRGeomType("point") |
| 31 |
OGRGeomType("GeometrycollectioN") |
| 32 |
OGRGeomType("LINearrING") |
| 33 |
OGRGeomType("Unknown") |
| 34 |
|
| 35 |
# Should throw TypeError on this input |
| 36 |
with self.assertRaises(GDALException): |
| 37 |
OGRGeomType(23) |
| 38 |
with self.assertRaises(GDALException): |
| 39 |
OGRGeomType("fooD") |
| 40 |
with self.assertRaises(GDALException): |
| 41 |
OGRGeomType(4001) |
| 42 |
|
| 43 |
# Equivalence can take strings, ints, and other OGRGeomTypes |
| 44 |
self.assertEqual(OGRGeomType(1), OGRGeomType(1)) |
| 45 |
self.assertEqual(OGRGeomType(7), "GeometryCollection") |
| 46 |
self.assertEqual(OGRGeomType("point"), "POINT") |
| 47 |
self.assertNotEqual(OGRGeomType("point"), 2) |
| 48 |
self.assertEqual(OGRGeomType("unknown"), 0) |
| 49 |
self.assertEqual(OGRGeomType(6), "MULtiPolyGON") |
| 50 |
self.assertEqual(OGRGeomType(1), OGRGeomType("point")) |
| 51 |
self.assertNotEqual(OGRGeomType("POINT"), OGRGeomType(6)) |
| 52 |
|
| 53 |
# Testing the Django field name equivalent property. |
| 54 |
self.assertEqual("PointField", OGRGeomType("Point").django) |
| 55 |
self.assertEqual("GeometryField", OGRGeomType("Geometry").django) |
| 56 |
self.assertEqual("GeometryField", OGRGeomType("Unknown").django) |
| 57 |
self.assertIsNone(OGRGeomType("none").django) |
| 58 |
|
| 59 |
# 'Geometry' initialization implies an unknown geometry type. |
| 60 |
gt = OGRGeomType("Geometry") |
| 61 |
self.assertEqual(0, gt.num) |
| 62 |
self.assertEqual("Unknown", gt.name) |
| 63 |
|
| 64 |
def test_geom_type_repr(self): |
| 65 |
self.assertEqual(repr(OGRGeomType("point")), "<OGRGeomType: Point>") |
| 66 |
|
| 67 |
def test_geomtype_25d(self): |
| 68 |
"Testing OGRGeomType object with 25D types." |
| 69 |
wkb25bit = OGRGeomType.wkb25bit |
| 70 |
self.assertEqual(OGRGeomType(wkb25bit + 1), "Point25D") |
| 71 |
self.assertEqual(OGRGeomType("MultiLineString25D"), (5 + wkb25bit)) |
| 72 |
self.assertEqual( |
| 73 |
"GeometryCollectionField", OGRGeomType("GeometryCollection25D").django |
| 74 |
) |
| 75 |
|
| 76 |
def test_wkt(self): |
| 77 |
"Testing WKT output." |
| 78 |
for g in self.geometries.wkt_out: |
| 79 |
geom = OGRGeometry(g.wkt) |
| 80 |
self.assertEqual(g.wkt, geom.wkt) |
| 81 |
|
| 82 |
def test_ewkt(self): |
| 83 |
"Testing EWKT input/output." |
| 84 |
for ewkt_val in ("POINT (1 2 3)", "LINEARRING (0 0,1 1,2 1,0 0)"): |
| 85 |
# First with ewkt output when no SRID in EWKT |
| 86 |
self.assertEqual(ewkt_val, OGRGeometry(ewkt_val).ewkt) |
| 87 |
# No test consumption with an SRID specified. |
| 88 |
ewkt_val = "SRID=4326;%s" % ewkt_val |
| 89 |
geom = OGRGeometry(ewkt_val) |
| 90 |
self.assertEqual(ewkt_val, geom.ewkt) |
| 91 |
self.assertEqual(4326, geom.srs.srid) |
| 92 |
|
| 93 |
def test_gml(self): |
| 94 |
"Testing GML output." |
| 95 |
for g in self.geometries.wkt_out: |
| 96 |
geom = OGRGeometry(g.wkt) |
| 97 |
exp_gml = g.gml |
| 98 |
self.assertEqual(exp_gml, geom.gml) |
| 99 |
|
| 100 |
def test_hex(self): |
| 101 |
"Testing HEX input/output." |
| 102 |
for g in self.geometries.hex_wkt: |
| 103 |
geom1 = OGRGeometry(g.wkt) |
| 104 |
self.assertEqual(g.hex.encode(), geom1.hex) |
| 105 |
# Constructing w/HEX |
| 106 |
geom2 = OGRGeometry(g.hex) |
| 107 |
self.assertEqual(geom1, geom2) |
| 108 |
|
| 109 |
def test_wkb(self): |
| 110 |
"Testing WKB input/output." |
| 111 |
for g in self.geometries.hex_wkt: |
| 112 |
geom1 = OGRGeometry(g.wkt) |
| 113 |
wkb = geom1.wkb |
| 114 |
self.assertEqual(wkb.hex().upper(), g.hex) |
| 115 |
# Constructing w/WKB. |
| 116 |
geom2 = OGRGeometry(wkb) |
| 117 |
self.assertEqual(geom1, geom2) |
| 118 |
|
| 119 |
def test_json(self): |
| 120 |
"Testing GeoJSON input/output." |
| 121 |
for g in self.geometries.json_geoms: |
| 122 |
geom = OGRGeometry(g.wkt) |
| 123 |
if not hasattr(g, "not_equal"): |
| 124 |
# Loading jsons to prevent decimal differences |
| 125 |
self.assertEqual(json.loads(g.json), json.loads(geom.json)) |
| 126 |
self.assertEqual(json.loads(g.json), json.loads(geom.geojson)) |
| 127 |
self.assertEqual(OGRGeometry(g.wkt), OGRGeometry(geom.json)) |
| 128 |
# Test input with some garbage content (but valid json) (#15529) |
| 129 |
geom = OGRGeometry( |
| 130 |
'{"type": "Point", "coordinates": [ 100.0, 0.0 ], "other": "<test>"}' |
| 131 |
) |
| 132 |
self.assertIsInstance(geom, OGRGeometry) |
| 133 |
|
| 134 |
def test_points(self): |
| 135 |
"Testing Point objects." |
| 136 |
|
| 137 |
OGRGeometry("POINT(0 0)") |
| 138 |
for p in self.geometries.points: |
| 139 |
if not hasattr(p, "z"): # No 3D |
| 140 |
pnt = OGRGeometry(p.wkt) |
| 141 |
self.assertEqual(1, pnt.geom_type) |
| 142 |
self.assertEqual("POINT", pnt.geom_name) |
| 143 |
self.assertEqual(p.x, pnt.x) |
| 144 |
self.assertEqual(p.y, pnt.y) |
| 145 |
self.assertEqual((p.x, p.y), pnt.tuple) |
| 146 |
|
| 147 |
def test_multipoints(self): |
| 148 |
"Testing MultiPoint objects." |
| 149 |
for mp in self.geometries.multipoints: |
| 150 |
mgeom1 = OGRGeometry(mp.wkt) # First one from WKT |
| 151 |
self.assertEqual(4, mgeom1.geom_type) |
| 152 |
self.assertEqual("MULTIPOINT", mgeom1.geom_name) |
| 153 |
mgeom2 = OGRGeometry("MULTIPOINT") # Creating empty multipoint |
| 154 |
mgeom3 = OGRGeometry("MULTIPOINT") |
| 155 |
for g in mgeom1: |
| 156 |
mgeom2.add(g) # adding each point from the multipoints |
| 157 |
mgeom3.add(g.wkt) # should take WKT as well |
| 158 |
self.assertEqual(mgeom1, mgeom2) # they should equal |
| 159 |
self.assertEqual(mgeom1, mgeom3) |
| 160 |
self.assertEqual(mp.coords, mgeom2.coords) |
| 161 |
self.assertEqual(mp.n_p, mgeom2.point_count) |
| 162 |
|
| 163 |
def test_linestring(self): |
| 164 |
"Testing LineString objects." |
| 165 |
prev = OGRGeometry("POINT(0 0)") |
| 166 |
for ls in self.geometries.linestrings: |
| 167 |
linestr = OGRGeometry(ls.wkt) |
| 168 |
self.assertEqual(2, linestr.geom_type) |
| 169 |
self.assertEqual("LINESTRING", linestr.geom_name) |
| 170 |
self.assertEqual(ls.n_p, linestr.point_count) |
| 171 |
self.assertEqual(ls.coords, linestr.tuple) |
| 172 |
self.assertEqual(linestr, OGRGeometry(ls.wkt)) |
| 173 |
self.assertNotEqual(linestr, prev) |
| 174 |
msg = "Index out of range when accessing points of a line string: %s." |
| 175 |
with self.assertRaisesMessage(IndexError, msg % len(linestr)): |
| 176 |
linestr.__getitem__(len(linestr)) |
| 177 |
prev = linestr |
| 178 |
|
| 179 |
# Testing the x, y properties. |
| 180 |
x = [tmpx for tmpx, tmpy in ls.coords] |
| 181 |
y = [tmpy for tmpx, tmpy in ls.coords] |
| 182 |
self.assertEqual(x, linestr.x) |
| 183 |
self.assertEqual(y, linestr.y) |
| 184 |
|
| 185 |
def test_multilinestring(self): |
| 186 |
"Testing MultiLineString objects." |
| 187 |
prev = OGRGeometry("POINT(0 0)") |
| 188 |
for mls in self.geometries.multilinestrings: |
| 189 |
mlinestr = OGRGeometry(mls.wkt) |
| 190 |
self.assertEqual(5, mlinestr.geom_type) |
| 191 |
self.assertEqual("MULTILINESTRING", mlinestr.geom_name) |
| 192 |
self.assertEqual(mls.n_p, mlinestr.point_count) |
| 193 |
self.assertEqual(mls.coords, mlinestr.tuple) |
| 194 |
self.assertEqual(mlinestr, OGRGeometry(mls.wkt)) |
| 195 |
self.assertNotEqual(mlinestr, prev) |
| 196 |
prev = mlinestr |
| 197 |
for ls in mlinestr: |
| 198 |
self.assertEqual(2, ls.geom_type) |
| 199 |
self.assertEqual("LINESTRING", ls.geom_name) |
| 200 |
msg = "Index out of range when accessing geometry in a collection: %s." |
| 201 |
with self.assertRaisesMessage(IndexError, msg % len(mlinestr)): |
| 202 |
mlinestr.__getitem__(len(mlinestr)) |
| 203 |
|
| 204 |
def test_linearring(self): |
| 205 |
"Testing LinearRing objects." |
| 206 |
prev = OGRGeometry("POINT(0 0)") |
| 207 |
for rr in self.geometries.linearrings: |
| 208 |
lr = OGRGeometry(rr.wkt) |
| 209 |
# self.assertEqual(101, lr.geom_type.num) |
| 210 |
self.assertEqual("LINEARRING", lr.geom_name) |
| 211 |
self.assertEqual(rr.n_p, len(lr)) |
| 212 |
self.assertEqual(lr, OGRGeometry(rr.wkt)) |
| 213 |
self.assertNotEqual(lr, prev) |
| 214 |
prev = lr |
| 215 |
|
| 216 |
def test_polygons(self): |
| 217 |
"Testing Polygon objects." |
| 218 |
|
| 219 |
# Testing `from_bbox` class method |
| 220 |
bbox = (-180, -90, 180, 90) |
| 221 |
p = OGRGeometry.from_bbox(bbox) |
| 222 |
self.assertEqual(bbox, p.extent) |
| 223 |
|
| 224 |
prev = OGRGeometry("POINT(0 0)") |
| 225 |
for p in self.geometries.polygons: |
| 226 |
poly = OGRGeometry(p.wkt) |
| 227 |
self.assertEqual(3, poly.geom_type) |
| 228 |
self.assertEqual("POLYGON", poly.geom_name) |
| 229 |
self.assertEqual(p.n_p, poly.point_count) |
| 230 |
self.assertEqual(p.n_i + 1, len(poly)) |
| 231 |
msg = "Index out of range when accessing rings of a polygon: %s." |
| 232 |
with self.assertRaisesMessage(IndexError, msg % len(poly)): |
| 233 |
poly.__getitem__(len(poly)) |
| 234 |
|
| 235 |
# Testing area & centroid. |
| 236 |
self.assertAlmostEqual(p.area, poly.area, 9) |
| 237 |
x, y = poly.centroid.tuple |
| 238 |
self.assertAlmostEqual(p.centroid[0], x, 9) |
| 239 |
self.assertAlmostEqual(p.centroid[1], y, 9) |
| 240 |
|
| 241 |
# Testing equivalence |
| 242 |
self.assertEqual(poly, OGRGeometry(p.wkt)) |
| 243 |
self.assertNotEqual(poly, prev) |
| 244 |
|
| 245 |
if p.ext_ring_cs: |
| 246 |
ring = poly[0] |
| 247 |
self.assertEqual(p.ext_ring_cs, ring.tuple) |
| 248 |
self.assertEqual(p.ext_ring_cs, poly[0].tuple) |
| 249 |
self.assertEqual(len(p.ext_ring_cs), ring.point_count) |
| 250 |
|
| 251 |
for r in poly: |
| 252 |
self.assertEqual("LINEARRING", r.geom_name) |
| 253 |
|
| 254 |
def test_polygons_templates(self): |
| 255 |
# Accessing Polygon attributes in templates should work. |
| 256 |
engine = Engine() |
| 257 |
template = engine.from_string("{{ polygons.0.wkt }}") |
| 258 |
polygons = [OGRGeometry(p.wkt) for p in self.geometries.multipolygons[:2]] |
| 259 |
content = template.render(Context({"polygons": polygons})) |
| 260 |
self.assertIn("MULTIPOLYGON (((100", content) |
| 261 |
|
| 262 |
def test_closepolygons(self): |
| 263 |
"Testing closing Polygon objects." |
| 264 |
# Both rings in this geometry are not closed. |
| 265 |
poly = OGRGeometry("POLYGON((0 0, 5 0, 5 5, 0 5), (1 1, 2 1, 2 2, 2 1))") |
| 266 |
self.assertEqual(8, poly.point_count) |
| 267 |
with self.assertRaises(GDALException): |
| 268 |
poly.centroid |
| 269 |
|
| 270 |
poly.close_rings() |
| 271 |
self.assertEqual( |
| 272 |
10, poly.point_count |
| 273 |
) # Two closing points should've been added |
| 274 |
self.assertEqual(OGRGeometry("POINT(2.5 2.5)"), poly.centroid) |
| 275 |
|
| 276 |
def test_multipolygons(self): |
| 277 |
"Testing MultiPolygon objects." |
| 278 |
OGRGeometry("POINT(0 0)") |
| 279 |
for mp in self.geometries.multipolygons: |
| 280 |
mpoly = OGRGeometry(mp.wkt) |
| 281 |
self.assertEqual(6, mpoly.geom_type) |
| 282 |
self.assertEqual("MULTIPOLYGON", mpoly.geom_name) |
| 283 |
if mp.valid: |
| 284 |
self.assertEqual(mp.n_p, mpoly.point_count) |
| 285 |
self.assertEqual(mp.num_geom, len(mpoly)) |
| 286 |
msg = "Index out of range when accessing geometry in a collection: %s." |
| 287 |
with self.assertRaisesMessage(IndexError, msg % len(mpoly)): |
| 288 |
mpoly.__getitem__(len(mpoly)) |
| 289 |
for p in mpoly: |
| 290 |
self.assertEqual("POLYGON", p.geom_name) |
| 291 |
self.assertEqual(3, p.geom_type) |
| 292 |
self.assertEqual(mpoly.wkt, OGRGeometry(mp.wkt).wkt) |
| 293 |
|
| 294 |
def test_srs(self): |
| 295 |
"Testing OGR Geometries with Spatial Reference objects." |
| 296 |
for mp in self.geometries.multipolygons: |
| 297 |
# Creating a geometry w/spatial reference |
| 298 |
sr = SpatialReference("WGS84") |
| 299 |
mpoly = OGRGeometry(mp.wkt, sr) |
| 300 |
self.assertEqual(sr.wkt, mpoly.srs.wkt) |
| 301 |
|
| 302 |
# Ensuring that SRS is propagated to clones. |
| 303 |
klone = mpoly.clone() |
| 304 |
self.assertEqual(sr.wkt, klone.srs.wkt) |
| 305 |
|
| 306 |
# Ensuring all children geometries (polygons and their rings) all |
| 307 |
# return the assigned spatial reference as well. |
| 308 |
for poly in mpoly: |
| 309 |
self.assertEqual(sr.wkt, poly.srs.wkt) |
| 310 |
for ring in poly: |
| 311 |
self.assertEqual(sr.wkt, ring.srs.wkt) |
| 312 |
|
| 313 |
# Ensuring SRS propagate in topological ops. |
| 314 |
a = OGRGeometry(self.geometries.topology_geoms[0].wkt_a, sr) |
| 315 |
b = OGRGeometry(self.geometries.topology_geoms[0].wkt_b, sr) |
| 316 |
diff = a.difference(b) |
| 317 |
union = a.union(b) |
| 318 |
self.assertEqual(sr.wkt, diff.srs.wkt) |
| 319 |
self.assertEqual(sr.srid, union.srs.srid) |
| 320 |
|
| 321 |
# Instantiating w/an integer SRID |
| 322 |
mpoly = OGRGeometry(mp.wkt, 4326) |
| 323 |
self.assertEqual(4326, mpoly.srid) |
| 324 |
mpoly.srs = SpatialReference(4269) |
| 325 |
self.assertEqual(4269, mpoly.srid) |
| 326 |
self.assertEqual("NAD83", mpoly.srs.name) |
| 327 |
|
| 328 |
# Incrementing through the multipolygon after the spatial reference |
| 329 |
# has been re-assigned. |
| 330 |
for poly in mpoly: |
| 331 |
self.assertEqual(mpoly.srs.wkt, poly.srs.wkt) |
| 332 |
poly.srs = 32140 |
| 333 |
for ring in poly: |
| 334 |
# Changing each ring in the polygon |
| 335 |
self.assertEqual(32140, ring.srs.srid) |
| 336 |
self.assertEqual("NAD83 / Texas South Central", ring.srs.name) |
| 337 |
ring.srs = str(SpatialReference(4326)) # back to WGS84 |
| 338 |
self.assertEqual(4326, ring.srs.srid) |
| 339 |
|
| 340 |
# Using the `srid` property. |
| 341 |
ring.srid = 4322 |
| 342 |
self.assertEqual("WGS 72", ring.srs.name) |
| 343 |
self.assertEqual(4322, ring.srid) |
| 344 |
|
| 345 |
# srs/srid may be assigned their own values, even when srs is None. |
| 346 |
mpoly = OGRGeometry(mp.wkt, srs=None) |
| 347 |
mpoly.srs = mpoly.srs |
| 348 |
mpoly.srid = mpoly.srid |
| 349 |
|
| 350 |
def test_srs_transform(self): |
| 351 |
"Testing transform()." |
| 352 |
orig = OGRGeometry("POINT (-104.609 38.255)", 4326) |
| 353 |
trans = OGRGeometry("POINT (992385.4472045 481455.4944650)", 2774) |
| 354 |
|
| 355 |
# Using an srid, a SpatialReference object, and a CoordTransform object |
| 356 |
# or transformations. |
| 357 |
t1, t2, t3 = orig.clone(), orig.clone(), orig.clone() |
| 358 |
t1.transform(trans.srid) |
| 359 |
t2.transform(SpatialReference("EPSG:2774")) |
| 360 |
ct = CoordTransform(SpatialReference("WGS84"), SpatialReference(2774)) |
| 361 |
t3.transform(ct) |
| 362 |
|
| 363 |
# Testing use of the `clone` keyword. |
| 364 |
k1 = orig.clone() |
| 365 |
k2 = k1.transform(trans.srid, clone=True) |
| 366 |
self.assertEqual(k1, orig) |
| 367 |
self.assertNotEqual(k1, k2) |
| 368 |
|
| 369 |
# Different PROJ versions use different transformations, all are |
| 370 |
# correct as having a 1 meter accuracy. |
| 371 |
prec = -1 |
| 372 |
for p in (t1, t2, t3, k2): |
| 373 |
self.assertAlmostEqual(trans.x, p.x, prec) |
| 374 |
self.assertAlmostEqual(trans.y, p.y, prec) |
| 375 |
|
| 376 |
def test_transform_dim(self): |
| 377 |
"Testing coordinate dimension is the same on transformed geometries." |
| 378 |
ls_orig = OGRGeometry("LINESTRING(-104.609 38.255)", 4326) |
| 379 |
ls_trans = OGRGeometry("LINESTRING(992385.4472045 481455.4944650)", 2774) |
| 380 |
|
| 381 |
# Different PROJ versions use different transformations, all are |
| 382 |
# correct as having a 1 meter accuracy. |
| 383 |
prec = -1 |
| 384 |
ls_orig.transform(ls_trans.srs) |
| 385 |
# Making sure the coordinate dimension is still 2D. |
| 386 |
self.assertEqual(2, ls_orig.coord_dim) |
| 387 |
self.assertAlmostEqual(ls_trans.x[0], ls_orig.x[0], prec) |
| 388 |
self.assertAlmostEqual(ls_trans.y[0], ls_orig.y[0], prec) |
| 389 |
|
| 390 |
def test_difference(self): |
| 391 |
"Testing difference()." |
| 392 |
for i in range(len(self.geometries.topology_geoms)): |
| 393 |
a = OGRGeometry(self.geometries.topology_geoms[i].wkt_a) |
| 394 |
b = OGRGeometry(self.geometries.topology_geoms[i].wkt_b) |
| 395 |
d1 = OGRGeometry(self.geometries.diff_geoms[i].wkt) |
| 396 |
d2 = a.difference(b) |
| 397 |
self.assertTrue(d1.geos.equals(d2.geos)) |
| 398 |
self.assertTrue( |
| 399 |
d1.geos.equals((a - b).geos) |
| 400 |
) # __sub__ is difference operator |
| 401 |
a -= b # testing __isub__ |
| 402 |
self.assertTrue(d1.geos.equals(a.geos)) |
| 403 |
|
| 404 |
def test_intersection(self): |
| 405 |
"Testing intersects() and intersection()." |
| 406 |
for i in range(len(self.geometries.topology_geoms)): |
| 407 |
a = OGRGeometry(self.geometries.topology_geoms[i].wkt_a) |
| 408 |
b = OGRGeometry(self.geometries.topology_geoms[i].wkt_b) |
| 409 |
i1 = OGRGeometry(self.geometries.intersect_geoms[i].wkt) |
| 410 |
self.assertTrue(a.intersects(b)) |
| 411 |
i2 = a.intersection(b) |
| 412 |
self.assertTrue(i1.geos.equals(i2.geos)) |
| 413 |
self.assertTrue( |
| 414 |
i1.geos.equals((a & b).geos) |
| 415 |
) # __and__ is intersection operator |
| 416 |
a &= b # testing __iand__ |
| 417 |
self.assertTrue(i1.geos.equals(a.geos)) |
| 418 |
|
| 419 |
def test_symdifference(self): |
| 420 |
"Testing sym_difference()." |
| 421 |
for i in range(len(self.geometries.topology_geoms)): |
| 422 |
a = OGRGeometry(self.geometries.topology_geoms[i].wkt_a) |
| 423 |
b = OGRGeometry(self.geometries.topology_geoms[i].wkt_b) |
| 424 |
d1 = OGRGeometry(self.geometries.sdiff_geoms[i].wkt) |
| 425 |
d2 = a.sym_difference(b) |
| 426 |
self.assertTrue(d1.geos.equals(d2.geos)) |
| 427 |
self.assertTrue( |
| 428 |
d1.geos.equals((a ^ b).geos) |
| 429 |
) # __xor__ is symmetric difference operator |
| 430 |
a ^= b # testing __ixor__ |
| 431 |
self.assertTrue(d1.geos.equals(a.geos)) |
| 432 |
|
| 433 |
def test_union(self): |
| 434 |
"Testing union()." |
| 435 |
for i in range(len(self.geometries.topology_geoms)): |
| 436 |
a = OGRGeometry(self.geometries.topology_geoms[i].wkt_a) |
| 437 |
b = OGRGeometry(self.geometries.topology_geoms[i].wkt_b) |
| 438 |
u1 = OGRGeometry(self.geometries.union_geoms[i].wkt) |
| 439 |
u2 = a.union(b) |
| 440 |
self.assertTrue(u1.geos.equals(u2.geos)) |
| 441 |
self.assertTrue(u1.geos.equals((a | b).geos)) # __or__ is union operator |
| 442 |
a |= b # testing __ior__ |
| 443 |
self.assertTrue(u1.geos.equals(a.geos)) |
| 444 |
|
| 445 |
def test_add(self): |
| 446 |
"Testing GeometryCollection.add()." |
| 447 |
# Can't insert a Point into a MultiPolygon. |
| 448 |
mp = OGRGeometry("MultiPolygon") |
| 449 |
pnt = OGRGeometry("POINT(5 23)") |
| 450 |
with self.assertRaises(GDALException): |
| 451 |
mp.add(pnt) |
| 452 |
|
| 453 |
# GeometryCollection.add may take an OGRGeometry (if another collection |
| 454 |
# of the same type all child geoms will be added individually) or WKT. |
| 455 |
for mp in self.geometries.multipolygons: |
| 456 |
mpoly = OGRGeometry(mp.wkt) |
| 457 |
mp1 = OGRGeometry("MultiPolygon") |
| 458 |
mp2 = OGRGeometry("MultiPolygon") |
| 459 |
mp3 = OGRGeometry("MultiPolygon") |
| 460 |
|
| 461 |
for poly in mpoly: |
| 462 |
mp1.add(poly) # Adding a geometry at a time |
| 463 |
mp2.add(poly.wkt) # Adding WKT |
| 464 |
mp3.add(mpoly) # Adding a MultiPolygon's entire contents at once. |
| 465 |
for tmp in (mp1, mp2, mp3): |
| 466 |
self.assertEqual(mpoly, tmp) |
| 467 |
|
| 468 |
def test_extent(self): |
| 469 |
"Testing `extent` property." |
| 470 |
# The xmin, ymin, xmax, ymax of the MultiPoint should be returned. |
| 471 |
mp = OGRGeometry("MULTIPOINT(5 23, 0 0, 10 50)") |
| 472 |
self.assertEqual((0.0, 0.0, 10.0, 50.0), mp.extent) |
| 473 |
# Testing on the 'real world' Polygon. |
| 474 |
poly = OGRGeometry(self.geometries.polygons[3].wkt) |
| 475 |
ring = poly.shell |
| 476 |
x, y = ring.x, ring.y |
| 477 |
xmin, ymin = min(x), min(y) |
| 478 |
xmax, ymax = max(x), max(y) |
| 479 |
self.assertEqual((xmin, ymin, xmax, ymax), poly.extent) |
| 480 |
|
| 481 |
def test_25D(self): |
| 482 |
"Testing 2.5D geometries." |
| 483 |
pnt_25d = OGRGeometry("POINT(1 2 3)") |
| 484 |
self.assertEqual("Point25D", pnt_25d.geom_type.name) |
| 485 |
self.assertEqual(3.0, pnt_25d.z) |
| 486 |
self.assertEqual(3, pnt_25d.coord_dim) |
| 487 |
ls_25d = OGRGeometry("LINESTRING(1 1 1,2 2 2,3 3 3)") |
| 488 |
self.assertEqual("LineString25D", ls_25d.geom_type.name) |
| 489 |
self.assertEqual([1.0, 2.0, 3.0], ls_25d.z) |
| 490 |
self.assertEqual(3, ls_25d.coord_dim) |
| 491 |
|
| 492 |
def test_pickle(self): |
| 493 |
"Testing pickle support." |
| 494 |
g1 = OGRGeometry("LINESTRING(1 1 1,2 2 2,3 3 3)", "WGS84") |
| 495 |
g2 = pickle.loads(pickle.dumps(g1)) |
| 496 |
self.assertEqual(g1, g2) |
| 497 |
self.assertEqual(4326, g2.srs.srid) |
| 498 |
self.assertEqual(g1.srs.wkt, g2.srs.wkt) |
| 499 |
|
| 500 |
def test_ogrgeometry_transform_workaround(self): |
| 501 |
"Testing coordinate dimensions on geometries after transformation." |
| 502 |
# A bug in GDAL versions prior to 1.7 changes the coordinate |
| 503 |
# dimension of a geometry after it has been transformed. |
| 504 |
# This test ensures that the bug workarounds employed within |
| 505 |
# `OGRGeometry.transform` indeed work. |
| 506 |
wkt_2d = "MULTILINESTRING ((0 0,1 1,2 2))" |
| 507 |
wkt_3d = "MULTILINESTRING ((0 0 0,1 1 1,2 2 2))" |
| 508 |
srid = 4326 |
| 509 |
|
| 510 |
# For both the 2D and 3D MultiLineString, ensure _both_ the dimension |
| 511 |
# of the collection and the component LineString have the expected |
| 512 |
# coordinate dimension after transform. |
| 513 |
geom = OGRGeometry(wkt_2d, srid) |
| 514 |
geom.transform(srid) |
| 515 |
self.assertEqual(2, geom.coord_dim) |
| 516 |
self.assertEqual(2, geom[0].coord_dim) |
| 517 |
self.assertEqual(wkt_2d, geom.wkt) |
| 518 |
|
| 519 |
geom = OGRGeometry(wkt_3d, srid) |
| 520 |
geom.transform(srid) |
| 521 |
self.assertEqual(3, geom.coord_dim) |
| 522 |
self.assertEqual(3, geom[0].coord_dim) |
| 523 |
self.assertEqual(wkt_3d, geom.wkt) |
| 524 |
|
| 525 |
# Testing binary predicates, `assertIs` is used to check that bool is returned. |
| 526 |
|
| 527 |
def test_equivalence_regression(self): |
| 528 |
"Testing equivalence methods with non-OGRGeometry instances." |
| 529 |
self.assertIsNotNone(OGRGeometry("POINT(0 0)")) |
| 530 |
self.assertNotEqual(OGRGeometry("LINESTRING(0 0, 1 1)"), 3) |
| 531 |
|
| 532 |
def test_contains(self): |
| 533 |
self.assertIs( |
| 534 |
OGRGeometry("POINT(0 0)").contains(OGRGeometry("POINT(0 0)")), True |
| 535 |
) |
| 536 |
self.assertIs( |
| 537 |
OGRGeometry("POINT(0 0)").contains(OGRGeometry("POINT(0 1)")), False |
| 538 |
) |
| 539 |
|
| 540 |
def test_crosses(self): |
| 541 |
self.assertIs( |
| 542 |
OGRGeometry("LINESTRING(0 0, 1 1)").crosses( |
| 543 |
OGRGeometry("LINESTRING(0 1, 1 0)") |
| 544 |
), |
| 545 |
True, |
| 546 |
) |
| 547 |
self.assertIs( |
| 548 |
OGRGeometry("LINESTRING(0 0, 0 1)").crosses( |
| 549 |
OGRGeometry("LINESTRING(1 0, 1 1)") |
| 550 |
), |
| 551 |
False, |
| 552 |
) |
| 553 |
|
| 554 |
def test_disjoint(self): |
| 555 |
self.assertIs( |
| 556 |
OGRGeometry("LINESTRING(0 0, 1 1)").disjoint( |
| 557 |
OGRGeometry("LINESTRING(0 1, 1 0)") |
| 558 |
), |
| 559 |
False, |
| 560 |
) |
| 561 |
self.assertIs( |
| 562 |
OGRGeometry("LINESTRING(0 0, 0 1)").disjoint( |
| 563 |
OGRGeometry("LINESTRING(1 0, 1 1)") |
| 564 |
), |
| 565 |
True, |
| 566 |
) |
| 567 |
|
| 568 |
def test_equals(self): |
| 569 |
self.assertIs( |
| 570 |
OGRGeometry("POINT(0 0)").contains(OGRGeometry("POINT(0 0)")), True |
| 571 |
) |
| 572 |
self.assertIs( |
| 573 |
OGRGeometry("POINT(0 0)").contains(OGRGeometry("POINT(0 1)")), False |
| 574 |
) |
| 575 |
|
| 576 |
def test_intersects(self): |
| 577 |
self.assertIs( |
| 578 |
OGRGeometry("LINESTRING(0 0, 1 1)").intersects( |
| 579 |
OGRGeometry("LINESTRING(0 1, 1 0)") |
| 580 |
), |
| 581 |
True, |
| 582 |
) |
| 583 |
self.assertIs( |
| 584 |
OGRGeometry("LINESTRING(0 0, 0 1)").intersects( |
| 585 |
OGRGeometry("LINESTRING(1 0, 1 1)") |
| 586 |
), |
| 587 |
False, |
| 588 |
) |
| 589 |
|
| 590 |
def test_overlaps(self): |
| 591 |
self.assertIs( |
| 592 |
OGRGeometry("POLYGON ((0 0, 0 2, 2 2, 2 0, 0 0))").overlaps( |
| 593 |
OGRGeometry("POLYGON ((1 1, 1 5, 5 5, 5 1, 1 1))") |
| 594 |
), |
| 595 |
True, |
| 596 |
) |
| 597 |
self.assertIs( |
| 598 |
OGRGeometry("POINT(0 0)").overlaps(OGRGeometry("POINT(0 1)")), False |
| 599 |
) |
| 600 |
|
| 601 |
def test_touches(self): |
| 602 |
self.assertIs( |
| 603 |
OGRGeometry("POLYGON ((0 0, 0 1, 1 1, 1 0, 0 0))").touches( |
| 604 |
OGRGeometry("LINESTRING(0 2, 2 0)") |
| 605 |
), |
| 606 |
True, |
| 607 |
) |
| 608 |
self.assertIs( |
| 609 |
OGRGeometry("POINT(0 0)").touches(OGRGeometry("POINT(0 1)")), False |
| 610 |
) |
| 611 |
|
| 612 |
def test_within(self): |
| 613 |
self.assertIs( |
| 614 |
OGRGeometry("POINT(0.5 0.5)").within( |
| 615 |
OGRGeometry("POLYGON ((0 0, 0 1, 1 1, 1 0, 0 0))") |
| 616 |
), |
| 617 |
True, |
| 618 |
) |
| 619 |
self.assertIs( |
| 620 |
OGRGeometry("POINT(0 0)").within(OGRGeometry("POINT(0 1)")), False |
| 621 |
) |
| 622 |
|
| 623 |
def test_from_gml(self): |
| 624 |
self.assertEqual( |
| 625 |
OGRGeometry("POINT(0 0)"), |
| 626 |
OGRGeometry.from_gml( |
| 627 |
'<gml:Point gml:id="p21" ' |
| 628 |
'srsName="http://www.opengis.net/def/crs/EPSG/0/4326">' |
| 629 |
' <gml:pos srsDimension="2">0 0</gml:pos>' |
| 630 |
"</gml:Point>" |
| 631 |
), |
| 632 |
) |
| 633 |
|
| 634 |
def test_empty(self): |
| 635 |
self.assertIs(OGRGeometry("POINT (0 0)").empty, False) |
| 636 |
self.assertIs(OGRGeometry("POINT EMPTY").empty, True) |
| 637 |
|
| 638 |
def test_empty_point_to_geos(self): |
| 639 |
p = OGRGeometry("POINT EMPTY", srs=4326) |
| 640 |
self.assertEqual(p.geos.ewkt, p.ewkt) |
| 641 |
|
| 642 |
def test_geometry_types(self): |
| 643 |
tests = [ |
| 644 |
("Point", 1, True), |
| 645 |
("LineString", 2, True), |
| 646 |
("Polygon", 3, True), |
| 647 |
("MultiPoint", 4, True), |
| 648 |
("Multilinestring", 5, True), |
| 649 |
("MultiPolygon", 6, True), |
| 650 |
("GeometryCollection", 7, True), |
| 651 |
("CircularString", 8, True), |
| 652 |
("CompoundCurve", 9, True), |
| 653 |
("CurvePolygon", 10, True), |
| 654 |
("MultiCurve", 11, True), |
| 655 |
("MultiSurface", 12, True), |
| 656 |
# 13 (Curve) and 14 (Surface) are abstract types. |
| 657 |
("PolyhedralSurface", 15, False), |
| 658 |
("TIN", 16, False), |
| 659 |
("Triangle", 17, False), |
| 660 |
("Linearring", 2, True), |
| 661 |
# Types 1 - 7 with Z dimension have 2.5D enums. |
| 662 |
("Point Z", -2147483647, True), # 1001 |
| 663 |
("LineString Z", -2147483646, True), # 1002 |
| 664 |
("Polygon Z", -2147483645, True), # 1003 |
| 665 |
("MultiPoint Z", -2147483644, True), # 1004 |
| 666 |
("Multilinestring Z", -2147483643, True), # 1005 |
| 667 |
("MultiPolygon Z", -2147483642, True), # 1006 |
| 668 |
("GeometryCollection Z", -2147483641, True), # 1007 |
| 669 |
("CircularString Z", 1008, True), |
| 670 |
("CompoundCurve Z", 1009, True), |
| 671 |
("CurvePolygon Z", 1010, True), |
| 672 |
("MultiCurve Z", 1011, True), |
| 673 |
("MultiSurface Z", 1012, True), |
| 674 |
("PolyhedralSurface Z", 1015, False), |
| 675 |
("TIN Z", 1016, False), |
| 676 |
("Triangle Z", 1017, False), |
| 677 |
("Point M", 2001, True), |
| 678 |
("LineString M", 2002, True), |
| 679 |
("Polygon M", 2003, True), |
| 680 |
("MultiPoint M", 2004, True), |
| 681 |
("MultiLineString M", 2005, True), |
| 682 |
("MultiPolygon M", 2006, True), |
| 683 |
("GeometryCollection M", 2007, True), |
| 684 |
("CircularString M", 2008, True), |
| 685 |
("CompoundCurve M", 2009, True), |
| 686 |
("CurvePolygon M", 2010, True), |
| 687 |
("MultiCurve M", 2011, True), |
| 688 |
("MultiSurface M", 2012, True), |
| 689 |
("PolyhedralSurface M", 2015, False), |
| 690 |
("TIN M", 2016, False), |
| 691 |
("Triangle M", 2017, False), |
| 692 |
("Point ZM", 3001, True), |
| 693 |
("LineString ZM", 3002, True), |
| 694 |
("Polygon ZM", 3003, True), |
| 695 |
("MultiPoint ZM", 3004, True), |
| 696 |
("MultiLineString ZM", 3005, True), |
| 697 |
("MultiPolygon ZM", 3006, True), |
| 698 |
("GeometryCollection ZM", 3007, True), |
| 699 |
("CircularString ZM", 3008, True), |
| 700 |
("CompoundCurve ZM", 3009, True), |
| 701 |
("CurvePolygon ZM", 3010, True), |
| 702 |
("MultiCurve ZM", 3011, True), |
| 703 |
("MultiSurface ZM", 3012, True), |
| 704 |
("PolyhedralSurface ZM", 3015, False), |
| 705 |
("TIN ZM", 3016, False), |
| 706 |
("Triangle ZM", 3017, False), |
| 707 |
] |
| 708 |
|
| 709 |
for test in tests: |
| 710 |
geom_type, num, supported = test |
| 711 |
with self.subTest(geom_type=geom_type, num=num, supported=supported): |
| 712 |
if supported: |
| 713 |
g = OGRGeometry(f"{geom_type} EMPTY") |
| 714 |
self.assertEqual(g.geom_type.num, num) |
| 715 |
else: |
| 716 |
type_ = geom_type.replace(" ", "") |
| 717 |
msg = f"Unsupported geometry type: {type_}" |
| 718 |
with self.assertRaisesMessage(TypeError, msg): |
| 719 |
OGRGeometry(f"{geom_type} EMPTY") |
| 720 |
|
| 721 |
def test_is_3d_and_set_3d(self): |
| 722 |
geom = OGRGeometry("POINT (1 2)") |
| 723 |
self.assertIs(geom.is_3d, False) |
| 724 |
geom.set_3d(True) |
| 725 |
self.assertIs(geom.is_3d, True) |
| 726 |
self.assertEqual(geom.wkt, "POINT (1 2 0)") |
| 727 |
geom.set_3d(False) |
| 728 |
self.assertIs(geom.is_3d, False) |
| 729 |
self.assertEqual(geom.wkt, "POINT (1 2)") |
| 730 |
msg = "Input to 'set_3d' must be a boolean, got 'None'" |
| 731 |
with self.assertRaisesMessage(ValueError, msg): |
| 732 |
geom.set_3d(None) |
| 733 |
|
| 734 |
def test_wkt_and_wkb_output(self): |
| 735 |
tests = [ |
| 736 |
# 2D |
| 737 |
("POINT (1 2)", "0101000000000000000000f03f0000000000000040"), |
| 738 |
( |
| 739 |
"LINESTRING (30 10,10 30)", |
| 740 |
"0102000000020000000000000000003e400000000000002" |
| 741 |
"44000000000000024400000000000003e40", |
| 742 |
), |
| 743 |
( |
| 744 |
"POLYGON ((30 10,40 40,20 40,30 10))", |
| 745 |
"010300000001000000040000000000000000003e400000000000002440000000000000" |
| 746 |
"44400000000000004440000000000000344000000000000044400000000000003e4000" |
| 747 |
"00000000002440", |
| 748 |
), |
| 749 |
( |
| 750 |
"MULTIPOINT (10 40,40 30)", |
| 751 |
"0104000000020000000101000000000000000000244000000000000044400101000000" |
| 752 |
"00000000000044400000000000003e40", |
| 753 |
), |
| 754 |
( |
| 755 |
"MULTILINESTRING ((10 10,20 20),(40 40,30 30,40 20))", |
| 756 |
"0105000000020000000102000000020000000000000000002440000000000000244000" |
| 757 |
"0000000000344000000000000034400102000000030000000000000000004440000000" |
| 758 |
"00000044400000000000003e400000000000003e400000000000004440000000000000" |
| 759 |
"3440", |
| 760 |
), |
| 761 |
( |
| 762 |
"MULTIPOLYGON (((30 20,45 40,10 40,30 20)),((15 5,40 10,10 20,15 5)))", |
| 763 |
"010600000002000000010300000001000000040000000000000000003e400000000000" |
| 764 |
"0034400000000000804640000000000000444000000000000024400000000000004440" |
| 765 |
"0000000000003e40000000000000344001030000000100000004000000000000000000" |
| 766 |
"2e40000000000000144000000000000044400000000000002440000000000000244000" |
| 767 |
"000000000034400000000000002e400000000000001440", |
| 768 |
), |
| 769 |
( |
| 770 |
"GEOMETRYCOLLECTION (POINT (40 10))", |
| 771 |
"010700000001000000010100000000000000000044400000000000002440", |
| 772 |
), |
| 773 |
# 3D |
| 774 |
( |
| 775 |
"POINT (1 2 3)", |
| 776 |
"0101000080000000000000f03f00000000000000400000000000000840", |
| 777 |
), |
| 778 |
( |
| 779 |
"LINESTRING (30 10 3,10 30 3)", |
| 780 |
"0102000080020000000000000000003e40000000000000244000000000000008400000" |
| 781 |
"0000000024400000000000003e400000000000000840", |
| 782 |
), |
| 783 |
( |
| 784 |
"POLYGON ((30 10 3,40 40 3,30 10 3))", |
| 785 |
"010300008001000000030000000000000000003e400000000000002440000000000000" |
| 786 |
"08400000000000004440000000000000444000000000000008400000000000003e4000" |
| 787 |
"000000000024400000000000000840", |
| 788 |
), |
| 789 |
( |
| 790 |
"MULTIPOINT (10 40 3,40 30 3)", |
| 791 |
"0104000080020000000101000080000000000000244000000000000044400000000000" |
| 792 |
"000840010100008000000000000044400000000000003e400000000000000840", |
| 793 |
), |
| 794 |
( |
| 795 |
"MULTILINESTRING ((10 10 3,20 20 3))", |
| 796 |
"0105000080010000000102000080020000000000000000002440000000000000244000" |
| 797 |
"00000000000840000000000000344000000000000034400000000000000840", |
| 798 |
), |
| 799 |
( |
| 800 |
"MULTIPOLYGON (((30 20 3,45 40 3,30 20 3)))", |
| 801 |
"010600008001000000010300008001000000030000000000000000003e400000000000" |
| 802 |
"0034400000000000000840000000000080464000000000000044400000000000000840" |
| 803 |
"0000000000003e4000000000000034400000000000000840", |
| 804 |
), |
| 805 |
( |
| 806 |
"GEOMETRYCOLLECTION (POINT (40 10 3))", |
| 807 |
"0107000080010000000101000080000000000000444000000000000024400000000000" |
| 808 |
"000840", |
| 809 |
), |
| 810 |
] |
| 811 |
for geom, wkb in tests: |
| 812 |
with self.subTest(geom=geom): |
| 813 |
g = OGRGeometry(geom) |
| 814 |
self.assertEqual(g.wkt, geom) |
| 815 |
self.assertEqual(g.wkb.hex(), wkb) |
| 816 |
|
| 817 |
def test_measure_is_measure_and_set_measure(self): |
| 818 |
geom = OGRGeometry("POINT (1 2 3)") |
| 819 |
self.assertIs(geom.is_measured, False) |
| 820 |
geom.set_measured(True) |
| 821 |
self.assertIs(geom.is_measured, True) |
| 822 |
self.assertEqual(geom.wkt, "POINT ZM (1 2 3 0)") |
| 823 |
geom.set_measured(False) |
| 824 |
self.assertIs(geom.is_measured, False) |
| 825 |
self.assertEqual(geom.wkt, "POINT (1 2 3)") |
| 826 |
msg = "Input to 'set_measured' must be a boolean, got 'None'" |
| 827 |
with self.assertRaisesMessage(ValueError, msg): |
| 828 |
geom.set_measured(None) |
| 829 |
|
| 830 |
def test_point_m_coordinate(self): |
| 831 |
geom = OGRGeometry("POINT ZM (1 2 3 4)") |
| 832 |
self.assertEqual(geom.m, 4) |
| 833 |
geom = OGRGeometry("POINT (1 2 3 4)") |
| 834 |
self.assertEqual(geom.m, 4) |
| 835 |
geom = OGRGeometry("POINT M (1 2 3)") |
| 836 |
self.assertEqual(geom.m, 3) |
| 837 |
geom = OGRGeometry("POINT Z (1 2 3)") |
| 838 |
self.assertEqual(geom.m, None) |
| 839 |
|
| 840 |
def test_point_m_tuple(self): |
| 841 |
geom = OGRGeometry("POINT ZM (1 2 3 4)") |
| 842 |
self.assertEqual(geom.tuple, (geom.x, geom.y, geom.z, geom.m)) |
| 843 |
geom = OGRGeometry("POINT M (1 2 3)") |
| 844 |
self.assertEqual(geom.tuple, (geom.x, geom.y, geom.m)) |
| 845 |
geom = OGRGeometry("POINT Z (1 2 3)") |
| 846 |
self.assertEqual(geom.tuple, (geom.x, geom.y, geom.z)) |
| 847 |
geom = OGRGeometry("POINT (1 2 3)") |
| 848 |
self.assertEqual(geom.tuple, (geom.x, geom.y, geom.z)) |
| 849 |
|
| 850 |
def test_point_m_wkt_wkb(self): |
| 851 |
wkt = "POINT ZM (1 2 3 4)" |
| 852 |
geom = OGRGeometry(wkt) |
| 853 |
self.assertEqual(geom.wkt, wkt) |
| 854 |
self.assertEqual( |
| 855 |
geom.wkb.hex(), |
| 856 |
"01b90b0000000000000000f03f00000000000000" |
| 857 |
"4000000000000008400000000000001040", |
| 858 |
) |
| 859 |
wkt = "POINT M (1 2 3)" |
| 860 |
geom = OGRGeometry(wkt) |
| 861 |
self.assertEqual(geom.wkt, wkt) |
| 862 |
self.assertEqual( |
| 863 |
geom.wkb.hex(), |
| 864 |
"01d1070000000000000000f03f00000000000000400000000000000840", |
| 865 |
) |
| 866 |
|
| 867 |
def test_point_m_dimension_types(self): |
| 868 |
geom = OGRGeometry("POINT ZM (1 2 3 4)") |
| 869 |
self.assertEqual(geom.geom_type.name, "PointZM") |
| 870 |
self.assertEqual(geom.geom_type.num, 3001) |
| 871 |
geom = OGRGeometry("POINT M (1 2 3)") |
| 872 |
self.assertEqual(geom.geom_type.name, "PointM") |
| 873 |
self.assertEqual(geom.geom_type.num, 2001) |
| 874 |
|
| 875 |
def test_point_m_dimension_geos(self): |
| 876 |
"""GEOSGeometry does not yet support the M dimension.""" |
| 877 |
geom = OGRGeometry("POINT ZM (1 2 3 4)") |
| 878 |
self.assertEqual(geom.geos.wkt, "POINT Z (1 2 3)") |
| 879 |
geom = OGRGeometry("POINT M (1 2 3)") |
| 880 |
self.assertEqual(geom.geos.wkt, "POINT (1 2)") |
| 881 |
|
| 882 |
def test_centroid(self): |
| 883 |
point = OGRGeometry("POINT (1 2 3)") |
| 884 |
self.assertEqual(point.centroid.wkt, "POINT (1 2)") |
| 885 |
linestring = OGRGeometry("LINESTRING (0 0 0, 1 1 1, 2 2 2)") |
| 886 |
self.assertEqual(linestring.centroid.wkt, "POINT (1 1)") |
| 887 |
polygon = OGRGeometry("POLYGON((0 0, 10 0, 10 10, 0 10, 0 0))") |
| 888 |
self.assertEqual(polygon.centroid.wkt, "POINT (5 5)") |
| 889 |
multipoint = OGRGeometry("MULTIPOINT (0 0,10 10)") |
| 890 |
self.assertEqual(multipoint.centroid.wkt, "POINT (5 5)") |
| 891 |
multilinestring = OGRGeometry( |
| 892 |
"MULTILINESTRING ((0 0,0 10,0 20),(10 0,10 10,10 20))" |
| 893 |
) |
| 894 |
self.assertEqual(multilinestring.centroid.wkt, "POINT (5 10)") |
| 895 |
multipolygon = OGRGeometry( |
| 896 |
"MULTIPOLYGON(((0 0, 10 0, 10 10, 0 10, 0 0))," |
| 897 |
"((20 20, 20 30, 30 30, 30 20, 20 20)))" |
| 898 |
) |
| 899 |
self.assertEqual(multipolygon.centroid.wkt, "POINT (15 15)") |
| 900 |
geometrycollection = OGRGeometry( |
| 901 |
"GEOMETRYCOLLECTION (POINT (110 260),LINESTRING (110 0,110 60))" |
| 902 |
) |
| 903 |
self.assertEqual(geometrycollection.centroid.wkt, "POINT (110 30)") |
| 904 |
|
| 905 |
def test_linestring_m_dimension(self): |
| 906 |
geom = OGRGeometry("LINESTRING(0 1 2 10, 1 2 3 11, 2 3 4 12)") |
| 907 |
self.assertIs(geom.is_measured, True) |
| 908 |
self.assertEqual(geom.m, [10.0, 11.0, 12.0]) |
| 909 |
self.assertEqual(geom[0], (0.0, 1.0, 2.0, 10.0)) |
| 910 |
|
| 911 |
geom = OGRGeometry("LINESTRING M (0 1 10, 1 2 11)") |
| 912 |
self.assertIs(geom.is_measured, True) |
| 913 |
self.assertEqual(geom.m, [10.0, 11.0]) |
| 914 |
self.assertEqual(geom[0], (0.0, 1.0, 10.0)) |
| 915 |
|
| 916 |
geom.set_measured(False) |
| 917 |
self.assertIs(geom.is_measured, False) |
| 918 |
self.assertIs(geom.m, None) |
| 919 |
|
| 920 |
def test_polygon_m_dimension(self): |
| 921 |
geom = OGRGeometry("POLYGON Z ((0 0 0, 10 0 0, 10 10 0, 0 10 0, 0 0 0))") |
| 922 |
self.assertIs(geom.is_measured, False) |
| 923 |
self.assertEqual( |
| 924 |
geom.shell.wkt, "LINEARRING (0 0 0,10 0 0,10 10 0,0 10 0,0 0 0)" |
| 925 |
) |
| 926 |
|
| 927 |
geom = OGRGeometry("POLYGON M ((0 0 0, 10 0 0, 10 10 0, 0 10 0, 0 0 0))") |
| 928 |
self.assertIs(geom.is_measured, True) |
| 929 |
self.assertEqual( |
| 930 |
geom.shell.wkt, "LINEARRING M (0 0 0,10 0 0,10 10 0,0 10 0,0 0 0)" |
| 931 |
) |
| 932 |
|
| 933 |
geom = OGRGeometry( |
| 934 |
"POLYGON ZM ((0 0 0 1, 10 0 0 1, 10 10 0 1, 0 10 0 1, 0 0 0 1))" |
| 935 |
) |
| 936 |
self.assertIs(geom.is_measured, True) |
| 937 |
self.assertEqual( |
| 938 |
geom.shell.wkt, |
| 939 |
"LINEARRING ZM (0 0 0 1,10 0 0 1,10 10 0 1,0 10 0 1,0 0 0 1)", |
| 940 |
) |
| 941 |
|
| 942 |
geom.set_measured(False) |
| 943 |
self.assertEqual(geom.wkt, "POLYGON ((0 0 0,10 0 0,10 10 0,0 10 0,0 0 0))") |
| 944 |
self.assertEqual( |
| 945 |
geom.shell.wkt, "LINEARRING (0 0 0,10 0 0,10 10 0,0 10 0,0 0 0)" |
| 946 |
) |
| 947 |
|
| 948 |
def test_multi_geometries_m_dimension(self): |
| 949 |
tests = [ |
| 950 |
"MULTIPOINT M ((10 40 10), (40 30 10), (20 20 10))", |
| 951 |
"MULTIPOINT ZM ((10 40 0 10), (40 30 1 10), (20 20 1 10))", |
| 952 |
"MULTILINESTRING M ((10 10 1, 20 20 2),(40 40 1, 30 30 2))", |
| 953 |
"MULTILINESTRING ZM ((10 10 0 1, 20 20 0 2),(40 40 1, 30 30 0 2))", |
| 954 |
( |
| 955 |
"MULTIPOLYGON ZM (((30 20 1 0, 45 40 1 0, 30 20 1 0))," |
| 956 |
"((15 5 0 0, 40 10 0 0, 15 5 0 0)))" |
| 957 |
), |
| 958 |
( |
| 959 |
"GEOMETRYCOLLECTION M (POINT M (40 10 0)," |
| 960 |
"LINESTRING M (10 10 0, 20 20 0, 10 40 0))" |
| 961 |
), |
| 962 |
( |
| 963 |
"GEOMETRYCOLLECTION ZM (POINT ZM (40 10 0 1)," |
| 964 |
"LINESTRING ZM (10 10 1 0, 20 20 1 0, 10 40 1 0))" |
| 965 |
), |
| 966 |
] |
| 967 |
for geom_input in tests: |
| 968 |
with self.subTest(geom_input=geom_input): |
| 969 |
geom = OGRGeometry(geom_input) |
| 970 |
self.assertIs(geom.is_measured, True) |
| 971 |
|
| 972 |
def test_has_curve(self): |
| 973 |
for geom in self.geometries.curved_geoms: |
| 974 |
with self.subTest(wkt=geom.wkt): |
| 975 |
geom = OGRGeometry(geom.wkt) |
| 976 |
self.assertIs(geom.has_curve, True) |
| 977 |
msg = f"GEOS does not support {geom.__class__.__qualname__}." |
| 978 |
with self.assertRaisesMessage(GEOSException, msg): |
| 979 |
geom.geos |
| 980 |
geom = OGRGeometry("POINT (0 1)") |
| 981 |
self.assertIs(geom.has_curve, False) |
| 982 |
|
| 983 |
def test_get_linear_geometry(self): |
| 984 |
geom = OGRGeometry("CIRCULARSTRING (-0.797 0.466,-0.481 0.62,-0.419 0.473)") |
| 985 |
linear = geom.get_linear_geometry() |
| 986 |
self.assertEqual(linear.geom_name, "LINESTRING") |
| 987 |
self.assertIs(linear.has_curve, False) |
| 988 |
|
| 989 |
def test_get_linear_geometry_no_conversion_possible(self): |
| 990 |
wkt = "POINT (0 0)" |
| 991 |
geom = OGRGeometry(wkt) |
| 992 |
geom2 = geom.get_linear_geometry() |
| 993 |
self.assertEqual(geom2.wkt, wkt) |
| 994 |
|
| 995 |
def test_get_curve_geometry(self): |
| 996 |
linear_string = OGRGeometry( |
| 997 |
"LINESTRING (-0.797 0.466,-0.797500910583869 0.479079607685707," |
| 998 |
"-0.797096828208069 0.49216256476959,-0.795789684575482 0.505186328593822," |
| 999 |
"-0.793585728444384 0.518088639471983,-0.79049549575663 0.530807818319715," |
| 1000 |
"-0.786533759270668 0.543283061509385,-0.781719457941079 0.555454731539925," |
| 1001 |
"-0.776075606381369 0.567264642132187,-0.769629184843353 0.578656336386302," |
| 1002 |
"-0.76241101023902 0.589575356672327,-0.754455588821145 0.599969504963013," |
| 1003 |
"-0.745800951227352 0.609789092364991,-0.736488470675795 0.618987176654798," |
| 1004 |
"-0.726562665181888 0.627519786684672,-0.716070984741265 0.635346132585369," |
| 1005 |
"-0.705063584496685 0.642428800760598,-0.693593084972889 0.648733932741749," |
| 1006 |
"-0.681714320525941 0.654231387047048,-0.669484077209319 0.658894883272069," |
| 1007 |
"-0.656960821309923 0.662702127722269,-0.644204419852031 0.665634919987354," |
| 1008 |
"-0.631275854404748 0.667679239947688,-0.618236929561618 0.668825314797118," |
| 1009 |
"-0.60514997748578 0.669067665761503,-0.592077559933017 0.66840513428977," |
| 1010 |
"-0.579082169177269 0.666840887592428,-0.566225929268313 0.664382403500809," |
| 1011 |
"-0.553570299049824 0.661041434719465,-0.541175778357228 0.656833952642756," |
| 1012 |
"-0.529101618800212 0.651780071004197,-0.5174055405123 0.645903949723276," |
| 1013 |
"-0.506143456221622 0.639233679409784,-0.495369203961872 0.631801147077652," |
| 1014 |
"-0.485134289701335 0.623641883709865,-0.475487641120239 0.614794894404014," |
| 1015 |
"-0.46647537371355 0.605302471909454,-0.458140570337321 0.595209994448282," |
| 1016 |
"-0.450523075252448 0.58456570878613,-0.443659303650563 0.573420499590156," |
| 1017 |
"-0.437582067572208 0.561827646176397,-0.432320419050072 0.549842567809747," |
| 1018 |
"-0.427899511226613 0.537522558773986,-0.424340478110267 0.524926514478182," |
| 1019 |
"-0.421660333544978 0.512114649909193,-0.419871889876113 0.499148211775737," |
| 1020 |
"-0.418983696701434 0.486089185720561,-0.419 0.473)" |
| 1021 |
) |
| 1022 |
curve = linear_string.get_curve_geometry() |
| 1023 |
self.assertEqual(curve.geom_name, "CIRCULARSTRING") |
| 1024 |
self.assertEqual( |
| 1025 |
curve.wkt, |
| 1026 |
"CIRCULARSTRING (-0.797 0.466,-0.618236929561618 " |
| 1027 |
"0.668825314797118,-0.419 0.473)", |
| 1028 |
) |
| 1029 |
|
| 1030 |
def test_get_curve_geometry_no_conversion_possible(self): |
| 1031 |
geom = OGRGeometry("LINESTRING (0 0, 1 0, 2 0)") |
| 1032 |
geom2 = geom.get_curve_geometry() |
| 1033 |
self.assertEqual(geom2.wkt, geom.wkt) |
| 1034 |
|
| 1035 |
def test_curved_geometries(self): |
| 1036 |
for geom in self.geometries.curved_geoms: |
| 1037 |
with self.subTest(wkt=geom.wkt, geom_name=geom.name): |
| 1038 |
g = OGRGeometry(geom.wkt) |
| 1039 |
self.assertEqual(geom.name, g.geom_type.name) |
| 1040 |
self.assertEqual(geom.num, g.geom_type.num) |
| 1041 |
msg = f"GEOS does not support {g.__class__.__qualname__}." |
| 1042 |
with self.assertRaisesMessage(GEOSException, msg): |
| 1043 |
g.geos |
| 1044 |
|
| 1045 |
def test_circularstring_has_linestring_features(self): |
| 1046 |
geom = OGRGeometry("CIRCULARSTRING ZM (1 5 0 1, 6 2 0 2, 7 3 0 3)") |
| 1047 |
self.assertIsInstance(geom, CircularString) |
| 1048 |
self.assertEqual(geom.x, [1, 6, 7]) |
| 1049 |
self.assertEqual(geom.y, [5, 2, 3]) |
| 1050 |
self.assertEqual(geom.z, [0, 0, 0]) |
| 1051 |
self.assertEqual(geom.m, [1, 2, 3]) |
| 1052 |
self.assertEqual( |
| 1053 |
geom.tuple, |
| 1054 |
((1.0, 5.0, 0.0, 1.0), (6.0, 2.0, 0.0, 2.0), (7.0, 3.0, 0.0, 3.0)), |
| 1055 |
) |
| 1056 |
self.assertEqual(geom[0], (1, 5, 0, 1)) |
| 1057 |
self.assertEqual(len(geom), 3) |
| 1058 |
|
| 1059 |
def test_curvepolygon_has_polygon_features(self): |
| 1060 |
geom = OGRGeometry( |
| 1061 |
"CURVEPOLYGON ZM (CIRCULARSTRING ZM (0 0 0 0, 4 0 0 0, 4 4 0 0, 0 4 0 0, " |
| 1062 |
"0 0 0 0), (1 1 0 0, 3 3 0 0, 3 1 0 0, 1 1 0 0))" |
| 1063 |
) |
| 1064 |
self.assertIsInstance(geom, CurvePolygon) |
| 1065 |
self.assertIsInstance(geom.shell, CircularString) |
| 1066 |
|
| 1067 |
|
| 1068 |
class DeprecationTests(SimpleTestCase): |
| 1069 |
def test_coord_setter_deprecation(self): |
| 1070 |
geom = OGRGeometry("POINT (1 2)") |
| 1071 |
msg = "coord_dim setter is deprecated. Use set_3d() instead." |
| 1072 |
with self.assertWarnsMessage(RemovedInDjango60Warning, msg) as ctx: |
| 1073 |
geom.coord_dim = 3 |
| 1074 |
self.assertEqual(geom.coord_dim, 3) |
| 1075 |
self.assertEqual(ctx.filename, __file__) |