| 1 |
import ctypes |
| 2 |
import itertools |
| 3 |
import json |
| 4 |
import math |
| 5 |
import pickle |
| 6 |
import random |
| 7 |
from binascii import a2b_hex |
| 8 |
from io import BytesIO |
| 9 |
from unittest import mock, skipIf |
| 10 |
|
| 11 |
from django.contrib.gis import gdal |
| 12 |
from django.contrib.gis.geos import ( |
| 13 |
GeometryCollection, |
| 14 |
GEOSException, |
| 15 |
GEOSGeometry, |
| 16 |
LinearRing, |
| 17 |
LineString, |
| 18 |
MultiLineString, |
| 19 |
MultiPoint, |
| 20 |
MultiPolygon, |
| 21 |
Point, |
| 22 |
Polygon, |
| 23 |
fromfile, |
| 24 |
fromstr, |
| 25 |
) |
| 26 |
from django.contrib.gis.geos.libgeos import geos_version_tuple |
| 27 |
from django.contrib.gis.shortcuts import numpy |
| 28 |
from django.template import Context |
| 29 |
from django.template.engine import Engine |
| 30 |
from django.test import SimpleTestCase |
| 31 |
|
| 32 |
from ..test_data import TestDataMixin |
| 33 |
|
| 34 |
|
| 35 |
class GEOSTest(SimpleTestCase, TestDataMixin): |
| 36 |
def test_wkt(self): |
| 37 |
"Testing WKT output." |
| 38 |
for g in self.geometries.wkt_out: |
| 39 |
geom = fromstr(g.wkt) |
| 40 |
if geom.hasz: |
| 41 |
self.assertEqual(g.ewkt, geom.wkt) |
| 42 |
|
| 43 |
def test_wkt_invalid(self): |
| 44 |
msg = "String input unrecognized as WKT EWKT, and HEXEWKB." |
| 45 |
with self.assertRaisesMessage(ValueError, msg): |
| 46 |
fromstr("POINT(٠٠١ ٠)") |
| 47 |
with self.assertRaisesMessage(ValueError, msg): |
| 48 |
fromstr("SRID=٧٥٨٣;POINT(100 0)") |
| 49 |
|
| 50 |
def test_hex(self): |
| 51 |
"Testing HEX output." |
| 52 |
for g in self.geometries.hex_wkt: |
| 53 |
geom = fromstr(g.wkt) |
| 54 |
self.assertEqual(g.hex, geom.hex.decode()) |
| 55 |
|
| 56 |
def test_hexewkb(self): |
| 57 |
"Testing (HEX)EWKB output." |
| 58 |
# For testing HEX(EWKB). |
| 59 |
ogc_hex = b"01010000000000000000000000000000000000F03F" |
| 60 |
ogc_hex_3d = b"01010000800000000000000000000000000000F03F0000000000000040" |
| 61 |
# `SELECT ST_AsHEXEWKB(ST_GeomFromText('POINT(0 1)', 4326));` |
| 62 |
hexewkb_2d = b"0101000020E61000000000000000000000000000000000F03F" |
| 63 |
# `SELECT ST_AsHEXEWKB(ST_GeomFromEWKT('SRID=4326;POINT(0 1 2)'));` |
| 64 |
hexewkb_3d = ( |
| 65 |
b"01010000A0E61000000000000000000000000000000000F03F0000000000000040" |
| 66 |
) |
| 67 |
|
| 68 |
pnt_2d = Point(0, 1, srid=4326) |
| 69 |
pnt_3d = Point(0, 1, 2, srid=4326) |
| 70 |
|
| 71 |
# OGC-compliant HEX will not have SRID value. |
| 72 |
self.assertEqual(ogc_hex, pnt_2d.hex) |
| 73 |
self.assertEqual(ogc_hex_3d, pnt_3d.hex) |
| 74 |
|
| 75 |
# HEXEWKB should be appropriate for its dimension -- have to use an |
| 76 |
# a WKBWriter w/dimension set accordingly, else GEOS will insert |
| 77 |
# garbage into 3D coordinate if there is none. |
| 78 |
self.assertEqual(hexewkb_2d, pnt_2d.hexewkb) |
| 79 |
self.assertEqual(hexewkb_3d, pnt_3d.hexewkb) |
| 80 |
self.assertIs(GEOSGeometry(hexewkb_3d).hasz, True) |
| 81 |
|
| 82 |
# Same for EWKB. |
| 83 |
self.assertEqual(memoryview(a2b_hex(hexewkb_2d)), pnt_2d.ewkb) |
| 84 |
self.assertEqual(memoryview(a2b_hex(hexewkb_3d)), pnt_3d.ewkb) |
| 85 |
|
| 86 |
# Redundant sanity check. |
| 87 |
self.assertEqual(4326, GEOSGeometry(hexewkb_2d).srid) |
| 88 |
|
| 89 |
def test_kml(self): |
| 90 |
"Testing KML output." |
| 91 |
for tg in self.geometries.wkt_out: |
| 92 |
geom = fromstr(tg.wkt) |
| 93 |
kml = getattr(tg, "kml", False) |
| 94 |
if kml: |
| 95 |
self.assertEqual(kml, geom.kml) |
| 96 |
|
| 97 |
def test_errors(self): |
| 98 |
"Testing the Error handlers." |
| 99 |
# string-based |
| 100 |
for err in self.geometries.errors: |
| 101 |
with self.assertRaises((GEOSException, ValueError)): |
| 102 |
fromstr(err.wkt) |
| 103 |
|
| 104 |
# Bad WKB |
| 105 |
with self.assertRaises(GEOSException): |
| 106 |
GEOSGeometry(memoryview(b"0")) |
| 107 |
|
| 108 |
class NotAGeometry: |
| 109 |
pass |
| 110 |
|
| 111 |
# Some other object |
| 112 |
with self.assertRaises(TypeError): |
| 113 |
GEOSGeometry(NotAGeometry()) |
| 114 |
# None |
| 115 |
with self.assertRaises(TypeError): |
| 116 |
GEOSGeometry(None) |
| 117 |
|
| 118 |
def test_wkb(self): |
| 119 |
"Testing WKB output." |
| 120 |
for g in self.geometries.hex_wkt: |
| 121 |
geom = fromstr(g.wkt) |
| 122 |
wkb = geom.wkb |
| 123 |
self.assertEqual(wkb.hex().upper(), g.hex) |
| 124 |
|
| 125 |
def test_create_hex(self): |
| 126 |
"Testing creation from HEX." |
| 127 |
for g in self.geometries.hex_wkt: |
| 128 |
geom_h = GEOSGeometry(g.hex) |
| 129 |
# we need to do this so decimal places get normalized |
| 130 |
geom_t = fromstr(g.wkt) |
| 131 |
self.assertEqual(geom_t.wkt, geom_h.wkt) |
| 132 |
|
| 133 |
def test_create_wkb(self): |
| 134 |
"Testing creation from WKB." |
| 135 |
for g in self.geometries.hex_wkt: |
| 136 |
wkb = memoryview(bytes.fromhex(g.hex)) |
| 137 |
geom_h = GEOSGeometry(wkb) |
| 138 |
# we need to do this so decimal places get normalized |
| 139 |
geom_t = fromstr(g.wkt) |
| 140 |
self.assertEqual(geom_t.wkt, geom_h.wkt) |
| 141 |
|
| 142 |
def test_ewkt(self): |
| 143 |
"Testing EWKT." |
| 144 |
srids = (-1, 32140) |
| 145 |
for srid in srids: |
| 146 |
for p in self.geometries.polygons: |
| 147 |
ewkt = "SRID=%d;%s" % (srid, p.wkt) |
| 148 |
poly = fromstr(ewkt) |
| 149 |
self.assertEqual(srid, poly.srid) |
| 150 |
self.assertEqual(srid, poly.shell.srid) |
| 151 |
self.assertEqual(srid, fromstr(poly.ewkt).srid) # Checking export |
| 152 |
|
| 153 |
def test_json(self): |
| 154 |
"Testing GeoJSON input/output (via GDAL)." |
| 155 |
for g in self.geometries.json_geoms: |
| 156 |
geom = GEOSGeometry(g.wkt) |
| 157 |
if not hasattr(g, "not_equal"): |
| 158 |
# Loading jsons to prevent decimal differences |
| 159 |
self.assertEqual(json.loads(g.json), json.loads(geom.json)) |
| 160 |
self.assertEqual(json.loads(g.json), json.loads(geom.geojson)) |
| 161 |
self.assertEqual(GEOSGeometry(g.wkt, 4326), GEOSGeometry(geom.json)) |
| 162 |
|
| 163 |
def test_json_srid(self): |
| 164 |
geojson_data = { |
| 165 |
"type": "Point", |
| 166 |
"coordinates": [2, 49], |
| 167 |
"crs": { |
| 168 |
"type": "name", |
| 169 |
"properties": {"name": "urn:ogc:def:crs:EPSG::4322"}, |
| 170 |
}, |
| 171 |
} |
| 172 |
self.assertEqual( |
| 173 |
GEOSGeometry(json.dumps(geojson_data)), Point(2, 49, srid=4322) |
| 174 |
) |
| 175 |
|
| 176 |
def test_fromfile(self): |
| 177 |
"Testing the fromfile() factory." |
| 178 |
ref_pnt = GEOSGeometry("POINT(5 23)") |
| 179 |
|
| 180 |
wkt_f = BytesIO() |
| 181 |
wkt_f.write(ref_pnt.wkt.encode()) |
| 182 |
wkb_f = BytesIO() |
| 183 |
wkb_f.write(bytes(ref_pnt.wkb)) |
| 184 |
|
| 185 |
# Other tests use `fromfile()` on string filenames so those |
| 186 |
# aren't tested here. |
| 187 |
for fh in (wkt_f, wkb_f): |
| 188 |
fh.seek(0) |
| 189 |
pnt = fromfile(fh) |
| 190 |
self.assertEqual(ref_pnt, pnt) |
| 191 |
|
| 192 |
def test_eq(self): |
| 193 |
"Testing equivalence." |
| 194 |
p = fromstr("POINT(5 23)") |
| 195 |
self.assertEqual(p, p.wkt) |
| 196 |
self.assertNotEqual(p, "foo") |
| 197 |
ls = fromstr("LINESTRING(0 0, 1 1, 5 5)") |
| 198 |
self.assertEqual(ls, ls.wkt) |
| 199 |
self.assertNotEqual(p, "bar") |
| 200 |
self.assertEqual(p, "POINT(5.0 23.0)") |
| 201 |
# Error shouldn't be raise on equivalence testing with |
| 202 |
# an invalid type. |
| 203 |
for g in (p, ls): |
| 204 |
self.assertIsNotNone(g) |
| 205 |
self.assertNotEqual(g, {"foo": "bar"}) |
| 206 |
self.assertIsNot(g, False) |
| 207 |
|
| 208 |
def test_hash(self): |
| 209 |
point_1 = Point(5, 23) |
| 210 |
point_2 = Point(5, 23, srid=4326) |
| 211 |
point_3 = Point(5, 23, srid=32632) |
| 212 |
multipoint_1 = MultiPoint(point_1, srid=4326) |
| 213 |
multipoint_2 = MultiPoint(point_2) |
| 214 |
multipoint_3 = MultiPoint(point_3) |
| 215 |
self.assertNotEqual(hash(point_1), hash(point_2)) |
| 216 |
self.assertNotEqual(hash(point_1), hash(point_3)) |
| 217 |
self.assertNotEqual(hash(point_2), hash(point_3)) |
| 218 |
self.assertNotEqual(hash(multipoint_1), hash(multipoint_2)) |
| 219 |
self.assertEqual(hash(multipoint_2), hash(multipoint_3)) |
| 220 |
self.assertNotEqual(hash(multipoint_1), hash(point_1)) |
| 221 |
self.assertNotEqual(hash(multipoint_2), hash(point_2)) |
| 222 |
self.assertNotEqual(hash(multipoint_3), hash(point_3)) |
| 223 |
|
| 224 |
def test_eq_with_srid(self): |
| 225 |
"Testing non-equivalence with different srids." |
| 226 |
p0 = Point(5, 23) |
| 227 |
p1 = Point(5, 23, srid=4326) |
| 228 |
p2 = Point(5, 23, srid=32632) |
| 229 |
# GEOS |
| 230 |
self.assertNotEqual(p0, p1) |
| 231 |
self.assertNotEqual(p1, p2) |
| 232 |
# EWKT |
| 233 |
self.assertNotEqual(p0, p1.ewkt) |
| 234 |
self.assertNotEqual(p1, p0.ewkt) |
| 235 |
self.assertNotEqual(p1, p2.ewkt) |
| 236 |
# Equivalence with matching SRIDs |
| 237 |
self.assertEqual(p2, p2) |
| 238 |
self.assertEqual(p2, p2.ewkt) |
| 239 |
# WKT contains no SRID so will not equal |
| 240 |
self.assertNotEqual(p2, p2.wkt) |
| 241 |
# SRID of 0 |
| 242 |
self.assertEqual(p0, "SRID=0;POINT (5 23)") |
| 243 |
self.assertNotEqual(p1, "SRID=0;POINT (5 23)") |
| 244 |
|
| 245 |
@skipIf(geos_version_tuple() < (3, 12), "GEOS >= 3.12.0 is required") |
| 246 |
def test_equals_identical(self): |
| 247 |
tests = [ |
| 248 |
# Empty inputs of different types are not equals_identical. |
| 249 |
("POINT EMPTY", "LINESTRING EMPTY", False), |
| 250 |
# Empty inputs of different dimensions are not equals_identical. |
| 251 |
("POINT EMPTY", "POINT Z EMPTY", False), |
| 252 |
# Non-empty inputs of different dimensions are not equals_identical. |
| 253 |
("POINT Z (1 2 3)", "POINT M (1 2 3)", False), |
| 254 |
("POINT ZM (1 2 3 4)", "POINT Z (1 2 3)", False), |
| 255 |
# Inputs with different structure are not equals_identical. |
| 256 |
("LINESTRING (1 1, 2 2)", "MULTILINESTRING ((1 1, 2 2))", False), |
| 257 |
# Inputs with different types are not equals_identical. |
| 258 |
( |
| 259 |
"GEOMETRYCOLLECTION (LINESTRING (1 1, 2 2))", |
| 260 |
"MULTILINESTRING ((1 1, 2 2))", |
| 261 |
False, |
| 262 |
), |
| 263 |
# Same lines are equals_identical. |
| 264 |
("LINESTRING M (1 1 0, 2 2 1)", "LINESTRING M (1 1 0, 2 2 1)", True), |
| 265 |
# Different lines are not equals_identical. |
| 266 |
("LINESTRING M (1 1 0, 2 2 1)", "LINESTRING M (1 1 1, 2 2 1)", False), |
| 267 |
# Same polygons are equals_identical. |
| 268 |
("POLYGON ((0 0, 1 0, 1 1, 0 0))", "POLYGON ((0 0, 1 0, 1 1, 0 0))", True), |
| 269 |
# Different polygons are not equals_identical. |
| 270 |
("POLYGON ((0 0, 1 0, 1 1, 0 0))", "POLYGON ((1 0, 1 1, 0 0, 1 0))", False), |
| 271 |
# Different polygons (number of holes) are not equals_identical. |
| 272 |
( |
| 273 |
"POLYGON ((0 0, 10 0, 10 10, 0 10, 0 0), (1 1, 2 1, 2 2, 1 1))", |
| 274 |
( |
| 275 |
"POLYGON ((0 0, 10 0, 10 10, 0 10, 0 0), (1 1, 2 1, 2 2, 1 1), " |
| 276 |
"(3 3, 4 3, 4 4, 3 3))" |
| 277 |
), |
| 278 |
False, |
| 279 |
), |
| 280 |
# Same collections are equals_identical. |
| 281 |
( |
| 282 |
"MULTILINESTRING ((1 1, 2 2), (2 2, 3 3))", |
| 283 |
"MULTILINESTRING ((1 1, 2 2), (2 2, 3 3))", |
| 284 |
True, |
| 285 |
), |
| 286 |
# Different collections (structure) are not equals_identical. |
| 287 |
( |
| 288 |
"MULTILINESTRING ((1 1, 2 2), (2 2, 3 3))", |
| 289 |
"MULTILINESTRING ((2 2, 3 3), (1 1, 2 2))", |
| 290 |
False, |
| 291 |
), |
| 292 |
] |
| 293 |
for g1, g2, is_equal_identical in tests: |
| 294 |
with self.subTest(g1=g1, g2=g2): |
| 295 |
self.assertIs( |
| 296 |
fromstr(g1).equals_identical(fromstr(g2)), is_equal_identical |
| 297 |
) |
| 298 |
|
| 299 |
@skipIf(geos_version_tuple() < (3, 12), "GEOS >= 3.12.0 is required") |
| 300 |
def test_infinite_values_equals_identical(self): |
| 301 |
# Input with identical infinite values are equals_identical. |
| 302 |
g1 = Point(x=float("nan"), y=math.inf) |
| 303 |
g2 = Point(x=float("nan"), y=math.inf) |
| 304 |
self.assertIs(g1.equals_identical(g2), True) |
| 305 |
|
| 306 |
@mock.patch("django.contrib.gis.geos.libgeos.geos_version", lambda: b"3.11.0") |
| 307 |
def test_equals_identical_geos_version(self): |
| 308 |
g1 = fromstr("POINT (1 2 3)") |
| 309 |
g2 = fromstr("POINT (1 2 3)") |
| 310 |
msg = "GEOSGeometry.equals_identical() requires GEOS >= 3.12.0" |
| 311 |
with self.assertRaisesMessage(GEOSException, msg): |
| 312 |
g1.equals_identical(g2) |
| 313 |
|
| 314 |
def test_points(self): |
| 315 |
"Testing Point objects." |
| 316 |
prev = fromstr("POINT(0 0)") |
| 317 |
for p in self.geometries.points: |
| 318 |
# Creating the point from the WKT |
| 319 |
pnt = fromstr(p.wkt) |
| 320 |
self.assertEqual(pnt.geom_type, "Point") |
| 321 |
self.assertEqual(pnt.geom_typeid, 0) |
| 322 |
self.assertEqual(pnt.dims, 0) |
| 323 |
self.assertEqual(p.x, pnt.x) |
| 324 |
self.assertEqual(p.y, pnt.y) |
| 325 |
self.assertEqual(pnt, fromstr(p.wkt)) |
| 326 |
self.assertIs(pnt == prev, False) # Use assertIs() to test __eq__. |
| 327 |
|
| 328 |
# Making sure that the point's X, Y components are what we expect |
| 329 |
self.assertAlmostEqual(p.x, pnt.tuple[0], 9) |
| 330 |
self.assertAlmostEqual(p.y, pnt.tuple[1], 9) |
| 331 |
|
| 332 |
# Testing the third dimension, and getting the tuple arguments |
| 333 |
if hasattr(p, "z"): |
| 334 |
self.assertIs(pnt.hasz, True) |
| 335 |
self.assertEqual(p.z, pnt.z) |
| 336 |
self.assertEqual(p.z, pnt.tuple[2], 9) |
| 337 |
tup_args = (p.x, p.y, p.z) |
| 338 |
set_tup1 = (2.71, 3.14, 5.23) |
| 339 |
set_tup2 = (5.23, 2.71, 3.14) |
| 340 |
else: |
| 341 |
self.assertIs(pnt.hasz, False) |
| 342 |
self.assertIsNone(pnt.z) |
| 343 |
tup_args = (p.x, p.y) |
| 344 |
set_tup1 = (2.71, 3.14) |
| 345 |
set_tup2 = (3.14, 2.71) |
| 346 |
|
| 347 |
# Centroid operation on point should be point itself |
| 348 |
self.assertEqual(p.centroid, pnt.centroid.tuple) |
| 349 |
|
| 350 |
# Now testing the different constructors |
| 351 |
pnt2 = Point(tup_args) # e.g., Point((1, 2)) |
| 352 |
pnt3 = Point(*tup_args) # e.g., Point(1, 2) |
| 353 |
self.assertEqual(pnt, pnt2) |
| 354 |
self.assertEqual(pnt, pnt3) |
| 355 |
|
| 356 |
# Now testing setting the x and y |
| 357 |
pnt.y = 3.14 |
| 358 |
pnt.x = 2.71 |
| 359 |
self.assertEqual(3.14, pnt.y) |
| 360 |
self.assertEqual(2.71, pnt.x) |
| 361 |
|
| 362 |
# Setting via the tuple/coords property |
| 363 |
pnt.tuple = set_tup1 |
| 364 |
self.assertEqual(set_tup1, pnt.tuple) |
| 365 |
pnt.coords = set_tup2 |
| 366 |
self.assertEqual(set_tup2, pnt.coords) |
| 367 |
|
| 368 |
prev = pnt # setting the previous geometry |
| 369 |
|
| 370 |
def test_point_reverse(self): |
| 371 |
point = GEOSGeometry("POINT(144.963 -37.8143)", 4326) |
| 372 |
self.assertEqual(point.srid, 4326) |
| 373 |
point.reverse() |
| 374 |
self.assertEqual(point.ewkt, "SRID=4326;POINT (-37.8143 144.963)") |
| 375 |
|
| 376 |
def test_multipoints(self): |
| 377 |
"Testing MultiPoint objects." |
| 378 |
for mp in self.geometries.multipoints: |
| 379 |
mpnt = fromstr(mp.wkt) |
| 380 |
self.assertEqual(mpnt.geom_type, "MultiPoint") |
| 381 |
self.assertEqual(mpnt.geom_typeid, 4) |
| 382 |
self.assertEqual(mpnt.dims, 0) |
| 383 |
|
| 384 |
self.assertAlmostEqual(mp.centroid[0], mpnt.centroid.tuple[0], 9) |
| 385 |
self.assertAlmostEqual(mp.centroid[1], mpnt.centroid.tuple[1], 9) |
| 386 |
|
| 387 |
with self.assertRaises(IndexError): |
| 388 |
mpnt.__getitem__(len(mpnt)) |
| 389 |
self.assertEqual(mp.centroid, mpnt.centroid.tuple) |
| 390 |
self.assertEqual(mp.coords, tuple(m.tuple for m in mpnt)) |
| 391 |
for p in mpnt: |
| 392 |
self.assertEqual(p.geom_type, "Point") |
| 393 |
self.assertEqual(p.geom_typeid, 0) |
| 394 |
self.assertIs(p.empty, False) |
| 395 |
self.assertIs(p.valid, True) |
| 396 |
|
| 397 |
def test_linestring(self): |
| 398 |
"Testing LineString objects." |
| 399 |
prev = fromstr("POINT(0 0)") |
| 400 |
for line in self.geometries.linestrings: |
| 401 |
ls = fromstr(line.wkt) |
| 402 |
self.assertEqual(ls.geom_type, "LineString") |
| 403 |
self.assertEqual(ls.geom_typeid, 1) |
| 404 |
self.assertEqual(ls.dims, 1) |
| 405 |
self.assertIs(ls.empty, False) |
| 406 |
self.assertIs(ls.ring, False) |
| 407 |
if hasattr(line, "centroid"): |
| 408 |
self.assertEqual(line.centroid, ls.centroid.tuple) |
| 409 |
if hasattr(line, "tup"): |
| 410 |
self.assertEqual(line.tup, ls.tuple) |
| 411 |
|
| 412 |
self.assertEqual(ls, fromstr(line.wkt)) |
| 413 |
self.assertIs(ls == prev, False) # Use assertIs() to test __eq__. |
| 414 |
with self.assertRaises(IndexError): |
| 415 |
ls.__getitem__(len(ls)) |
| 416 |
prev = ls |
| 417 |
|
| 418 |
# Creating a LineString from a tuple, list, and numpy array |
| 419 |
self.assertEqual(ls, LineString(ls.tuple)) # tuple |
| 420 |
self.assertEqual(ls, LineString(*ls.tuple)) # as individual arguments |
| 421 |
self.assertEqual(ls, LineString([list(tup) for tup in ls.tuple])) # as list |
| 422 |
# Point individual arguments |
| 423 |
self.assertEqual( |
| 424 |
ls.wkt, LineString(*tuple(Point(tup) for tup in ls.tuple)).wkt |
| 425 |
) |
| 426 |
if numpy: |
| 427 |
self.assertEqual( |
| 428 |
ls, LineString(numpy.array(ls.tuple)) |
| 429 |
) # as numpy array |
| 430 |
|
| 431 |
with self.assertRaisesMessage( |
| 432 |
TypeError, "Each coordinate should be a sequence (list or tuple)" |
| 433 |
): |
| 434 |
LineString((0, 0)) |
| 435 |
|
| 436 |
with self.assertRaisesMessage( |
| 437 |
ValueError, "LineString requires at least 2 points, got 1." |
| 438 |
): |
| 439 |
LineString([(0, 0)]) |
| 440 |
|
| 441 |
if numpy: |
| 442 |
with self.assertRaisesMessage( |
| 443 |
ValueError, "LineString requires at least 2 points, got 1." |
| 444 |
): |
| 445 |
LineString(numpy.array([(0, 0)])) |
| 446 |
|
| 447 |
with mock.patch("django.contrib.gis.geos.linestring.numpy", False): |
| 448 |
with self.assertRaisesMessage( |
| 449 |
TypeError, "Invalid initialization input for LineStrings." |
| 450 |
): |
| 451 |
LineString("wrong input") |
| 452 |
|
| 453 |
# Test __iter__(). |
| 454 |
self.assertEqual( |
| 455 |
list(LineString((0, 0), (1, 1), (2, 2))), [(0, 0), (1, 1), (2, 2)] |
| 456 |
) |
| 457 |
|
| 458 |
def test_linestring_reverse(self): |
| 459 |
line = GEOSGeometry("LINESTRING(144.963 -37.8143,151.2607 -33.887)", 4326) |
| 460 |
self.assertEqual(line.srid, 4326) |
| 461 |
line.reverse() |
| 462 |
self.assertEqual( |
| 463 |
line.ewkt, "SRID=4326;LINESTRING (151.2607 -33.887, 144.963 -37.8143)" |
| 464 |
) |
| 465 |
|
| 466 |
def test_is_counterclockwise(self): |
| 467 |
lr = LinearRing((0, 0), (1, 0), (0, 1), (0, 0)) |
| 468 |
self.assertIs(lr.is_counterclockwise, True) |
| 469 |
lr.reverse() |
| 470 |
self.assertIs(lr.is_counterclockwise, False) |
| 471 |
msg = "Orientation of an empty LinearRing cannot be determined." |
| 472 |
with self.assertRaisesMessage(ValueError, msg): |
| 473 |
LinearRing().is_counterclockwise |
| 474 |
|
| 475 |
def test_is_counterclockwise_geos_error(self): |
| 476 |
with mock.patch("django.contrib.gis.geos.prototypes.cs_is_ccw") as mocked: |
| 477 |
mocked.return_value = 0 |
| 478 |
mocked.func_name = "GEOSCoordSeq_isCCW" |
| 479 |
msg = 'Error encountered in GEOS C function "GEOSCoordSeq_isCCW".' |
| 480 |
with self.assertRaisesMessage(GEOSException, msg): |
| 481 |
LinearRing((0, 0), (1, 0), (0, 1), (0, 0)).is_counterclockwise |
| 482 |
|
| 483 |
def test_multilinestring(self): |
| 484 |
"Testing MultiLineString objects." |
| 485 |
prev = fromstr("POINT(0 0)") |
| 486 |
for line in self.geometries.multilinestrings: |
| 487 |
ml = fromstr(line.wkt) |
| 488 |
self.assertEqual(ml.geom_type, "MultiLineString") |
| 489 |
self.assertEqual(ml.geom_typeid, 5) |
| 490 |
self.assertEqual(ml.dims, 1) |
| 491 |
|
| 492 |
self.assertAlmostEqual(line.centroid[0], ml.centroid.x, 9) |
| 493 |
self.assertAlmostEqual(line.centroid[1], ml.centroid.y, 9) |
| 494 |
|
| 495 |
self.assertEqual(ml, fromstr(line.wkt)) |
| 496 |
self.assertIs(ml == prev, False) # Use assertIs() to test __eq__. |
| 497 |
prev = ml |
| 498 |
|
| 499 |
for ls in ml: |
| 500 |
self.assertEqual(ls.geom_type, "LineString") |
| 501 |
self.assertEqual(ls.geom_typeid, 1) |
| 502 |
self.assertIs(ls.empty, False) |
| 503 |
|
| 504 |
with self.assertRaises(IndexError): |
| 505 |
ml.__getitem__(len(ml)) |
| 506 |
self.assertEqual(ml.wkt, MultiLineString(*tuple(s.clone() for s in ml)).wkt) |
| 507 |
self.assertEqual( |
| 508 |
ml, MultiLineString(*tuple(LineString(s.tuple) for s in ml)) |
| 509 |
) |
| 510 |
|
| 511 |
def test_linearring(self): |
| 512 |
"Testing LinearRing objects." |
| 513 |
for rr in self.geometries.linearrings: |
| 514 |
lr = fromstr(rr.wkt) |
| 515 |
self.assertEqual(lr.geom_type, "LinearRing") |
| 516 |
self.assertEqual(lr.geom_typeid, 2) |
| 517 |
self.assertEqual(lr.dims, 1) |
| 518 |
self.assertEqual(rr.n_p, len(lr)) |
| 519 |
self.assertIs(lr.valid, True) |
| 520 |
self.assertIs(lr.empty, False) |
| 521 |
|
| 522 |
# Creating a LinearRing from a tuple, list, and numpy array |
| 523 |
self.assertEqual(lr, LinearRing(lr.tuple)) |
| 524 |
self.assertEqual(lr, LinearRing(*lr.tuple)) |
| 525 |
self.assertEqual(lr, LinearRing([list(tup) for tup in lr.tuple])) |
| 526 |
if numpy: |
| 527 |
self.assertEqual(lr, LinearRing(numpy.array(lr.tuple))) |
| 528 |
|
| 529 |
with self.assertRaisesMessage( |
| 530 |
ValueError, "LinearRing requires at least 4 points, got 3." |
| 531 |
): |
| 532 |
LinearRing((0, 0), (1, 1), (0, 0)) |
| 533 |
|
| 534 |
with self.assertRaisesMessage( |
| 535 |
ValueError, "LinearRing requires at least 4 points, got 1." |
| 536 |
): |
| 537 |
LinearRing([(0, 0)]) |
| 538 |
|
| 539 |
if numpy: |
| 540 |
with self.assertRaisesMessage( |
| 541 |
ValueError, "LinearRing requires at least 4 points, got 1." |
| 542 |
): |
| 543 |
LinearRing(numpy.array([(0, 0)])) |
| 544 |
|
| 545 |
def test_linearring_json(self): |
| 546 |
self.assertJSONEqual( |
| 547 |
LinearRing((0, 0), (0, 1), (1, 1), (0, 0)).json, |
| 548 |
'{"coordinates": [[0, 0], [0, 1], [1, 1], [0, 0]], "type": "LineString"}', |
| 549 |
) |
| 550 |
|
| 551 |
def test_polygons_from_bbox(self): |
| 552 |
"Testing `from_bbox` class method." |
| 553 |
bbox = (-180, -90, 180, 90) |
| 554 |
p = Polygon.from_bbox(bbox) |
| 555 |
self.assertEqual(bbox, p.extent) |
| 556 |
|
| 557 |
# Testing numerical precision |
| 558 |
x = 3.14159265358979323 |
| 559 |
bbox = (0, 0, 1, x) |
| 560 |
p = Polygon.from_bbox(bbox) |
| 561 |
y = p.extent[-1] |
| 562 |
self.assertEqual(format(x, ".13f"), format(y, ".13f")) |
| 563 |
|
| 564 |
def test_polygons(self): |
| 565 |
"Testing Polygon objects." |
| 566 |
|
| 567 |
prev = fromstr("POINT(0 0)") |
| 568 |
for p in self.geometries.polygons: |
| 569 |
# Creating the Polygon, testing its properties. |
| 570 |
poly = fromstr(p.wkt) |
| 571 |
self.assertEqual(poly.geom_type, "Polygon") |
| 572 |
self.assertEqual(poly.geom_typeid, 3) |
| 573 |
self.assertEqual(poly.dims, 2) |
| 574 |
self.assertIs(poly.empty, False) |
| 575 |
self.assertIs(poly.ring, False) |
| 576 |
self.assertEqual(p.n_i, poly.num_interior_rings) |
| 577 |
self.assertEqual(p.n_i + 1, len(poly)) # Testing __len__ |
| 578 |
self.assertEqual(p.n_p, poly.num_points) |
| 579 |
|
| 580 |
# Area & Centroid |
| 581 |
self.assertAlmostEqual(p.area, poly.area, 9) |
| 582 |
self.assertAlmostEqual(p.centroid[0], poly.centroid.tuple[0], 9) |
| 583 |
self.assertAlmostEqual(p.centroid[1], poly.centroid.tuple[1], 9) |
| 584 |
|
| 585 |
# Testing the geometry equivalence |
| 586 |
self.assertEqual(poly, fromstr(p.wkt)) |
| 587 |
# Should not be equal to previous geometry |
| 588 |
self.assertIs(poly == prev, False) # Use assertIs() to test __eq__. |
| 589 |
self.assertIs(poly != prev, True) # Use assertIs() to test __ne__. |
| 590 |
|
| 591 |
# Testing the exterior ring |
| 592 |
ring = poly.exterior_ring |
| 593 |
self.assertEqual(ring.geom_type, "LinearRing") |
| 594 |
self.assertEqual(ring.geom_typeid, 2) |
| 595 |
if p.ext_ring_cs: |
| 596 |
self.assertEqual(p.ext_ring_cs, ring.tuple) |
| 597 |
self.assertEqual(p.ext_ring_cs, poly[0].tuple) # Testing __getitem__ |
| 598 |
|
| 599 |
# Testing __getitem__ and __setitem__ on invalid indices |
| 600 |
with self.assertRaises(IndexError): |
| 601 |
poly.__getitem__(len(poly)) |
| 602 |
with self.assertRaises(IndexError): |
| 603 |
poly.__setitem__(len(poly), False) |
| 604 |
with self.assertRaises(IndexError): |
| 605 |
poly.__getitem__(-1 * len(poly) - 1) |
| 606 |
|
| 607 |
# Testing __iter__ |
| 608 |
for r in poly: |
| 609 |
self.assertEqual(r.geom_type, "LinearRing") |
| 610 |
self.assertEqual(r.geom_typeid, 2) |
| 611 |
|
| 612 |
# Testing polygon construction. |
| 613 |
with self.assertRaises(TypeError): |
| 614 |
Polygon(0, [1, 2, 3]) |
| 615 |
with self.assertRaises(TypeError): |
| 616 |
Polygon("foo") |
| 617 |
|
| 618 |
# Polygon(shell, (hole1, ... holeN)) |
| 619 |
ext_ring, *int_rings = poly |
| 620 |
self.assertEqual(poly, Polygon(ext_ring, int_rings)) |
| 621 |
|
| 622 |
# Polygon(shell_tuple, hole_tuple1, ... , hole_tupleN) |
| 623 |
ring_tuples = tuple(r.tuple for r in poly) |
| 624 |
self.assertEqual(poly, Polygon(*ring_tuples)) |
| 625 |
|
| 626 |
# Constructing with tuples of LinearRings. |
| 627 |
self.assertEqual(poly.wkt, Polygon(*tuple(r for r in poly)).wkt) |
| 628 |
self.assertEqual( |
| 629 |
poly.wkt, Polygon(*tuple(LinearRing(r.tuple) for r in poly)).wkt |
| 630 |
) |
| 631 |
|
| 632 |
def test_polygons_templates(self): |
| 633 |
# Accessing Polygon attributes in templates should work. |
| 634 |
engine = Engine() |
| 635 |
template = engine.from_string("{{ polygons.0.wkt }}") |
| 636 |
polygons = [fromstr(p.wkt) for p in self.geometries.multipolygons[:2]] |
| 637 |
content = template.render(Context({"polygons": polygons})) |
| 638 |
self.assertIn("MULTIPOLYGON (((100", content) |
| 639 |
|
| 640 |
def test_polygon_comparison(self): |
| 641 |
p1 = Polygon(((0, 0), (0, 1), (1, 1), (1, 0), (0, 0))) |
| 642 |
p2 = Polygon(((0, 0), (0, 1), (1, 0), (0, 0))) |
| 643 |
self.assertGreater(p1, p2) |
| 644 |
self.assertLess(p2, p1) |
| 645 |
|
| 646 |
p3 = Polygon(((0, 0), (0, 1), (1, 1), (2, 0), (0, 0))) |
| 647 |
p4 = Polygon(((0, 0), (0, 1), (2, 2), (1, 0), (0, 0))) |
| 648 |
self.assertGreater(p4, p3) |
| 649 |
self.assertLess(p3, p4) |
| 650 |
|
| 651 |
def test_multipolygons(self): |
| 652 |
"Testing MultiPolygon objects." |
| 653 |
fromstr("POINT (0 0)") |
| 654 |
for mp in self.geometries.multipolygons: |
| 655 |
mpoly = fromstr(mp.wkt) |
| 656 |
self.assertEqual(mpoly.geom_type, "MultiPolygon") |
| 657 |
self.assertEqual(mpoly.geom_typeid, 6) |
| 658 |
self.assertEqual(mpoly.dims, 2) |
| 659 |
self.assertEqual(mp.valid, mpoly.valid) |
| 660 |
|
| 661 |
if mp.valid: |
| 662 |
self.assertEqual(mp.num_geom, mpoly.num_geom) |
| 663 |
self.assertEqual(mp.n_p, mpoly.num_coords) |
| 664 |
self.assertEqual(mp.num_geom, len(mpoly)) |
| 665 |
with self.assertRaises(IndexError): |
| 666 |
mpoly.__getitem__(len(mpoly)) |
| 667 |
for p in mpoly: |
| 668 |
self.assertEqual(p.geom_type, "Polygon") |
| 669 |
self.assertEqual(p.geom_typeid, 3) |
| 670 |
self.assertIs(p.valid, True) |
| 671 |
self.assertEqual( |
| 672 |
mpoly.wkt, MultiPolygon(*tuple(poly.clone() for poly in mpoly)).wkt |
| 673 |
) |
| 674 |
|
| 675 |
def test_memory_hijinks(self): |
| 676 |
"Testing Geometry __del__() on rings and polygons." |
| 677 |
# #### Memory issues with rings and poly |
| 678 |
|
| 679 |
# These tests are needed to ensure sanity with writable geometries. |
| 680 |
|
| 681 |
# Getting a polygon with interior rings, and pulling out the interior rings |
| 682 |
poly = fromstr(self.geometries.polygons[1].wkt) |
| 683 |
ring1 = poly[0] |
| 684 |
ring2 = poly[1] |
| 685 |
|
| 686 |
# These deletes should be 'harmless' since they are done on child geometries |
| 687 |
del ring1 |
| 688 |
del ring2 |
| 689 |
ring1 = poly[0] |
| 690 |
ring2 = poly[1] |
| 691 |
|
| 692 |
# Deleting the polygon |
| 693 |
del poly |
| 694 |
|
| 695 |
# Access to these rings is OK since they are clones. |
| 696 |
str(ring1) |
| 697 |
str(ring2) |
| 698 |
|
| 699 |
def test_coord_seq(self): |
| 700 |
"Testing Coordinate Sequence objects." |
| 701 |
for p in self.geometries.polygons: |
| 702 |
if p.ext_ring_cs: |
| 703 |
# Constructing the polygon and getting the coordinate sequence |
| 704 |
poly = fromstr(p.wkt) |
| 705 |
cs = poly.exterior_ring.coord_seq |
| 706 |
|
| 707 |
self.assertEqual( |
| 708 |
p.ext_ring_cs, cs.tuple |
| 709 |
) # done in the Polygon test too. |
| 710 |
self.assertEqual( |
| 711 |
len(p.ext_ring_cs), len(cs) |
| 712 |
) # Making sure __len__ works |
| 713 |
|
| 714 |
# Checks __getitem__ and __setitem__ |
| 715 |
for i in range(len(p.ext_ring_cs)): |
| 716 |
c1 = p.ext_ring_cs[i] # Expected value |
| 717 |
c2 = cs[i] # Value from coordseq |
| 718 |
self.assertEqual(c1, c2) |
| 719 |
|
| 720 |
# Constructing the test value to set the coordinate sequence with |
| 721 |
if len(c1) == 2: |
| 722 |
tset = (5, 23) |
| 723 |
else: |
| 724 |
tset = (5, 23, 8) |
| 725 |
cs[i] = tset |
| 726 |
|
| 727 |
# Making sure every set point matches what we expect |
| 728 |
for j in range(len(tset)): |
| 729 |
cs[i] = tset |
| 730 |
self.assertEqual(tset[j], cs[i][j]) |
| 731 |
|
| 732 |
def test_relate_pattern(self): |
| 733 |
"Testing relate() and relate_pattern()." |
| 734 |
g = fromstr("POINT (0 0)") |
| 735 |
with self.assertRaises(GEOSException): |
| 736 |
g.relate_pattern(0, "invalid pattern, yo") |
| 737 |
for rg in self.geometries.relate_geoms: |
| 738 |
a = fromstr(rg.wkt_a) |
| 739 |
b = fromstr(rg.wkt_b) |
| 740 |
self.assertEqual(rg.result, a.relate_pattern(b, rg.pattern)) |
| 741 |
self.assertEqual(rg.pattern, a.relate(b)) |
| 742 |
|
| 743 |
def test_intersection(self): |
| 744 |
"Testing intersects() and intersection()." |
| 745 |
for i in range(len(self.geometries.topology_geoms)): |
| 746 |
a = fromstr(self.geometries.topology_geoms[i].wkt_a) |
| 747 |
b = fromstr(self.geometries.topology_geoms[i].wkt_b) |
| 748 |
i1 = fromstr(self.geometries.intersect_geoms[i].wkt) |
| 749 |
self.assertIs(a.intersects(b), True) |
| 750 |
i2 = a.intersection(b) |
| 751 |
self.assertTrue(i1.equals(i2)) |
| 752 |
self.assertTrue(i1.equals(a & b)) # __and__ is intersection operator |
| 753 |
a &= b # testing __iand__ |
| 754 |
self.assertTrue(i1.equals(a)) |
| 755 |
|
| 756 |
def test_union(self): |
| 757 |
"Testing union()." |
| 758 |
for i in range(len(self.geometries.topology_geoms)): |
| 759 |
a = fromstr(self.geometries.topology_geoms[i].wkt_a) |
| 760 |
b = fromstr(self.geometries.topology_geoms[i].wkt_b) |
| 761 |
u1 = fromstr(self.geometries.union_geoms[i].wkt) |
| 762 |
u2 = a.union(b) |
| 763 |
self.assertTrue(u1.equals(u2)) |
| 764 |
self.assertTrue(u1.equals(a | b)) # __or__ is union operator |
| 765 |
a |= b # testing __ior__ |
| 766 |
self.assertTrue(u1.equals(a)) |
| 767 |
|
| 768 |
def test_unary_union(self): |
| 769 |
"Testing unary_union." |
| 770 |
for i in range(len(self.geometries.topology_geoms)): |
| 771 |
a = fromstr(self.geometries.topology_geoms[i].wkt_a) |
| 772 |
b = fromstr(self.geometries.topology_geoms[i].wkt_b) |
| 773 |
u1 = fromstr(self.geometries.union_geoms[i].wkt) |
| 774 |
u2 = GeometryCollection(a, b).unary_union |
| 775 |
self.assertTrue(u1.equals(u2)) |
| 776 |
|
| 777 |
def test_difference(self): |
| 778 |
"Testing difference()." |
| 779 |
for i in range(len(self.geometries.topology_geoms)): |
| 780 |
a = fromstr(self.geometries.topology_geoms[i].wkt_a) |
| 781 |
b = fromstr(self.geometries.topology_geoms[i].wkt_b) |
| 782 |
d1 = fromstr(self.geometries.diff_geoms[i].wkt) |
| 783 |
d2 = a.difference(b) |
| 784 |
self.assertTrue(d1.equals(d2)) |
| 785 |
self.assertTrue(d1.equals(a - b)) # __sub__ is difference operator |
| 786 |
a -= b # testing __isub__ |
| 787 |
self.assertTrue(d1.equals(a)) |
| 788 |
|
| 789 |
def test_symdifference(self): |
| 790 |
"Testing sym_difference()." |
| 791 |
for i in range(len(self.geometries.topology_geoms)): |
| 792 |
a = fromstr(self.geometries.topology_geoms[i].wkt_a) |
| 793 |
b = fromstr(self.geometries.topology_geoms[i].wkt_b) |
| 794 |
d1 = fromstr(self.geometries.sdiff_geoms[i].wkt) |
| 795 |
d2 = a.sym_difference(b) |
| 796 |
self.assertTrue(d1.equals(d2)) |
| 797 |
self.assertTrue( |
| 798 |
d1.equals(a ^ b) |
| 799 |
) # __xor__ is symmetric difference operator |
| 800 |
a ^= b # testing __ixor__ |
| 801 |
self.assertTrue(d1.equals(a)) |
| 802 |
|
| 803 |
def test_buffer(self): |
| 804 |
bg = self.geometries.buffer_geoms[0] |
| 805 |
g = fromstr(bg.wkt) |
| 806 |
|
| 807 |
# Can't use a floating-point for the number of quadsegs. |
| 808 |
with self.assertRaises(ctypes.ArgumentError): |
| 809 |
g.buffer(bg.width, quadsegs=1.1) |
| 810 |
|
| 811 |
self._test_buffer(self.geometries.buffer_geoms, "buffer") |
| 812 |
|
| 813 |
def test_buffer_with_style(self): |
| 814 |
bg = self.geometries.buffer_with_style_geoms[0] |
| 815 |
g = fromstr(bg.wkt) |
| 816 |
|
| 817 |
# Can't use a floating-point for the number of quadsegs. |
| 818 |
with self.assertRaises(ctypes.ArgumentError): |
| 819 |
g.buffer_with_style(bg.width, quadsegs=1.1) |
| 820 |
|
| 821 |
# Can't use a floating-point for the end cap style. |
| 822 |
with self.assertRaises(ctypes.ArgumentError): |
| 823 |
g.buffer_with_style(bg.width, end_cap_style=1.2) |
| 824 |
# Can't use a end cap style that is not in the enum. |
| 825 |
with self.assertRaises(GEOSException): |
| 826 |
g.buffer_with_style(bg.width, end_cap_style=55) |
| 827 |
|
| 828 |
# Can't use a floating-point for the join style. |
| 829 |
with self.assertRaises(ctypes.ArgumentError): |
| 830 |
g.buffer_with_style(bg.width, join_style=1.3) |
| 831 |
# Can't use a join style that is not in the enum. |
| 832 |
with self.assertRaises(GEOSException): |
| 833 |
g.buffer_with_style(bg.width, join_style=66) |
| 834 |
|
| 835 |
self._test_buffer( |
| 836 |
itertools.chain( |
| 837 |
self.geometries.buffer_geoms, self.geometries.buffer_with_style_geoms |
| 838 |
), |
| 839 |
"buffer_with_style", |
| 840 |
) |
| 841 |
|
| 842 |
def _test_buffer(self, geometries, buffer_method_name): |
| 843 |
for bg in geometries: |
| 844 |
g = fromstr(bg.wkt) |
| 845 |
|
| 846 |
# The buffer we expect |
| 847 |
exp_buf = fromstr(bg.buffer_wkt) |
| 848 |
|
| 849 |
# Constructing our buffer |
| 850 |
buf_kwargs = { |
| 851 |
kwarg_name: getattr(bg, kwarg_name) |
| 852 |
for kwarg_name in ( |
| 853 |
"width", |
| 854 |
"quadsegs", |
| 855 |
"end_cap_style", |
| 856 |
"join_style", |
| 857 |
"mitre_limit", |
| 858 |
) |
| 859 |
if hasattr(bg, kwarg_name) |
| 860 |
} |
| 861 |
buf = getattr(g, buffer_method_name)(**buf_kwargs) |
| 862 |
self.assertEqual(exp_buf.num_coords, buf.num_coords) |
| 863 |
self.assertEqual(len(exp_buf), len(buf)) |
| 864 |
|
| 865 |
# Now assuring that each point in the buffer is almost equal |
| 866 |
for j in range(len(exp_buf)): |
| 867 |
exp_ring = exp_buf[j] |
| 868 |
buf_ring = buf[j] |
| 869 |
self.assertEqual(len(exp_ring), len(buf_ring)) |
| 870 |
for k in range(len(exp_ring)): |
| 871 |
# Asserting the X, Y of each point are almost equal (due to |
| 872 |
# floating point imprecision). |
| 873 |
self.assertAlmostEqual(exp_ring[k][0], buf_ring[k][0], 9) |
| 874 |
self.assertAlmostEqual(exp_ring[k][1], buf_ring[k][1], 9) |
| 875 |
|
| 876 |
def test_covers(self): |
| 877 |
poly = Polygon(((0, 0), (0, 10), (10, 10), (10, 0), (0, 0))) |
| 878 |
self.assertTrue(poly.covers(Point(5, 5))) |
| 879 |
self.assertFalse(poly.covers(Point(100, 100))) |
| 880 |
|
| 881 |
def test_closed(self): |
| 882 |
ls_closed = LineString((0, 0), (1, 1), (0, 0)) |
| 883 |
ls_not_closed = LineString((0, 0), (1, 1)) |
| 884 |
self.assertFalse(ls_not_closed.closed) |
| 885 |
self.assertTrue(ls_closed.closed) |
| 886 |
|
| 887 |
def test_srid(self): |
| 888 |
"Testing the SRID property and keyword." |
| 889 |
# Testing SRID keyword on Point |
| 890 |
pnt = Point(5, 23, srid=4326) |
| 891 |
self.assertEqual(4326, pnt.srid) |
| 892 |
pnt.srid = 3084 |
| 893 |
self.assertEqual(3084, pnt.srid) |
| 894 |
with self.assertRaises(ctypes.ArgumentError): |
| 895 |
pnt.srid = "4326" |
| 896 |
|
| 897 |
# Testing SRID keyword on fromstr(), and on Polygon rings. |
| 898 |
poly = fromstr(self.geometries.polygons[1].wkt, srid=4269) |
| 899 |
self.assertEqual(4269, poly.srid) |
| 900 |
for ring in poly: |
| 901 |
self.assertEqual(4269, ring.srid) |
| 902 |
poly.srid = 4326 |
| 903 |
self.assertEqual(4326, poly.shell.srid) |
| 904 |
|
| 905 |
# Testing SRID keyword on GeometryCollection |
| 906 |
gc = GeometryCollection( |
| 907 |
Point(5, 23), LineString((0, 0), (1.5, 1.5), (3, 3)), srid=32021 |
| 908 |
) |
| 909 |
self.assertEqual(32021, gc.srid) |
| 910 |
for i in range(len(gc)): |
| 911 |
self.assertEqual(32021, gc[i].srid) |
| 912 |
|
| 913 |
# GEOS may get the SRID from HEXEWKB |
| 914 |
# 'POINT(5 23)' at SRID=4326 in hex form -- obtained from PostGIS |
| 915 |
# using `SELECT GeomFromText('POINT (5 23)', 4326);`. |
| 916 |
hex = "0101000020E610000000000000000014400000000000003740" |
| 917 |
p1 = fromstr(hex) |
| 918 |
self.assertEqual(4326, p1.srid) |
| 919 |
|
| 920 |
p2 = fromstr(p1.hex) |
| 921 |
self.assertIsNone(p2.srid) |
| 922 |
p3 = fromstr(p1.hex, srid=-1) # -1 is intended. |
| 923 |
self.assertEqual(-1, p3.srid) |
| 924 |
|
| 925 |
# Testing that geometry SRID could be set to its own value |
| 926 |
pnt_wo_srid = Point(1, 1) |
| 927 |
pnt_wo_srid.srid = pnt_wo_srid.srid |
| 928 |
|
| 929 |
# Input geometries that have an SRID. |
| 930 |
self.assertEqual(GEOSGeometry(pnt.ewkt, srid=pnt.srid).srid, pnt.srid) |
| 931 |
self.assertEqual(GEOSGeometry(pnt.ewkb, srid=pnt.srid).srid, pnt.srid) |
| 932 |
with self.assertRaisesMessage( |
| 933 |
ValueError, "Input geometry already has SRID: %d." % pnt.srid |
| 934 |
): |
| 935 |
GEOSGeometry(pnt.ewkt, srid=1) |
| 936 |
with self.assertRaisesMessage( |
| 937 |
ValueError, "Input geometry already has SRID: %d." % pnt.srid |
| 938 |
): |
| 939 |
GEOSGeometry(pnt.ewkb, srid=1) |
| 940 |
|
| 941 |
def test_custom_srid(self): |
| 942 |
"""Test with a null srid and a srid unknown to GDAL.""" |
| 943 |
for srid in [None, 999999]: |
| 944 |
pnt = Point(111200, 220900, srid=srid) |
| 945 |
self.assertTrue( |
| 946 |
pnt.ewkt.startswith( |
| 947 |
("SRID=%s;" % srid if srid else "") + "POINT (111200" |
| 948 |
) |
| 949 |
) |
| 950 |
self.assertIsInstance(pnt.ogr, gdal.OGRGeometry) |
| 951 |
self.assertIsNone(pnt.srs) |
| 952 |
|
| 953 |
# Test conversion from custom to a known srid |
| 954 |
c2w = gdal.CoordTransform( |
| 955 |
gdal.SpatialReference( |
| 956 |
"+proj=mill +lat_0=0 +lon_0=0 +x_0=0 +y_0=0 +R_A +datum=WGS84 " |
| 957 |
"+units=m +no_defs" |
| 958 |
), |
| 959 |
gdal.SpatialReference(4326), |
| 960 |
) |
| 961 |
new_pnt = pnt.transform(c2w, clone=True) |
| 962 |
self.assertEqual(new_pnt.srid, 4326) |
| 963 |
self.assertAlmostEqual(new_pnt.x, 1, 1) |
| 964 |
self.assertAlmostEqual(new_pnt.y, 2, 1) |
| 965 |
|
| 966 |
def test_mutable_geometries(self): |
| 967 |
"Testing the mutability of Polygons and Geometry Collections." |
| 968 |
# ### Testing the mutability of Polygons ### |
| 969 |
for p in self.geometries.polygons: |
| 970 |
poly = fromstr(p.wkt) |
| 971 |
|
| 972 |
# Should only be able to use __setitem__ with LinearRing geometries. |
| 973 |
with self.assertRaises(TypeError): |
| 974 |
poly.__setitem__(0, LineString((1, 1), (2, 2))) |
| 975 |
|
| 976 |
# Constructing the new shell by adding 500 to every point in the old shell. |
| 977 |
shell_tup = poly.shell.tuple |
| 978 |
new_coords = [] |
| 979 |
for point in shell_tup: |
| 980 |
new_coords.append((point[0] + 500.0, point[1] + 500.0)) |
| 981 |
new_shell = LinearRing(*tuple(new_coords)) |
| 982 |
|
| 983 |
# Assigning polygon's exterior ring w/the new shell |
| 984 |
poly.exterior_ring = new_shell |
| 985 |
str(new_shell) # new shell is still accessible |
| 986 |
self.assertEqual(poly.exterior_ring, new_shell) |
| 987 |
self.assertEqual(poly[0], new_shell) |
| 988 |
|
| 989 |
# ### Testing the mutability of Geometry Collections |
| 990 |
for tg in self.geometries.multipoints: |
| 991 |
mp = fromstr(tg.wkt) |
| 992 |
for i in range(len(mp)): |
| 993 |
# Creating a random point. |
| 994 |
pnt = mp[i] |
| 995 |
new = Point(random.randint(21, 100), random.randint(21, 100)) |
| 996 |
# Testing the assignment |
| 997 |
mp[i] = new |
| 998 |
str(new) # what was used for the assignment is still accessible |
| 999 |
self.assertEqual(mp[i], new) |
| 1000 |
self.assertEqual(mp[i].wkt, new.wkt) |
| 1001 |
self.assertNotEqual(pnt, mp[i]) |
| 1002 |
|
| 1003 |
# MultiPolygons involve much more memory management because each |
| 1004 |
# Polygon w/in the collection has its own rings. |
| 1005 |
for tg in self.geometries.multipolygons: |
| 1006 |
mpoly = fromstr(tg.wkt) |
| 1007 |
for i in range(len(mpoly)): |
| 1008 |
poly = mpoly[i] |
| 1009 |
old_poly = mpoly[i] |
| 1010 |
# Offsetting the each ring in the polygon by 500. |
| 1011 |
for j in range(len(poly)): |
| 1012 |
r = poly[j] |
| 1013 |
for k in range(len(r)): |
| 1014 |
r[k] = (r[k][0] + 500.0, r[k][1] + 500.0) |
| 1015 |
poly[j] = r |
| 1016 |
|
| 1017 |
self.assertNotEqual(mpoly[i], poly) |
| 1018 |
# Testing the assignment |
| 1019 |
mpoly[i] = poly |
| 1020 |
str(poly) # Still accessible |
| 1021 |
self.assertEqual(mpoly[i], poly) |
| 1022 |
self.assertNotEqual(mpoly[i], old_poly) |
| 1023 |
|
| 1024 |
# Extreme (!!) __setitem__ -- no longer works, have to detect |
| 1025 |
# in the first object that __setitem__ is called in the subsequent |
| 1026 |
# objects -- maybe mpoly[0, 0, 0] = (3.14, 2.71)? |
| 1027 |
# mpoly[0][0][0] = (3.14, 2.71) |
| 1028 |
# self.assertEqual((3.14, 2.71), mpoly[0][0][0]) |
| 1029 |
# Doing it more slowly.. |
| 1030 |
# self.assertEqual((3.14, 2.71), mpoly[0].shell[0]) |
| 1031 |
# del mpoly |
| 1032 |
|
| 1033 |
def test_point_list_assignment(self): |
| 1034 |
p = Point(0, 0) |
| 1035 |
|
| 1036 |
p[:] = (1, 2, 3) |
| 1037 |
self.assertEqual(p, Point(1, 2, 3)) |
| 1038 |
|
| 1039 |
p[:] = () |
| 1040 |
self.assertEqual(p.wkt, Point()) |
| 1041 |
|
| 1042 |
p[:] = (1, 2) |
| 1043 |
self.assertEqual(p.wkt, Point(1, 2)) |
| 1044 |
|
| 1045 |
with self.assertRaises(ValueError): |
| 1046 |
p[:] = (1,) |
| 1047 |
with self.assertRaises(ValueError): |
| 1048 |
p[:] = (1, 2, 3, 4, 5) |
| 1049 |
|
| 1050 |
def test_linestring_list_assignment(self): |
| 1051 |
ls = LineString((0, 0), (1, 1)) |
| 1052 |
|
| 1053 |
ls[:] = () |
| 1054 |
self.assertEqual(ls, LineString()) |
| 1055 |
|
| 1056 |
ls[:] = ((0, 0), (1, 1), (2, 2)) |
| 1057 |
self.assertEqual(ls, LineString((0, 0), (1, 1), (2, 2))) |
| 1058 |
|
| 1059 |
with self.assertRaises(ValueError): |
| 1060 |
ls[:] = (1,) |
| 1061 |
|
| 1062 |
def test_linearring_list_assignment(self): |
| 1063 |
ls = LinearRing((0, 0), (0, 1), (1, 1), (0, 0)) |
| 1064 |
|
| 1065 |
ls[:] = () |
| 1066 |
self.assertEqual(ls, LinearRing()) |
| 1067 |
|
| 1068 |
ls[:] = ((0, 0), (0, 1), (1, 1), (1, 0), (0, 0)) |
| 1069 |
self.assertEqual(ls, LinearRing((0, 0), (0, 1), (1, 1), (1, 0), (0, 0))) |
| 1070 |
|
| 1071 |
with self.assertRaises(ValueError): |
| 1072 |
ls[:] = ((0, 0), (1, 1), (2, 2)) |
| 1073 |
|
| 1074 |
def test_polygon_list_assignment(self): |
| 1075 |
pol = Polygon() |
| 1076 |
|
| 1077 |
pol[:] = (((0, 0), (0, 1), (1, 1), (1, 0), (0, 0)),) |
| 1078 |
self.assertEqual( |
| 1079 |
pol, |
| 1080 |
Polygon( |
| 1081 |
((0, 0), (0, 1), (1, 1), (1, 0), (0, 0)), |
| 1082 |
), |
| 1083 |
) |
| 1084 |
|
| 1085 |
pol[:] = () |
| 1086 |
self.assertEqual(pol, Polygon()) |
| 1087 |
|
| 1088 |
def test_geometry_collection_list_assignment(self): |
| 1089 |
p = Point() |
| 1090 |
gc = GeometryCollection() |
| 1091 |
|
| 1092 |
gc[:] = [p] |
| 1093 |
self.assertEqual(gc, GeometryCollection(p)) |
| 1094 |
|
| 1095 |
gc[:] = () |
| 1096 |
self.assertEqual(gc, GeometryCollection()) |
| 1097 |
|
| 1098 |
def test_threed(self): |
| 1099 |
"Testing three-dimensional geometries." |
| 1100 |
# Testing a 3D Point |
| 1101 |
pnt = Point(2, 3, 8) |
| 1102 |
self.assertEqual((2.0, 3.0, 8.0), pnt.coords) |
| 1103 |
with self.assertRaises(TypeError): |
| 1104 |
pnt.tuple = (1.0, 2.0) |
| 1105 |
pnt.coords = (1.0, 2.0, 3.0) |
| 1106 |
self.assertEqual((1.0, 2.0, 3.0), pnt.coords) |
| 1107 |
|
| 1108 |
# Testing a 3D LineString |
| 1109 |
ls = LineString((2.0, 3.0, 8.0), (50.0, 250.0, -117.0)) |
| 1110 |
self.assertEqual(((2.0, 3.0, 8.0), (50.0, 250.0, -117.0)), ls.tuple) |
| 1111 |
with self.assertRaises(TypeError): |
| 1112 |
ls.__setitem__(0, (1.0, 2.0)) |
| 1113 |
ls[0] = (1.0, 2.0, 3.0) |
| 1114 |
self.assertEqual((1.0, 2.0, 3.0), ls[0]) |
| 1115 |
|
| 1116 |
def test_distance(self): |
| 1117 |
"Testing the distance() function." |
| 1118 |
# Distance to self should be 0. |
| 1119 |
pnt = Point(0, 0) |
| 1120 |
self.assertEqual(0.0, pnt.distance(Point(0, 0))) |
| 1121 |
|
| 1122 |
# Distance should be 1 |
| 1123 |
self.assertEqual(1.0, pnt.distance(Point(0, 1))) |
| 1124 |
|
| 1125 |
# Distance should be ~ sqrt(2) |
| 1126 |
self.assertAlmostEqual(1.41421356237, pnt.distance(Point(1, 1)), 11) |
| 1127 |
|
| 1128 |
# Distances are from the closest vertex in each geometry -- |
| 1129 |
# should be 3 (distance from (2, 2) to (5, 2)). |
| 1130 |
ls1 = LineString((0, 0), (1, 1), (2, 2)) |
| 1131 |
ls2 = LineString((5, 2), (6, 1), (7, 0)) |
| 1132 |
self.assertEqual(3, ls1.distance(ls2)) |
| 1133 |
|
| 1134 |
def test_length(self): |
| 1135 |
"Testing the length property." |
| 1136 |
# Points have 0 length. |
| 1137 |
pnt = Point(0, 0) |
| 1138 |
self.assertEqual(0.0, pnt.length) |
| 1139 |
|
| 1140 |
# Should be ~ sqrt(2) |
| 1141 |
ls = LineString((0, 0), (1, 1)) |
| 1142 |
self.assertAlmostEqual(1.41421356237, ls.length, 11) |
| 1143 |
|
| 1144 |
# Should be circumference of Polygon |
| 1145 |
poly = Polygon(LinearRing((0, 0), (0, 1), (1, 1), (1, 0), (0, 0))) |
| 1146 |
self.assertEqual(4.0, poly.length) |
| 1147 |
|
| 1148 |
# Should be sum of each element's length in collection. |
| 1149 |
mpoly = MultiPolygon(poly.clone(), poly) |
| 1150 |
self.assertEqual(8.0, mpoly.length) |
| 1151 |
|
| 1152 |
def test_emptyCollections(self): |
| 1153 |
"Testing empty geometries and collections." |
| 1154 |
geoms = [ |
| 1155 |
GeometryCollection([]), |
| 1156 |
fromstr("GEOMETRYCOLLECTION EMPTY"), |
| 1157 |
GeometryCollection(), |
| 1158 |
fromstr("POINT EMPTY"), |
| 1159 |
Point(), |
| 1160 |
fromstr("LINESTRING EMPTY"), |
| 1161 |
LineString(), |
| 1162 |
fromstr("POLYGON EMPTY"), |
| 1163 |
Polygon(), |
| 1164 |
fromstr("MULTILINESTRING EMPTY"), |
| 1165 |
MultiLineString(), |
| 1166 |
fromstr("MULTIPOLYGON EMPTY"), |
| 1167 |
MultiPolygon(()), |
| 1168 |
MultiPolygon(), |
| 1169 |
] |
| 1170 |
|
| 1171 |
if numpy: |
| 1172 |
geoms.append(LineString(numpy.array([]))) |
| 1173 |
|
| 1174 |
for g in geoms: |
| 1175 |
self.assertIs(g.empty, True) |
| 1176 |
|
| 1177 |
# Testing len() and num_geom. |
| 1178 |
if isinstance(g, Polygon): |
| 1179 |
self.assertEqual(1, len(g)) # Has one empty linear ring |
| 1180 |
self.assertEqual(1, g.num_geom) |
| 1181 |
self.assertEqual(0, len(g[0])) |
| 1182 |
elif isinstance(g, (Point, LineString)): |
| 1183 |
self.assertEqual(1, g.num_geom) |
| 1184 |
self.assertEqual(0, len(g)) |
| 1185 |
else: |
| 1186 |
self.assertEqual(0, g.num_geom) |
| 1187 |
self.assertEqual(0, len(g)) |
| 1188 |
|
| 1189 |
# Testing __getitem__ (doesn't work on Point or Polygon) |
| 1190 |
if isinstance(g, Point): |
| 1191 |
# IndexError is not raised in GEOS 3.8.0. |
| 1192 |
if geos_version_tuple() != (3, 8, 0): |
| 1193 |
with self.assertRaises(IndexError): |
| 1194 |
g.x |
| 1195 |
elif isinstance(g, Polygon): |
| 1196 |
lr = g.shell |
| 1197 |
self.assertEqual("LINEARRING EMPTY", lr.wkt) |
| 1198 |
self.assertEqual(0, len(lr)) |
| 1199 |
self.assertIs(lr.empty, True) |
| 1200 |
with self.assertRaises(IndexError): |
| 1201 |
lr.__getitem__(0) |
| 1202 |
else: |
| 1203 |
with self.assertRaises(IndexError): |
| 1204 |
g.__getitem__(0) |
| 1205 |
|
| 1206 |
def test_collection_dims(self): |
| 1207 |
gc = GeometryCollection([]) |
| 1208 |
self.assertEqual(gc.dims, -1) |
| 1209 |
|
| 1210 |
gc = GeometryCollection(Point(0, 0)) |
| 1211 |
self.assertEqual(gc.dims, 0) |
| 1212 |
|
| 1213 |
gc = GeometryCollection(LineString((0, 0), (1, 1)), Point(0, 0)) |
| 1214 |
self.assertEqual(gc.dims, 1) |
| 1215 |
|
| 1216 |
gc = GeometryCollection( |
| 1217 |
LineString((0, 0), (1, 1)), |
| 1218 |
Polygon(((0, 0), (0, 1), (1, 1), (0, 0))), |
| 1219 |
Point(0, 0), |
| 1220 |
) |
| 1221 |
self.assertEqual(gc.dims, 2) |
| 1222 |
|
| 1223 |
def test_collections_of_collections(self): |
| 1224 |
"Testing GeometryCollection handling of other collections." |
| 1225 |
# Creating a GeometryCollection WKT string composed of other |
| 1226 |
# collections and polygons. |
| 1227 |
coll = [mp.wkt for mp in self.geometries.multipolygons if mp.valid] |
| 1228 |
coll.extend(mls.wkt for mls in self.geometries.multilinestrings) |
| 1229 |
coll.extend(p.wkt for p in self.geometries.polygons) |
| 1230 |
coll.extend(mp.wkt for mp in self.geometries.multipoints) |
| 1231 |
gc_wkt = "GEOMETRYCOLLECTION(%s)" % ",".join(coll) |
| 1232 |
|
| 1233 |
# Should construct ok from WKT |
| 1234 |
gc1 = GEOSGeometry(gc_wkt) |
| 1235 |
|
| 1236 |
# Should also construct ok from individual geometry arguments. |
| 1237 |
gc2 = GeometryCollection(*tuple(g for g in gc1)) |
| 1238 |
|
| 1239 |
# And, they should be equal. |
| 1240 |
self.assertEqual(gc1, gc2) |
| 1241 |
|
| 1242 |
def test_gdal(self): |
| 1243 |
"Testing `ogr` and `srs` properties." |
| 1244 |
g1 = fromstr("POINT(5 23)") |
| 1245 |
self.assertIsInstance(g1.ogr, gdal.OGRGeometry) |
| 1246 |
self.assertIsNone(g1.srs) |
| 1247 |
|
| 1248 |
g1_3d = fromstr("POINT(5 23 8)") |
| 1249 |
self.assertIsInstance(g1_3d.ogr, gdal.OGRGeometry) |
| 1250 |
self.assertEqual(g1_3d.ogr.z, 8) |
| 1251 |
|
| 1252 |
g2 = fromstr("LINESTRING(0 0, 5 5, 23 23)", srid=4326) |
| 1253 |
self.assertIsInstance(g2.ogr, gdal.OGRGeometry) |
| 1254 |
self.assertIsInstance(g2.srs, gdal.SpatialReference) |
| 1255 |
self.assertEqual(g2.hex, g2.ogr.hex) |
| 1256 |
self.assertEqual("WGS 84", g2.srs.name) |
| 1257 |
|
| 1258 |
def test_copy(self): |
| 1259 |
"Testing use with the Python `copy` module." |
| 1260 |
import copy |
| 1261 |
|
| 1262 |
poly = GEOSGeometry( |
| 1263 |
"POLYGON((0 0, 0 23, 23 23, 23 0, 0 0), (5 5, 5 10, 10 10, 10 5, 5 5))" |
| 1264 |
) |
| 1265 |
cpy1 = copy.copy(poly) |
| 1266 |
cpy2 = copy.deepcopy(poly) |
| 1267 |
self.assertNotEqual(poly._ptr, cpy1._ptr) |
| 1268 |
self.assertNotEqual(poly._ptr, cpy2._ptr) |
| 1269 |
|
| 1270 |
def test_transform(self): |
| 1271 |
"Testing `transform` method." |
| 1272 |
orig = GEOSGeometry("POINT (-104.609 38.255)", 4326) |
| 1273 |
trans = GEOSGeometry("POINT (992385.4472045 481455.4944650)", 2774) |
| 1274 |
|
| 1275 |
# Using a srid, a SpatialReference object, and a CoordTransform object |
| 1276 |
# for transformations. |
| 1277 |
t1, t2, t3 = orig.clone(), orig.clone(), orig.clone() |
| 1278 |
t1.transform(trans.srid) |
| 1279 |
t2.transform(gdal.SpatialReference("EPSG:2774")) |
| 1280 |
ct = gdal.CoordTransform( |
| 1281 |
gdal.SpatialReference("WGS84"), gdal.SpatialReference(2774) |
| 1282 |
) |
| 1283 |
t3.transform(ct) |
| 1284 |
|
| 1285 |
# Testing use of the `clone` keyword. |
| 1286 |
k1 = orig.clone() |
| 1287 |
k2 = k1.transform(trans.srid, clone=True) |
| 1288 |
self.assertEqual(k1, orig) |
| 1289 |
self.assertNotEqual(k1, k2) |
| 1290 |
|
| 1291 |
# Different PROJ versions use different transformations, all are |
| 1292 |
# correct as having a 1 meter accuracy. |
| 1293 |
prec = -1 |
| 1294 |
for p in (t1, t2, t3, k2): |
| 1295 |
self.assertAlmostEqual(trans.x, p.x, prec) |
| 1296 |
self.assertAlmostEqual(trans.y, p.y, prec) |
| 1297 |
|
| 1298 |
def test_transform_3d(self): |
| 1299 |
p3d = GEOSGeometry("POINT (5 23 100)", 4326) |
| 1300 |
p3d.transform(2774) |
| 1301 |
self.assertAlmostEqual(p3d.z, 100, 3) |
| 1302 |
|
| 1303 |
def test_transform_noop(self): |
| 1304 |
"""Testing `transform` method (SRID match)""" |
| 1305 |
# transform() should no-op if source & dest SRIDs match, |
| 1306 |
# regardless of whether GDAL is available. |
| 1307 |
g = GEOSGeometry("POINT (-104.609 38.255)", 4326) |
| 1308 |
gt = g.tuple |
| 1309 |
g.transform(4326) |
| 1310 |
self.assertEqual(g.tuple, gt) |
| 1311 |
self.assertEqual(g.srid, 4326) |
| 1312 |
|
| 1313 |
g = GEOSGeometry("POINT (-104.609 38.255)", 4326) |
| 1314 |
g1 = g.transform(4326, clone=True) |
| 1315 |
self.assertEqual(g1.tuple, g.tuple) |
| 1316 |
self.assertEqual(g1.srid, 4326) |
| 1317 |
self.assertIsNot(g1, g, "Clone didn't happen") |
| 1318 |
|
| 1319 |
def test_transform_nosrid(self): |
| 1320 |
"""Testing `transform` method (no SRID or negative SRID)""" |
| 1321 |
|
| 1322 |
g = GEOSGeometry("POINT (-104.609 38.255)", srid=None) |
| 1323 |
with self.assertRaises(GEOSException): |
| 1324 |
g.transform(2774) |
| 1325 |
|
| 1326 |
g = GEOSGeometry("POINT (-104.609 38.255)", srid=None) |
| 1327 |
with self.assertRaises(GEOSException): |
| 1328 |
g.transform(2774, clone=True) |
| 1329 |
|
| 1330 |
g = GEOSGeometry("POINT (-104.609 38.255)", srid=-1) |
| 1331 |
with self.assertRaises(GEOSException): |
| 1332 |
g.transform(2774) |
| 1333 |
|
| 1334 |
g = GEOSGeometry("POINT (-104.609 38.255)", srid=-1) |
| 1335 |
with self.assertRaises(GEOSException): |
| 1336 |
g.transform(2774, clone=True) |
| 1337 |
|
| 1338 |
def test_extent(self): |
| 1339 |
"Testing `extent` method." |
| 1340 |
# The xmin, ymin, xmax, ymax of the MultiPoint should be returned. |
| 1341 |
mp = MultiPoint(Point(5, 23), Point(0, 0), Point(10, 50)) |
| 1342 |
self.assertEqual((0.0, 0.0, 10.0, 50.0), mp.extent) |
| 1343 |
pnt = Point(5.23, 17.8) |
| 1344 |
# Extent of points is just the point itself repeated. |
| 1345 |
self.assertEqual((5.23, 17.8, 5.23, 17.8), pnt.extent) |
| 1346 |
# Testing on the 'real world' Polygon. |
| 1347 |
poly = fromstr(self.geometries.polygons[3].wkt) |
| 1348 |
ring = poly.shell |
| 1349 |
x, y = ring.x, ring.y |
| 1350 |
xmin, ymin = min(x), min(y) |
| 1351 |
xmax, ymax = max(x), max(y) |
| 1352 |
self.assertEqual((xmin, ymin, xmax, ymax), poly.extent) |
| 1353 |
|
| 1354 |
def test_pickle(self): |
| 1355 |
"Testing pickling and unpickling support." |
| 1356 |
|
| 1357 |
# Creating a list of test geometries for pickling, |
| 1358 |
# and setting the SRID on some of them. |
| 1359 |
def get_geoms(lst, srid=None): |
| 1360 |
return [GEOSGeometry(tg.wkt, srid) for tg in lst] |
| 1361 |
|
| 1362 |
tgeoms = get_geoms(self.geometries.points) |
| 1363 |
tgeoms.extend(get_geoms(self.geometries.multilinestrings, 4326)) |
| 1364 |
tgeoms.extend(get_geoms(self.geometries.polygons, 3084)) |
| 1365 |
tgeoms.extend(get_geoms(self.geometries.multipolygons, 3857)) |
| 1366 |
tgeoms.append(Point(srid=4326)) |
| 1367 |
tgeoms.append(Point()) |
| 1368 |
for geom in tgeoms: |
| 1369 |
s1 = pickle.dumps(geom) |
| 1370 |
g1 = pickle.loads(s1) |
| 1371 |
self.assertEqual(geom, g1) |
| 1372 |
self.assertEqual(geom.srid, g1.srid) |
| 1373 |
|
| 1374 |
def test_prepared(self): |
| 1375 |
"Testing PreparedGeometry support." |
| 1376 |
# Creating a simple multipolygon and getting a prepared version. |
| 1377 |
mpoly = GEOSGeometry( |
| 1378 |
"MULTIPOLYGON(((0 0,0 5,5 5,5 0,0 0)),((5 5,5 10,10 10,10 5,5 5)))" |
| 1379 |
) |
| 1380 |
prep = mpoly.prepared |
| 1381 |
|
| 1382 |
# A set of test points. |
| 1383 |
pnts = [Point(5, 5), Point(7.5, 7.5), Point(2.5, 7.5)] |
| 1384 |
for pnt in pnts: |
| 1385 |
# Results should be the same (but faster) |
| 1386 |
self.assertEqual(mpoly.contains(pnt), prep.contains(pnt)) |
| 1387 |
self.assertEqual(mpoly.intersects(pnt), prep.intersects(pnt)) |
| 1388 |
self.assertEqual(mpoly.covers(pnt), prep.covers(pnt)) |
| 1389 |
|
| 1390 |
self.assertTrue(prep.crosses(fromstr("LINESTRING(1 1, 15 15)"))) |
| 1391 |
self.assertTrue(prep.disjoint(Point(-5, -5))) |
| 1392 |
poly = Polygon(((-1, -1), (1, 1), (1, 0), (-1, -1))) |
| 1393 |
self.assertTrue(prep.overlaps(poly)) |
| 1394 |
poly = Polygon(((-5, 0), (-5, 5), (0, 5), (-5, 0))) |
| 1395 |
self.assertTrue(prep.touches(poly)) |
| 1396 |
poly = Polygon(((-1, -1), (-1, 11), (11, 11), (11, -1), (-1, -1))) |
| 1397 |
self.assertTrue(prep.within(poly)) |
| 1398 |
|
| 1399 |
# Original geometry deletion should not crash the prepared one (#21662) |
| 1400 |
del mpoly |
| 1401 |
self.assertTrue(prep.covers(Point(5, 5))) |
| 1402 |
|
| 1403 |
def test_line_merge(self): |
| 1404 |
"Testing line merge support" |
| 1405 |
ref_geoms = ( |
| 1406 |
fromstr("LINESTRING(1 1, 1 1, 3 3)"), |
| 1407 |
fromstr("MULTILINESTRING((1 1, 3 3), (3 3, 4 2))"), |
| 1408 |
) |
| 1409 |
ref_merged = ( |
| 1410 |
fromstr("LINESTRING(1 1, 3 3)"), |
| 1411 |
fromstr("LINESTRING (1 1, 3 3, 4 2)"), |
| 1412 |
) |
| 1413 |
for geom, merged in zip(ref_geoms, ref_merged): |
| 1414 |
self.assertEqual(merged, geom.merged) |
| 1415 |
|
| 1416 |
def test_valid_reason(self): |
| 1417 |
"Testing IsValidReason support" |
| 1418 |
|
| 1419 |
g = GEOSGeometry("POINT(0 0)") |
| 1420 |
self.assertTrue(g.valid) |
| 1421 |
self.assertIsInstance(g.valid_reason, str) |
| 1422 |
self.assertEqual(g.valid_reason, "Valid Geometry") |
| 1423 |
|
| 1424 |
g = GEOSGeometry("LINESTRING(0 0, 0 0)") |
| 1425 |
|
| 1426 |
self.assertFalse(g.valid) |
| 1427 |
self.assertIsInstance(g.valid_reason, str) |
| 1428 |
self.assertTrue( |
| 1429 |
g.valid_reason.startswith("Too few points in geometry component") |
| 1430 |
) |
| 1431 |
|
| 1432 |
def test_linearref(self): |
| 1433 |
"Testing linear referencing" |
| 1434 |
|
| 1435 |
ls = fromstr("LINESTRING(0 0, 0 10, 10 10, 10 0)") |
| 1436 |
mls = fromstr("MULTILINESTRING((0 0, 0 10), (10 0, 10 10))") |
| 1437 |
|
| 1438 |
self.assertEqual(ls.project(Point(0, 20)), 10.0) |
| 1439 |
self.assertEqual(ls.project(Point(7, 6)), 24) |
| 1440 |
self.assertEqual(ls.project_normalized(Point(0, 20)), 1.0 / 3) |
| 1441 |
|
| 1442 |
self.assertEqual(ls.interpolate(10), Point(0, 10)) |
| 1443 |
self.assertEqual(ls.interpolate(24), Point(10, 6)) |
| 1444 |
self.assertEqual(ls.interpolate_normalized(1.0 / 3), Point(0, 10)) |
| 1445 |
|
| 1446 |
self.assertEqual(mls.project(Point(0, 20)), 10) |
| 1447 |
self.assertEqual(mls.project(Point(7, 6)), 16) |
| 1448 |
|
| 1449 |
self.assertEqual(mls.interpolate(9), Point(0, 9)) |
| 1450 |
self.assertEqual(mls.interpolate(17), Point(10, 7)) |
| 1451 |
|
| 1452 |
def test_deconstructible(self): |
| 1453 |
""" |
| 1454 |
Geometry classes should be deconstructible. |
| 1455 |
""" |
| 1456 |
point = Point(4.337844, 50.827537, srid=4326) |
| 1457 |
path, args, kwargs = point.deconstruct() |
| 1458 |
self.assertEqual(path, "django.contrib.gis.geos.point.Point") |
| 1459 |
self.assertEqual(args, (4.337844, 50.827537)) |
| 1460 |
self.assertEqual(kwargs, {"srid": 4326}) |
| 1461 |
|
| 1462 |
ls = LineString(((0, 0), (1, 1))) |
| 1463 |
path, args, kwargs = ls.deconstruct() |
| 1464 |
self.assertEqual(path, "django.contrib.gis.geos.linestring.LineString") |
| 1465 |
self.assertEqual(args, (((0, 0), (1, 1)),)) |
| 1466 |
self.assertEqual(kwargs, {}) |
| 1467 |
|
| 1468 |
ls2 = LineString([Point(0, 0), Point(1, 1)], srid=4326) |
| 1469 |
path, args, kwargs = ls2.deconstruct() |
| 1470 |
self.assertEqual(path, "django.contrib.gis.geos.linestring.LineString") |
| 1471 |
self.assertEqual(args, ([Point(0, 0), Point(1, 1)],)) |
| 1472 |
self.assertEqual(kwargs, {"srid": 4326}) |
| 1473 |
|
| 1474 |
ext_coords = ((0, 0), (0, 1), (1, 1), (1, 0), (0, 0)) |
| 1475 |
int_coords = ((0.4, 0.4), (0.4, 0.6), (0.6, 0.6), (0.6, 0.4), (0.4, 0.4)) |
| 1476 |
poly = Polygon(ext_coords, int_coords) |
| 1477 |
path, args, kwargs = poly.deconstruct() |
| 1478 |
self.assertEqual(path, "django.contrib.gis.geos.polygon.Polygon") |
| 1479 |
self.assertEqual(args, (ext_coords, int_coords)) |
| 1480 |
self.assertEqual(kwargs, {}) |
| 1481 |
|
| 1482 |
lr = LinearRing((0, 0), (0, 1), (1, 1), (0, 0)) |
| 1483 |
path, args, kwargs = lr.deconstruct() |
| 1484 |
self.assertEqual(path, "django.contrib.gis.geos.linestring.LinearRing") |
| 1485 |
self.assertEqual(args, ((0, 0), (0, 1), (1, 1), (0, 0))) |
| 1486 |
self.assertEqual(kwargs, {}) |
| 1487 |
|
| 1488 |
mp = MultiPoint(Point(0, 0), Point(1, 1)) |
| 1489 |
path, args, kwargs = mp.deconstruct() |
| 1490 |
self.assertEqual(path, "django.contrib.gis.geos.collections.MultiPoint") |
| 1491 |
self.assertEqual(args, (Point(0, 0), Point(1, 1))) |
| 1492 |
self.assertEqual(kwargs, {}) |
| 1493 |
|
| 1494 |
ls1 = LineString((0, 0), (1, 1)) |
| 1495 |
ls2 = LineString((2, 2), (3, 3)) |
| 1496 |
mls = MultiLineString(ls1, ls2) |
| 1497 |
path, args, kwargs = mls.deconstruct() |
| 1498 |
self.assertEqual(path, "django.contrib.gis.geos.collections.MultiLineString") |
| 1499 |
self.assertEqual(args, (ls1, ls2)) |
| 1500 |
self.assertEqual(kwargs, {}) |
| 1501 |
|
| 1502 |
p1 = Polygon(((0, 0), (0, 1), (1, 1), (0, 0))) |
| 1503 |
p2 = Polygon(((1, 1), (1, 2), (2, 2), (1, 1))) |
| 1504 |
mp = MultiPolygon(p1, p2) |
| 1505 |
path, args, kwargs = mp.deconstruct() |
| 1506 |
self.assertEqual(path, "django.contrib.gis.geos.collections.MultiPolygon") |
| 1507 |
self.assertEqual(args, (p1, p2)) |
| 1508 |
self.assertEqual(kwargs, {}) |
| 1509 |
|
| 1510 |
poly = Polygon(((0, 0), (0, 1), (1, 1), (0, 0))) |
| 1511 |
gc = GeometryCollection(Point(0, 0), MultiPoint(Point(0, 0), Point(1, 1)), poly) |
| 1512 |
path, args, kwargs = gc.deconstruct() |
| 1513 |
self.assertEqual(path, "django.contrib.gis.geos.collections.GeometryCollection") |
| 1514 |
self.assertEqual( |
| 1515 |
args, (Point(0, 0), MultiPoint(Point(0, 0), Point(1, 1)), poly) |
| 1516 |
) |
| 1517 |
self.assertEqual(kwargs, {}) |
| 1518 |
|
| 1519 |
def test_subclassing(self): |
| 1520 |
""" |
| 1521 |
GEOSGeometry subclass may itself be subclassed without being forced-cast |
| 1522 |
to the parent class during `__init__`. |
| 1523 |
""" |
| 1524 |
|
| 1525 |
class ExtendedPolygon(Polygon): |
| 1526 |
def __init__(self, *args, data=0, **kwargs): |
| 1527 |
super().__init__(*args, **kwargs) |
| 1528 |
self._data = data |
| 1529 |
|
| 1530 |
def __str__(self): |
| 1531 |
return "EXT_POLYGON - data: %d - %s" % (self._data, self.wkt) |
| 1532 |
|
| 1533 |
ext_poly = ExtendedPolygon(((0, 0), (0, 1), (1, 1), (0, 0)), data=3) |
| 1534 |
self.assertEqual(type(ext_poly), ExtendedPolygon) |
| 1535 |
# ExtendedPolygon.__str__ should be called (instead of Polygon.__str__). |
| 1536 |
self.assertEqual( |
| 1537 |
str(ext_poly), "EXT_POLYGON - data: 3 - POLYGON ((0 0, 0 1, 1 1, 0 0))" |
| 1538 |
) |
| 1539 |
self.assertJSONEqual( |
| 1540 |
ext_poly.json, |
| 1541 |
'{"coordinates": [[[0, 0], [0, 1], [1, 1], [0, 0]]], "type": "Polygon"}', |
| 1542 |
) |
| 1543 |
|
| 1544 |
def test_geos_version_tuple(self): |
| 1545 |
versions = ( |
| 1546 |
(b"3.0.0rc4-CAPI-1.3.3", (3, 0, 0)), |
| 1547 |
(b"3.0.0-CAPI-1.4.1", (3, 0, 0)), |
| 1548 |
(b"3.4.0dev-CAPI-1.8.0", (3, 4, 0)), |
| 1549 |
(b"3.4.0dev-CAPI-1.8.0 r0", (3, 4, 0)), |
| 1550 |
(b"3.6.2-CAPI-1.10.2 4d2925d6", (3, 6, 2)), |
| 1551 |
) |
| 1552 |
for version_string, version_tuple in versions: |
| 1553 |
with self.subTest(version_string=version_string): |
| 1554 |
with mock.patch( |
| 1555 |
"django.contrib.gis.geos.libgeos.geos_version", |
| 1556 |
lambda: version_string, |
| 1557 |
): |
| 1558 |
self.assertEqual(geos_version_tuple(), version_tuple) |
| 1559 |
|
| 1560 |
def test_from_gml(self): |
| 1561 |
self.assertEqual( |
| 1562 |
GEOSGeometry("POINT(0 0)"), |
| 1563 |
GEOSGeometry.from_gml( |
| 1564 |
'<gml:Point gml:id="p21" ' |
| 1565 |
'srsName="http://www.opengis.net/def/crs/EPSG/0/4326">' |
| 1566 |
' <gml:pos srsDimension="2">0 0</gml:pos>' |
| 1567 |
"</gml:Point>" |
| 1568 |
), |
| 1569 |
) |
| 1570 |
|
| 1571 |
def test_from_ewkt(self): |
| 1572 |
self.assertEqual( |
| 1573 |
GEOSGeometry.from_ewkt("SRID=1;POINT(1 1)"), Point(1, 1, srid=1) |
| 1574 |
) |
| 1575 |
self.assertEqual(GEOSGeometry.from_ewkt("POINT(1 1)"), Point(1, 1)) |
| 1576 |
|
| 1577 |
def test_from_ewkt_empty_string(self): |
| 1578 |
msg = "Expected WKT but got an empty string." |
| 1579 |
with self.assertRaisesMessage(ValueError, msg): |
| 1580 |
GEOSGeometry.from_ewkt("") |
| 1581 |
with self.assertRaisesMessage(ValueError, msg): |
| 1582 |
GEOSGeometry.from_ewkt("SRID=1;") |
| 1583 |
|
| 1584 |
def test_from_ewkt_invalid_srid(self): |
| 1585 |
msg = "EWKT has invalid SRID part." |
| 1586 |
with self.assertRaisesMessage(ValueError, msg): |
| 1587 |
GEOSGeometry.from_ewkt("SRUD=1;POINT(1 1)") |
| 1588 |
with self.assertRaisesMessage(ValueError, msg): |
| 1589 |
GEOSGeometry.from_ewkt("SRID=WGS84;POINT(1 1)") |
| 1590 |
|
| 1591 |
def test_fromstr_scientific_wkt(self): |
| 1592 |
self.assertEqual(GEOSGeometry("POINT(1.0e-1 1.0e+1)"), Point(0.1, 10)) |
| 1593 |
|
| 1594 |
def test_normalize(self): |
| 1595 |
multipoint = MultiPoint(Point(0, 0), Point(2, 2), Point(1, 1)) |
| 1596 |
normalized = MultiPoint(Point(2, 2), Point(1, 1), Point(0, 0)) |
| 1597 |
# Geometry is normalized in-place and nothing is returned. |
| 1598 |
multipoint_1 = multipoint.clone() |
| 1599 |
self.assertIsNone(multipoint_1.normalize()) |
| 1600 |
self.assertEqual(multipoint_1, normalized) |
| 1601 |
# If the `clone` keyword is set, then the geometry is not modified and |
| 1602 |
# a normalized clone of the geometry is returned instead. |
| 1603 |
multipoint_2 = multipoint.normalize(clone=True) |
| 1604 |
self.assertEqual(multipoint_2, normalized) |
| 1605 |
self.assertNotEqual(multipoint, normalized) |
| 1606 |
|
| 1607 |
def test_make_valid(self): |
| 1608 |
poly = GEOSGeometry("POLYGON((0 0, 0 23, 23 0, 23 23, 0 0))") |
| 1609 |
self.assertIs(poly.valid, False) |
| 1610 |
valid_poly = poly.make_valid() |
| 1611 |
self.assertIs(valid_poly.valid, True) |
| 1612 |
self.assertNotEqual(valid_poly, poly) |
| 1613 |
|
| 1614 |
valid_poly2 = valid_poly.make_valid() |
| 1615 |
self.assertIs(valid_poly2.valid, True) |
| 1616 |
self.assertEqual(valid_poly, valid_poly2) |
| 1617 |
|
| 1618 |
def test_empty_point(self): |
| 1619 |
p = Point(srid=4326) |
| 1620 |
self.assertEqual(p.ogr.ewkt, p.ewkt) |
| 1621 |
|
| 1622 |
self.assertEqual(p.transform(2774, clone=True), Point(srid=2774)) |
| 1623 |
p.transform(2774) |
| 1624 |
self.assertEqual(p, Point(srid=2774)) |
| 1625 |
|
| 1626 |
def test_linestring_iter(self): |
| 1627 |
ls = LineString((0, 0), (1, 1)) |
| 1628 |
it = iter(ls) |
| 1629 |
# Step into CoordSeq iterator. |
| 1630 |
next(it) |
| 1631 |
ls[:] = [] |
| 1632 |
with self.assertRaises(IndexError): |
| 1633 |
next(it) |