2012-09-13 22:17:32 +08:00
|
|
|
import json
|
2017-01-07 19:11:46 +08:00
|
|
|
import pickle
|
2011-07-13 17:35:51 +08:00
|
|
|
|
2017-05-03 09:27:11 +08:00
|
|
|
from django.contrib.gis.gdal import (
|
2017-09-04 09:19:37 +08:00
|
|
|
CoordTransform,
|
|
|
|
GDALException,
|
|
|
|
OGRGeometry,
|
|
|
|
OGRGeomType,
|
|
|
|
SpatialReference,
|
2017-05-03 09:27:11 +08:00
|
|
|
)
|
2017-09-04 09:19:37 +08:00
|
|
|
from django.template import Context
|
|
|
|
from django.template.engine import Engine
|
2017-09-02 15:38:17 +08:00
|
|
|
from django.test import SimpleTestCase
|
2010-10-11 20:55:17 +08:00
|
|
|
|
2015-02-10 23:07:44 +08:00
|
|
|
from ..test_data import TestDataMixin
|
|
|
|
|
2013-05-11 11:08:45 +08:00
|
|
|
|
2017-09-02 15:38:17 +08:00
|
|
|
class OGRGeomTest(SimpleTestCase, TestDataMixin):
|
2008-08-06 02:13:06 +08:00
|
|
|
"This tests the OGR Geometry."
|
|
|
|
|
2015-05-13 20:12:53 +08:00
|
|
|
def test_geomtype(self):
|
2008-08-06 02:13:06 +08:00
|
|
|
"Testing OGRGeomType object."
|
|
|
|
|
|
|
|
# OGRGeomType should initialize on all these inputs.
|
2013-08-05 00:17:10 +08:00
|
|
|
OGRGeomType(1)
|
|
|
|
OGRGeomType(7)
|
|
|
|
OGRGeomType("point")
|
|
|
|
OGRGeomType("GeometrycollectioN")
|
|
|
|
OGRGeomType("LINearrING")
|
|
|
|
OGRGeomType("Unknown")
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
# Should throw TypeError on this input
|
2016-01-17 19:26:39 +08:00
|
|
|
with self.assertRaises(GDALException):
|
|
|
|
OGRGeomType(23)
|
|
|
|
with self.assertRaises(GDALException):
|
|
|
|
OGRGeomType("fooD")
|
|
|
|
with self.assertRaises(GDALException):
|
|
|
|
OGRGeomType(9)
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
# Equivalence can take strings, ints, and other OGRGeomTypes
|
2013-10-11 05:01:07 +08:00
|
|
|
self.assertEqual(OGRGeomType(1), OGRGeomType(1))
|
|
|
|
self.assertEqual(OGRGeomType(7), "GeometryCollection")
|
|
|
|
self.assertEqual(OGRGeomType("point"), "POINT")
|
|
|
|
self.assertNotEqual(OGRGeomType("point"), 2)
|
|
|
|
self.assertEqual(OGRGeomType("unknown"), 0)
|
|
|
|
self.assertEqual(OGRGeomType(6), "MULtiPolyGON")
|
|
|
|
self.assertEqual(OGRGeomType(1), OGRGeomType("point"))
|
|
|
|
self.assertNotEqual(OGRGeomType("POINT"), OGRGeomType(6))
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
# Testing the Django field name equivalent property.
|
|
|
|
self.assertEqual("PointField", OGRGeomType("Point").django)
|
2013-10-11 05:01:07 +08:00
|
|
|
self.assertEqual("GeometryField", OGRGeomType("Geometry").django)
|
2009-03-08 07:02:48 +08:00
|
|
|
self.assertEqual("GeometryField", OGRGeomType("Unknown").django)
|
2013-10-11 05:01:07 +08:00
|
|
|
self.assertIsNone(OGRGeomType("none").django)
|
2008-08-06 02:13:06 +08:00
|
|
|
|
2009-03-08 07:02:48 +08:00
|
|
|
# 'Geometry' initialization implies an unknown geometry type.
|
|
|
|
gt = OGRGeomType("Geometry")
|
|
|
|
self.assertEqual(0, gt.num)
|
|
|
|
self.assertEqual("Unknown", gt.name)
|
|
|
|
|
2015-05-13 20:12:53 +08:00
|
|
|
def test_geomtype_25d(self):
|
2009-11-15 07:25:44 +08:00
|
|
|
"Testing OGRGeomType object with 25D types."
|
|
|
|
wkb25bit = OGRGeomType.wkb25bit
|
2013-10-11 05:01:07 +08:00
|
|
|
self.assertEqual(OGRGeomType(wkb25bit + 1), "Point25D")
|
|
|
|
self.assertEqual(OGRGeomType("MultiLineString25D"), (5 + wkb25bit))
|
2009-11-15 07:25:44 +08:00
|
|
|
self.assertEqual(
|
|
|
|
"GeometryCollectionField", OGRGeomType("GeometryCollection25D").django
|
|
|
|
)
|
|
|
|
|
2015-05-13 20:12:53 +08:00
|
|
|
def test_wkt(self):
|
2008-08-06 02:13:06 +08:00
|
|
|
"Testing WKT output."
|
2010-11-02 05:56:48 +08:00
|
|
|
for g in self.geometries.wkt_out:
|
2008-08-06 02:13:06 +08:00
|
|
|
geom = OGRGeometry(g.wkt)
|
|
|
|
self.assertEqual(g.wkt, geom.wkt)
|
|
|
|
|
2015-05-13 20:12:53 +08:00
|
|
|
def test_ewkt(self):
|
2010-01-27 11:32:30 +08:00
|
|
|
"Testing EWKT input/output."
|
|
|
|
for ewkt_val in ("POINT (1 2 3)", "LINEARRING (0 0,1 1,2 1,0 0)"):
|
|
|
|
# First with ewkt output when no SRID in EWKT
|
|
|
|
self.assertEqual(ewkt_val, OGRGeometry(ewkt_val).ewkt)
|
|
|
|
# No test consumption with an SRID specified.
|
|
|
|
ewkt_val = "SRID=4326;%s" % ewkt_val
|
|
|
|
geom = OGRGeometry(ewkt_val)
|
|
|
|
self.assertEqual(ewkt_val, geom.ewkt)
|
|
|
|
self.assertEqual(4326, geom.srs.srid)
|
|
|
|
|
2015-05-13 20:12:53 +08:00
|
|
|
def test_gml(self):
|
2008-08-06 02:13:06 +08:00
|
|
|
"Testing GML output."
|
2010-11-02 05:56:48 +08:00
|
|
|
for g in self.geometries.wkt_out:
|
2008-08-06 02:13:06 +08:00
|
|
|
geom = OGRGeometry(g.wkt)
|
2011-03-15 05:15:34 +08:00
|
|
|
exp_gml = g.gml
|
|
|
|
self.assertEqual(exp_gml, geom.gml)
|
2008-08-06 02:13:06 +08:00
|
|
|
|
2015-05-13 20:12:53 +08:00
|
|
|
def test_hex(self):
|
2008-08-06 02:13:06 +08:00
|
|
|
"Testing HEX input/output."
|
2010-11-02 05:56:48 +08:00
|
|
|
for g in self.geometries.hex_wkt:
|
2008-08-06 02:13:06 +08:00
|
|
|
geom1 = OGRGeometry(g.wkt)
|
2012-09-24 01:59:27 +08:00
|
|
|
self.assertEqual(g.hex.encode(), geom1.hex)
|
2008-08-06 02:13:06 +08:00
|
|
|
# Constructing w/HEX
|
|
|
|
geom2 = OGRGeometry(g.hex)
|
|
|
|
self.assertEqual(geom1, geom2)
|
|
|
|
|
2015-05-13 20:12:53 +08:00
|
|
|
def test_wkb(self):
|
2008-08-06 02:13:06 +08:00
|
|
|
"Testing WKB input/output."
|
2010-11-02 05:56:48 +08:00
|
|
|
for g in self.geometries.hex_wkt:
|
2008-08-06 02:13:06 +08:00
|
|
|
geom1 = OGRGeometry(g.wkt)
|
|
|
|
wkb = geom1.wkb
|
2017-11-23 03:25:32 +08:00
|
|
|
self.assertEqual(wkb.hex().upper(), g.hex)
|
2008-08-06 02:13:06 +08:00
|
|
|
# Constructing w/WKB.
|
|
|
|
geom2 = OGRGeometry(wkb)
|
|
|
|
self.assertEqual(geom1, geom2)
|
|
|
|
|
2015-05-13 20:12:53 +08:00
|
|
|
def test_json(self):
|
2008-08-06 02:13:06 +08:00
|
|
|
"Testing GeoJSON input/output."
|
2010-11-02 05:56:48 +08:00
|
|
|
for g in self.geometries.json_geoms:
|
2008-08-06 02:13:06 +08:00
|
|
|
geom = OGRGeometry(g.wkt)
|
2008-11-12 01:21:43 +08:00
|
|
|
if not hasattr(g, "not_equal"):
|
2012-09-13 22:17:32 +08:00
|
|
|
# Loading jsons to prevent decimal differences
|
|
|
|
self.assertEqual(json.loads(g.json), json.loads(geom.json))
|
|
|
|
self.assertEqual(json.loads(g.json), json.loads(geom.geojson))
|
2008-08-06 02:13:06 +08:00
|
|
|
self.assertEqual(OGRGeometry(g.wkt), OGRGeometry(geom.json))
|
2013-11-01 01:15:54 +08:00
|
|
|
# Test input with some garbage content (but valid json) (#15529)
|
|
|
|
geom = OGRGeometry(
|
|
|
|
'{"type": "Point", "coordinates": [ 100.0, 0.0 ], "other": "<test>"}'
|
|
|
|
)
|
|
|
|
self.assertIsInstance(geom, OGRGeometry)
|
2008-08-06 02:13:06 +08:00
|
|
|
|
2015-05-13 20:12:53 +08:00
|
|
|
def test_points(self):
|
2008-08-06 02:13:06 +08:00
|
|
|
"Testing Point objects."
|
|
|
|
|
2013-08-05 00:17:10 +08:00
|
|
|
OGRGeometry("POINT(0 0)")
|
2010-11-02 05:56:48 +08:00
|
|
|
for p in self.geometries.points:
|
2013-11-03 05:02:56 +08:00
|
|
|
if not hasattr(p, "z"): # No 3D
|
2008-08-06 02:13:06 +08:00
|
|
|
pnt = OGRGeometry(p.wkt)
|
|
|
|
self.assertEqual(1, pnt.geom_type)
|
|
|
|
self.assertEqual("POINT", pnt.geom_name)
|
|
|
|
self.assertEqual(p.x, pnt.x)
|
|
|
|
self.assertEqual(p.y, pnt.y)
|
|
|
|
self.assertEqual((p.x, p.y), pnt.tuple)
|
|
|
|
|
2015-05-13 20:12:53 +08:00
|
|
|
def test_multipoints(self):
|
2008-08-06 02:13:06 +08:00
|
|
|
"Testing MultiPoint objects."
|
2010-11-02 05:56:48 +08:00
|
|
|
for mp in self.geometries.multipoints:
|
2013-11-03 05:02:56 +08:00
|
|
|
mgeom1 = OGRGeometry(mp.wkt) # First one from WKT
|
2008-08-06 02:13:06 +08:00
|
|
|
self.assertEqual(4, mgeom1.geom_type)
|
|
|
|
self.assertEqual("MULTIPOINT", mgeom1.geom_name)
|
2013-11-03 05:02:56 +08:00
|
|
|
mgeom2 = OGRGeometry("MULTIPOINT") # Creating empty multipoint
|
2008-08-06 02:13:06 +08:00
|
|
|
mgeom3 = OGRGeometry("MULTIPOINT")
|
|
|
|
for g in mgeom1:
|
2013-11-03 05:02:56 +08:00
|
|
|
mgeom2.add(g) # adding each point from the multipoints
|
|
|
|
mgeom3.add(g.wkt) # should take WKT as well
|
|
|
|
self.assertEqual(mgeom1, mgeom2) # they should equal
|
2008-08-06 02:13:06 +08:00
|
|
|
self.assertEqual(mgeom1, mgeom3)
|
2010-11-02 05:56:48 +08:00
|
|
|
self.assertEqual(mp.coords, mgeom2.coords)
|
2008-08-06 02:13:06 +08:00
|
|
|
self.assertEqual(mp.n_p, mgeom2.point_count)
|
2010-03-30 01:33:59 +08:00
|
|
|
|
2015-05-13 20:12:53 +08:00
|
|
|
def test_linestring(self):
|
2008-08-06 02:13:06 +08:00
|
|
|
"Testing LineString objects."
|
|
|
|
prev = OGRGeometry("POINT(0 0)")
|
2010-11-02 05:56:48 +08:00
|
|
|
for ls in self.geometries.linestrings:
|
2008-08-06 02:13:06 +08:00
|
|
|
linestr = OGRGeometry(ls.wkt)
|
|
|
|
self.assertEqual(2, linestr.geom_type)
|
|
|
|
self.assertEqual("LINESTRING", linestr.geom_name)
|
|
|
|
self.assertEqual(ls.n_p, linestr.point_count)
|
2010-11-02 05:56:48 +08:00
|
|
|
self.assertEqual(ls.coords, linestr.tuple)
|
2013-10-11 05:01:07 +08:00
|
|
|
self.assertEqual(linestr, OGRGeometry(ls.wkt))
|
|
|
|
self.assertNotEqual(linestr, prev)
|
2017-09-02 15:38:17 +08:00
|
|
|
msg = "Index out of range when accessing points of a line string: %s."
|
|
|
|
with self.assertRaisesMessage(IndexError, msg % len(linestr)):
|
2016-01-17 19:26:39 +08:00
|
|
|
linestr.__getitem__(len(linestr))
|
2008-08-06 02:13:06 +08:00
|
|
|
prev = linestr
|
|
|
|
|
|
|
|
# Testing the x, y properties.
|
2010-11-02 05:56:48 +08:00
|
|
|
x = [tmpx for tmpx, tmpy in ls.coords]
|
|
|
|
y = [tmpy for tmpx, tmpy in ls.coords]
|
2008-08-06 02:13:06 +08:00
|
|
|
self.assertEqual(x, linestr.x)
|
|
|
|
self.assertEqual(y, linestr.y)
|
|
|
|
|
2015-05-13 20:12:53 +08:00
|
|
|
def test_multilinestring(self):
|
2008-08-06 02:13:06 +08:00
|
|
|
"Testing MultiLineString objects."
|
|
|
|
prev = OGRGeometry("POINT(0 0)")
|
2010-11-02 05:56:48 +08:00
|
|
|
for mls in self.geometries.multilinestrings:
|
2008-08-06 02:13:06 +08:00
|
|
|
mlinestr = OGRGeometry(mls.wkt)
|
|
|
|
self.assertEqual(5, mlinestr.geom_type)
|
|
|
|
self.assertEqual("MULTILINESTRING", mlinestr.geom_name)
|
|
|
|
self.assertEqual(mls.n_p, mlinestr.point_count)
|
2010-11-02 05:56:48 +08:00
|
|
|
self.assertEqual(mls.coords, mlinestr.tuple)
|
2013-10-11 05:01:07 +08:00
|
|
|
self.assertEqual(mlinestr, OGRGeometry(mls.wkt))
|
|
|
|
self.assertNotEqual(mlinestr, prev)
|
2008-08-06 02:13:06 +08:00
|
|
|
prev = mlinestr
|
|
|
|
for ls in mlinestr:
|
|
|
|
self.assertEqual(2, ls.geom_type)
|
|
|
|
self.assertEqual("LINESTRING", ls.geom_name)
|
2017-09-02 15:38:17 +08:00
|
|
|
msg = "Index out of range when accessing geometry in a collection: %s."
|
|
|
|
with self.assertRaisesMessage(IndexError, msg % len(mlinestr)):
|
2016-01-17 19:26:39 +08:00
|
|
|
mlinestr.__getitem__(len(mlinestr))
|
2008-08-06 02:13:06 +08:00
|
|
|
|
2015-05-13 20:12:53 +08:00
|
|
|
def test_linearring(self):
|
2008-08-06 02:13:06 +08:00
|
|
|
"Testing LinearRing objects."
|
|
|
|
prev = OGRGeometry("POINT(0 0)")
|
2010-11-02 05:56:48 +08:00
|
|
|
for rr in self.geometries.linearrings:
|
2008-08-06 02:13:06 +08:00
|
|
|
lr = OGRGeometry(rr.wkt)
|
2015-02-06 02:25:34 +08:00
|
|
|
# self.assertEqual(101, lr.geom_type.num)
|
2008-08-06 02:13:06 +08:00
|
|
|
self.assertEqual("LINEARRING", lr.geom_name)
|
|
|
|
self.assertEqual(rr.n_p, len(lr))
|
2013-10-11 05:01:07 +08:00
|
|
|
self.assertEqual(lr, OGRGeometry(rr.wkt))
|
|
|
|
self.assertNotEqual(lr, prev)
|
2008-08-06 02:13:06 +08:00
|
|
|
prev = lr
|
|
|
|
|
2015-05-13 20:12:53 +08:00
|
|
|
def test_polygons(self):
|
2008-08-06 02:13:06 +08:00
|
|
|
"Testing Polygon objects."
|
2009-03-08 07:02:48 +08:00
|
|
|
|
|
|
|
# Testing `from_bbox` class method
|
2013-10-11 19:25:14 +08:00
|
|
|
bbox = (-180, -90, 180, 90)
|
2013-10-15 03:13:14 +08:00
|
|
|
p = OGRGeometry.from_bbox(bbox)
|
2009-03-08 07:02:48 +08:00
|
|
|
self.assertEqual(bbox, p.extent)
|
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
prev = OGRGeometry("POINT(0 0)")
|
2010-11-02 05:56:48 +08:00
|
|
|
for p in self.geometries.polygons:
|
2008-08-06 02:13:06 +08:00
|
|
|
poly = OGRGeometry(p.wkt)
|
|
|
|
self.assertEqual(3, poly.geom_type)
|
|
|
|
self.assertEqual("POLYGON", poly.geom_name)
|
|
|
|
self.assertEqual(p.n_p, poly.point_count)
|
|
|
|
self.assertEqual(p.n_i + 1, len(poly))
|
2017-09-02 15:38:17 +08:00
|
|
|
msg = "Index out of range when accessing rings of a polygon: %s."
|
|
|
|
with self.assertRaisesMessage(IndexError, msg % len(poly)):
|
|
|
|
poly.__getitem__(len(poly))
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
# Testing area & centroid.
|
|
|
|
self.assertAlmostEqual(p.area, poly.area, 9)
|
|
|
|
x, y = poly.centroid.tuple
|
|
|
|
self.assertAlmostEqual(p.centroid[0], x, 9)
|
|
|
|
self.assertAlmostEqual(p.centroid[1], y, 9)
|
|
|
|
|
|
|
|
# Testing equivalence
|
2013-10-11 05:01:07 +08:00
|
|
|
self.assertEqual(poly, OGRGeometry(p.wkt))
|
|
|
|
self.assertNotEqual(poly, prev)
|
2010-03-30 01:33:59 +08:00
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
if p.ext_ring_cs:
|
|
|
|
ring = poly[0]
|
|
|
|
self.assertEqual(p.ext_ring_cs, ring.tuple)
|
|
|
|
self.assertEqual(p.ext_ring_cs, poly[0].tuple)
|
|
|
|
self.assertEqual(len(p.ext_ring_cs), ring.point_count)
|
2010-03-30 01:33:59 +08:00
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
for r in poly:
|
|
|
|
self.assertEqual("LINEARRING", r.geom_name)
|
|
|
|
|
2017-09-04 09:19:37 +08:00
|
|
|
def test_polygons_templates(self):
|
|
|
|
# Accessing Polygon attributes in templates should work.
|
|
|
|
engine = Engine()
|
|
|
|
template = engine.from_string("{{ polygons.0.wkt }}")
|
|
|
|
polygons = [OGRGeometry(p.wkt) for p in self.geometries.multipolygons[:2]]
|
|
|
|
content = template.render(Context({"polygons": polygons}))
|
|
|
|
self.assertIn("MULTIPOLYGON (((100", content)
|
|
|
|
|
2015-05-13 20:12:53 +08:00
|
|
|
def test_closepolygons(self):
|
2008-08-06 02:13:06 +08:00
|
|
|
"Testing closing Polygon objects."
|
|
|
|
# Both rings in this geometry are not closed.
|
|
|
|
poly = OGRGeometry("POLYGON((0 0, 5 0, 5 5, 0 5), (1 1, 2 1, 2 2, 2 1))")
|
|
|
|
self.assertEqual(8, poly.point_count)
|
2014-12-18 05:00:05 +08:00
|
|
|
with self.assertRaises(GDALException):
|
2013-08-05 00:17:10 +08:00
|
|
|
poly.centroid
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
poly.close_rings()
|
2013-11-03 05:02:56 +08:00
|
|
|
self.assertEqual(
|
|
|
|
10, poly.point_count
|
|
|
|
) # Two closing points should've been added
|
2008-08-06 02:13:06 +08:00
|
|
|
self.assertEqual(OGRGeometry("POINT(2.5 2.5)"), poly.centroid)
|
|
|
|
|
2015-05-13 20:12:53 +08:00
|
|
|
def test_multipolygons(self):
|
2008-08-06 02:13:06 +08:00
|
|
|
"Testing MultiPolygon objects."
|
2013-08-05 00:17:10 +08:00
|
|
|
OGRGeometry("POINT(0 0)")
|
2010-11-02 05:56:48 +08:00
|
|
|
for mp in self.geometries.multipolygons:
|
2008-08-06 02:13:06 +08:00
|
|
|
mpoly = OGRGeometry(mp.wkt)
|
|
|
|
self.assertEqual(6, mpoly.geom_type)
|
|
|
|
self.assertEqual("MULTIPOLYGON", mpoly.geom_name)
|
|
|
|
if mp.valid:
|
|
|
|
self.assertEqual(mp.n_p, mpoly.point_count)
|
|
|
|
self.assertEqual(mp.num_geom, len(mpoly))
|
2017-09-02 15:38:17 +08:00
|
|
|
msg = "Index out of range when accessing geometry in a collection: %s."
|
|
|
|
with self.assertRaisesMessage(IndexError, msg % len(mpoly)):
|
2016-01-17 19:26:39 +08:00
|
|
|
mpoly.__getitem__(len(mpoly))
|
2008-08-06 02:13:06 +08:00
|
|
|
for p in mpoly:
|
|
|
|
self.assertEqual("POLYGON", p.geom_name)
|
|
|
|
self.assertEqual(3, p.geom_type)
|
|
|
|
self.assertEqual(mpoly.wkt, OGRGeometry(mp.wkt).wkt)
|
|
|
|
|
2015-05-13 20:12:53 +08:00
|
|
|
def test_srs(self):
|
2008-08-06 02:13:06 +08:00
|
|
|
"Testing OGR Geometries with Spatial Reference objects."
|
2010-11-02 05:56:48 +08:00
|
|
|
for mp in self.geometries.multipolygons:
|
2008-08-06 02:13:06 +08:00
|
|
|
# Creating a geometry w/spatial reference
|
|
|
|
sr = SpatialReference("WGS84")
|
|
|
|
mpoly = OGRGeometry(mp.wkt, sr)
|
|
|
|
self.assertEqual(sr.wkt, mpoly.srs.wkt)
|
2010-03-30 01:33:59 +08:00
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
# Ensuring that SRS is propagated to clones.
|
|
|
|
klone = mpoly.clone()
|
|
|
|
self.assertEqual(sr.wkt, klone.srs.wkt)
|
2010-03-30 01:33:59 +08:00
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
# Ensuring all children geometries (polygons and their rings) all
|
|
|
|
# return the assigned spatial reference as well.
|
|
|
|
for poly in mpoly:
|
|
|
|
self.assertEqual(sr.wkt, poly.srs.wkt)
|
|
|
|
for ring in poly:
|
|
|
|
self.assertEqual(sr.wkt, ring.srs.wkt)
|
|
|
|
|
|
|
|
# Ensuring SRS propagate in topological ops.
|
2010-11-02 05:56:48 +08:00
|
|
|
a = OGRGeometry(self.geometries.topology_geoms[0].wkt_a, sr)
|
|
|
|
b = OGRGeometry(self.geometries.topology_geoms[0].wkt_b, sr)
|
2008-08-06 02:13:06 +08:00
|
|
|
diff = a.difference(b)
|
|
|
|
union = a.union(b)
|
|
|
|
self.assertEqual(sr.wkt, diff.srs.wkt)
|
|
|
|
self.assertEqual(sr.srid, union.srs.srid)
|
|
|
|
|
|
|
|
# Instantiating w/an integer SRID
|
|
|
|
mpoly = OGRGeometry(mp.wkt, 4326)
|
|
|
|
self.assertEqual(4326, mpoly.srid)
|
|
|
|
mpoly.srs = SpatialReference(4269)
|
|
|
|
self.assertEqual(4269, mpoly.srid)
|
|
|
|
self.assertEqual("NAD83", mpoly.srs.name)
|
2010-03-30 01:33:59 +08:00
|
|
|
|
2014-03-02 22:25:53 +08:00
|
|
|
# Incrementing through the multipolygon after the spatial reference
|
2008-08-06 02:13:06 +08:00
|
|
|
# has been re-assigned.
|
|
|
|
for poly in mpoly:
|
|
|
|
self.assertEqual(mpoly.srs.wkt, poly.srs.wkt)
|
|
|
|
poly.srs = 32140
|
|
|
|
for ring in poly:
|
|
|
|
# Changing each ring in the polygon
|
|
|
|
self.assertEqual(32140, ring.srs.srid)
|
|
|
|
self.assertEqual("NAD83 / Texas South Central", ring.srs.name)
|
2013-11-03 05:02:56 +08:00
|
|
|
ring.srs = str(SpatialReference(4326)) # back to WGS84
|
2008-08-06 02:13:06 +08:00
|
|
|
self.assertEqual(4326, ring.srs.srid)
|
|
|
|
|
|
|
|
# Using the `srid` property.
|
|
|
|
ring.srid = 4322
|
|
|
|
self.assertEqual("WGS 72", ring.srs.name)
|
|
|
|
self.assertEqual(4322, ring.srid)
|
|
|
|
|
2015-10-21 21:14:12 +08:00
|
|
|
# srs/srid may be assigned their own values, even when srs is None.
|
|
|
|
mpoly = OGRGeometry(mp.wkt, srs=None)
|
|
|
|
mpoly.srs = mpoly.srs
|
|
|
|
mpoly.srid = mpoly.srid
|
|
|
|
|
2015-05-13 20:12:53 +08:00
|
|
|
def test_srs_transform(self):
|
2008-08-06 02:13:06 +08:00
|
|
|
"Testing transform()."
|
|
|
|
orig = OGRGeometry("POINT (-104.609 38.255)", 4326)
|
|
|
|
trans = OGRGeometry("POINT (992385.4472045 481455.4944650)", 2774)
|
|
|
|
|
|
|
|
# Using an srid, a SpatialReference object, and a CoordTransform object
|
|
|
|
# or transformations.
|
|
|
|
t1, t2, t3 = orig.clone(), orig.clone(), orig.clone()
|
|
|
|
t1.transform(trans.srid)
|
|
|
|
t2.transform(SpatialReference("EPSG:2774"))
|
|
|
|
ct = CoordTransform(SpatialReference("WGS84"), SpatialReference(2774))
|
|
|
|
t3.transform(ct)
|
|
|
|
|
|
|
|
# Testing use of the `clone` keyword.
|
|
|
|
k1 = orig.clone()
|
|
|
|
k2 = k1.transform(trans.srid, clone=True)
|
|
|
|
self.assertEqual(k1, orig)
|
|
|
|
self.assertNotEqual(k1, k2)
|
|
|
|
|
2021-03-23 16:16:33 +08:00
|
|
|
# Different PROJ versions use different transformations, all are
|
|
|
|
# correct as having a 1 meter accuracy.
|
|
|
|
prec = -1
|
2008-08-06 02:13:06 +08:00
|
|
|
for p in (t1, t2, t3, k2):
|
|
|
|
self.assertAlmostEqual(trans.x, p.x, prec)
|
|
|
|
self.assertAlmostEqual(trans.y, p.y, prec)
|
|
|
|
|
2015-05-13 20:12:53 +08:00
|
|
|
def test_transform_dim(self):
|
2009-10-18 01:32:25 +08:00
|
|
|
"Testing coordinate dimension is the same on transformed geometries."
|
|
|
|
ls_orig = OGRGeometry("LINESTRING(-104.609 38.255)", 4326)
|
|
|
|
ls_trans = OGRGeometry("LINESTRING(992385.4472045 481455.4944650)", 2774)
|
2010-03-30 01:33:59 +08:00
|
|
|
|
2021-03-23 16:16:33 +08:00
|
|
|
# Different PROJ versions use different transformations, all are
|
|
|
|
# correct as having a 1 meter accuracy.
|
|
|
|
prec = -1
|
2009-10-18 01:32:25 +08:00
|
|
|
ls_orig.transform(ls_trans.srs)
|
|
|
|
# Making sure the coordinate dimension is still 2D.
|
|
|
|
self.assertEqual(2, ls_orig.coord_dim)
|
|
|
|
self.assertAlmostEqual(ls_trans.x[0], ls_orig.x[0], prec)
|
|
|
|
self.assertAlmostEqual(ls_trans.y[0], ls_orig.y[0], prec)
|
|
|
|
|
2015-05-13 20:12:53 +08:00
|
|
|
def test_difference(self):
|
2008-08-06 02:13:06 +08:00
|
|
|
"Testing difference()."
|
2014-12-13 21:04:36 +08:00
|
|
|
for i in range(len(self.geometries.topology_geoms)):
|
2010-11-02 05:56:48 +08:00
|
|
|
a = OGRGeometry(self.geometries.topology_geoms[i].wkt_a)
|
|
|
|
b = OGRGeometry(self.geometries.topology_geoms[i].wkt_b)
|
|
|
|
d1 = OGRGeometry(self.geometries.diff_geoms[i].wkt)
|
2008-08-06 02:13:06 +08:00
|
|
|
d2 = a.difference(b)
|
2021-04-03 20:47:27 +08:00
|
|
|
self.assertTrue(d1.geos.equals(d2.geos))
|
|
|
|
self.assertTrue(
|
|
|
|
d1.geos.equals((a - b).geos)
|
|
|
|
) # __sub__ is difference operator
|
2013-11-03 05:02:56 +08:00
|
|
|
a -= b # testing __isub__
|
2021-04-03 20:47:27 +08:00
|
|
|
self.assertTrue(d1.geos.equals(a.geos))
|
2008-08-06 02:13:06 +08:00
|
|
|
|
2015-05-13 20:12:53 +08:00
|
|
|
def test_intersection(self):
|
2008-08-06 02:13:06 +08:00
|
|
|
"Testing intersects() and intersection()."
|
2014-12-13 21:04:36 +08:00
|
|
|
for i in range(len(self.geometries.topology_geoms)):
|
2010-11-02 05:56:48 +08:00
|
|
|
a = OGRGeometry(self.geometries.topology_geoms[i].wkt_a)
|
|
|
|
b = OGRGeometry(self.geometries.topology_geoms[i].wkt_b)
|
|
|
|
i1 = OGRGeometry(self.geometries.intersect_geoms[i].wkt)
|
2013-10-11 05:01:07 +08:00
|
|
|
self.assertTrue(a.intersects(b))
|
2008-08-06 02:13:06 +08:00
|
|
|
i2 = a.intersection(b)
|
2021-04-03 20:47:27 +08:00
|
|
|
self.assertTrue(i1.geos.equals(i2.geos))
|
|
|
|
self.assertTrue(
|
|
|
|
i1.geos.equals((a & b).geos)
|
|
|
|
) # __and__ is intersection operator
|
2013-11-03 05:02:56 +08:00
|
|
|
a &= b # testing __iand__
|
2021-04-03 20:47:27 +08:00
|
|
|
self.assertTrue(i1.geos.equals(a.geos))
|
2008-08-06 02:13:06 +08:00
|
|
|
|
2015-05-13 20:12:53 +08:00
|
|
|
def test_symdifference(self):
|
2008-08-06 02:13:06 +08:00
|
|
|
"Testing sym_difference()."
|
2014-12-13 21:04:36 +08:00
|
|
|
for i in range(len(self.geometries.topology_geoms)):
|
2010-11-02 05:56:48 +08:00
|
|
|
a = OGRGeometry(self.geometries.topology_geoms[i].wkt_a)
|
|
|
|
b = OGRGeometry(self.geometries.topology_geoms[i].wkt_b)
|
|
|
|
d1 = OGRGeometry(self.geometries.sdiff_geoms[i].wkt)
|
2008-08-06 02:13:06 +08:00
|
|
|
d2 = a.sym_difference(b)
|
2021-04-03 20:47:27 +08:00
|
|
|
self.assertTrue(d1.geos.equals(d2.geos))
|
|
|
|
self.assertTrue(
|
|
|
|
d1.geos.equals((a ^ b).geos)
|
|
|
|
) # __xor__ is symmetric difference operator
|
2013-11-03 05:02:56 +08:00
|
|
|
a ^= b # testing __ixor__
|
2021-04-03 20:47:27 +08:00
|
|
|
self.assertTrue(d1.geos.equals(a.geos))
|
2010-03-30 01:33:59 +08:00
|
|
|
|
2015-05-13 20:12:53 +08:00
|
|
|
def test_union(self):
|
2008-08-06 02:13:06 +08:00
|
|
|
"Testing union()."
|
2014-12-13 21:04:36 +08:00
|
|
|
for i in range(len(self.geometries.topology_geoms)):
|
2010-11-02 05:56:48 +08:00
|
|
|
a = OGRGeometry(self.geometries.topology_geoms[i].wkt_a)
|
|
|
|
b = OGRGeometry(self.geometries.topology_geoms[i].wkt_b)
|
|
|
|
u1 = OGRGeometry(self.geometries.union_geoms[i].wkt)
|
2008-08-06 02:13:06 +08:00
|
|
|
u2 = a.union(b)
|
2021-04-03 20:47:27 +08:00
|
|
|
self.assertTrue(u1.geos.equals(u2.geos))
|
|
|
|
self.assertTrue(u1.geos.equals((a | b).geos)) # __or__ is union operator
|
2013-11-03 05:02:56 +08:00
|
|
|
a |= b # testing __ior__
|
2021-04-03 20:47:27 +08:00
|
|
|
self.assertTrue(u1.geos.equals(a.geos))
|
2008-08-06 02:13:06 +08:00
|
|
|
|
2015-05-13 20:12:53 +08:00
|
|
|
def test_add(self):
|
2008-08-06 02:13:06 +08:00
|
|
|
"Testing GeometryCollection.add()."
|
|
|
|
# Can't insert a Point into a MultiPolygon.
|
|
|
|
mp = OGRGeometry("MultiPolygon")
|
|
|
|
pnt = OGRGeometry("POINT(5 23)")
|
2016-01-17 19:26:39 +08:00
|
|
|
with self.assertRaises(GDALException):
|
|
|
|
mp.add(pnt)
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
# GeometryCollection.add may take an OGRGeometry (if another collection
|
|
|
|
# of the same type all child geoms will be added individually) or WKT.
|
2010-11-02 05:56:48 +08:00
|
|
|
for mp in self.geometries.multipolygons:
|
2008-08-06 02:13:06 +08:00
|
|
|
mpoly = OGRGeometry(mp.wkt)
|
|
|
|
mp1 = OGRGeometry("MultiPolygon")
|
|
|
|
mp2 = OGRGeometry("MultiPolygon")
|
|
|
|
mp3 = OGRGeometry("MultiPolygon")
|
|
|
|
|
|
|
|
for poly in mpoly:
|
2013-11-03 05:02:56 +08:00
|
|
|
mp1.add(poly) # Adding a geometry at a time
|
|
|
|
mp2.add(poly.wkt) # Adding WKT
|
|
|
|
mp3.add(mpoly) # Adding a MultiPolygon's entire contents at once.
|
2013-10-17 16:17:41 +08:00
|
|
|
for tmp in (mp1, mp2, mp3):
|
|
|
|
self.assertEqual(mpoly, tmp)
|
2008-08-06 02:13:06 +08:00
|
|
|
|
2015-05-13 20:12:53 +08:00
|
|
|
def test_extent(self):
|
2008-08-06 02:13:06 +08:00
|
|
|
"Testing `extent` property."
|
|
|
|
# The xmin, ymin, xmax, ymax of the MultiPoint should be returned.
|
|
|
|
mp = OGRGeometry("MULTIPOINT(5 23, 0 0, 10 50)")
|
|
|
|
self.assertEqual((0.0, 0.0, 10.0, 50.0), mp.extent)
|
|
|
|
# Testing on the 'real world' Polygon.
|
2010-11-02 05:56:48 +08:00
|
|
|
poly = OGRGeometry(self.geometries.polygons[3].wkt)
|
2008-08-06 02:13:06 +08:00
|
|
|
ring = poly.shell
|
|
|
|
x, y = ring.x, ring.y
|
|
|
|
xmin, ymin = min(x), min(y)
|
|
|
|
xmax, ymax = max(x), max(y)
|
|
|
|
self.assertEqual((xmin, ymin, xmax, ymax), poly.extent)
|
|
|
|
|
2015-05-13 20:12:53 +08:00
|
|
|
def test_25D(self):
|
2009-11-15 07:25:44 +08:00
|
|
|
"Testing 2.5D geometries."
|
|
|
|
pnt_25d = OGRGeometry("POINT(1 2 3)")
|
|
|
|
self.assertEqual("Point25D", pnt_25d.geom_type.name)
|
|
|
|
self.assertEqual(3.0, pnt_25d.z)
|
|
|
|
self.assertEqual(3, pnt_25d.coord_dim)
|
|
|
|
ls_25d = OGRGeometry("LINESTRING(1 1 1,2 2 2,3 3 3)")
|
|
|
|
self.assertEqual("LineString25D", ls_25d.geom_type.name)
|
|
|
|
self.assertEqual([1.0, 2.0, 3.0], ls_25d.z)
|
|
|
|
self.assertEqual(3, ls_25d.coord_dim)
|
|
|
|
|
2015-05-13 20:12:53 +08:00
|
|
|
def test_pickle(self):
|
2010-01-27 11:47:37 +08:00
|
|
|
"Testing pickle support."
|
|
|
|
g1 = OGRGeometry("LINESTRING(1 1 1,2 2 2,3 3 3)", "WGS84")
|
2011-07-13 17:35:51 +08:00
|
|
|
g2 = pickle.loads(pickle.dumps(g1))
|
2010-01-27 11:47:37 +08:00
|
|
|
self.assertEqual(g1, g2)
|
|
|
|
self.assertEqual(4326, g2.srs.srid)
|
|
|
|
self.assertEqual(g1.srs.wkt, g2.srs.wkt)
|
2010-03-30 01:33:59 +08:00
|
|
|
|
2015-05-13 20:12:53 +08:00
|
|
|
def test_ogrgeometry_transform_workaround(self):
|
2010-03-30 01:33:59 +08:00
|
|
|
"Testing coordinate dimensions on geometries after transformation."
|
|
|
|
# A bug in GDAL versions prior to 1.7 changes the coordinate
|
|
|
|
# dimension of a geometry after it has been transformed.
|
|
|
|
# This test ensures that the bug workarounds employed within
|
|
|
|
# `OGRGeometry.transform` indeed work.
|
|
|
|
wkt_2d = "MULTILINESTRING ((0 0,1 1,2 2))"
|
|
|
|
wkt_3d = "MULTILINESTRING ((0 0 0,1 1 1,2 2 2))"
|
|
|
|
srid = 4326
|
|
|
|
|
|
|
|
# For both the 2D and 3D MultiLineString, ensure _both_ the dimension
|
|
|
|
# of the collection and the component LineString have the expected
|
|
|
|
# coordinate dimension after transform.
|
|
|
|
geom = OGRGeometry(wkt_2d, srid)
|
|
|
|
geom.transform(srid)
|
|
|
|
self.assertEqual(2, geom.coord_dim)
|
|
|
|
self.assertEqual(2, geom[0].coord_dim)
|
|
|
|
self.assertEqual(wkt_2d, geom.wkt)
|
|
|
|
|
|
|
|
geom = OGRGeometry(wkt_3d, srid)
|
|
|
|
geom.transform(srid)
|
|
|
|
self.assertEqual(3, geom.coord_dim)
|
|
|
|
self.assertEqual(3, geom[0].coord_dim)
|
|
|
|
self.assertEqual(wkt_3d, geom.wkt)
|
2010-01-27 11:47:37 +08:00
|
|
|
|
2015-12-14 12:33:15 +08:00
|
|
|
# Testing binary predicates, `assertIs` is used to check that bool is returned.
|
|
|
|
|
2015-05-13 20:12:53 +08:00
|
|
|
def test_equivalence_regression(self):
|
2010-04-02 00:56:55 +08:00
|
|
|
"Testing equivalence methods with non-OGRGeometry instances."
|
2013-10-11 05:01:07 +08:00
|
|
|
self.assertIsNotNone(OGRGeometry("POINT(0 0)"))
|
|
|
|
self.assertNotEqual(OGRGeometry("LINESTRING(0 0, 1 1)"), 3)
|
2015-12-14 12:33:15 +08:00
|
|
|
|
|
|
|
def test_contains(self):
|
|
|
|
self.assertIs(
|
|
|
|
OGRGeometry("POINT(0 0)").contains(OGRGeometry("POINT(0 0)")), True
|
|
|
|
)
|
|
|
|
self.assertIs(
|
|
|
|
OGRGeometry("POINT(0 0)").contains(OGRGeometry("POINT(0 1)")), False
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_crosses(self):
|
|
|
|
self.assertIs(
|
|
|
|
OGRGeometry("LINESTRING(0 0, 1 1)").crosses(
|
|
|
|
OGRGeometry("LINESTRING(0 1, 1 0)")
|
|
|
|
),
|
|
|
|
True,
|
|
|
|
)
|
|
|
|
self.assertIs(
|
|
|
|
OGRGeometry("LINESTRING(0 0, 0 1)").crosses(
|
|
|
|
OGRGeometry("LINESTRING(1 0, 1 1)")
|
|
|
|
),
|
|
|
|
False,
|
2022-02-04 03:24:19 +08:00
|
|
|
)
|
2015-12-14 12:33:15 +08:00
|
|
|
|
|
|
|
def test_disjoint(self):
|
|
|
|
self.assertIs(
|
|
|
|
OGRGeometry("LINESTRING(0 0, 1 1)").disjoint(
|
|
|
|
OGRGeometry("LINESTRING(0 1, 1 0)")
|
|
|
|
),
|
|
|
|
False,
|
|
|
|
)
|
|
|
|
self.assertIs(
|
|
|
|
OGRGeometry("LINESTRING(0 0, 0 1)").disjoint(
|
|
|
|
OGRGeometry("LINESTRING(1 0, 1 1)")
|
|
|
|
),
|
|
|
|
True,
|
2022-02-04 03:24:19 +08:00
|
|
|
)
|
2015-12-14 12:33:15 +08:00
|
|
|
|
|
|
|
def test_equals(self):
|
|
|
|
self.assertIs(
|
|
|
|
OGRGeometry("POINT(0 0)").contains(OGRGeometry("POINT(0 0)")), True
|
|
|
|
)
|
|
|
|
self.assertIs(
|
|
|
|
OGRGeometry("POINT(0 0)").contains(OGRGeometry("POINT(0 1)")), False
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_intersects(self):
|
|
|
|
self.assertIs(
|
|
|
|
OGRGeometry("LINESTRING(0 0, 1 1)").intersects(
|
|
|
|
OGRGeometry("LINESTRING(0 1, 1 0)")
|
|
|
|
),
|
|
|
|
True,
|
|
|
|
)
|
|
|
|
self.assertIs(
|
|
|
|
OGRGeometry("LINESTRING(0 0, 0 1)").intersects(
|
|
|
|
OGRGeometry("LINESTRING(1 0, 1 1)")
|
|
|
|
),
|
|
|
|
False,
|
2022-02-04 03:24:19 +08:00
|
|
|
)
|
2015-12-14 12:33:15 +08:00
|
|
|
|
|
|
|
def test_overlaps(self):
|
|
|
|
self.assertIs(
|
|
|
|
OGRGeometry("POLYGON ((0 0, 0 2, 2 2, 2 0, 0 0))").overlaps(
|
|
|
|
OGRGeometry("POLYGON ((1 1, 1 5, 5 5, 5 1, 1 1))")
|
|
|
|
),
|
|
|
|
True,
|
2022-02-04 03:24:19 +08:00
|
|
|
)
|
2015-12-14 12:33:15 +08:00
|
|
|
self.assertIs(
|
|
|
|
OGRGeometry("POINT(0 0)").overlaps(OGRGeometry("POINT(0 1)")), False
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_touches(self):
|
|
|
|
self.assertIs(
|
|
|
|
OGRGeometry("POLYGON ((0 0, 0 1, 1 1, 1 0, 0 0))").touches(
|
|
|
|
OGRGeometry("LINESTRING(0 2, 2 0)")
|
2022-02-04 03:24:19 +08:00
|
|
|
),
|
2015-12-14 12:33:15 +08:00
|
|
|
True,
|
2022-02-04 03:24:19 +08:00
|
|
|
)
|
2015-12-14 12:33:15 +08:00
|
|
|
self.assertIs(
|
|
|
|
OGRGeometry("POINT(0 0)").touches(OGRGeometry("POINT(0 1)")), False
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_within(self):
|
|
|
|
self.assertIs(
|
|
|
|
OGRGeometry("POINT(0.5 0.5)").within(
|
|
|
|
OGRGeometry("POLYGON ((0 0, 0 1, 1 1, 1 0, 0 0))")
|
2022-02-04 03:24:19 +08:00
|
|
|
),
|
2015-12-14 12:33:15 +08:00
|
|
|
True,
|
2022-02-04 03:24:19 +08:00
|
|
|
)
|
2015-12-14 12:33:15 +08:00
|
|
|
self.assertIs(
|
|
|
|
OGRGeometry("POINT(0 0)").within(OGRGeometry("POINT(0 1)")), False
|
|
|
|
)
|
2016-06-10 14:50:07 +08:00
|
|
|
|
|
|
|
def test_from_gml(self):
|
|
|
|
self.assertEqual(
|
|
|
|
OGRGeometry("POINT(0 0)"),
|
|
|
|
OGRGeometry.from_gml(
|
|
|
|
'<gml:Point gml:id="p21" '
|
|
|
|
'srsName="http://www.opengis.net/def/crs/EPSG/0/4326">'
|
|
|
|
' <gml:pos srsDimension="2">0 0</gml:pos>'
|
|
|
|
"</gml:Point>"
|
|
|
|
),
|
|
|
|
)
|
2016-12-02 16:18:41 +08:00
|
|
|
|
|
|
|
def test_empty(self):
|
|
|
|
self.assertIs(OGRGeometry("POINT (0 0)").empty, False)
|
|
|
|
self.assertIs(OGRGeometry("POINT EMPTY").empty, True)
|
|
|
|
|
|
|
|
def test_empty_point_to_geos(self):
|
|
|
|
p = OGRGeometry("POINT EMPTY", srs=4326)
|
|
|
|
self.assertEqual(p.geos.ewkt, p.ewkt)
|