2012-09-13 14:17:32 +00:00
|
|
|
import json
|
2013-07-01 12:22:27 +00:00
|
|
|
import unittest
|
2015-01-28 12:35:27 +00:00
|
|
|
from binascii import b2a_hex
|
2013-07-01 12:22:27 +00:00
|
|
|
from unittest import skipUnless
|
2011-07-13 09:35:51 +00:00
|
|
|
|
2013-05-11 03:08:45 +00:00
|
|
|
from django.contrib.gis.gdal import HAS_GDAL
|
2014-12-13 13:04:36 +00:00
|
|
|
from django.utils.six.moves import range
|
2010-10-11 12:55:17 +00:00
|
|
|
|
2015-02-10 15:07:44 +00:00
|
|
|
from ..test_data import TestDataMixin
|
|
|
|
|
2015-01-28 12:35:27 +00:00
|
|
|
try:
|
|
|
|
from django.utils.six.moves import cPickle as pickle
|
|
|
|
except ImportError:
|
|
|
|
import pickle
|
|
|
|
|
|
|
|
|
2013-05-11 03:08:45 +00:00
|
|
|
if HAS_GDAL:
|
2016-04-08 02:04:45 +00:00
|
|
|
from django.contrib.gis.gdal import (
|
2016-11-12 16:06:05 +00:00
|
|
|
CoordTransform, GDALException, OGRGeometry, OGRGeomType, OGRIndexError,
|
|
|
|
SpatialReference,
|
2016-04-08 02:04:45 +00:00
|
|
|
)
|
2013-05-11 03:08:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
@skipUnless(HAS_GDAL, "GDAL is required")
|
2010-11-01 21:56:48 +00:00
|
|
|
class OGRGeomTest(unittest.TestCase, TestDataMixin):
|
2008-08-05 18:13:06 +00:00
|
|
|
"This tests the OGR Geometry."
|
|
|
|
|
2015-05-13 12:12:53 +00:00
|
|
|
def test_geomtype(self):
|
2008-08-05 18:13:06 +00:00
|
|
|
"Testing OGRGeomType object."
|
|
|
|
|
|
|
|
# OGRGeomType should initialize on all these inputs.
|
2013-08-04 16:17:10 +00:00
|
|
|
OGRGeomType(1)
|
|
|
|
OGRGeomType(7)
|
|
|
|
OGRGeomType('point')
|
|
|
|
OGRGeomType('GeometrycollectioN')
|
|
|
|
OGRGeomType('LINearrING')
|
|
|
|
OGRGeomType('Unknown')
|
2008-08-05 18:13:06 +00:00
|
|
|
|
|
|
|
# Should throw TypeError on this input
|
2016-01-17 11:26:39 +00:00
|
|
|
with self.assertRaises(GDALException):
|
|
|
|
OGRGeomType(23)
|
|
|
|
with self.assertRaises(GDALException):
|
|
|
|
OGRGeomType('fooD')
|
|
|
|
with self.assertRaises(GDALException):
|
|
|
|
OGRGeomType(9)
|
2008-08-05 18:13:06 +00:00
|
|
|
|
|
|
|
# Equivalence can take strings, ints, and other OGRGeomTypes
|
2013-10-10 21:01:07 +00: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-05 18:13:06 +00:00
|
|
|
|
|
|
|
# Testing the Django field name equivalent property.
|
|
|
|
self.assertEqual('PointField', OGRGeomType('Point').django)
|
2013-10-10 21:01:07 +00:00
|
|
|
self.assertEqual('GeometryField', OGRGeomType('Geometry').django)
|
2009-03-07 23:02:48 +00:00
|
|
|
self.assertEqual('GeometryField', OGRGeomType('Unknown').django)
|
2013-10-10 21:01:07 +00:00
|
|
|
self.assertIsNone(OGRGeomType('none').django)
|
2008-08-05 18:13:06 +00:00
|
|
|
|
2009-03-07 23:02:48 +00:00
|
|
|
# 'Geometry' initialization implies an unknown geometry type.
|
|
|
|
gt = OGRGeomType('Geometry')
|
|
|
|
self.assertEqual(0, gt.num)
|
|
|
|
self.assertEqual('Unknown', gt.name)
|
|
|
|
|
2015-05-13 12:12:53 +00:00
|
|
|
def test_geomtype_25d(self):
|
2009-11-14 23:25:44 +00:00
|
|
|
"Testing OGRGeomType object with 25D types."
|
|
|
|
wkb25bit = OGRGeomType.wkb25bit
|
2013-10-10 21:01:07 +00:00
|
|
|
self.assertEqual(OGRGeomType(wkb25bit + 1), 'Point25D')
|
|
|
|
self.assertEqual(OGRGeomType('MultiLineString25D'), (5 + wkb25bit))
|
2009-11-14 23:25:44 +00:00
|
|
|
self.assertEqual('GeometryCollectionField', OGRGeomType('GeometryCollection25D').django)
|
|
|
|
|
2015-05-13 12:12:53 +00:00
|
|
|
def test_wkt(self):
|
2008-08-05 18:13:06 +00:00
|
|
|
"Testing WKT output."
|
2010-11-01 21:56:48 +00:00
|
|
|
for g in self.geometries.wkt_out:
|
2008-08-05 18:13:06 +00:00
|
|
|
geom = OGRGeometry(g.wkt)
|
|
|
|
self.assertEqual(g.wkt, geom.wkt)
|
|
|
|
|
2015-05-13 12:12:53 +00:00
|
|
|
def test_ewkt(self):
|
2010-01-27 03:32:30 +00: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 12:12:53 +00:00
|
|
|
def test_gml(self):
|
2008-08-05 18:13:06 +00:00
|
|
|
"Testing GML output."
|
2010-11-01 21:56:48 +00:00
|
|
|
for g in self.geometries.wkt_out:
|
2008-08-05 18:13:06 +00:00
|
|
|
geom = OGRGeometry(g.wkt)
|
2011-03-14 21:15:34 +00:00
|
|
|
exp_gml = g.gml
|
|
|
|
self.assertEqual(exp_gml, geom.gml)
|
2008-08-05 18:13:06 +00:00
|
|
|
|
2015-05-13 12:12:53 +00:00
|
|
|
def test_hex(self):
|
2008-08-05 18:13:06 +00:00
|
|
|
"Testing HEX input/output."
|
2010-11-01 21:56:48 +00:00
|
|
|
for g in self.geometries.hex_wkt:
|
2008-08-05 18:13:06 +00:00
|
|
|
geom1 = OGRGeometry(g.wkt)
|
2012-09-23 17:59:27 +00:00
|
|
|
self.assertEqual(g.hex.encode(), geom1.hex)
|
2008-08-05 18:13:06 +00:00
|
|
|
# Constructing w/HEX
|
|
|
|
geom2 = OGRGeometry(g.hex)
|
|
|
|
self.assertEqual(geom1, geom2)
|
|
|
|
|
2015-05-13 12:12:53 +00:00
|
|
|
def test_wkb(self):
|
2008-08-05 18:13:06 +00:00
|
|
|
"Testing WKB input/output."
|
2010-11-01 21:56:48 +00:00
|
|
|
for g in self.geometries.hex_wkt:
|
2008-08-05 18:13:06 +00:00
|
|
|
geom1 = OGRGeometry(g.wkt)
|
|
|
|
wkb = geom1.wkb
|
2012-09-23 17:59:27 +00:00
|
|
|
self.assertEqual(b2a_hex(wkb).upper(), g.hex.encode())
|
2008-08-05 18:13:06 +00:00
|
|
|
# Constructing w/WKB.
|
|
|
|
geom2 = OGRGeometry(wkb)
|
|
|
|
self.assertEqual(geom1, geom2)
|
|
|
|
|
2015-05-13 12:12:53 +00:00
|
|
|
def test_json(self):
|
2008-08-05 18:13:06 +00:00
|
|
|
"Testing GeoJSON input/output."
|
2010-11-01 21:56:48 +00:00
|
|
|
for g in self.geometries.json_geoms:
|
2008-08-05 18:13:06 +00:00
|
|
|
geom = OGRGeometry(g.wkt)
|
2008-11-11 17:21:43 +00:00
|
|
|
if not hasattr(g, 'not_equal'):
|
2012-09-13 14:17:32 +00: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-05 18:13:06 +00:00
|
|
|
self.assertEqual(OGRGeometry(g.wkt), OGRGeometry(geom.json))
|
2013-10-31 17:15:54 +00: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-05 18:13:06 +00:00
|
|
|
|
2015-05-13 12:12:53 +00:00
|
|
|
def test_points(self):
|
2008-08-05 18:13:06 +00:00
|
|
|
"Testing Point objects."
|
|
|
|
|
2013-08-04 16:17:10 +00:00
|
|
|
OGRGeometry('POINT(0 0)')
|
2010-11-01 21:56:48 +00:00
|
|
|
for p in self.geometries.points:
|
2013-11-02 21:02:56 +00:00
|
|
|
if not hasattr(p, 'z'): # No 3D
|
2008-08-05 18:13:06 +00: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 12:12:53 +00:00
|
|
|
def test_multipoints(self):
|
2008-08-05 18:13:06 +00:00
|
|
|
"Testing MultiPoint objects."
|
2010-11-01 21:56:48 +00:00
|
|
|
for mp in self.geometries.multipoints:
|
2013-11-02 21:02:56 +00:00
|
|
|
mgeom1 = OGRGeometry(mp.wkt) # First one from WKT
|
2008-08-05 18:13:06 +00:00
|
|
|
self.assertEqual(4, mgeom1.geom_type)
|
|
|
|
self.assertEqual('MULTIPOINT', mgeom1.geom_name)
|
2013-11-02 21:02:56 +00:00
|
|
|
mgeom2 = OGRGeometry('MULTIPOINT') # Creating empty multipoint
|
2008-08-05 18:13:06 +00:00
|
|
|
mgeom3 = OGRGeometry('MULTIPOINT')
|
|
|
|
for g in mgeom1:
|
2013-11-02 21:02:56 +00: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-05 18:13:06 +00:00
|
|
|
self.assertEqual(mgeom1, mgeom3)
|
2010-11-01 21:56:48 +00:00
|
|
|
self.assertEqual(mp.coords, mgeom2.coords)
|
2008-08-05 18:13:06 +00:00
|
|
|
self.assertEqual(mp.n_p, mgeom2.point_count)
|
2010-03-29 17:33:59 +00:00
|
|
|
|
2015-05-13 12:12:53 +00:00
|
|
|
def test_linestring(self):
|
2008-08-05 18:13:06 +00:00
|
|
|
"Testing LineString objects."
|
|
|
|
prev = OGRGeometry('POINT(0 0)')
|
2010-11-01 21:56:48 +00:00
|
|
|
for ls in self.geometries.linestrings:
|
2008-08-05 18:13:06 +00: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-01 21:56:48 +00:00
|
|
|
self.assertEqual(ls.coords, linestr.tuple)
|
2013-10-10 21:01:07 +00:00
|
|
|
self.assertEqual(linestr, OGRGeometry(ls.wkt))
|
|
|
|
self.assertNotEqual(linestr, prev)
|
2016-01-17 11:26:39 +00:00
|
|
|
with self.assertRaises(OGRIndexError):
|
|
|
|
linestr.__getitem__(len(linestr))
|
2008-08-05 18:13:06 +00:00
|
|
|
prev = linestr
|
|
|
|
|
|
|
|
# Testing the x, y properties.
|
2010-11-01 21:56:48 +00:00
|
|
|
x = [tmpx for tmpx, tmpy in ls.coords]
|
|
|
|
y = [tmpy for tmpx, tmpy in ls.coords]
|
2008-08-05 18:13:06 +00:00
|
|
|
self.assertEqual(x, linestr.x)
|
|
|
|
self.assertEqual(y, linestr.y)
|
|
|
|
|
2015-05-13 12:12:53 +00:00
|
|
|
def test_multilinestring(self):
|
2008-08-05 18:13:06 +00:00
|
|
|
"Testing MultiLineString objects."
|
|
|
|
prev = OGRGeometry('POINT(0 0)')
|
2010-11-01 21:56:48 +00:00
|
|
|
for mls in self.geometries.multilinestrings:
|
2008-08-05 18:13:06 +00: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-01 21:56:48 +00:00
|
|
|
self.assertEqual(mls.coords, mlinestr.tuple)
|
2013-10-10 21:01:07 +00:00
|
|
|
self.assertEqual(mlinestr, OGRGeometry(mls.wkt))
|
|
|
|
self.assertNotEqual(mlinestr, prev)
|
2008-08-05 18:13:06 +00:00
|
|
|
prev = mlinestr
|
|
|
|
for ls in mlinestr:
|
|
|
|
self.assertEqual(2, ls.geom_type)
|
|
|
|
self.assertEqual('LINESTRING', ls.geom_name)
|
2016-01-17 11:26:39 +00:00
|
|
|
with self.assertRaises(OGRIndexError):
|
|
|
|
mlinestr.__getitem__(len(mlinestr))
|
2008-08-05 18:13:06 +00:00
|
|
|
|
2015-05-13 12:12:53 +00:00
|
|
|
def test_linearring(self):
|
2008-08-05 18:13:06 +00:00
|
|
|
"Testing LinearRing objects."
|
|
|
|
prev = OGRGeometry('POINT(0 0)')
|
2010-11-01 21:56:48 +00:00
|
|
|
for rr in self.geometries.linearrings:
|
2008-08-05 18:13:06 +00:00
|
|
|
lr = OGRGeometry(rr.wkt)
|
2015-02-05 18:25:34 +00:00
|
|
|
# self.assertEqual(101, lr.geom_type.num)
|
2008-08-05 18:13:06 +00:00
|
|
|
self.assertEqual('LINEARRING', lr.geom_name)
|
|
|
|
self.assertEqual(rr.n_p, len(lr))
|
2013-10-10 21:01:07 +00:00
|
|
|
self.assertEqual(lr, OGRGeometry(rr.wkt))
|
|
|
|
self.assertNotEqual(lr, prev)
|
2008-08-05 18:13:06 +00:00
|
|
|
prev = lr
|
|
|
|
|
2015-05-13 12:12:53 +00:00
|
|
|
def test_polygons(self):
|
2008-08-05 18:13:06 +00:00
|
|
|
"Testing Polygon objects."
|
2009-03-07 23:02:48 +00:00
|
|
|
|
|
|
|
# Testing `from_bbox` class method
|
2013-10-11 11:25:14 +00:00
|
|
|
bbox = (-180, -90, 180, 90)
|
2013-10-14 19:13:14 +00:00
|
|
|
p = OGRGeometry.from_bbox(bbox)
|
2009-03-07 23:02:48 +00:00
|
|
|
self.assertEqual(bbox, p.extent)
|
|
|
|
|
2008-08-05 18:13:06 +00:00
|
|
|
prev = OGRGeometry('POINT(0 0)')
|
2010-11-01 21:56:48 +00:00
|
|
|
for p in self.geometries.polygons:
|
2008-08-05 18:13:06 +00: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))
|
|
|
|
|
|
|
|
# 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-10 21:01:07 +00:00
|
|
|
self.assertEqual(poly, OGRGeometry(p.wkt))
|
|
|
|
self.assertNotEqual(poly, prev)
|
2010-03-29 17:33:59 +00:00
|
|
|
|
2008-08-05 18:13:06 +00: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-29 17:33:59 +00:00
|
|
|
|
2008-08-05 18:13:06 +00:00
|
|
|
for r in poly:
|
|
|
|
self.assertEqual('LINEARRING', r.geom_name)
|
|
|
|
|
2015-05-13 12:12:53 +00:00
|
|
|
def test_closepolygons(self):
|
2008-08-05 18:13:06 +00: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-17 21:00:05 +00:00
|
|
|
with self.assertRaises(GDALException):
|
2013-08-04 16:17:10 +00:00
|
|
|
poly.centroid
|
2008-08-05 18:13:06 +00:00
|
|
|
|
|
|
|
poly.close_rings()
|
2013-11-02 21:02:56 +00:00
|
|
|
self.assertEqual(10, poly.point_count) # Two closing points should've been added
|
2008-08-05 18:13:06 +00:00
|
|
|
self.assertEqual(OGRGeometry('POINT(2.5 2.5)'), poly.centroid)
|
|
|
|
|
2015-05-13 12:12:53 +00:00
|
|
|
def test_multipolygons(self):
|
2008-08-05 18:13:06 +00:00
|
|
|
"Testing MultiPolygon objects."
|
2013-08-04 16:17:10 +00:00
|
|
|
OGRGeometry('POINT(0 0)')
|
2010-11-01 21:56:48 +00:00
|
|
|
for mp in self.geometries.multipolygons:
|
2008-08-05 18:13:06 +00: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))
|
2016-01-17 11:26:39 +00:00
|
|
|
with self.assertRaises(OGRIndexError):
|
|
|
|
mpoly.__getitem__(len(mpoly))
|
2008-08-05 18:13:06 +00: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 12:12:53 +00:00
|
|
|
def test_srs(self):
|
2008-08-05 18:13:06 +00:00
|
|
|
"Testing OGR Geometries with Spatial Reference objects."
|
2010-11-01 21:56:48 +00:00
|
|
|
for mp in self.geometries.multipolygons:
|
2008-08-05 18:13:06 +00:00
|
|
|
# Creating a geometry w/spatial reference
|
|
|
|
sr = SpatialReference('WGS84')
|
|
|
|
mpoly = OGRGeometry(mp.wkt, sr)
|
|
|
|
self.assertEqual(sr.wkt, mpoly.srs.wkt)
|
2010-03-29 17:33:59 +00:00
|
|
|
|
2008-08-05 18:13:06 +00:00
|
|
|
# Ensuring that SRS is propagated to clones.
|
|
|
|
klone = mpoly.clone()
|
|
|
|
self.assertEqual(sr.wkt, klone.srs.wkt)
|
2010-03-29 17:33:59 +00:00
|
|
|
|
2008-08-05 18:13:06 +00: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-01 21:56:48 +00:00
|
|
|
a = OGRGeometry(self.geometries.topology_geoms[0].wkt_a, sr)
|
|
|
|
b = OGRGeometry(self.geometries.topology_geoms[0].wkt_b, sr)
|
2008-08-05 18:13:06 +00: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-29 17:33:59 +00:00
|
|
|
|
2014-03-02 14:25:53 +00:00
|
|
|
# Incrementing through the multipolygon after the spatial reference
|
2008-08-05 18:13:06 +00: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-02 21:02:56 +00:00
|
|
|
ring.srs = str(SpatialReference(4326)) # back to WGS84
|
2008-08-05 18:13:06 +00: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 13:14:12 +00: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 12:12:53 +00:00
|
|
|
def test_srs_transform(self):
|
2008-08-05 18:13:06 +00: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)
|
|
|
|
|
|
|
|
prec = 3
|
|
|
|
for p in (t1, t2, t3, k2):
|
|
|
|
self.assertAlmostEqual(trans.x, p.x, prec)
|
|
|
|
self.assertAlmostEqual(trans.y, p.y, prec)
|
|
|
|
|
2015-05-13 12:12:53 +00:00
|
|
|
def test_transform_dim(self):
|
2009-10-17 17:32:25 +00: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-29 17:33:59 +00:00
|
|
|
|
2009-10-17 17:32:25 +00:00
|
|
|
prec = 3
|
|
|
|
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 12:12:53 +00:00
|
|
|
def test_difference(self):
|
2008-08-05 18:13:06 +00:00
|
|
|
"Testing difference()."
|
2014-12-13 13:04:36 +00:00
|
|
|
for i in range(len(self.geometries.topology_geoms)):
|
2010-11-01 21:56:48 +00: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-05 18:13:06 +00:00
|
|
|
d2 = a.difference(b)
|
|
|
|
self.assertEqual(d1, d2)
|
2013-11-02 21:02:56 +00:00
|
|
|
self.assertEqual(d1, a - b) # __sub__ is difference operator
|
|
|
|
a -= b # testing __isub__
|
2008-08-05 18:13:06 +00:00
|
|
|
self.assertEqual(d1, a)
|
|
|
|
|
2015-05-13 12:12:53 +00:00
|
|
|
def test_intersection(self):
|
2008-08-05 18:13:06 +00:00
|
|
|
"Testing intersects() and intersection()."
|
2014-12-13 13:04:36 +00:00
|
|
|
for i in range(len(self.geometries.topology_geoms)):
|
2010-11-01 21:56:48 +00: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-10 21:01:07 +00:00
|
|
|
self.assertTrue(a.intersects(b))
|
2008-08-05 18:13:06 +00:00
|
|
|
i2 = a.intersection(b)
|
|
|
|
self.assertEqual(i1, i2)
|
2013-11-02 21:02:56 +00:00
|
|
|
self.assertEqual(i1, a & b) # __and__ is intersection operator
|
|
|
|
a &= b # testing __iand__
|
2008-08-05 18:13:06 +00:00
|
|
|
self.assertEqual(i1, a)
|
|
|
|
|
2015-05-13 12:12:53 +00:00
|
|
|
def test_symdifference(self):
|
2008-08-05 18:13:06 +00:00
|
|
|
"Testing sym_difference()."
|
2014-12-13 13:04:36 +00:00
|
|
|
for i in range(len(self.geometries.topology_geoms)):
|
2010-11-01 21:56:48 +00: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-05 18:13:06 +00:00
|
|
|
d2 = a.sym_difference(b)
|
|
|
|
self.assertEqual(d1, d2)
|
2013-11-02 21:02:56 +00:00
|
|
|
self.assertEqual(d1, a ^ b) # __xor__ is symmetric difference operator
|
|
|
|
a ^= b # testing __ixor__
|
2008-08-05 18:13:06 +00:00
|
|
|
self.assertEqual(d1, a)
|
2010-03-29 17:33:59 +00:00
|
|
|
|
2015-05-13 12:12:53 +00:00
|
|
|
def test_union(self):
|
2008-08-05 18:13:06 +00:00
|
|
|
"Testing union()."
|
2014-12-13 13:04:36 +00:00
|
|
|
for i in range(len(self.geometries.topology_geoms)):
|
2010-11-01 21:56:48 +00: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-05 18:13:06 +00:00
|
|
|
u2 = a.union(b)
|
|
|
|
self.assertEqual(u1, u2)
|
2013-11-02 21:02:56 +00:00
|
|
|
self.assertEqual(u1, a | b) # __or__ is union operator
|
|
|
|
a |= b # testing __ior__
|
2008-08-05 18:13:06 +00:00
|
|
|
self.assertEqual(u1, a)
|
|
|
|
|
2015-05-13 12:12:53 +00:00
|
|
|
def test_add(self):
|
2008-08-05 18:13:06 +00:00
|
|
|
"Testing GeometryCollection.add()."
|
|
|
|
# Can't insert a Point into a MultiPolygon.
|
|
|
|
mp = OGRGeometry('MultiPolygon')
|
|
|
|
pnt = OGRGeometry('POINT(5 23)')
|
2016-01-17 11:26:39 +00:00
|
|
|
with self.assertRaises(GDALException):
|
|
|
|
mp.add(pnt)
|
2008-08-05 18:13:06 +00: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-01 21:56:48 +00:00
|
|
|
for mp in self.geometries.multipolygons:
|
2008-08-05 18:13:06 +00:00
|
|
|
mpoly = OGRGeometry(mp.wkt)
|
|
|
|
mp1 = OGRGeometry('MultiPolygon')
|
|
|
|
mp2 = OGRGeometry('MultiPolygon')
|
|
|
|
mp3 = OGRGeometry('MultiPolygon')
|
|
|
|
|
|
|
|
for poly in mpoly:
|
2013-11-02 21:02:56 +00: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 08:17:41 +00:00
|
|
|
for tmp in (mp1, mp2, mp3):
|
|
|
|
self.assertEqual(mpoly, tmp)
|
2008-08-05 18:13:06 +00:00
|
|
|
|
2015-05-13 12:12:53 +00:00
|
|
|
def test_extent(self):
|
2008-08-05 18:13:06 +00: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-01 21:56:48 +00:00
|
|
|
poly = OGRGeometry(self.geometries.polygons[3].wkt)
|
2008-08-05 18:13:06 +00: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 12:12:53 +00:00
|
|
|
def test_25D(self):
|
2009-11-14 23:25:44 +00: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 12:12:53 +00:00
|
|
|
def test_pickle(self):
|
2010-01-27 03:47:37 +00:00
|
|
|
"Testing pickle support."
|
|
|
|
g1 = OGRGeometry('LINESTRING(1 1 1,2 2 2,3 3 3)', 'WGS84')
|
2011-07-13 09:35:51 +00:00
|
|
|
g2 = pickle.loads(pickle.dumps(g1))
|
2010-01-27 03:47:37 +00:00
|
|
|
self.assertEqual(g1, g2)
|
|
|
|
self.assertEqual(4326, g2.srs.srid)
|
|
|
|
self.assertEqual(g1.srs.wkt, g2.srs.wkt)
|
2010-03-29 17:33:59 +00:00
|
|
|
|
2015-05-13 12:12:53 +00:00
|
|
|
def test_ogrgeometry_transform_workaround(self):
|
2010-03-29 17:33:59 +00: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 03:47:37 +00:00
|
|
|
|
2015-12-14 04:33:15 +00:00
|
|
|
# Testing binary predicates, `assertIs` is used to check that bool is returned.
|
|
|
|
|
2015-05-13 12:12:53 +00:00
|
|
|
def test_equivalence_regression(self):
|
2010-04-01 16:56:55 +00:00
|
|
|
"Testing equivalence methods with non-OGRGeometry instances."
|
2013-10-10 21:01:07 +00:00
|
|
|
self.assertIsNotNone(OGRGeometry('POINT(0 0)'))
|
|
|
|
self.assertNotEqual(OGRGeometry('LINESTRING(0 0, 1 1)'), 3)
|
2015-12-14 04:33:15 +00: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)
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
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
|
|
|
|
)
|
|
|
|
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)')), True
|
|
|
|
)
|
|
|
|
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))')), True
|
|
|
|
)
|
|
|
|
self.assertIs(OGRGeometry('POINT(0 0)').within(OGRGeometry('POINT(0 1)')), False)
|
2016-06-10 06:50:07 +00: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>'
|
|
|
|
),
|
|
|
|
)
|