2011-07-13 17:35:51 +08:00
|
|
|
import ctypes
|
2018-01-10 00:15:04 +08:00
|
|
|
import itertools
|
2012-09-13 22:17:32 +08:00
|
|
|
import json
|
2017-01-07 19:11:46 +08:00
|
|
|
import pickle
|
2011-07-13 17:35:51 +08:00
|
|
|
import random
|
2017-11-23 03:25:32 +08:00
|
|
|
from binascii import a2b_hex
|
2012-09-24 01:59:27 +08:00
|
|
|
from io import BytesIO
|
2017-05-04 23:03:23 +08:00
|
|
|
from unittest import mock
|
2012-05-28 17:15:31 +08:00
|
|
|
|
2015-04-24 23:24:07 +08:00
|
|
|
from django.contrib.gis import gdal
|
|
|
|
from django.contrib.gis.geos import (
|
2017-05-04 23:03:23 +08:00
|
|
|
GeometryCollection, GEOSException, GEOSGeometry, LinearRing, LineString,
|
|
|
|
MultiLineString, MultiPoint, MultiPolygon, Point, Polygon, fromfile,
|
|
|
|
fromstr,
|
2015-04-24 23:24:07 +08:00
|
|
|
)
|
2017-02-16 00:14:21 +08:00
|
|
|
from django.contrib.gis.geos.libgeos import geos_version_tuple
|
2015-02-09 18:14:48 +08:00
|
|
|
from django.contrib.gis.shortcuts import numpy
|
2015-08-12 20:42:01 +08:00
|
|
|
from django.template import Context
|
|
|
|
from django.template.engine import Engine
|
2017-01-20 01:16:04 +08:00
|
|
|
from django.test import SimpleTestCase
|
2013-05-11 11:08:45 +08:00
|
|
|
|
2015-02-10 23:07:44 +08:00
|
|
|
from ..test_data import TestDataMixin
|
2013-05-11 11:08:45 +08:00
|
|
|
|
2012-05-28 17:15:31 +08:00
|
|
|
|
2015-11-05 16:25:44 +08:00
|
|
|
class GEOSTest(SimpleTestCase, TestDataMixin):
|
2008-08-06 02:13:06 +08:00
|
|
|
|
2012-05-28 17:15:31 +08:00
|
|
|
def test_wkt(self):
|
2008-08-06 02:13:06 +08:00
|
|
|
"Testing WKT output."
|
2010-11-02 05:56:48 +08:00
|
|
|
for g in self.geometries.wkt_out:
|
2008-08-06 02:13:06 +08:00
|
|
|
geom = fromstr(g.wkt)
|
2015-11-01 04:28:46 +08:00
|
|
|
if geom.hasz:
|
2012-09-22 02:22:54 +08:00
|
|
|
self.assertEqual(g.ewkt, geom.wkt)
|
2008-08-06 02:13:06 +08:00
|
|
|
|
2012-05-28 17:15:31 +08:00
|
|
|
def test_hex(self):
|
2008-08-06 02:13:06 +08:00
|
|
|
"Testing HEX output."
|
2010-11-02 05:56:48 +08:00
|
|
|
for g in self.geometries.hex_wkt:
|
2008-08-06 02:13:06 +08:00
|
|
|
geom = fromstr(g.wkt)
|
2012-09-24 01:59:27 +08:00
|
|
|
self.assertEqual(g.hex, geom.hex.decode())
|
2008-08-06 02:13:06 +08:00
|
|
|
|
2012-05-28 17:15:31 +08:00
|
|
|
def test_hexewkb(self):
|
2009-11-10 11:35:25 +08:00
|
|
|
"Testing (HEX)EWKB output."
|
2010-11-02 05:56:48 +08:00
|
|
|
# For testing HEX(EWKB).
|
2012-09-24 01:59:27 +08:00
|
|
|
ogc_hex = b'01010000000000000000000000000000000000F03F'
|
2012-09-22 17:55:37 +08:00
|
|
|
ogc_hex_3d = b'01010000800000000000000000000000000000F03F0000000000000040'
|
2010-11-02 05:56:48 +08:00
|
|
|
# `SELECT ST_AsHEXEWKB(ST_GeomFromText('POINT(0 1)', 4326));`
|
2012-09-24 01:59:27 +08:00
|
|
|
hexewkb_2d = b'0101000020E61000000000000000000000000000000000F03F'
|
2010-11-02 05:56:48 +08:00
|
|
|
# `SELECT ST_AsHEXEWKB(ST_GeomFromEWKT('SRID=4326;POINT(0 1 2)'));`
|
2012-09-24 01:59:27 +08:00
|
|
|
hexewkb_3d = b'01010000A0E61000000000000000000000000000000000F03F0000000000000040'
|
2010-11-02 05:56:48 +08:00
|
|
|
|
2009-11-10 11:35:25 +08:00
|
|
|
pnt_2d = Point(0, 1, srid=4326)
|
|
|
|
pnt_3d = Point(0, 1, 2, srid=4326)
|
2010-11-02 05:56:48 +08:00
|
|
|
|
2012-09-22 17:55:37 +08:00
|
|
|
# OGC-compliant HEX will not have SRID value.
|
2009-11-10 11:35:25 +08:00
|
|
|
self.assertEqual(ogc_hex, pnt_2d.hex)
|
2012-09-22 17:55:37 +08:00
|
|
|
self.assertEqual(ogc_hex_3d, pnt_3d.hex)
|
2009-11-10 11:35:25 +08:00
|
|
|
|
|
|
|
# HEXEWKB should be appropriate for its dimension -- have to use an
|
|
|
|
# a WKBWriter w/dimension set accordingly, else GEOS will insert
|
2014-03-28 05:53:04 +08:00
|
|
|
# garbage into 3D coordinate if there is none.
|
2009-11-10 11:35:25 +08:00
|
|
|
self.assertEqual(hexewkb_2d, pnt_2d.hexewkb)
|
2013-12-24 22:57:13 +08:00
|
|
|
self.assertEqual(hexewkb_3d, pnt_3d.hexewkb)
|
2016-06-17 02:19:18 +08:00
|
|
|
self.assertIs(GEOSGeometry(hexewkb_3d).hasz, True)
|
2009-11-10 11:35:25 +08:00
|
|
|
|
|
|
|
# Same for EWKB.
|
2016-12-29 23:27:49 +08:00
|
|
|
self.assertEqual(memoryview(a2b_hex(hexewkb_2d)), pnt_2d.ewkb)
|
|
|
|
self.assertEqual(memoryview(a2b_hex(hexewkb_3d)), pnt_3d.ewkb)
|
2010-11-02 05:56:48 +08:00
|
|
|
|
2009-11-10 11:35:25 +08:00
|
|
|
# Redundant sanity check.
|
|
|
|
self.assertEqual(4326, GEOSGeometry(hexewkb_2d).srid)
|
|
|
|
|
2012-05-28 17:15:31 +08:00
|
|
|
def test_kml(self):
|
2008-08-06 02:13:06 +08:00
|
|
|
"Testing KML output."
|
2010-11-02 05:56:48 +08:00
|
|
|
for tg in self.geometries.wkt_out:
|
2008-08-06 02:13:06 +08:00
|
|
|
geom = fromstr(tg.wkt)
|
|
|
|
kml = getattr(tg, 'kml', False)
|
2013-10-17 16:17:41 +08:00
|
|
|
if kml:
|
|
|
|
self.assertEqual(kml, geom.kml)
|
2008-08-06 02:13:06 +08:00
|
|
|
|
2012-05-28 17:15:31 +08:00
|
|
|
def test_errors(self):
|
2008-08-06 02:13:06 +08:00
|
|
|
"Testing the Error handlers."
|
|
|
|
# string-based
|
2010-11-02 05:56:48 +08:00
|
|
|
for err in self.geometries.errors:
|
2012-10-05 04:41:03 +08:00
|
|
|
with self.assertRaises((GEOSException, ValueError)):
|
2013-08-05 00:17:10 +08:00
|
|
|
fromstr(err.wkt)
|
Refactored the GEOS interface. Improvements include:
* Geometries now allow list-like manipulation, e.g., can add, insert, delete vertexes (or other geometries in collections) like Python lists. Thanks, Aryeh Leib Taurog.
* Added support for GEOS prepared geometries via `prepared` property. Prepared geometries significantly speed up certain operations.
* Added support for GEOS cascaded union as `MultiPolygon.cascaded_union` property.
* Added support for GEOS line merge as `merged` property on `LineString`, and `MultiLineString` geometries. Thanks, Paul Smith.
* No longer use the deprecated C API for serialization to/from WKB and WKT. Now use the GEOS I/O classes, which are now exposed as `WKTReader`, `WKTWriter`, `WKBReader`, and `WKBWriter` (which supports 3D and SRID inclusion)
* Moved each type of geometry to their own module, eliminating the cluttered `geometries.py`.
* Internally, all C API methods are explicitly called from a module rather than a star import.
Fixed #9557, #9877, #10222
git-svn-id: http://code.djangoproject.com/svn/django/trunk@10131 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2009-03-24 08:12:21 +08:00
|
|
|
|
|
|
|
# Bad WKB
|
2016-01-17 19:26:39 +08:00
|
|
|
with self.assertRaises(GEOSException):
|
2016-12-29 23:27:49 +08:00
|
|
|
GEOSGeometry(memoryview(b'0'))
|
Refactored the GEOS interface. Improvements include:
* Geometries now allow list-like manipulation, e.g., can add, insert, delete vertexes (or other geometries in collections) like Python lists. Thanks, Aryeh Leib Taurog.
* Added support for GEOS prepared geometries via `prepared` property. Prepared geometries significantly speed up certain operations.
* Added support for GEOS cascaded union as `MultiPolygon.cascaded_union` property.
* Added support for GEOS line merge as `merged` property on `LineString`, and `MultiLineString` geometries. Thanks, Paul Smith.
* No longer use the deprecated C API for serialization to/from WKB and WKT. Now use the GEOS I/O classes, which are now exposed as `WKTReader`, `WKTWriter`, `WKBReader`, and `WKBWriter` (which supports 3D and SRID inclusion)
* Moved each type of geometry to their own module, eliminating the cluttered `geometries.py`.
* Internally, all C API methods are explicitly called from a module rather than a star import.
Fixed #9557, #9877, #10222
git-svn-id: http://code.djangoproject.com/svn/django/trunk@10131 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2009-03-24 08:12:21 +08:00
|
|
|
|
2017-01-19 15:39:46 +08:00
|
|
|
class NotAGeometry:
|
2008-08-06 02:13:06 +08:00
|
|
|
pass
|
Refactored the GEOS interface. Improvements include:
* Geometries now allow list-like manipulation, e.g., can add, insert, delete vertexes (or other geometries in collections) like Python lists. Thanks, Aryeh Leib Taurog.
* Added support for GEOS prepared geometries via `prepared` property. Prepared geometries significantly speed up certain operations.
* Added support for GEOS cascaded union as `MultiPolygon.cascaded_union` property.
* Added support for GEOS line merge as `merged` property on `LineString`, and `MultiLineString` geometries. Thanks, Paul Smith.
* No longer use the deprecated C API for serialization to/from WKB and WKT. Now use the GEOS I/O classes, which are now exposed as `WKTReader`, `WKTWriter`, `WKBReader`, and `WKBWriter` (which supports 3D and SRID inclusion)
* Moved each type of geometry to their own module, eliminating the cluttered `geometries.py`.
* Internally, all C API methods are explicitly called from a module rather than a star import.
Fixed #9557, #9877, #10222
git-svn-id: http://code.djangoproject.com/svn/django/trunk@10131 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2009-03-24 08:12:21 +08:00
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
# Some other object
|
2016-01-17 19:26:39 +08:00
|
|
|
with self.assertRaises(TypeError):
|
|
|
|
GEOSGeometry(NotAGeometry())
|
2008-08-06 02:13:06 +08:00
|
|
|
# None
|
2016-01-17 19:26:39 +08:00
|
|
|
with self.assertRaises(TypeError):
|
|
|
|
GEOSGeometry(None)
|
Refactored the GEOS interface. Improvements include:
* Geometries now allow list-like manipulation, e.g., can add, insert, delete vertexes (or other geometries in collections) like Python lists. Thanks, Aryeh Leib Taurog.
* Added support for GEOS prepared geometries via `prepared` property. Prepared geometries significantly speed up certain operations.
* Added support for GEOS cascaded union as `MultiPolygon.cascaded_union` property.
* Added support for GEOS line merge as `merged` property on `LineString`, and `MultiLineString` geometries. Thanks, Paul Smith.
* No longer use the deprecated C API for serialization to/from WKB and WKT. Now use the GEOS I/O classes, which are now exposed as `WKTReader`, `WKTWriter`, `WKBReader`, and `WKBWriter` (which supports 3D and SRID inclusion)
* Moved each type of geometry to their own module, eliminating the cluttered `geometries.py`.
* Internally, all C API methods are explicitly called from a module rather than a star import.
Fixed #9557, #9877, #10222
git-svn-id: http://code.djangoproject.com/svn/django/trunk@10131 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2009-03-24 08:12:21 +08:00
|
|
|
|
2012-05-28 17:15:31 +08:00
|
|
|
def test_wkb(self):
|
2008-08-06 02:13:06 +08:00
|
|
|
"Testing WKB output."
|
2010-11-02 05:56:48 +08:00
|
|
|
for g in self.geometries.hex_wkt:
|
2008-08-06 02:13:06 +08:00
|
|
|
geom = fromstr(g.wkt)
|
|
|
|
wkb = geom.wkb
|
2017-11-23 03:25:32 +08:00
|
|
|
self.assertEqual(wkb.hex().upper(), g.hex)
|
2008-08-06 02:13:06 +08:00
|
|
|
|
2012-05-28 17:15:31 +08:00
|
|
|
def test_create_hex(self):
|
2008-08-06 02:13:06 +08:00
|
|
|
"Testing creation from HEX."
|
2010-11-02 05:56:48 +08:00
|
|
|
for g in self.geometries.hex_wkt:
|
2008-08-06 02:13:06 +08:00
|
|
|
geom_h = GEOSGeometry(g.hex)
|
2014-03-02 22:25:53 +08:00
|
|
|
# we need to do this so decimal places get normalized
|
2008-08-06 02:13:06 +08:00
|
|
|
geom_t = fromstr(g.wkt)
|
|
|
|
self.assertEqual(geom_t.wkt, geom_h.wkt)
|
|
|
|
|
2012-05-28 17:15:31 +08:00
|
|
|
def test_create_wkb(self):
|
2008-08-06 02:13:06 +08:00
|
|
|
"Testing creation from WKB."
|
2010-11-02 05:56:48 +08:00
|
|
|
for g in self.geometries.hex_wkt:
|
2017-11-23 03:25:32 +08:00
|
|
|
wkb = memoryview(bytes.fromhex(g.hex))
|
2008-08-06 02:13:06 +08:00
|
|
|
geom_h = GEOSGeometry(wkb)
|
2014-03-02 22:25:53 +08:00
|
|
|
# we need to do this so decimal places get normalized
|
2008-08-06 02:13:06 +08:00
|
|
|
geom_t = fromstr(g.wkt)
|
|
|
|
self.assertEqual(geom_t.wkt, geom_h.wkt)
|
|
|
|
|
2012-05-28 17:15:31 +08:00
|
|
|
def test_ewkt(self):
|
2008-08-06 02:13:06 +08:00
|
|
|
"Testing EWKT."
|
2011-09-17 05:19:30 +08:00
|
|
|
srids = (-1, 32140)
|
|
|
|
for srid in srids:
|
|
|
|
for p in self.geometries.polygons:
|
|
|
|
ewkt = 'SRID=%d;%s' % (srid, p.wkt)
|
|
|
|
poly = fromstr(ewkt)
|
|
|
|
self.assertEqual(srid, poly.srid)
|
|
|
|
self.assertEqual(srid, poly.shell.srid)
|
2013-11-03 05:02:56 +08:00
|
|
|
self.assertEqual(srid, fromstr(poly.ewkt).srid) # Checking export
|
Refactored the GEOS interface. Improvements include:
* Geometries now allow list-like manipulation, e.g., can add, insert, delete vertexes (or other geometries in collections) like Python lists. Thanks, Aryeh Leib Taurog.
* Added support for GEOS prepared geometries via `prepared` property. Prepared geometries significantly speed up certain operations.
* Added support for GEOS cascaded union as `MultiPolygon.cascaded_union` property.
* Added support for GEOS line merge as `merged` property on `LineString`, and `MultiLineString` geometries. Thanks, Paul Smith.
* No longer use the deprecated C API for serialization to/from WKB and WKT. Now use the GEOS I/O classes, which are now exposed as `WKTReader`, `WKTWriter`, `WKBReader`, and `WKBWriter` (which supports 3D and SRID inclusion)
* Moved each type of geometry to their own module, eliminating the cluttered `geometries.py`.
* Internally, all C API methods are explicitly called from a module rather than a star import.
Fixed #9557, #9877, #10222
git-svn-id: http://code.djangoproject.com/svn/django/trunk@10131 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2009-03-24 08:12:21 +08:00
|
|
|
|
2012-05-28 17:15:31 +08:00
|
|
|
def test_json(self):
|
2008-08-06 02:13:06 +08:00
|
|
|
"Testing GeoJSON input/output (via GDAL)."
|
2010-11-02 05:56:48 +08:00
|
|
|
for g in self.geometries.json_geoms:
|
2008-08-06 02:13:06 +08:00
|
|
|
geom = GEOSGeometry(g.wkt)
|
2008-11-12 01:21:43 +08:00
|
|
|
if not hasattr(g, 'not_equal'):
|
2012-09-13 22:17:32 +08:00
|
|
|
# Loading jsons to prevent decimal differences
|
|
|
|
self.assertEqual(json.loads(g.json), json.loads(geom.json))
|
|
|
|
self.assertEqual(json.loads(g.json), json.loads(geom.geojson))
|
2017-03-30 23:17:13 +08:00
|
|
|
self.assertEqual(GEOSGeometry(g.wkt, 4326), GEOSGeometry(geom.json))
|
|
|
|
|
|
|
|
def test_json_srid(self):
|
|
|
|
geojson_data = {
|
|
|
|
"type": "Point",
|
|
|
|
"coordinates": [2, 49],
|
|
|
|
"crs": {
|
|
|
|
"type": "name",
|
|
|
|
"properties": {
|
|
|
|
"name": "urn:ogc:def:crs:EPSG::4322"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.assertEqual(GEOSGeometry(json.dumps(geojson_data)), Point(2, 49, srid=4322))
|
2008-08-06 02:13:06 +08:00
|
|
|
|
2012-05-28 17:15:31 +08:00
|
|
|
def test_fromfile(self):
|
Refactored the GEOS interface. Improvements include:
* Geometries now allow list-like manipulation, e.g., can add, insert, delete vertexes (or other geometries in collections) like Python lists. Thanks, Aryeh Leib Taurog.
* Added support for GEOS prepared geometries via `prepared` property. Prepared geometries significantly speed up certain operations.
* Added support for GEOS cascaded union as `MultiPolygon.cascaded_union` property.
* Added support for GEOS line merge as `merged` property on `LineString`, and `MultiLineString` geometries. Thanks, Paul Smith.
* No longer use the deprecated C API for serialization to/from WKB and WKT. Now use the GEOS I/O classes, which are now exposed as `WKTReader`, `WKTWriter`, `WKBReader`, and `WKBWriter` (which supports 3D and SRID inclusion)
* Moved each type of geometry to their own module, eliminating the cluttered `geometries.py`.
* Internally, all C API methods are explicitly called from a module rather than a star import.
Fixed #9557, #9877, #10222
git-svn-id: http://code.djangoproject.com/svn/django/trunk@10131 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2009-03-24 08:12:21 +08:00
|
|
|
"Testing the fromfile() factory."
|
|
|
|
ref_pnt = GEOSGeometry('POINT(5 23)')
|
|
|
|
|
2012-05-06 01:47:03 +08:00
|
|
|
wkt_f = BytesIO()
|
2018-02-08 03:20:04 +08:00
|
|
|
wkt_f.write(ref_pnt.wkt.encode())
|
2012-05-06 01:47:03 +08:00
|
|
|
wkb_f = BytesIO()
|
2012-09-24 01:59:27 +08:00
|
|
|
wkb_f.write(bytes(ref_pnt.wkb))
|
Refactored the GEOS interface. Improvements include:
* Geometries now allow list-like manipulation, e.g., can add, insert, delete vertexes (or other geometries in collections) like Python lists. Thanks, Aryeh Leib Taurog.
* Added support for GEOS prepared geometries via `prepared` property. Prepared geometries significantly speed up certain operations.
* Added support for GEOS cascaded union as `MultiPolygon.cascaded_union` property.
* Added support for GEOS line merge as `merged` property on `LineString`, and `MultiLineString` geometries. Thanks, Paul Smith.
* No longer use the deprecated C API for serialization to/from WKB and WKT. Now use the GEOS I/O classes, which are now exposed as `WKTReader`, `WKTWriter`, `WKBReader`, and `WKBWriter` (which supports 3D and SRID inclusion)
* Moved each type of geometry to their own module, eliminating the cluttered `geometries.py`.
* Internally, all C API methods are explicitly called from a module rather than a star import.
Fixed #9557, #9877, #10222
git-svn-id: http://code.djangoproject.com/svn/django/trunk@10131 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2009-03-24 08:12:21 +08:00
|
|
|
|
|
|
|
# Other tests use `fromfile()` on string filenames so those
|
|
|
|
# aren't tested here.
|
|
|
|
for fh in (wkt_f, wkb_f):
|
|
|
|
fh.seek(0)
|
|
|
|
pnt = fromfile(fh)
|
|
|
|
self.assertEqual(ref_pnt, pnt)
|
|
|
|
|
2012-05-28 17:15:31 +08:00
|
|
|
def test_eq(self):
|
2008-08-06 02:13:06 +08:00
|
|
|
"Testing equivalence."
|
|
|
|
p = fromstr('POINT(5 23)')
|
|
|
|
self.assertEqual(p, p.wkt)
|
|
|
|
self.assertNotEqual(p, 'foo')
|
|
|
|
ls = fromstr('LINESTRING(0 0, 1 1, 5 5)')
|
|
|
|
self.assertEqual(ls, ls.wkt)
|
|
|
|
self.assertNotEqual(p, 'bar')
|
2017-07-25 17:11:43 +08:00
|
|
|
self.assertEqual(p, 'POINT(5.0 23.0)')
|
Refactored the GEOS interface. Improvements include:
* Geometries now allow list-like manipulation, e.g., can add, insert, delete vertexes (or other geometries in collections) like Python lists. Thanks, Aryeh Leib Taurog.
* Added support for GEOS prepared geometries via `prepared` property. Prepared geometries significantly speed up certain operations.
* Added support for GEOS cascaded union as `MultiPolygon.cascaded_union` property.
* Added support for GEOS line merge as `merged` property on `LineString`, and `MultiLineString` geometries. Thanks, Paul Smith.
* No longer use the deprecated C API for serialization to/from WKB and WKT. Now use the GEOS I/O classes, which are now exposed as `WKTReader`, `WKTWriter`, `WKBReader`, and `WKBWriter` (which supports 3D and SRID inclusion)
* Moved each type of geometry to their own module, eliminating the cluttered `geometries.py`.
* Internally, all C API methods are explicitly called from a module rather than a star import.
Fixed #9557, #9877, #10222
git-svn-id: http://code.djangoproject.com/svn/django/trunk@10131 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2009-03-24 08:12:21 +08:00
|
|
|
# Error shouldn't be raise on equivalence testing with
|
2008-08-06 02:13:06 +08:00
|
|
|
# an invalid type.
|
|
|
|
for g in (p, ls):
|
|
|
|
self.assertNotEqual(g, None)
|
2013-10-27 09:27:42 +08:00
|
|
|
self.assertNotEqual(g, {'foo': 'bar'})
|
2008-08-06 02:13:06 +08:00
|
|
|
self.assertNotEqual(g, False)
|
|
|
|
|
2018-01-27 18:12:11 +08:00
|
|
|
def test_hash(self):
|
|
|
|
point_1 = Point(5, 23)
|
|
|
|
point_2 = Point(5, 23, srid=4326)
|
|
|
|
point_3 = Point(5, 23, srid=32632)
|
|
|
|
multipoint_1 = MultiPoint(point_1, srid=4326)
|
|
|
|
multipoint_2 = MultiPoint(point_2)
|
|
|
|
multipoint_3 = MultiPoint(point_3)
|
|
|
|
self.assertNotEqual(hash(point_1), hash(point_2))
|
|
|
|
self.assertNotEqual(hash(point_1), hash(point_3))
|
|
|
|
self.assertNotEqual(hash(point_2), hash(point_3))
|
|
|
|
self.assertNotEqual(hash(multipoint_1), hash(multipoint_2))
|
|
|
|
self.assertEqual(hash(multipoint_2), hash(multipoint_3))
|
|
|
|
self.assertNotEqual(hash(multipoint_1), hash(point_1))
|
|
|
|
self.assertNotEqual(hash(multipoint_2), hash(point_2))
|
|
|
|
self.assertNotEqual(hash(multipoint_3), hash(point_3))
|
|
|
|
|
2016-11-23 16:23:06 +08:00
|
|
|
def test_eq_with_srid(self):
|
|
|
|
"Testing non-equivalence with different srids."
|
|
|
|
p0 = Point(5, 23)
|
|
|
|
p1 = Point(5, 23, srid=4326)
|
|
|
|
p2 = Point(5, 23, srid=32632)
|
|
|
|
# GEOS
|
|
|
|
self.assertNotEqual(p0, p1)
|
|
|
|
self.assertNotEqual(p1, p2)
|
|
|
|
# EWKT
|
|
|
|
self.assertNotEqual(p0, p1.ewkt)
|
|
|
|
self.assertNotEqual(p1, p0.ewkt)
|
|
|
|
self.assertNotEqual(p1, p2.ewkt)
|
|
|
|
# Equivalence with matching SRIDs
|
|
|
|
self.assertEqual(p2, p2)
|
|
|
|
self.assertEqual(p2, p2.ewkt)
|
|
|
|
# WKT contains no SRID so will not equal
|
|
|
|
self.assertNotEqual(p2, p2.wkt)
|
|
|
|
# SRID of 0
|
|
|
|
self.assertEqual(p0, 'SRID=0;POINT (5 23)')
|
|
|
|
self.assertNotEqual(p1, 'SRID=0;POINT (5 23)')
|
|
|
|
|
2012-05-28 17:15:31 +08:00
|
|
|
def test_points(self):
|
2008-08-06 02:13:06 +08:00
|
|
|
"Testing Point objects."
|
|
|
|
prev = fromstr('POINT(0 0)')
|
2010-11-02 05:56:48 +08:00
|
|
|
for p in self.geometries.points:
|
2008-08-06 02:13:06 +08:00
|
|
|
# Creating the point from the WKT
|
|
|
|
pnt = fromstr(p.wkt)
|
|
|
|
self.assertEqual(pnt.geom_type, 'Point')
|
|
|
|
self.assertEqual(pnt.geom_typeid, 0)
|
2015-11-02 23:20:53 +08:00
|
|
|
self.assertEqual(pnt.dims, 0)
|
2008-08-06 02:13:06 +08:00
|
|
|
self.assertEqual(p.x, pnt.x)
|
|
|
|
self.assertEqual(p.y, pnt.y)
|
2015-04-27 22:59:16 +08:00
|
|
|
self.assertEqual(pnt, fromstr(p.wkt))
|
|
|
|
self.assertEqual(False, pnt == prev) # Use assertEqual to test __eq__
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
# Making sure that the point's X, Y components are what we expect
|
|
|
|
self.assertAlmostEqual(p.x, pnt.tuple[0], 9)
|
|
|
|
self.assertAlmostEqual(p.y, pnt.tuple[1], 9)
|
|
|
|
|
|
|
|
# Testing the third dimension, and getting the tuple arguments
|
|
|
|
if hasattr(p, 'z'):
|
2016-06-17 02:19:18 +08:00
|
|
|
self.assertIs(pnt.hasz, True)
|
2008-08-06 02:13:06 +08:00
|
|
|
self.assertEqual(p.z, pnt.z)
|
|
|
|
self.assertEqual(p.z, pnt.tuple[2], 9)
|
|
|
|
tup_args = (p.x, p.y, p.z)
|
|
|
|
set_tup1 = (2.71, 3.14, 5.23)
|
|
|
|
set_tup2 = (5.23, 2.71, 3.14)
|
|
|
|
else:
|
2016-06-17 02:19:18 +08:00
|
|
|
self.assertIs(pnt.hasz, False)
|
2015-04-27 22:59:16 +08:00
|
|
|
self.assertIsNone(pnt.z)
|
2008-08-06 02:13:06 +08:00
|
|
|
tup_args = (p.x, p.y)
|
|
|
|
set_tup1 = (2.71, 3.14)
|
|
|
|
set_tup2 = (3.14, 2.71)
|
|
|
|
|
|
|
|
# Centroid operation on point should be point itself
|
|
|
|
self.assertEqual(p.centroid, pnt.centroid.tuple)
|
|
|
|
|
|
|
|
# Now testing the different constructors
|
|
|
|
pnt2 = Point(tup_args) # e.g., Point((1, 2))
|
2013-11-03 05:02:56 +08:00
|
|
|
pnt3 = Point(*tup_args) # e.g., Point(1, 2)
|
2015-04-27 22:59:16 +08:00
|
|
|
self.assertEqual(pnt, pnt2)
|
|
|
|
self.assertEqual(pnt, pnt3)
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
# Now testing setting the x and y
|
|
|
|
pnt.y = 3.14
|
|
|
|
pnt.x = 2.71
|
|
|
|
self.assertEqual(3.14, pnt.y)
|
|
|
|
self.assertEqual(2.71, pnt.x)
|
|
|
|
|
|
|
|
# Setting via the tuple/coords property
|
|
|
|
pnt.tuple = set_tup1
|
|
|
|
self.assertEqual(set_tup1, pnt.tuple)
|
|
|
|
pnt.coords = set_tup2
|
|
|
|
self.assertEqual(set_tup2, pnt.coords)
|
Refactored the GEOS interface. Improvements include:
* Geometries now allow list-like manipulation, e.g., can add, insert, delete vertexes (or other geometries in collections) like Python lists. Thanks, Aryeh Leib Taurog.
* Added support for GEOS prepared geometries via `prepared` property. Prepared geometries significantly speed up certain operations.
* Added support for GEOS cascaded union as `MultiPolygon.cascaded_union` property.
* Added support for GEOS line merge as `merged` property on `LineString`, and `MultiLineString` geometries. Thanks, Paul Smith.
* No longer use the deprecated C API for serialization to/from WKB and WKT. Now use the GEOS I/O classes, which are now exposed as `WKTReader`, `WKTWriter`, `WKBReader`, and `WKBWriter` (which supports 3D and SRID inclusion)
* Moved each type of geometry to their own module, eliminating the cluttered `geometries.py`.
* Internally, all C API methods are explicitly called from a module rather than a star import.
Fixed #9557, #9877, #10222
git-svn-id: http://code.djangoproject.com/svn/django/trunk@10131 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2009-03-24 08:12:21 +08:00
|
|
|
|
2013-11-03 05:02:56 +08:00
|
|
|
prev = pnt # setting the previous geometry
|
2008-08-06 02:13:06 +08:00
|
|
|
|
2012-05-28 17:15:31 +08:00
|
|
|
def test_multipoints(self):
|
2008-08-06 02:13:06 +08:00
|
|
|
"Testing MultiPoint objects."
|
2010-11-02 05:56:48 +08:00
|
|
|
for mp in self.geometries.multipoints:
|
2008-08-06 02:13:06 +08:00
|
|
|
mpnt = fromstr(mp.wkt)
|
|
|
|
self.assertEqual(mpnt.geom_type, 'MultiPoint')
|
|
|
|
self.assertEqual(mpnt.geom_typeid, 4)
|
2015-11-02 23:20:53 +08:00
|
|
|
self.assertEqual(mpnt.dims, 0)
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
self.assertAlmostEqual(mp.centroid[0], mpnt.centroid.tuple[0], 9)
|
|
|
|
self.assertAlmostEqual(mp.centroid[1], mpnt.centroid.tuple[1], 9)
|
|
|
|
|
2016-01-17 19:26:39 +08:00
|
|
|
with self.assertRaises(IndexError):
|
|
|
|
mpnt.__getitem__(len(mpnt))
|
2008-08-06 02:13:06 +08:00
|
|
|
self.assertEqual(mp.centroid, mpnt.centroid.tuple)
|
2010-11-02 05:56:48 +08:00
|
|
|
self.assertEqual(mp.coords, tuple(m.tuple for m in mpnt))
|
2008-08-06 02:13:06 +08:00
|
|
|
for p in mpnt:
|
|
|
|
self.assertEqual(p.geom_type, 'Point')
|
|
|
|
self.assertEqual(p.geom_typeid, 0)
|
2016-06-17 02:19:18 +08:00
|
|
|
self.assertIs(p.empty, False)
|
|
|
|
self.assertIs(p.valid, True)
|
2008-08-06 02:13:06 +08:00
|
|
|
|
2012-05-28 17:15:31 +08:00
|
|
|
def test_linestring(self):
|
2008-08-06 02:13:06 +08:00
|
|
|
"Testing LineString objects."
|
|
|
|
prev = fromstr('POINT(0 0)')
|
2010-11-02 05:56:48 +08:00
|
|
|
for l in self.geometries.linestrings:
|
2008-08-06 02:13:06 +08:00
|
|
|
ls = fromstr(l.wkt)
|
|
|
|
self.assertEqual(ls.geom_type, 'LineString')
|
|
|
|
self.assertEqual(ls.geom_typeid, 1)
|
2015-11-02 23:20:53 +08:00
|
|
|
self.assertEqual(ls.dims, 1)
|
2016-06-17 02:19:18 +08:00
|
|
|
self.assertIs(ls.empty, False)
|
|
|
|
self.assertIs(ls.ring, False)
|
2008-08-06 02:13:06 +08:00
|
|
|
if hasattr(l, 'centroid'):
|
|
|
|
self.assertEqual(l.centroid, ls.centroid.tuple)
|
|
|
|
if hasattr(l, 'tup'):
|
|
|
|
self.assertEqual(l.tup, ls.tuple)
|
Refactored the GEOS interface. Improvements include:
* Geometries now allow list-like manipulation, e.g., can add, insert, delete vertexes (or other geometries in collections) like Python lists. Thanks, Aryeh Leib Taurog.
* Added support for GEOS prepared geometries via `prepared` property. Prepared geometries significantly speed up certain operations.
* Added support for GEOS cascaded union as `MultiPolygon.cascaded_union` property.
* Added support for GEOS line merge as `merged` property on `LineString`, and `MultiLineString` geometries. Thanks, Paul Smith.
* No longer use the deprecated C API for serialization to/from WKB and WKT. Now use the GEOS I/O classes, which are now exposed as `WKTReader`, `WKTWriter`, `WKBReader`, and `WKBWriter` (which supports 3D and SRID inclusion)
* Moved each type of geometry to their own module, eliminating the cluttered `geometries.py`.
* Internally, all C API methods are explicitly called from a module rather than a star import.
Fixed #9557, #9877, #10222
git-svn-id: http://code.djangoproject.com/svn/django/trunk@10131 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2009-03-24 08:12:21 +08:00
|
|
|
|
2015-04-27 22:59:16 +08:00
|
|
|
self.assertEqual(ls, fromstr(l.wkt))
|
|
|
|
self.assertEqual(False, ls == prev) # Use assertEqual to test __eq__
|
2016-01-17 19:26:39 +08:00
|
|
|
with self.assertRaises(IndexError):
|
|
|
|
ls.__getitem__(len(ls))
|
2008-08-06 02:13:06 +08:00
|
|
|
prev = ls
|
|
|
|
|
|
|
|
# Creating a LineString from a tuple, list, and numpy array
|
|
|
|
self.assertEqual(ls, LineString(ls.tuple)) # tuple
|
2013-11-03 05:02:56 +08:00
|
|
|
self.assertEqual(ls, LineString(*ls.tuple)) # as individual arguments
|
|
|
|
self.assertEqual(ls, LineString([list(tup) for tup in ls.tuple])) # as list
|
2014-09-04 20:15:09 +08:00
|
|
|
# Point individual arguments
|
|
|
|
self.assertEqual(ls.wkt, LineString(*tuple(Point(tup) for tup in ls.tuple)).wkt)
|
2013-10-17 16:17:41 +08:00
|
|
|
if numpy:
|
2013-11-03 05:02:56 +08:00
|
|
|
self.assertEqual(ls, LineString(numpy.array(ls.tuple))) # as numpy array
|
2008-08-06 02:13:06 +08:00
|
|
|
|
2015-11-23 13:19:06 +08:00
|
|
|
with self.assertRaisesMessage(TypeError, 'Each coordinate should be a sequence (list or tuple)'):
|
|
|
|
LineString((0, 0))
|
2015-11-05 16:25:44 +08:00
|
|
|
|
2015-11-23 13:19:06 +08:00
|
|
|
with self.assertRaisesMessage(ValueError, 'LineString requires at least 2 points, got 1.'):
|
|
|
|
LineString([(0, 0)])
|
|
|
|
|
|
|
|
if numpy:
|
|
|
|
with self.assertRaisesMessage(ValueError, 'LineString requires at least 2 points, got 1.'):
|
|
|
|
LineString(numpy.array([(0, 0)]))
|
|
|
|
|
|
|
|
with mock.patch('django.contrib.gis.geos.linestring.numpy', False):
|
|
|
|
with self.assertRaisesMessage(TypeError, 'Invalid initialization input for LineStrings.'):
|
|
|
|
LineString('wrong input')
|
2015-11-05 16:25:44 +08:00
|
|
|
|
2017-07-13 01:13:06 +08:00
|
|
|
# Test __iter__().
|
|
|
|
self.assertEqual(list(LineString((0, 0), (1, 1), (2, 2))), [(0, 0), (1, 1), (2, 2)])
|
|
|
|
|
2012-05-28 17:15:31 +08:00
|
|
|
def test_multilinestring(self):
|
2008-08-06 02:13:06 +08:00
|
|
|
"Testing MultiLineString objects."
|
|
|
|
prev = fromstr('POINT(0 0)')
|
2010-11-02 05:56:48 +08:00
|
|
|
for l in self.geometries.multilinestrings:
|
2008-08-06 02:13:06 +08:00
|
|
|
ml = fromstr(l.wkt)
|
|
|
|
self.assertEqual(ml.geom_type, 'MultiLineString')
|
|
|
|
self.assertEqual(ml.geom_typeid, 5)
|
2015-11-02 23:20:53 +08:00
|
|
|
self.assertEqual(ml.dims, 1)
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
self.assertAlmostEqual(l.centroid[0], ml.centroid.x, 9)
|
|
|
|
self.assertAlmostEqual(l.centroid[1], ml.centroid.y, 9)
|
|
|
|
|
2015-04-27 22:59:16 +08:00
|
|
|
self.assertEqual(ml, fromstr(l.wkt))
|
|
|
|
self.assertEqual(False, ml == prev) # Use assertEqual to test __eq__
|
2008-08-06 02:13:06 +08:00
|
|
|
prev = ml
|
|
|
|
|
|
|
|
for ls in ml:
|
|
|
|
self.assertEqual(ls.geom_type, 'LineString')
|
|
|
|
self.assertEqual(ls.geom_typeid, 1)
|
2016-06-17 02:19:18 +08:00
|
|
|
self.assertIs(ls.empty, False)
|
2008-08-06 02:13:06 +08:00
|
|
|
|
2016-01-17 19:26:39 +08:00
|
|
|
with self.assertRaises(IndexError):
|
|
|
|
ml.__getitem__(len(ml))
|
2008-08-06 02:13:06 +08:00
|
|
|
self.assertEqual(ml.wkt, MultiLineString(*tuple(s.clone() for s in ml)).wkt)
|
|
|
|
self.assertEqual(ml, MultiLineString(*tuple(LineString(s.tuple) for s in ml)))
|
|
|
|
|
2012-05-28 17:15:31 +08:00
|
|
|
def test_linearring(self):
|
2008-08-06 02:13:06 +08:00
|
|
|
"Testing LinearRing objects."
|
2010-11-02 05:56:48 +08:00
|
|
|
for rr in self.geometries.linearrings:
|
2008-08-06 02:13:06 +08:00
|
|
|
lr = fromstr(rr.wkt)
|
|
|
|
self.assertEqual(lr.geom_type, 'LinearRing')
|
|
|
|
self.assertEqual(lr.geom_typeid, 2)
|
2015-11-02 23:20:53 +08:00
|
|
|
self.assertEqual(lr.dims, 1)
|
2008-08-06 02:13:06 +08:00
|
|
|
self.assertEqual(rr.n_p, len(lr))
|
2016-06-17 02:19:18 +08:00
|
|
|
self.assertIs(lr.valid, True)
|
|
|
|
self.assertIs(lr.empty, False)
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
# Creating a LinearRing from a tuple, list, and numpy array
|
|
|
|
self.assertEqual(lr, LinearRing(lr.tuple))
|
|
|
|
self.assertEqual(lr, LinearRing(*lr.tuple))
|
|
|
|
self.assertEqual(lr, LinearRing([list(tup) for tup in lr.tuple]))
|
2013-10-17 16:17:41 +08:00
|
|
|
if numpy:
|
|
|
|
self.assertEqual(lr, LinearRing(numpy.array(lr.tuple)))
|
Refactored the GEOS interface. Improvements include:
* Geometries now allow list-like manipulation, e.g., can add, insert, delete vertexes (or other geometries in collections) like Python lists. Thanks, Aryeh Leib Taurog.
* Added support for GEOS prepared geometries via `prepared` property. Prepared geometries significantly speed up certain operations.
* Added support for GEOS cascaded union as `MultiPolygon.cascaded_union` property.
* Added support for GEOS line merge as `merged` property on `LineString`, and `MultiLineString` geometries. Thanks, Paul Smith.
* No longer use the deprecated C API for serialization to/from WKB and WKT. Now use the GEOS I/O classes, which are now exposed as `WKTReader`, `WKTWriter`, `WKBReader`, and `WKBWriter` (which supports 3D and SRID inclusion)
* Moved each type of geometry to their own module, eliminating the cluttered `geometries.py`.
* Internally, all C API methods are explicitly called from a module rather than a star import.
Fixed #9557, #9877, #10222
git-svn-id: http://code.djangoproject.com/svn/django/trunk@10131 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2009-03-24 08:12:21 +08:00
|
|
|
|
2015-11-23 13:19:06 +08:00
|
|
|
with self.assertRaisesMessage(ValueError, 'LinearRing requires at least 4 points, got 3.'):
|
2015-11-05 16:25:44 +08:00
|
|
|
LinearRing((0, 0), (1, 1), (0, 0))
|
|
|
|
|
2015-11-23 13:19:06 +08:00
|
|
|
with self.assertRaisesMessage(ValueError, 'LinearRing requires at least 4 points, got 1.'):
|
2015-11-05 16:25:44 +08:00
|
|
|
LinearRing([(0, 0)])
|
|
|
|
|
2015-11-23 13:19:06 +08:00
|
|
|
if numpy:
|
|
|
|
with self.assertRaisesMessage(ValueError, 'LinearRing requires at least 4 points, got 1.'):
|
|
|
|
LinearRing(numpy.array([(0, 0)]))
|
|
|
|
|
2017-03-30 01:17:31 +08:00
|
|
|
def test_linearring_json(self):
|
|
|
|
self.assertJSONEqual(
|
|
|
|
LinearRing((0, 0), (0, 1), (1, 1), (0, 0)).json,
|
|
|
|
'{"coordinates": [[0, 0], [0, 1], [1, 1], [0, 0]], "type": "LineString"}',
|
|
|
|
)
|
|
|
|
|
2012-05-28 17:15:31 +08:00
|
|
|
def test_polygons_from_bbox(self):
|
|
|
|
"Testing `from_bbox` class method."
|
Refactored the GEOS interface. Improvements include:
* Geometries now allow list-like manipulation, e.g., can add, insert, delete vertexes (or other geometries in collections) like Python lists. Thanks, Aryeh Leib Taurog.
* Added support for GEOS prepared geometries via `prepared` property. Prepared geometries significantly speed up certain operations.
* Added support for GEOS cascaded union as `MultiPolygon.cascaded_union` property.
* Added support for GEOS line merge as `merged` property on `LineString`, and `MultiLineString` geometries. Thanks, Paul Smith.
* No longer use the deprecated C API for serialization to/from WKB and WKT. Now use the GEOS I/O classes, which are now exposed as `WKTReader`, `WKTWriter`, `WKBReader`, and `WKBWriter` (which supports 3D and SRID inclusion)
* Moved each type of geometry to their own module, eliminating the cluttered `geometries.py`.
* Internally, all C API methods are explicitly called from a module rather than a star import.
Fixed #9557, #9877, #10222
git-svn-id: http://code.djangoproject.com/svn/django/trunk@10131 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2009-03-24 08:12:21 +08:00
|
|
|
bbox = (-180, -90, 180, 90)
|
2012-05-28 17:15:31 +08:00
|
|
|
p = Polygon.from_bbox(bbox)
|
Refactored the GEOS interface. Improvements include:
* Geometries now allow list-like manipulation, e.g., can add, insert, delete vertexes (or other geometries in collections) like Python lists. Thanks, Aryeh Leib Taurog.
* Added support for GEOS prepared geometries via `prepared` property. Prepared geometries significantly speed up certain operations.
* Added support for GEOS cascaded union as `MultiPolygon.cascaded_union` property.
* Added support for GEOS line merge as `merged` property on `LineString`, and `MultiLineString` geometries. Thanks, Paul Smith.
* No longer use the deprecated C API for serialization to/from WKB and WKT. Now use the GEOS I/O classes, which are now exposed as `WKTReader`, `WKTWriter`, `WKBReader`, and `WKBWriter` (which supports 3D and SRID inclusion)
* Moved each type of geometry to their own module, eliminating the cluttered `geometries.py`.
* Internally, all C API methods are explicitly called from a module rather than a star import.
Fixed #9557, #9877, #10222
git-svn-id: http://code.djangoproject.com/svn/django/trunk@10131 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2009-03-24 08:12:21 +08:00
|
|
|
self.assertEqual(bbox, p.extent)
|
|
|
|
|
2012-06-06 16:05:27 +08:00
|
|
|
# Testing numerical precision
|
|
|
|
x = 3.14159265358979323
|
|
|
|
bbox = (0, 0, 1, x)
|
|
|
|
p = Polygon.from_bbox(bbox)
|
|
|
|
y = p.extent[-1]
|
|
|
|
self.assertEqual(format(x, '.13f'), format(y, '.13f'))
|
|
|
|
|
2012-05-28 17:15:31 +08:00
|
|
|
def test_polygons(self):
|
|
|
|
"Testing Polygon objects."
|
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
prev = fromstr('POINT(0 0)')
|
2010-11-02 05:56:48 +08:00
|
|
|
for p in self.geometries.polygons:
|
2008-08-06 02:13:06 +08:00
|
|
|
# Creating the Polygon, testing its properties.
|
|
|
|
poly = fromstr(p.wkt)
|
|
|
|
self.assertEqual(poly.geom_type, 'Polygon')
|
|
|
|
self.assertEqual(poly.geom_typeid, 3)
|
2015-11-02 23:20:53 +08:00
|
|
|
self.assertEqual(poly.dims, 2)
|
2016-06-17 02:19:18 +08:00
|
|
|
self.assertIs(poly.empty, False)
|
|
|
|
self.assertIs(poly.ring, False)
|
2008-08-06 02:13:06 +08:00
|
|
|
self.assertEqual(p.n_i, poly.num_interior_rings)
|
2013-11-03 05:02:56 +08:00
|
|
|
self.assertEqual(p.n_i + 1, len(poly)) # Testing __len__
|
2008-08-06 02:13:06 +08:00
|
|
|
self.assertEqual(p.n_p, poly.num_points)
|
|
|
|
|
|
|
|
# Area & Centroid
|
|
|
|
self.assertAlmostEqual(p.area, poly.area, 9)
|
|
|
|
self.assertAlmostEqual(p.centroid[0], poly.centroid.tuple[0], 9)
|
|
|
|
self.assertAlmostEqual(p.centroid[1], poly.centroid.tuple[1], 9)
|
|
|
|
|
|
|
|
# Testing the geometry equivalence
|
2015-04-27 22:59:16 +08:00
|
|
|
self.assertEqual(poly, fromstr(p.wkt))
|
|
|
|
# Should not be equal to previous geometry
|
|
|
|
self.assertEqual(False, poly == prev) # Use assertEqual to test __eq__
|
|
|
|
self.assertNotEqual(poly, prev) # Use assertNotEqual to test __ne__
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
# Testing the exterior ring
|
|
|
|
ring = poly.exterior_ring
|
|
|
|
self.assertEqual(ring.geom_type, 'LinearRing')
|
|
|
|
self.assertEqual(ring.geom_typeid, 2)
|
|
|
|
if p.ext_ring_cs:
|
|
|
|
self.assertEqual(p.ext_ring_cs, ring.tuple)
|
2013-11-03 05:02:56 +08:00
|
|
|
self.assertEqual(p.ext_ring_cs, poly[0].tuple) # Testing __getitem__
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
# Testing __getitem__ and __setitem__ on invalid indices
|
2016-01-17 19:26:39 +08:00
|
|
|
with self.assertRaises(IndexError):
|
|
|
|
poly.__getitem__(len(poly))
|
|
|
|
with self.assertRaises(IndexError):
|
|
|
|
poly.__setitem__(len(poly), False)
|
|
|
|
with self.assertRaises(IndexError):
|
|
|
|
poly.__getitem__(-1 * len(poly) - 1)
|
2008-08-06 02:13:06 +08:00
|
|
|
|
Refactored the GEOS interface. Improvements include:
* Geometries now allow list-like manipulation, e.g., can add, insert, delete vertexes (or other geometries in collections) like Python lists. Thanks, Aryeh Leib Taurog.
* Added support for GEOS prepared geometries via `prepared` property. Prepared geometries significantly speed up certain operations.
* Added support for GEOS cascaded union as `MultiPolygon.cascaded_union` property.
* Added support for GEOS line merge as `merged` property on `LineString`, and `MultiLineString` geometries. Thanks, Paul Smith.
* No longer use the deprecated C API for serialization to/from WKB and WKT. Now use the GEOS I/O classes, which are now exposed as `WKTReader`, `WKTWriter`, `WKBReader`, and `WKBWriter` (which supports 3D and SRID inclusion)
* Moved each type of geometry to their own module, eliminating the cluttered `geometries.py`.
* Internally, all C API methods are explicitly called from a module rather than a star import.
Fixed #9557, #9877, #10222
git-svn-id: http://code.djangoproject.com/svn/django/trunk@10131 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2009-03-24 08:12:21 +08:00
|
|
|
# Testing __iter__
|
2008-08-06 02:13:06 +08:00
|
|
|
for r in poly:
|
|
|
|
self.assertEqual(r.geom_type, 'LinearRing')
|
|
|
|
self.assertEqual(r.geom_typeid, 2)
|
|
|
|
|
|
|
|
# Testing polygon construction.
|
2016-01-17 19:26:39 +08:00
|
|
|
with self.assertRaises(TypeError):
|
|
|
|
Polygon(0, [1, 2, 3])
|
|
|
|
with self.assertRaises(TypeError):
|
|
|
|
Polygon('foo')
|
Refactored the GEOS interface. Improvements include:
* Geometries now allow list-like manipulation, e.g., can add, insert, delete vertexes (or other geometries in collections) like Python lists. Thanks, Aryeh Leib Taurog.
* Added support for GEOS prepared geometries via `prepared` property. Prepared geometries significantly speed up certain operations.
* Added support for GEOS cascaded union as `MultiPolygon.cascaded_union` property.
* Added support for GEOS line merge as `merged` property on `LineString`, and `MultiLineString` geometries. Thanks, Paul Smith.
* No longer use the deprecated C API for serialization to/from WKB and WKT. Now use the GEOS I/O classes, which are now exposed as `WKTReader`, `WKTWriter`, `WKBReader`, and `WKBWriter` (which supports 3D and SRID inclusion)
* Moved each type of geometry to their own module, eliminating the cluttered `geometries.py`.
* Internally, all C API methods are explicitly called from a module rather than a star import.
Fixed #9557, #9877, #10222
git-svn-id: http://code.djangoproject.com/svn/django/trunk@10131 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2009-03-24 08:12:21 +08:00
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
# Polygon(shell, (hole1, ... holeN))
|
2018-09-28 21:57:12 +08:00
|
|
|
ext_ring, *int_rings = poly
|
|
|
|
self.assertEqual(poly, Polygon(ext_ring, int_rings))
|
Refactored the GEOS interface. Improvements include:
* Geometries now allow list-like manipulation, e.g., can add, insert, delete vertexes (or other geometries in collections) like Python lists. Thanks, Aryeh Leib Taurog.
* Added support for GEOS prepared geometries via `prepared` property. Prepared geometries significantly speed up certain operations.
* Added support for GEOS cascaded union as `MultiPolygon.cascaded_union` property.
* Added support for GEOS line merge as `merged` property on `LineString`, and `MultiLineString` geometries. Thanks, Paul Smith.
* No longer use the deprecated C API for serialization to/from WKB and WKT. Now use the GEOS I/O classes, which are now exposed as `WKTReader`, `WKTWriter`, `WKBReader`, and `WKBWriter` (which supports 3D and SRID inclusion)
* Moved each type of geometry to their own module, eliminating the cluttered `geometries.py`.
* Internally, all C API methods are explicitly called from a module rather than a star import.
Fixed #9557, #9877, #10222
git-svn-id: http://code.djangoproject.com/svn/django/trunk@10131 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2009-03-24 08:12:21 +08:00
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
# Polygon(shell_tuple, hole_tuple1, ... , hole_tupleN)
|
|
|
|
ring_tuples = tuple(r.tuple for r in poly)
|
|
|
|
self.assertEqual(poly, Polygon(*ring_tuples))
|
|
|
|
|
|
|
|
# Constructing with tuples of LinearRings.
|
|
|
|
self.assertEqual(poly.wkt, Polygon(*tuple(r for r in poly)).wkt)
|
|
|
|
self.assertEqual(poly.wkt, Polygon(*tuple(LinearRing(r.tuple) for r in poly)).wkt)
|
|
|
|
|
2015-08-12 20:42:01 +08:00
|
|
|
def test_polygons_templates(self):
|
|
|
|
# Accessing Polygon attributes in templates should work.
|
|
|
|
engine = Engine()
|
|
|
|
template = engine.from_string('{{ polygons.0.wkt }}')
|
|
|
|
polygons = [fromstr(p.wkt) for p in self.geometries.multipolygons[:2]]
|
|
|
|
content = template.render(Context({'polygons': polygons}))
|
|
|
|
self.assertIn('MULTIPOLYGON (((100', content)
|
|
|
|
|
2012-11-29 05:31:13 +08:00
|
|
|
def test_polygon_comparison(self):
|
|
|
|
p1 = Polygon(((0, 0), (0, 1), (1, 1), (1, 0), (0, 0)))
|
|
|
|
p2 = Polygon(((0, 0), (0, 1), (1, 0), (0, 0)))
|
2014-10-28 18:02:56 +08:00
|
|
|
self.assertGreater(p1, p2)
|
|
|
|
self.assertLess(p2, p1)
|
2012-11-29 05:31:13 +08:00
|
|
|
|
|
|
|
p3 = Polygon(((0, 0), (0, 1), (1, 1), (2, 0), (0, 0)))
|
|
|
|
p4 = Polygon(((0, 0), (0, 1), (2, 2), (1, 0), (0, 0)))
|
2014-10-28 18:02:56 +08:00
|
|
|
self.assertGreater(p4, p3)
|
|
|
|
self.assertLess(p3, p4)
|
2012-11-29 05:31:13 +08:00
|
|
|
|
2012-05-28 17:15:31 +08:00
|
|
|
def test_multipolygons(self):
|
2008-08-06 02:13:06 +08:00
|
|
|
"Testing MultiPolygon objects."
|
2013-09-08 23:05:16 +08:00
|
|
|
fromstr('POINT (0 0)')
|
2010-11-02 05:56:48 +08:00
|
|
|
for mp in self.geometries.multipolygons:
|
2008-08-06 02:13:06 +08:00
|
|
|
mpoly = fromstr(mp.wkt)
|
|
|
|
self.assertEqual(mpoly.geom_type, 'MultiPolygon')
|
|
|
|
self.assertEqual(mpoly.geom_typeid, 6)
|
2015-11-02 23:20:53 +08:00
|
|
|
self.assertEqual(mpoly.dims, 2)
|
2008-08-06 02:13:06 +08:00
|
|
|
self.assertEqual(mp.valid, mpoly.valid)
|
|
|
|
|
|
|
|
if mp.valid:
|
|
|
|
self.assertEqual(mp.num_geom, mpoly.num_geom)
|
|
|
|
self.assertEqual(mp.n_p, mpoly.num_coords)
|
|
|
|
self.assertEqual(mp.num_geom, len(mpoly))
|
2016-01-17 19:26:39 +08:00
|
|
|
with self.assertRaises(IndexError):
|
|
|
|
mpoly.__getitem__(len(mpoly))
|
2008-08-06 02:13:06 +08:00
|
|
|
for p in mpoly:
|
|
|
|
self.assertEqual(p.geom_type, 'Polygon')
|
|
|
|
self.assertEqual(p.geom_typeid, 3)
|
2016-06-17 02:19:18 +08:00
|
|
|
self.assertIs(p.valid, True)
|
2008-08-06 02:13:06 +08:00
|
|
|
self.assertEqual(mpoly.wkt, MultiPolygon(*tuple(poly.clone() for poly in mpoly)).wkt)
|
|
|
|
|
2012-05-28 17:15:31 +08:00
|
|
|
def test_memory_hijinks(self):
|
2008-08-06 02:13:06 +08:00
|
|
|
"Testing Geometry __del__() on rings and polygons."
|
2015-02-06 02:25:34 +08:00
|
|
|
# #### Memory issues with rings and poly
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
# These tests are needed to ensure sanity with writable geometries.
|
|
|
|
|
|
|
|
# Getting a polygon with interior rings, and pulling out the interior rings
|
2010-11-02 05:56:48 +08:00
|
|
|
poly = fromstr(self.geometries.polygons[1].wkt)
|
2008-08-06 02:13:06 +08:00
|
|
|
ring1 = poly[0]
|
|
|
|
ring2 = poly[1]
|
|
|
|
|
|
|
|
# These deletes should be 'harmless' since they are done on child geometries
|
Refactored the GEOS interface. Improvements include:
* Geometries now allow list-like manipulation, e.g., can add, insert, delete vertexes (or other geometries in collections) like Python lists. Thanks, Aryeh Leib Taurog.
* Added support for GEOS prepared geometries via `prepared` property. Prepared geometries significantly speed up certain operations.
* Added support for GEOS cascaded union as `MultiPolygon.cascaded_union` property.
* Added support for GEOS line merge as `merged` property on `LineString`, and `MultiLineString` geometries. Thanks, Paul Smith.
* No longer use the deprecated C API for serialization to/from WKB and WKT. Now use the GEOS I/O classes, which are now exposed as `WKTReader`, `WKTWriter`, `WKBReader`, and `WKBWriter` (which supports 3D and SRID inclusion)
* Moved each type of geometry to their own module, eliminating the cluttered `geometries.py`.
* Internally, all C API methods are explicitly called from a module rather than a star import.
Fixed #9557, #9877, #10222
git-svn-id: http://code.djangoproject.com/svn/django/trunk@10131 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2009-03-24 08:12:21 +08:00
|
|
|
del ring1
|
2008-08-06 02:13:06 +08:00
|
|
|
del ring2
|
|
|
|
ring1 = poly[0]
|
|
|
|
ring2 = poly[1]
|
|
|
|
|
|
|
|
# Deleting the polygon
|
|
|
|
del poly
|
|
|
|
|
|
|
|
# Access to these rings is OK since they are clones.
|
2014-03-31 03:11:05 +08:00
|
|
|
str(ring1)
|
|
|
|
str(ring2)
|
2008-08-06 02:13:06 +08:00
|
|
|
|
2012-05-28 17:15:31 +08:00
|
|
|
def test_coord_seq(self):
|
2008-08-06 02:13:06 +08:00
|
|
|
"Testing Coordinate Sequence objects."
|
2010-11-02 05:56:48 +08:00
|
|
|
for p in self.geometries.polygons:
|
2008-08-06 02:13:06 +08:00
|
|
|
if p.ext_ring_cs:
|
|
|
|
# Constructing the polygon and getting the coordinate sequence
|
|
|
|
poly = fromstr(p.wkt)
|
|
|
|
cs = poly.exterior_ring.coord_seq
|
|
|
|
|
2013-11-03 05:02:56 +08:00
|
|
|
self.assertEqual(p.ext_ring_cs, cs.tuple) # done in the Polygon test too.
|
|
|
|
self.assertEqual(len(p.ext_ring_cs), len(cs)) # Making sure __len__ works
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
# Checks __getitem__ and __setitem__
|
2014-12-13 21:04:36 +08:00
|
|
|
for i in range(len(p.ext_ring_cs)):
|
2013-11-03 05:02:56 +08:00
|
|
|
c1 = p.ext_ring_cs[i] # Expected value
|
|
|
|
c2 = cs[i] # Value from coordseq
|
2008-08-06 02:13:06 +08:00
|
|
|
self.assertEqual(c1, c2)
|
|
|
|
|
|
|
|
# Constructing the test value to set the coordinate sequence with
|
2013-10-17 16:17:41 +08:00
|
|
|
if len(c1) == 2:
|
|
|
|
tset = (5, 23)
|
|
|
|
else:
|
|
|
|
tset = (5, 23, 8)
|
2008-08-06 02:13:06 +08:00
|
|
|
cs[i] = tset
|
Refactored the GEOS interface. Improvements include:
* Geometries now allow list-like manipulation, e.g., can add, insert, delete vertexes (or other geometries in collections) like Python lists. Thanks, Aryeh Leib Taurog.
* Added support for GEOS prepared geometries via `prepared` property. Prepared geometries significantly speed up certain operations.
* Added support for GEOS cascaded union as `MultiPolygon.cascaded_union` property.
* Added support for GEOS line merge as `merged` property on `LineString`, and `MultiLineString` geometries. Thanks, Paul Smith.
* No longer use the deprecated C API for serialization to/from WKB and WKT. Now use the GEOS I/O classes, which are now exposed as `WKTReader`, `WKTWriter`, `WKBReader`, and `WKBWriter` (which supports 3D and SRID inclusion)
* Moved each type of geometry to their own module, eliminating the cluttered `geometries.py`.
* Internally, all C API methods are explicitly called from a module rather than a star import.
Fixed #9557, #9877, #10222
git-svn-id: http://code.djangoproject.com/svn/django/trunk@10131 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2009-03-24 08:12:21 +08:00
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
# Making sure every set point matches what we expect
|
|
|
|
for j in range(len(tset)):
|
|
|
|
cs[i] = tset
|
|
|
|
self.assertEqual(tset[j], cs[i][j])
|
|
|
|
|
2012-05-28 17:15:31 +08:00
|
|
|
def test_relate_pattern(self):
|
2008-08-06 02:13:06 +08:00
|
|
|
"Testing relate() and relate_pattern()."
|
|
|
|
g = fromstr('POINT (0 0)')
|
2016-01-17 19:26:39 +08:00
|
|
|
with self.assertRaises(GEOSException):
|
|
|
|
g.relate_pattern(0, 'invalid pattern, yo')
|
2010-11-02 05:56:48 +08:00
|
|
|
for rg in self.geometries.relate_geoms:
|
|
|
|
a = fromstr(rg.wkt_a)
|
|
|
|
b = fromstr(rg.wkt_b)
|
|
|
|
self.assertEqual(rg.result, a.relate_pattern(b, rg.pattern))
|
|
|
|
self.assertEqual(rg.pattern, a.relate(b))
|
2008-08-06 02:13:06 +08:00
|
|
|
|
2012-05-28 17:15:31 +08:00
|
|
|
def test_intersection(self):
|
2008-08-06 02:13:06 +08:00
|
|
|
"Testing intersects() and intersection()."
|
2014-12-13 21:04:36 +08:00
|
|
|
for i in range(len(self.geometries.topology_geoms)):
|
2010-11-02 05:56:48 +08:00
|
|
|
a = fromstr(self.geometries.topology_geoms[i].wkt_a)
|
|
|
|
b = fromstr(self.geometries.topology_geoms[i].wkt_b)
|
|
|
|
i1 = fromstr(self.geometries.intersect_geoms[i].wkt)
|
2016-06-17 02:19:18 +08:00
|
|
|
self.assertIs(a.intersects(b), True)
|
2008-08-06 02:13:06 +08:00
|
|
|
i2 = a.intersection(b)
|
|
|
|
self.assertEqual(i1, i2)
|
2013-11-03 05:02:56 +08:00
|
|
|
self.assertEqual(i1, a & b) # __and__ is intersection operator
|
|
|
|
a &= b # testing __iand__
|
2008-08-06 02:13:06 +08:00
|
|
|
self.assertEqual(i1, a)
|
|
|
|
|
2012-05-28 17:15:31 +08:00
|
|
|
def test_union(self):
|
2008-08-06 02:13:06 +08:00
|
|
|
"Testing union()."
|
2014-12-13 21:04:36 +08:00
|
|
|
for i in range(len(self.geometries.topology_geoms)):
|
2010-11-02 05:56:48 +08:00
|
|
|
a = fromstr(self.geometries.topology_geoms[i].wkt_a)
|
|
|
|
b = fromstr(self.geometries.topology_geoms[i].wkt_b)
|
|
|
|
u1 = fromstr(self.geometries.union_geoms[i].wkt)
|
2008-08-06 02:13:06 +08:00
|
|
|
u2 = a.union(b)
|
|
|
|
self.assertEqual(u1, u2)
|
2013-11-03 05:02:56 +08:00
|
|
|
self.assertEqual(u1, a | b) # __or__ is union operator
|
|
|
|
a |= b # testing __ior__
|
Refactored the GEOS interface. Improvements include:
* Geometries now allow list-like manipulation, e.g., can add, insert, delete vertexes (or other geometries in collections) like Python lists. Thanks, Aryeh Leib Taurog.
* Added support for GEOS prepared geometries via `prepared` property. Prepared geometries significantly speed up certain operations.
* Added support for GEOS cascaded union as `MultiPolygon.cascaded_union` property.
* Added support for GEOS line merge as `merged` property on `LineString`, and `MultiLineString` geometries. Thanks, Paul Smith.
* No longer use the deprecated C API for serialization to/from WKB and WKT. Now use the GEOS I/O classes, which are now exposed as `WKTReader`, `WKTWriter`, `WKBReader`, and `WKBWriter` (which supports 3D and SRID inclusion)
* Moved each type of geometry to their own module, eliminating the cluttered `geometries.py`.
* Internally, all C API methods are explicitly called from a module rather than a star import.
Fixed #9557, #9877, #10222
git-svn-id: http://code.djangoproject.com/svn/django/trunk@10131 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2009-03-24 08:12:21 +08:00
|
|
|
self.assertEqual(u1, a)
|
2008-08-06 02:13:06 +08:00
|
|
|
|
2015-11-10 01:36:12 +08:00
|
|
|
def test_unary_union(self):
|
|
|
|
"Testing unary_union."
|
|
|
|
for i in range(len(self.geometries.topology_geoms)):
|
|
|
|
a = fromstr(self.geometries.topology_geoms[i].wkt_a)
|
|
|
|
b = fromstr(self.geometries.topology_geoms[i].wkt_b)
|
|
|
|
u1 = fromstr(self.geometries.union_geoms[i].wkt)
|
|
|
|
u2 = GeometryCollection(a, b).unary_union
|
|
|
|
self.assertTrue(u1.equals(u2))
|
|
|
|
|
2012-05-28 17:15:31 +08:00
|
|
|
def test_difference(self):
|
2008-08-06 02:13:06 +08:00
|
|
|
"Testing difference()."
|
2014-12-13 21:04:36 +08:00
|
|
|
for i in range(len(self.geometries.topology_geoms)):
|
2010-11-02 05:56:48 +08:00
|
|
|
a = fromstr(self.geometries.topology_geoms[i].wkt_a)
|
|
|
|
b = fromstr(self.geometries.topology_geoms[i].wkt_b)
|
|
|
|
d1 = fromstr(self.geometries.diff_geoms[i].wkt)
|
2008-08-06 02:13:06 +08:00
|
|
|
d2 = a.difference(b)
|
|
|
|
self.assertEqual(d1, d2)
|
2013-11-03 05:02:56 +08:00
|
|
|
self.assertEqual(d1, a - b) # __sub__ is difference operator
|
|
|
|
a -= b # testing __isub__
|
2008-08-06 02:13:06 +08:00
|
|
|
self.assertEqual(d1, a)
|
|
|
|
|
2012-05-28 17:15:31 +08:00
|
|
|
def test_symdifference(self):
|
2008-08-06 02:13:06 +08:00
|
|
|
"Testing sym_difference()."
|
2014-12-13 21:04:36 +08:00
|
|
|
for i in range(len(self.geometries.topology_geoms)):
|
2010-11-02 05:56:48 +08:00
|
|
|
a = fromstr(self.geometries.topology_geoms[i].wkt_a)
|
|
|
|
b = fromstr(self.geometries.topology_geoms[i].wkt_b)
|
|
|
|
d1 = fromstr(self.geometries.sdiff_geoms[i].wkt)
|
2008-08-06 02:13:06 +08:00
|
|
|
d2 = a.sym_difference(b)
|
|
|
|
self.assertEqual(d1, d2)
|
2013-11-03 05:02:56 +08:00
|
|
|
self.assertEqual(d1, a ^ b) # __xor__ is symmetric difference operator
|
|
|
|
a ^= b # testing __ixor__
|
2008-08-06 02:13:06 +08:00
|
|
|
self.assertEqual(d1, a)
|
|
|
|
|
2012-05-28 17:15:31 +08:00
|
|
|
def test_buffer(self):
|
2018-01-10 00:15:04 +08:00
|
|
|
bg = self.geometries.buffer_geoms[0]
|
|
|
|
g = fromstr(bg.wkt)
|
|
|
|
|
|
|
|
# Can't use a floating-point for the number of quadsegs.
|
|
|
|
with self.assertRaises(ctypes.ArgumentError):
|
|
|
|
g.buffer(bg.width, quadsegs=1.1)
|
|
|
|
|
|
|
|
self._test_buffer(self.geometries.buffer_geoms, 'buffer')
|
|
|
|
|
|
|
|
def test_buffer_with_style(self):
|
|
|
|
bg = self.geometries.buffer_with_style_geoms[0]
|
|
|
|
g = fromstr(bg.wkt)
|
|
|
|
|
|
|
|
# Can't use a floating-point for the number of quadsegs.
|
|
|
|
with self.assertRaises(ctypes.ArgumentError):
|
|
|
|
g.buffer_with_style(bg.width, quadsegs=1.1)
|
|
|
|
|
|
|
|
# Can't use a floating-point for the end cap style.
|
|
|
|
with self.assertRaises(ctypes.ArgumentError):
|
|
|
|
g.buffer_with_style(bg.width, end_cap_style=1.2)
|
|
|
|
# Can't use a end cap style that is not in the enum.
|
|
|
|
with self.assertRaises(GEOSException):
|
|
|
|
g.buffer_with_style(bg.width, end_cap_style=55)
|
|
|
|
|
|
|
|
# Can't use a floating-point for the join style.
|
|
|
|
with self.assertRaises(ctypes.ArgumentError):
|
|
|
|
g.buffer_with_style(bg.width, join_style=1.3)
|
|
|
|
# Can't use a join style that is not in the enum.
|
|
|
|
with self.assertRaises(GEOSException):
|
|
|
|
g.buffer_with_style(bg.width, join_style=66)
|
|
|
|
|
|
|
|
self._test_buffer(
|
|
|
|
itertools.chain(self.geometries.buffer_geoms, self.geometries.buffer_with_style_geoms),
|
|
|
|
'buffer_with_style',
|
|
|
|
)
|
|
|
|
|
|
|
|
def _test_buffer(self, geometries, buffer_method_name):
|
|
|
|
for bg in geometries:
|
2010-11-02 05:56:48 +08:00
|
|
|
g = fromstr(bg.wkt)
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
# The buffer we expect
|
2010-11-02 05:56:48 +08:00
|
|
|
exp_buf = fromstr(bg.buffer_wkt)
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
# Constructing our buffer
|
2018-01-10 00:15:04 +08:00
|
|
|
buf_kwargs = {
|
|
|
|
kwarg_name: getattr(bg, kwarg_name)
|
|
|
|
for kwarg_name in ('width', 'quadsegs', 'end_cap_style', 'join_style', 'mitre_limit')
|
|
|
|
if hasattr(bg, kwarg_name)
|
|
|
|
}
|
|
|
|
buf = getattr(g, buffer_method_name)(**buf_kwargs)
|
2008-08-06 02:13:06 +08:00
|
|
|
self.assertEqual(exp_buf.num_coords, buf.num_coords)
|
|
|
|
self.assertEqual(len(exp_buf), len(buf))
|
|
|
|
|
|
|
|
# Now assuring that each point in the buffer is almost equal
|
2014-12-13 21:04:36 +08:00
|
|
|
for j in range(len(exp_buf)):
|
2008-08-06 02:13:06 +08:00
|
|
|
exp_ring = exp_buf[j]
|
|
|
|
buf_ring = buf[j]
|
|
|
|
self.assertEqual(len(exp_ring), len(buf_ring))
|
2014-12-13 21:04:36 +08:00
|
|
|
for k in range(len(exp_ring)):
|
2008-08-06 02:13:06 +08:00
|
|
|
# Asserting the X, Y of each point are almost equal (due to floating point imprecision)
|
|
|
|
self.assertAlmostEqual(exp_ring[k][0], buf_ring[k][0], 9)
|
|
|
|
self.assertAlmostEqual(exp_ring[k][1], buf_ring[k][1], 9)
|
|
|
|
|
2015-11-20 18:11:25 +08:00
|
|
|
def test_covers(self):
|
|
|
|
poly = Polygon(((0, 0), (0, 10), (10, 10), (10, 0), (0, 0)))
|
|
|
|
self.assertTrue(poly.covers(Point(5, 5)))
|
|
|
|
self.assertFalse(poly.covers(Point(100, 100)))
|
|
|
|
|
2015-12-18 18:26:22 +08:00
|
|
|
def test_closed(self):
|
|
|
|
ls_closed = LineString((0, 0), (1, 1), (0, 0))
|
|
|
|
ls_not_closed = LineString((0, 0), (1, 1))
|
|
|
|
self.assertFalse(ls_not_closed.closed)
|
|
|
|
self.assertTrue(ls_closed.closed)
|
|
|
|
|
2017-02-16 00:14:21 +08:00
|
|
|
if geos_version_tuple() >= (3, 5):
|
2015-12-18 18:26:22 +08:00
|
|
|
self.assertFalse(MultiLineString(ls_closed, ls_not_closed).closed)
|
|
|
|
self.assertTrue(MultiLineString(ls_closed, ls_closed).closed)
|
|
|
|
|
2017-07-28 01:52:17 +08:00
|
|
|
with mock.patch('django.contrib.gis.geos.libgeos.geos_version', lambda: b'3.4.9'):
|
2015-12-18 18:26:22 +08:00
|
|
|
with self.assertRaisesMessage(GEOSException, "MultiLineString.closed requires GEOS >= 3.5.0."):
|
|
|
|
MultiLineString().closed
|
|
|
|
|
2012-05-28 17:15:31 +08:00
|
|
|
def test_srid(self):
|
2008-08-06 02:13:06 +08:00
|
|
|
"Testing the SRID property and keyword."
|
|
|
|
# Testing SRID keyword on Point
|
|
|
|
pnt = Point(5, 23, srid=4326)
|
|
|
|
self.assertEqual(4326, pnt.srid)
|
|
|
|
pnt.srid = 3084
|
|
|
|
self.assertEqual(3084, pnt.srid)
|
2015-11-05 02:56:14 +08:00
|
|
|
with self.assertRaises(ctypes.ArgumentError):
|
|
|
|
pnt.srid = '4326'
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
# Testing SRID keyword on fromstr(), and on Polygon rings.
|
2010-11-02 05:56:48 +08:00
|
|
|
poly = fromstr(self.geometries.polygons[1].wkt, srid=4269)
|
2008-08-06 02:13:06 +08:00
|
|
|
self.assertEqual(4269, poly.srid)
|
2013-10-17 16:17:41 +08:00
|
|
|
for ring in poly:
|
|
|
|
self.assertEqual(4269, ring.srid)
|
2008-08-06 02:13:06 +08:00
|
|
|
poly.srid = 4326
|
|
|
|
self.assertEqual(4326, poly.shell.srid)
|
|
|
|
|
|
|
|
# Testing SRID keyword on GeometryCollection
|
|
|
|
gc = GeometryCollection(Point(5, 23), LineString((0, 0), (1.5, 1.5), (3, 3)), srid=32021)
|
|
|
|
self.assertEqual(32021, gc.srid)
|
2013-10-17 16:17:41 +08:00
|
|
|
for i in range(len(gc)):
|
|
|
|
self.assertEqual(32021, gc[i].srid)
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
# GEOS may get the SRID from HEXEWKB
|
|
|
|
# 'POINT(5 23)' at SRID=4326 in hex form -- obtained from PostGIS
|
|
|
|
# using `SELECT GeomFromText('POINT (5 23)', 4326);`.
|
|
|
|
hex = '0101000020E610000000000000000014400000000000003740'
|
|
|
|
p1 = fromstr(hex)
|
|
|
|
self.assertEqual(4326, p1.srid)
|
|
|
|
|
|
|
|
p2 = fromstr(p1.hex)
|
2014-03-28 05:53:04 +08:00
|
|
|
self.assertIsNone(p2.srid)
|
2013-11-03 05:02:56 +08:00
|
|
|
p3 = fromstr(p1.hex, srid=-1) # -1 is intended.
|
2008-08-06 02:13:06 +08:00
|
|
|
self.assertEqual(-1, p3.srid)
|
|
|
|
|
2015-10-21 13:21:29 +08:00
|
|
|
# Testing that geometry SRID could be set to its own value
|
|
|
|
pnt_wo_srid = Point(1, 1)
|
|
|
|
pnt_wo_srid.srid = pnt_wo_srid.srid
|
|
|
|
|
2017-03-30 21:38:26 +08:00
|
|
|
# Input geometries that have an SRID.
|
|
|
|
self.assertEqual(GEOSGeometry(pnt.ewkt, srid=pnt.srid).srid, pnt.srid)
|
|
|
|
self.assertEqual(GEOSGeometry(pnt.ewkb, srid=pnt.srid).srid, pnt.srid)
|
|
|
|
with self.assertRaisesMessage(ValueError, 'Input geometry already has SRID: %d.' % pnt.srid):
|
|
|
|
GEOSGeometry(pnt.ewkt, srid=1)
|
|
|
|
with self.assertRaisesMessage(ValueError, 'Input geometry already has SRID: %d.' % pnt.srid):
|
|
|
|
GEOSGeometry(pnt.ewkb, srid=1)
|
|
|
|
|
2013-03-10 00:46:20 +08:00
|
|
|
def test_custom_srid(self):
|
2015-10-24 22:12:38 +08:00
|
|
|
"""Test with a null srid and a srid unknown to GDAL."""
|
|
|
|
for srid in [None, 999999]:
|
|
|
|
pnt = Point(111200, 220900, srid=srid)
|
2016-04-17 02:05:47 +08:00
|
|
|
self.assertTrue(pnt.ewkt.startswith(("SRID=%s;" % srid if srid else '') + "POINT (111200"))
|
2015-10-24 22:12:38 +08:00
|
|
|
self.assertIsInstance(pnt.ogr, gdal.OGRGeometry)
|
|
|
|
self.assertIsNone(pnt.srs)
|
|
|
|
|
|
|
|
# Test conversion from custom to a known srid
|
|
|
|
c2w = gdal.CoordTransform(
|
|
|
|
gdal.SpatialReference(
|
|
|
|
'+proj=mill +lat_0=0 +lon_0=0 +x_0=0 +y_0=0 +R_A +ellps=WGS84 '
|
|
|
|
'+datum=WGS84 +units=m +no_defs'
|
|
|
|
),
|
|
|
|
gdal.SpatialReference(4326))
|
|
|
|
new_pnt = pnt.transform(c2w, clone=True)
|
|
|
|
self.assertEqual(new_pnt.srid, 4326)
|
|
|
|
self.assertAlmostEqual(new_pnt.x, 1, 3)
|
|
|
|
self.assertAlmostEqual(new_pnt.y, 2, 3)
|
2013-03-10 00:46:20 +08:00
|
|
|
|
2012-05-28 17:15:31 +08:00
|
|
|
def test_mutable_geometries(self):
|
2008-08-06 02:13:06 +08:00
|
|
|
"Testing the mutability of Polygons and Geometry Collections."
|
2015-02-06 02:25:34 +08:00
|
|
|
# ### Testing the mutability of Polygons ###
|
2010-11-02 05:56:48 +08:00
|
|
|
for p in self.geometries.polygons:
|
2008-08-06 02:13:06 +08:00
|
|
|
poly = fromstr(p.wkt)
|
|
|
|
|
|
|
|
# Should only be able to use __setitem__ with LinearRing geometries.
|
2016-01-17 19:26:39 +08:00
|
|
|
with self.assertRaises(TypeError):
|
|
|
|
poly.__setitem__(0, LineString((1, 1), (2, 2)))
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
# Constructing the new shell by adding 500 to every point in the old shell.
|
|
|
|
shell_tup = poly.shell.tuple
|
|
|
|
new_coords = []
|
2013-10-17 16:17:41 +08:00
|
|
|
for point in shell_tup:
|
|
|
|
new_coords.append((point[0] + 500., point[1] + 500.))
|
2008-08-06 02:13:06 +08:00
|
|
|
new_shell = LinearRing(*tuple(new_coords))
|
|
|
|
|
|
|
|
# Assigning polygon's exterior ring w/the new shell
|
|
|
|
poly.exterior_ring = new_shell
|
2013-11-03 05:02:56 +08:00
|
|
|
str(new_shell) # new shell is still accessible
|
2008-08-06 02:13:06 +08:00
|
|
|
self.assertEqual(poly.exterior_ring, new_shell)
|
|
|
|
self.assertEqual(poly[0], new_shell)
|
|
|
|
|
2015-02-06 02:25:34 +08:00
|
|
|
# ### Testing the mutability of Geometry Collections
|
2010-11-02 05:56:48 +08:00
|
|
|
for tg in self.geometries.multipoints:
|
2008-08-06 02:13:06 +08:00
|
|
|
mp = fromstr(tg.wkt)
|
|
|
|
for i in range(len(mp)):
|
|
|
|
# Creating a random point.
|
|
|
|
pnt = mp[i]
|
2013-02-16 03:11:46 +08:00
|
|
|
new = Point(random.randint(21, 100), random.randint(21, 100))
|
2008-08-06 02:13:06 +08:00
|
|
|
# Testing the assignment
|
|
|
|
mp[i] = new
|
2013-11-03 05:02:56 +08:00
|
|
|
str(new) # what was used for the assignment is still accessible
|
2008-08-06 02:13:06 +08:00
|
|
|
self.assertEqual(mp[i], new)
|
|
|
|
self.assertEqual(mp[i].wkt, new.wkt)
|
|
|
|
self.assertNotEqual(pnt, mp[i])
|
|
|
|
|
|
|
|
# MultiPolygons involve much more memory management because each
|
|
|
|
# Polygon w/in the collection has its own rings.
|
2010-11-02 05:56:48 +08:00
|
|
|
for tg in self.geometries.multipolygons:
|
2008-08-06 02:13:06 +08:00
|
|
|
mpoly = fromstr(tg.wkt)
|
2014-12-13 21:04:36 +08:00
|
|
|
for i in range(len(mpoly)):
|
2008-08-06 02:13:06 +08:00
|
|
|
poly = mpoly[i]
|
|
|
|
old_poly = mpoly[i]
|
|
|
|
# Offsetting the each ring in the polygon by 500.
|
2014-12-13 21:04:36 +08:00
|
|
|
for j in range(len(poly)):
|
2008-08-06 02:13:06 +08:00
|
|
|
r = poly[j]
|
2014-12-13 21:04:36 +08:00
|
|
|
for k in range(len(r)):
|
2013-10-17 16:17:41 +08:00
|
|
|
r[k] = (r[k][0] + 500., r[k][1] + 500.)
|
2008-08-06 02:13:06 +08:00
|
|
|
poly[j] = r
|
Refactored the GEOS interface. Improvements include:
* Geometries now allow list-like manipulation, e.g., can add, insert, delete vertexes (or other geometries in collections) like Python lists. Thanks, Aryeh Leib Taurog.
* Added support for GEOS prepared geometries via `prepared` property. Prepared geometries significantly speed up certain operations.
* Added support for GEOS cascaded union as `MultiPolygon.cascaded_union` property.
* Added support for GEOS line merge as `merged` property on `LineString`, and `MultiLineString` geometries. Thanks, Paul Smith.
* No longer use the deprecated C API for serialization to/from WKB and WKT. Now use the GEOS I/O classes, which are now exposed as `WKTReader`, `WKTWriter`, `WKBReader`, and `WKBWriter` (which supports 3D and SRID inclusion)
* Moved each type of geometry to their own module, eliminating the cluttered `geometries.py`.
* Internally, all C API methods are explicitly called from a module rather than a star import.
Fixed #9557, #9877, #10222
git-svn-id: http://code.djangoproject.com/svn/django/trunk@10131 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2009-03-24 08:12:21 +08:00
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
self.assertNotEqual(mpoly[i], poly)
|
|
|
|
# Testing the assignment
|
|
|
|
mpoly[i] = poly
|
2013-11-03 05:02:56 +08:00
|
|
|
str(poly) # Still accessible
|
2008-08-06 02:13:06 +08:00
|
|
|
self.assertEqual(mpoly[i], poly)
|
|
|
|
self.assertNotEqual(mpoly[i], old_poly)
|
|
|
|
|
|
|
|
# Extreme (!!) __setitem__ -- no longer works, have to detect
|
|
|
|
# in the first object that __setitem__ is called in the subsequent
|
|
|
|
# objects -- maybe mpoly[0, 0, 0] = (3.14, 2.71)?
|
2015-02-06 02:25:34 +08:00
|
|
|
# mpoly[0][0][0] = (3.14, 2.71)
|
|
|
|
# self.assertEqual((3.14, 2.71), mpoly[0][0][0])
|
2008-08-06 02:13:06 +08:00
|
|
|
# Doing it more slowly..
|
2015-02-06 02:25:34 +08:00
|
|
|
# self.assertEqual((3.14, 2.71), mpoly[0].shell[0])
|
|
|
|
# del mpoly
|
Refactored the GEOS interface. Improvements include:
* Geometries now allow list-like manipulation, e.g., can add, insert, delete vertexes (or other geometries in collections) like Python lists. Thanks, Aryeh Leib Taurog.
* Added support for GEOS prepared geometries via `prepared` property. Prepared geometries significantly speed up certain operations.
* Added support for GEOS cascaded union as `MultiPolygon.cascaded_union` property.
* Added support for GEOS line merge as `merged` property on `LineString`, and `MultiLineString` geometries. Thanks, Paul Smith.
* No longer use the deprecated C API for serialization to/from WKB and WKT. Now use the GEOS I/O classes, which are now exposed as `WKTReader`, `WKTWriter`, `WKBReader`, and `WKBWriter` (which supports 3D and SRID inclusion)
* Moved each type of geometry to their own module, eliminating the cluttered `geometries.py`.
* Internally, all C API methods are explicitly called from a module rather than a star import.
Fixed #9557, #9877, #10222
git-svn-id: http://code.djangoproject.com/svn/django/trunk@10131 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2009-03-24 08:12:21 +08:00
|
|
|
|
2015-11-03 03:35:50 +08:00
|
|
|
def test_point_list_assignment(self):
|
|
|
|
p = Point(0, 0)
|
|
|
|
|
|
|
|
p[:] = (1, 2, 3)
|
|
|
|
self.assertEqual(p, Point(1, 2, 3))
|
|
|
|
|
2015-11-24 17:21:32 +08:00
|
|
|
p[:] = ()
|
|
|
|
self.assertEqual(p.wkt, Point())
|
|
|
|
|
2015-11-03 03:35:50 +08:00
|
|
|
p[:] = (1, 2)
|
|
|
|
self.assertEqual(p.wkt, Point(1, 2))
|
|
|
|
|
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
p[:] = (1,)
|
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
p[:] = (1, 2, 3, 4, 5)
|
|
|
|
|
|
|
|
def test_linestring_list_assignment(self):
|
|
|
|
ls = LineString((0, 0), (1, 1))
|
|
|
|
|
2015-11-24 17:21:32 +08:00
|
|
|
ls[:] = ()
|
|
|
|
self.assertEqual(ls, LineString())
|
|
|
|
|
2015-11-03 03:35:50 +08:00
|
|
|
ls[:] = ((0, 0), (1, 1), (2, 2))
|
|
|
|
self.assertEqual(ls, LineString((0, 0), (1, 1), (2, 2)))
|
|
|
|
|
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
ls[:] = (1,)
|
|
|
|
|
|
|
|
def test_linearring_list_assignment(self):
|
|
|
|
ls = LinearRing((0, 0), (0, 1), (1, 1), (0, 0))
|
|
|
|
|
2015-11-24 17:21:32 +08:00
|
|
|
ls[:] = ()
|
|
|
|
self.assertEqual(ls, LinearRing())
|
|
|
|
|
2015-11-03 03:35:50 +08:00
|
|
|
ls[:] = ((0, 0), (0, 1), (1, 1), (1, 0), (0, 0))
|
|
|
|
self.assertEqual(ls, LinearRing((0, 0), (0, 1), (1, 1), (1, 0), (0, 0)))
|
|
|
|
|
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
ls[:] = ((0, 0), (1, 1), (2, 2))
|
|
|
|
|
2015-11-24 17:21:32 +08:00
|
|
|
def test_polygon_list_assignment(self):
|
|
|
|
pol = Polygon()
|
|
|
|
|
|
|
|
pol[:] = (((0, 0), (0, 1), (1, 1), (1, 0), (0, 0)),)
|
|
|
|
self.assertEqual(pol, Polygon(((0, 0), (0, 1), (1, 1), (1, 0), (0, 0)),))
|
|
|
|
|
|
|
|
pol[:] = ()
|
|
|
|
self.assertEqual(pol, Polygon())
|
|
|
|
|
|
|
|
def test_geometry_collection_list_assignment(self):
|
|
|
|
p = Point()
|
|
|
|
gc = GeometryCollection()
|
|
|
|
|
|
|
|
gc[:] = [p]
|
|
|
|
self.assertEqual(gc, GeometryCollection(p))
|
|
|
|
|
|
|
|
gc[:] = ()
|
|
|
|
self.assertEqual(gc, GeometryCollection())
|
|
|
|
|
2012-05-28 17:15:31 +08:00
|
|
|
def test_threed(self):
|
2008-08-06 02:13:06 +08:00
|
|
|
"Testing three-dimensional geometries."
|
|
|
|
# Testing a 3D Point
|
|
|
|
pnt = Point(2, 3, 8)
|
2013-10-25 01:30:03 +08:00
|
|
|
self.assertEqual((2., 3., 8.), pnt.coords)
|
2015-11-05 13:37:51 +08:00
|
|
|
with self.assertRaises(TypeError):
|
|
|
|
pnt.tuple = (1., 2.)
|
2013-10-25 01:30:03 +08:00
|
|
|
pnt.coords = (1., 2., 3.)
|
|
|
|
self.assertEqual((1., 2., 3.), pnt.coords)
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
# Testing a 3D LineString
|
|
|
|
ls = LineString((2., 3., 8.), (50., 250., -117.))
|
2013-10-25 01:30:03 +08:00
|
|
|
self.assertEqual(((2., 3., 8.), (50., 250., -117.)), ls.tuple)
|
2016-01-17 19:26:39 +08:00
|
|
|
with self.assertRaises(TypeError):
|
|
|
|
ls.__setitem__(0, (1., 2.))
|
2013-10-25 01:30:03 +08:00
|
|
|
ls[0] = (1., 2., 3.)
|
|
|
|
self.assertEqual((1., 2., 3.), ls[0])
|
Refactored the GEOS interface. Improvements include:
* Geometries now allow list-like manipulation, e.g., can add, insert, delete vertexes (or other geometries in collections) like Python lists. Thanks, Aryeh Leib Taurog.
* Added support for GEOS prepared geometries via `prepared` property. Prepared geometries significantly speed up certain operations.
* Added support for GEOS cascaded union as `MultiPolygon.cascaded_union` property.
* Added support for GEOS line merge as `merged` property on `LineString`, and `MultiLineString` geometries. Thanks, Paul Smith.
* No longer use the deprecated C API for serialization to/from WKB and WKT. Now use the GEOS I/O classes, which are now exposed as `WKTReader`, `WKTWriter`, `WKBReader`, and `WKBWriter` (which supports 3D and SRID inclusion)
* Moved each type of geometry to their own module, eliminating the cluttered `geometries.py`.
* Internally, all C API methods are explicitly called from a module rather than a star import.
Fixed #9557, #9877, #10222
git-svn-id: http://code.djangoproject.com/svn/django/trunk@10131 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2009-03-24 08:12:21 +08:00
|
|
|
|
2012-05-28 17:15:31 +08:00
|
|
|
def test_distance(self):
|
2008-08-06 02:13:06 +08:00
|
|
|
"Testing the distance() function."
|
Refactored the GEOS interface. Improvements include:
* Geometries now allow list-like manipulation, e.g., can add, insert, delete vertexes (or other geometries in collections) like Python lists. Thanks, Aryeh Leib Taurog.
* Added support for GEOS prepared geometries via `prepared` property. Prepared geometries significantly speed up certain operations.
* Added support for GEOS cascaded union as `MultiPolygon.cascaded_union` property.
* Added support for GEOS line merge as `merged` property on `LineString`, and `MultiLineString` geometries. Thanks, Paul Smith.
* No longer use the deprecated C API for serialization to/from WKB and WKT. Now use the GEOS I/O classes, which are now exposed as `WKTReader`, `WKTWriter`, `WKBReader`, and `WKBWriter` (which supports 3D and SRID inclusion)
* Moved each type of geometry to their own module, eliminating the cluttered `geometries.py`.
* Internally, all C API methods are explicitly called from a module rather than a star import.
Fixed #9557, #9877, #10222
git-svn-id: http://code.djangoproject.com/svn/django/trunk@10131 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2009-03-24 08:12:21 +08:00
|
|
|
# Distance to self should be 0.
|
2008-08-06 02:13:06 +08:00
|
|
|
pnt = Point(0, 0)
|
|
|
|
self.assertEqual(0.0, pnt.distance(Point(0, 0)))
|
Refactored the GEOS interface. Improvements include:
* Geometries now allow list-like manipulation, e.g., can add, insert, delete vertexes (or other geometries in collections) like Python lists. Thanks, Aryeh Leib Taurog.
* Added support for GEOS prepared geometries via `prepared` property. Prepared geometries significantly speed up certain operations.
* Added support for GEOS cascaded union as `MultiPolygon.cascaded_union` property.
* Added support for GEOS line merge as `merged` property on `LineString`, and `MultiLineString` geometries. Thanks, Paul Smith.
* No longer use the deprecated C API for serialization to/from WKB and WKT. Now use the GEOS I/O classes, which are now exposed as `WKTReader`, `WKTWriter`, `WKBReader`, and `WKBWriter` (which supports 3D and SRID inclusion)
* Moved each type of geometry to their own module, eliminating the cluttered `geometries.py`.
* Internally, all C API methods are explicitly called from a module rather than a star import.
Fixed #9557, #9877, #10222
git-svn-id: http://code.djangoproject.com/svn/django/trunk@10131 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2009-03-24 08:12:21 +08:00
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
# Distance should be 1
|
|
|
|
self.assertEqual(1.0, pnt.distance(Point(0, 1)))
|
|
|
|
|
|
|
|
# Distance should be ~ sqrt(2)
|
|
|
|
self.assertAlmostEqual(1.41421356237, pnt.distance(Point(1, 1)), 11)
|
|
|
|
|
|
|
|
# Distances are from the closest vertex in each geometry --
|
|
|
|
# should be 3 (distance from (2, 2) to (5, 2)).
|
|
|
|
ls1 = LineString((0, 0), (1, 1), (2, 2))
|
|
|
|
ls2 = LineString((5, 2), (6, 1), (7, 0))
|
|
|
|
self.assertEqual(3, ls1.distance(ls2))
|
|
|
|
|
2012-05-28 17:15:31 +08:00
|
|
|
def test_length(self):
|
2008-08-06 02:13:06 +08:00
|
|
|
"Testing the length property."
|
|
|
|
# Points have 0 length.
|
|
|
|
pnt = Point(0, 0)
|
|
|
|
self.assertEqual(0.0, pnt.length)
|
Refactored the GEOS interface. Improvements include:
* Geometries now allow list-like manipulation, e.g., can add, insert, delete vertexes (or other geometries in collections) like Python lists. Thanks, Aryeh Leib Taurog.
* Added support for GEOS prepared geometries via `prepared` property. Prepared geometries significantly speed up certain operations.
* Added support for GEOS cascaded union as `MultiPolygon.cascaded_union` property.
* Added support for GEOS line merge as `merged` property on `LineString`, and `MultiLineString` geometries. Thanks, Paul Smith.
* No longer use the deprecated C API for serialization to/from WKB and WKT. Now use the GEOS I/O classes, which are now exposed as `WKTReader`, `WKTWriter`, `WKBReader`, and `WKBWriter` (which supports 3D and SRID inclusion)
* Moved each type of geometry to their own module, eliminating the cluttered `geometries.py`.
* Internally, all C API methods are explicitly called from a module rather than a star import.
Fixed #9557, #9877, #10222
git-svn-id: http://code.djangoproject.com/svn/django/trunk@10131 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2009-03-24 08:12:21 +08:00
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
# Should be ~ sqrt(2)
|
|
|
|
ls = LineString((0, 0), (1, 1))
|
|
|
|
self.assertAlmostEqual(1.41421356237, ls.length, 11)
|
|
|
|
|
2014-03-02 22:25:53 +08:00
|
|
|
# Should be circumference of Polygon
|
2008-08-06 02:13:06 +08:00
|
|
|
poly = Polygon(LinearRing((0, 0), (0, 1), (1, 1), (1, 0), (0, 0)))
|
|
|
|
self.assertEqual(4.0, poly.length)
|
|
|
|
|
|
|
|
# Should be sum of each element's length in collection.
|
|
|
|
mpoly = MultiPolygon(poly.clone(), poly)
|
|
|
|
self.assertEqual(8.0, mpoly.length)
|
|
|
|
|
2012-05-28 17:15:31 +08:00
|
|
|
def test_emptyCollections(self):
|
2008-08-06 02:13:06 +08:00
|
|
|
"Testing empty geometries and collections."
|
2015-11-24 17:21:32 +08:00
|
|
|
geoms = [
|
|
|
|
GeometryCollection([]),
|
|
|
|
fromstr('GEOMETRYCOLLECTION EMPTY'),
|
|
|
|
GeometryCollection(),
|
|
|
|
fromstr('POINT EMPTY'),
|
|
|
|
Point(),
|
|
|
|
fromstr('LINESTRING EMPTY'),
|
|
|
|
LineString(),
|
|
|
|
fromstr('POLYGON EMPTY'),
|
|
|
|
Polygon(),
|
|
|
|
fromstr('MULTILINESTRING EMPTY'),
|
|
|
|
MultiLineString(),
|
|
|
|
fromstr('MULTIPOLYGON EMPTY'),
|
|
|
|
MultiPolygon(()),
|
|
|
|
MultiPolygon(),
|
|
|
|
]
|
|
|
|
|
|
|
|
if numpy:
|
|
|
|
geoms.append(LineString(numpy.array([])))
|
|
|
|
|
|
|
|
for g in geoms:
|
2016-06-17 02:19:18 +08:00
|
|
|
self.assertIs(g.empty, True)
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
# Testing len() and num_geom.
|
|
|
|
if isinstance(g, Polygon):
|
2013-11-03 05:02:56 +08:00
|
|
|
self.assertEqual(1, len(g)) # Has one empty linear ring
|
2008-08-06 02:13:06 +08:00
|
|
|
self.assertEqual(1, g.num_geom)
|
|
|
|
self.assertEqual(0, len(g[0]))
|
|
|
|
elif isinstance(g, (Point, LineString)):
|
|
|
|
self.assertEqual(1, g.num_geom)
|
|
|
|
self.assertEqual(0, len(g))
|
|
|
|
else:
|
|
|
|
self.assertEqual(0, g.num_geom)
|
|
|
|
self.assertEqual(0, len(g))
|
|
|
|
|
|
|
|
# Testing __getitem__ (doesn't work on Point or Polygon)
|
|
|
|
if isinstance(g, Point):
|
2015-11-05 11:45:42 +08:00
|
|
|
with self.assertRaises(IndexError):
|
|
|
|
g.x
|
2008-08-06 02:13:06 +08:00
|
|
|
elif isinstance(g, Polygon):
|
|
|
|
lr = g.shell
|
|
|
|
self.assertEqual('LINEARRING EMPTY', lr.wkt)
|
|
|
|
self.assertEqual(0, len(lr))
|
2016-06-17 02:19:18 +08:00
|
|
|
self.assertIs(lr.empty, True)
|
2016-01-17 19:26:39 +08:00
|
|
|
with self.assertRaises(IndexError):
|
|
|
|
lr.__getitem__(0)
|
2008-08-06 02:13:06 +08:00
|
|
|
else:
|
2016-01-17 19:26:39 +08:00
|
|
|
with self.assertRaises(IndexError):
|
|
|
|
g.__getitem__(0)
|
2008-08-06 02:13:06 +08:00
|
|
|
|
2015-11-02 23:20:53 +08:00
|
|
|
def test_collection_dims(self):
|
|
|
|
gc = GeometryCollection([])
|
|
|
|
self.assertEqual(gc.dims, -1)
|
|
|
|
|
|
|
|
gc = GeometryCollection(Point(0, 0))
|
|
|
|
self.assertEqual(gc.dims, 0)
|
|
|
|
|
|
|
|
gc = GeometryCollection(LineString((0, 0), (1, 1)), Point(0, 0))
|
|
|
|
self.assertEqual(gc.dims, 1)
|
|
|
|
|
|
|
|
gc = GeometryCollection(LineString((0, 0), (1, 1)), Polygon(((0, 0), (0, 1), (1, 1), (0, 0))), Point(0, 0))
|
|
|
|
self.assertEqual(gc.dims, 2)
|
|
|
|
|
2012-05-28 17:15:31 +08:00
|
|
|
def test_collections_of_collections(self):
|
Refactored the GEOS interface. Improvements include:
* Geometries now allow list-like manipulation, e.g., can add, insert, delete vertexes (or other geometries in collections) like Python lists. Thanks, Aryeh Leib Taurog.
* Added support for GEOS prepared geometries via `prepared` property. Prepared geometries significantly speed up certain operations.
* Added support for GEOS cascaded union as `MultiPolygon.cascaded_union` property.
* Added support for GEOS line merge as `merged` property on `LineString`, and `MultiLineString` geometries. Thanks, Paul Smith.
* No longer use the deprecated C API for serialization to/from WKB and WKT. Now use the GEOS I/O classes, which are now exposed as `WKTReader`, `WKTWriter`, `WKBReader`, and `WKBWriter` (which supports 3D and SRID inclusion)
* Moved each type of geometry to their own module, eliminating the cluttered `geometries.py`.
* Internally, all C API methods are explicitly called from a module rather than a star import.
Fixed #9557, #9877, #10222
git-svn-id: http://code.djangoproject.com/svn/django/trunk@10131 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2009-03-24 08:12:21 +08:00
|
|
|
"Testing GeometryCollection handling of other collections."
|
|
|
|
# Creating a GeometryCollection WKT string composed of other
|
|
|
|
# collections and polygons.
|
2010-11-02 05:56:48 +08:00
|
|
|
coll = [mp.wkt for mp in self.geometries.multipolygons if mp.valid]
|
2013-08-30 07:20:00 +08:00
|
|
|
coll.extend(mls.wkt for mls in self.geometries.multilinestrings)
|
|
|
|
coll.extend(p.wkt for p in self.geometries.polygons)
|
|
|
|
coll.extend(mp.wkt for mp in self.geometries.multipoints)
|
Refactored the GEOS interface. Improvements include:
* Geometries now allow list-like manipulation, e.g., can add, insert, delete vertexes (or other geometries in collections) like Python lists. Thanks, Aryeh Leib Taurog.
* Added support for GEOS prepared geometries via `prepared` property. Prepared geometries significantly speed up certain operations.
* Added support for GEOS cascaded union as `MultiPolygon.cascaded_union` property.
* Added support for GEOS line merge as `merged` property on `LineString`, and `MultiLineString` geometries. Thanks, Paul Smith.
* No longer use the deprecated C API for serialization to/from WKB and WKT. Now use the GEOS I/O classes, which are now exposed as `WKTReader`, `WKTWriter`, `WKBReader`, and `WKBWriter` (which supports 3D and SRID inclusion)
* Moved each type of geometry to their own module, eliminating the cluttered `geometries.py`.
* Internally, all C API methods are explicitly called from a module rather than a star import.
Fixed #9557, #9877, #10222
git-svn-id: http://code.djangoproject.com/svn/django/trunk@10131 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2009-03-24 08:12:21 +08:00
|
|
|
gc_wkt = 'GEOMETRYCOLLECTION(%s)' % ','.join(coll)
|
|
|
|
|
|
|
|
# Should construct ok from WKT
|
|
|
|
gc1 = GEOSGeometry(gc_wkt)
|
|
|
|
|
|
|
|
# Should also construct ok from individual geometry arguments.
|
|
|
|
gc2 = GeometryCollection(*tuple(g for g in gc1))
|
|
|
|
|
|
|
|
# And, they should be equal.
|
|
|
|
self.assertEqual(gc1, gc2)
|
|
|
|
|
2012-05-28 17:15:31 +08:00
|
|
|
def test_gdal(self):
|
2008-08-06 02:13:06 +08:00
|
|
|
"Testing `ogr` and `srs` properties."
|
|
|
|
g1 = fromstr('POINT(5 23)')
|
2012-09-22 17:55:37 +08:00
|
|
|
self.assertIsInstance(g1.ogr, gdal.OGRGeometry)
|
|
|
|
self.assertIsNone(g1.srs)
|
|
|
|
|
2013-12-24 22:57:13 +08:00
|
|
|
g1_3d = fromstr('POINT(5 23 8)')
|
|
|
|
self.assertIsInstance(g1_3d.ogr, gdal.OGRGeometry)
|
|
|
|
self.assertEqual(g1_3d.ogr.z, 8)
|
Refactored the GEOS interface. Improvements include:
* Geometries now allow list-like manipulation, e.g., can add, insert, delete vertexes (or other geometries in collections) like Python lists. Thanks, Aryeh Leib Taurog.
* Added support for GEOS prepared geometries via `prepared` property. Prepared geometries significantly speed up certain operations.
* Added support for GEOS cascaded union as `MultiPolygon.cascaded_union` property.
* Added support for GEOS line merge as `merged` property on `LineString`, and `MultiLineString` geometries. Thanks, Paul Smith.
* No longer use the deprecated C API for serialization to/from WKB and WKT. Now use the GEOS I/O classes, which are now exposed as `WKTReader`, `WKTWriter`, `WKBReader`, and `WKBWriter` (which supports 3D and SRID inclusion)
* Moved each type of geometry to their own module, eliminating the cluttered `geometries.py`.
* Internally, all C API methods are explicitly called from a module rather than a star import.
Fixed #9557, #9877, #10222
git-svn-id: http://code.djangoproject.com/svn/django/trunk@10131 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2009-03-24 08:12:21 +08:00
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
g2 = fromstr('LINESTRING(0 0, 5 5, 23 23)', srid=4326)
|
2012-09-22 17:55:37 +08:00
|
|
|
self.assertIsInstance(g2.ogr, gdal.OGRGeometry)
|
|
|
|
self.assertIsInstance(g2.srs, gdal.SpatialReference)
|
2008-08-06 02:13:06 +08:00
|
|
|
self.assertEqual(g2.hex, g2.ogr.hex)
|
|
|
|
self.assertEqual('WGS 84', g2.srs.name)
|
|
|
|
|
2012-05-28 17:15:31 +08:00
|
|
|
def test_copy(self):
|
2008-08-06 02:13:06 +08:00
|
|
|
"Testing use with the Python `copy` module."
|
2011-03-28 09:40:43 +08:00
|
|
|
import copy
|
2008-08-06 02:13:06 +08:00
|
|
|
poly = GEOSGeometry('POLYGON((0 0, 0 23, 23 23, 23 0, 0 0), (5 5, 5 10, 10 10, 10 5, 5 5))')
|
|
|
|
cpy1 = copy.copy(poly)
|
|
|
|
cpy2 = copy.deepcopy(poly)
|
|
|
|
self.assertNotEqual(poly._ptr, cpy1._ptr)
|
|
|
|
self.assertNotEqual(poly._ptr, cpy2._ptr)
|
|
|
|
|
2012-05-28 17:15:31 +08:00
|
|
|
def test_transform(self):
|
2008-08-06 02:13:06 +08:00
|
|
|
"Testing `transform` method."
|
|
|
|
orig = GEOSGeometry('POINT (-104.609 38.255)', 4326)
|
|
|
|
trans = GEOSGeometry('POINT (992385.4472045 481455.4944650)', 2774)
|
|
|
|
|
|
|
|
# Using a srid, a SpatialReference object, and a CoordTransform object
|
|
|
|
# for transformations.
|
|
|
|
t1, t2, t3 = orig.clone(), orig.clone(), orig.clone()
|
|
|
|
t1.transform(trans.srid)
|
Refactored the GEOS interface. Improvements include:
* Geometries now allow list-like manipulation, e.g., can add, insert, delete vertexes (or other geometries in collections) like Python lists. Thanks, Aryeh Leib Taurog.
* Added support for GEOS prepared geometries via `prepared` property. Prepared geometries significantly speed up certain operations.
* Added support for GEOS cascaded union as `MultiPolygon.cascaded_union` property.
* Added support for GEOS line merge as `merged` property on `LineString`, and `MultiLineString` geometries. Thanks, Paul Smith.
* No longer use the deprecated C API for serialization to/from WKB and WKT. Now use the GEOS I/O classes, which are now exposed as `WKTReader`, `WKTWriter`, `WKBReader`, and `WKBWriter` (which supports 3D and SRID inclusion)
* Moved each type of geometry to their own module, eliminating the cluttered `geometries.py`.
* Internally, all C API methods are explicitly called from a module rather than a star import.
Fixed #9557, #9877, #10222
git-svn-id: http://code.djangoproject.com/svn/django/trunk@10131 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2009-03-24 08:12:21 +08:00
|
|
|
t2.transform(gdal.SpatialReference('EPSG:2774'))
|
|
|
|
ct = gdal.CoordTransform(gdal.SpatialReference('WGS84'), gdal.SpatialReference(2774))
|
2008-08-06 02:13:06 +08:00
|
|
|
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)
|
|
|
|
|
2012-09-22 17:55:37 +08:00
|
|
|
def test_transform_3d(self):
|
|
|
|
p3d = GEOSGeometry('POINT (5 23 100)', 4326)
|
|
|
|
p3d.transform(2774)
|
2018-09-18 00:03:30 +08:00
|
|
|
self.assertAlmostEqual(p3d.z, 100, 3)
|
2012-09-22 17:55:37 +08:00
|
|
|
|
2012-05-28 17:15:31 +08:00
|
|
|
def test_transform_noop(self):
|
2010-12-23 01:43:30 +08:00
|
|
|
""" Testing `transform` method (SRID match) """
|
|
|
|
# transform() should no-op if source & dest SRIDs match,
|
|
|
|
# regardless of whether GDAL is available.
|
2015-07-18 21:27:59 +08:00
|
|
|
g = GEOSGeometry('POINT (-104.609 38.255)', 4326)
|
|
|
|
gt = g.tuple
|
|
|
|
g.transform(4326)
|
|
|
|
self.assertEqual(g.tuple, gt)
|
|
|
|
self.assertEqual(g.srid, 4326)
|
|
|
|
|
|
|
|
g = GEOSGeometry('POINT (-104.609 38.255)', 4326)
|
|
|
|
g1 = g.transform(4326, clone=True)
|
|
|
|
self.assertEqual(g1.tuple, g.tuple)
|
|
|
|
self.assertEqual(g1.srid, 4326)
|
|
|
|
self.assertIsNot(g1, g, "Clone didn't happen")
|
|
|
|
|
2012-05-28 17:15:31 +08:00
|
|
|
def test_transform_nosrid(self):
|
2012-04-13 00:35:28 +08:00
|
|
|
""" Testing `transform` method (no SRID or negative SRID) """
|
2010-12-23 01:43:30 +08:00
|
|
|
|
2012-04-13 00:35:28 +08:00
|
|
|
g = GEOSGeometry('POINT (-104.609 38.255)', srid=None)
|
2016-01-17 19:26:39 +08:00
|
|
|
with self.assertRaises(GEOSException):
|
|
|
|
g.transform(2774)
|
2010-12-23 01:43:30 +08:00
|
|
|
|
2012-04-13 00:35:28 +08:00
|
|
|
g = GEOSGeometry('POINT (-104.609 38.255)', srid=None)
|
2016-01-17 19:26:39 +08:00
|
|
|
with self.assertRaises(GEOSException):
|
|
|
|
g.transform(2774, clone=True)
|
2010-12-23 01:43:30 +08:00
|
|
|
|
2012-04-13 00:35:28 +08:00
|
|
|
g = GEOSGeometry('POINT (-104.609 38.255)', srid=-1)
|
2016-01-17 19:26:39 +08:00
|
|
|
with self.assertRaises(GEOSException):
|
|
|
|
g.transform(2774)
|
2010-12-23 01:43:30 +08:00
|
|
|
|
2012-04-13 00:35:28 +08:00
|
|
|
g = GEOSGeometry('POINT (-104.609 38.255)', srid=-1)
|
2016-01-17 19:26:39 +08:00
|
|
|
with self.assertRaises(GEOSException):
|
|
|
|
g.transform(2774, clone=True)
|
2010-12-23 01:43:30 +08:00
|
|
|
|
2012-05-28 17:15:31 +08:00
|
|
|
def test_extent(self):
|
2008-08-06 02:13:06 +08:00
|
|
|
"Testing `extent` method."
|
|
|
|
# The xmin, ymin, xmax, ymax of the MultiPoint should be returned.
|
|
|
|
mp = MultiPoint(Point(5, 23), Point(0, 0), Point(10, 50))
|
|
|
|
self.assertEqual((0.0, 0.0, 10.0, 50.0), mp.extent)
|
|
|
|
pnt = Point(5.23, 17.8)
|
|
|
|
# Extent of points is just the point itself repeated.
|
|
|
|
self.assertEqual((5.23, 17.8, 5.23, 17.8), pnt.extent)
|
|
|
|
# Testing on the 'real world' Polygon.
|
2010-11-02 05:56:48 +08:00
|
|
|
poly = fromstr(self.geometries.polygons[3].wkt)
|
2008-08-06 02:13:06 +08:00
|
|
|
ring = poly.shell
|
|
|
|
x, y = ring.x, ring.y
|
|
|
|
xmin, ymin = min(x), min(y)
|
|
|
|
xmax, ymax = max(x), max(y)
|
|
|
|
self.assertEqual((xmin, ymin, xmax, ymax), poly.extent)
|
|
|
|
|
2012-05-28 17:15:31 +08:00
|
|
|
def test_pickle(self):
|
2008-08-06 02:13:06 +08:00
|
|
|
"Testing pickling and unpickling support."
|
Refactored the GEOS interface. Improvements include:
* Geometries now allow list-like manipulation, e.g., can add, insert, delete vertexes (or other geometries in collections) like Python lists. Thanks, Aryeh Leib Taurog.
* Added support for GEOS prepared geometries via `prepared` property. Prepared geometries significantly speed up certain operations.
* Added support for GEOS cascaded union as `MultiPolygon.cascaded_union` property.
* Added support for GEOS line merge as `merged` property on `LineString`, and `MultiLineString` geometries. Thanks, Paul Smith.
* No longer use the deprecated C API for serialization to/from WKB and WKT. Now use the GEOS I/O classes, which are now exposed as `WKTReader`, `WKTWriter`, `WKBReader`, and `WKBWriter` (which supports 3D and SRID inclusion)
* Moved each type of geometry to their own module, eliminating the cluttered `geometries.py`.
* Internally, all C API methods are explicitly called from a module rather than a star import.
Fixed #9557, #9877, #10222
git-svn-id: http://code.djangoproject.com/svn/django/trunk@10131 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2009-03-24 08:12:21 +08:00
|
|
|
# Creating a list of test geometries for pickling,
|
2008-08-06 02:13:06 +08:00
|
|
|
# and setting the SRID on some of them.
|
|
|
|
def get_geoms(lst, srid=None):
|
|
|
|
return [GEOSGeometry(tg.wkt, srid) for tg in lst]
|
2010-11-02 05:56:48 +08:00
|
|
|
tgeoms = get_geoms(self.geometries.points)
|
|
|
|
tgeoms.extend(get_geoms(self.geometries.multilinestrings, 4326))
|
|
|
|
tgeoms.extend(get_geoms(self.geometries.polygons, 3084))
|
2014-04-18 03:10:41 +08:00
|
|
|
tgeoms.extend(get_geoms(self.geometries.multipolygons, 3857))
|
2018-07-09 23:02:12 +08:00
|
|
|
tgeoms.append(Point(srid=4326))
|
|
|
|
tgeoms.append(Point())
|
2008-08-06 02:13:06 +08:00
|
|
|
for geom in tgeoms:
|
2017-01-07 19:11:46 +08:00
|
|
|
s1 = pickle.dumps(geom)
|
|
|
|
g1 = pickle.loads(s1)
|
|
|
|
self.assertEqual(geom, g1)
|
|
|
|
self.assertEqual(geom.srid, g1.srid)
|
2008-08-06 02:13:06 +08:00
|
|
|
|
2012-05-28 17:15:31 +08:00
|
|
|
def test_prepared(self):
|
Refactored the GEOS interface. Improvements include:
* Geometries now allow list-like manipulation, e.g., can add, insert, delete vertexes (or other geometries in collections) like Python lists. Thanks, Aryeh Leib Taurog.
* Added support for GEOS prepared geometries via `prepared` property. Prepared geometries significantly speed up certain operations.
* Added support for GEOS cascaded union as `MultiPolygon.cascaded_union` property.
* Added support for GEOS line merge as `merged` property on `LineString`, and `MultiLineString` geometries. Thanks, Paul Smith.
* No longer use the deprecated C API for serialization to/from WKB and WKT. Now use the GEOS I/O classes, which are now exposed as `WKTReader`, `WKTWriter`, `WKBReader`, and `WKBWriter` (which supports 3D and SRID inclusion)
* Moved each type of geometry to their own module, eliminating the cluttered `geometries.py`.
* Internally, all C API methods are explicitly called from a module rather than a star import.
Fixed #9557, #9877, #10222
git-svn-id: http://code.djangoproject.com/svn/django/trunk@10131 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2009-03-24 08:12:21 +08:00
|
|
|
"Testing PreparedGeometry support."
|
|
|
|
# Creating a simple multipolygon and getting a prepared version.
|
|
|
|
mpoly = GEOSGeometry('MULTIPOLYGON(((0 0,0 5,5 5,5 0,0 0)),((5 5,5 10,10 10,10 5,5 5)))')
|
|
|
|
prep = mpoly.prepared
|
|
|
|
|
|
|
|
# A set of test points.
|
|
|
|
pnts = [Point(5, 5), Point(7.5, 7.5), Point(2.5, 7.5)]
|
2015-11-20 18:11:25 +08:00
|
|
|
for pnt in pnts:
|
Refactored the GEOS interface. Improvements include:
* Geometries now allow list-like manipulation, e.g., can add, insert, delete vertexes (or other geometries in collections) like Python lists. Thanks, Aryeh Leib Taurog.
* Added support for GEOS prepared geometries via `prepared` property. Prepared geometries significantly speed up certain operations.
* Added support for GEOS cascaded union as `MultiPolygon.cascaded_union` property.
* Added support for GEOS line merge as `merged` property on `LineString`, and `MultiLineString` geometries. Thanks, Paul Smith.
* No longer use the deprecated C API for serialization to/from WKB and WKT. Now use the GEOS I/O classes, which are now exposed as `WKTReader`, `WKTWriter`, `WKBReader`, and `WKBWriter` (which supports 3D and SRID inclusion)
* Moved each type of geometry to their own module, eliminating the cluttered `geometries.py`.
* Internally, all C API methods are explicitly called from a module rather than a star import.
Fixed #9557, #9877, #10222
git-svn-id: http://code.djangoproject.com/svn/django/trunk@10131 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2009-03-24 08:12:21 +08:00
|
|
|
# Results should be the same (but faster)
|
|
|
|
self.assertEqual(mpoly.contains(pnt), prep.contains(pnt))
|
|
|
|
self.assertEqual(mpoly.intersects(pnt), prep.intersects(pnt))
|
2015-11-20 18:11:25 +08:00
|
|
|
self.assertEqual(mpoly.covers(pnt), prep.covers(pnt))
|
Refactored the GEOS interface. Improvements include:
* Geometries now allow list-like manipulation, e.g., can add, insert, delete vertexes (or other geometries in collections) like Python lists. Thanks, Aryeh Leib Taurog.
* Added support for GEOS prepared geometries via `prepared` property. Prepared geometries significantly speed up certain operations.
* Added support for GEOS cascaded union as `MultiPolygon.cascaded_union` property.
* Added support for GEOS line merge as `merged` property on `LineString`, and `MultiLineString` geometries. Thanks, Paul Smith.
* No longer use the deprecated C API for serialization to/from WKB and WKT. Now use the GEOS I/O classes, which are now exposed as `WKTReader`, `WKTWriter`, `WKBReader`, and `WKBWriter` (which supports 3D and SRID inclusion)
* Moved each type of geometry to their own module, eliminating the cluttered `geometries.py`.
* Internally, all C API methods are explicitly called from a module rather than a star import.
Fixed #9557, #9877, #10222
git-svn-id: http://code.djangoproject.com/svn/django/trunk@10131 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2009-03-24 08:12:21 +08:00
|
|
|
|
2015-11-01 04:28:46 +08:00
|
|
|
self.assertTrue(prep.crosses(fromstr('LINESTRING(1 1, 15 15)')))
|
|
|
|
self.assertTrue(prep.disjoint(Point(-5, -5)))
|
|
|
|
poly = Polygon(((-1, -1), (1, 1), (1, 0), (-1, -1)))
|
|
|
|
self.assertTrue(prep.overlaps(poly))
|
|
|
|
poly = Polygon(((-5, 0), (-5, 5), (0, 5), (-5, 0)))
|
|
|
|
self.assertTrue(prep.touches(poly))
|
|
|
|
poly = Polygon(((-1, -1), (-1, 11), (11, 11), (11, -1), (-1, -1)))
|
|
|
|
self.assertTrue(prep.within(poly))
|
2013-12-24 23:53:09 +08:00
|
|
|
|
2013-12-24 22:19:25 +08:00
|
|
|
# Original geometry deletion should not crash the prepared one (#21662)
|
|
|
|
del mpoly
|
|
|
|
self.assertTrue(prep.covers(Point(5, 5)))
|
|
|
|
|
2012-05-28 17:15:31 +08:00
|
|
|
def test_line_merge(self):
|
Refactored the GEOS interface. Improvements include:
* Geometries now allow list-like manipulation, e.g., can add, insert, delete vertexes (or other geometries in collections) like Python lists. Thanks, Aryeh Leib Taurog.
* Added support for GEOS prepared geometries via `prepared` property. Prepared geometries significantly speed up certain operations.
* Added support for GEOS cascaded union as `MultiPolygon.cascaded_union` property.
* Added support for GEOS line merge as `merged` property on `LineString`, and `MultiLineString` geometries. Thanks, Paul Smith.
* No longer use the deprecated C API for serialization to/from WKB and WKT. Now use the GEOS I/O classes, which are now exposed as `WKTReader`, `WKTWriter`, `WKBReader`, and `WKBWriter` (which supports 3D and SRID inclusion)
* Moved each type of geometry to their own module, eliminating the cluttered `geometries.py`.
* Internally, all C API methods are explicitly called from a module rather than a star import.
Fixed #9557, #9877, #10222
git-svn-id: http://code.djangoproject.com/svn/django/trunk@10131 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2009-03-24 08:12:21 +08:00
|
|
|
"Testing line merge support"
|
|
|
|
ref_geoms = (fromstr('LINESTRING(1 1, 1 1, 3 3)'),
|
|
|
|
fromstr('MULTILINESTRING((1 1, 3 3), (3 3, 4 2))'),
|
|
|
|
)
|
|
|
|
ref_merged = (fromstr('LINESTRING(1 1, 3 3)'),
|
|
|
|
fromstr('LINESTRING (1 1, 3 3, 4 2)'),
|
|
|
|
)
|
|
|
|
for geom, merged in zip(ref_geoms, ref_merged):
|
|
|
|
self.assertEqual(merged, geom.merged)
|
|
|
|
|
2012-05-28 17:15:31 +08:00
|
|
|
def test_valid_reason(self):
|
2010-11-04 09:29:37 +08:00
|
|
|
"Testing IsValidReason support"
|
|
|
|
|
|
|
|
g = GEOSGeometry("POINT(0 0)")
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertTrue(g.valid)
|
2016-12-29 23:27:49 +08:00
|
|
|
self.assertIsInstance(g.valid_reason, str)
|
2010-11-04 09:29:37 +08:00
|
|
|
self.assertEqual(g.valid_reason, "Valid Geometry")
|
|
|
|
|
|
|
|
g = GEOSGeometry("LINESTRING(0 0, 0 0)")
|
|
|
|
|
2012-10-05 04:41:03 +08:00
|
|
|
self.assertFalse(g.valid)
|
2016-12-29 23:27:49 +08:00
|
|
|
self.assertIsInstance(g.valid_reason, str)
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertTrue(g.valid_reason.startswith("Too few points in geometry component"))
|
2010-11-04 09:29:37 +08:00
|
|
|
|
2012-09-29 17:01:08 +08:00
|
|
|
def test_linearref(self):
|
|
|
|
"Testing linear referencing"
|
|
|
|
|
|
|
|
ls = fromstr('LINESTRING(0 0, 0 10, 10 10, 10 0)')
|
|
|
|
mls = fromstr('MULTILINESTRING((0 0, 0 10), (10 0, 10 10))')
|
|
|
|
|
|
|
|
self.assertEqual(ls.project(Point(0, 20)), 10.0)
|
|
|
|
self.assertEqual(ls.project(Point(7, 6)), 24)
|
2013-11-04 02:08:55 +08:00
|
|
|
self.assertEqual(ls.project_normalized(Point(0, 20)), 1.0 / 3)
|
2012-09-29 17:01:08 +08:00
|
|
|
|
|
|
|
self.assertEqual(ls.interpolate(10), Point(0, 10))
|
|
|
|
self.assertEqual(ls.interpolate(24), Point(10, 6))
|
2013-11-04 02:08:55 +08:00
|
|
|
self.assertEqual(ls.interpolate_normalized(1.0 / 3), Point(0, 10))
|
2012-09-29 17:01:08 +08:00
|
|
|
|
|
|
|
self.assertEqual(mls.project(Point(0, 20)), 10)
|
|
|
|
self.assertEqual(mls.project(Point(7, 6)), 16)
|
|
|
|
|
|
|
|
self.assertEqual(mls.interpolate(9), Point(0, 9))
|
|
|
|
self.assertEqual(mls.interpolate(17), Point(10, 7))
|
|
|
|
|
2016-05-11 17:29:16 +08:00
|
|
|
def test_deconstructible(self):
|
|
|
|
"""
|
|
|
|
Geometry classes should be deconstructible.
|
|
|
|
"""
|
|
|
|
point = Point(4.337844, 50.827537, srid=4326)
|
|
|
|
path, args, kwargs = point.deconstruct()
|
|
|
|
self.assertEqual(path, 'django.contrib.gis.geos.point.Point')
|
|
|
|
self.assertEqual(args, (4.337844, 50.827537))
|
|
|
|
self.assertEqual(kwargs, {'srid': 4326})
|
|
|
|
|
|
|
|
ls = LineString(((0, 0), (1, 1)))
|
|
|
|
path, args, kwargs = ls.deconstruct()
|
|
|
|
self.assertEqual(path, 'django.contrib.gis.geos.linestring.LineString')
|
2016-05-14 00:21:44 +08:00
|
|
|
self.assertEqual(args, (((0, 0), (1, 1)),))
|
2016-05-11 17:29:16 +08:00
|
|
|
self.assertEqual(kwargs, {})
|
|
|
|
|
|
|
|
ls2 = LineString([Point(0, 0), Point(1, 1)], srid=4326)
|
|
|
|
path, args, kwargs = ls2.deconstruct()
|
|
|
|
self.assertEqual(path, 'django.contrib.gis.geos.linestring.LineString')
|
2016-05-14 00:21:44 +08:00
|
|
|
self.assertEqual(args, ([Point(0, 0), Point(1, 1)],))
|
2016-05-11 17:29:16 +08:00
|
|
|
self.assertEqual(kwargs, {'srid': 4326})
|
|
|
|
|
|
|
|
ext_coords = ((0, 0), (0, 1), (1, 1), (1, 0), (0, 0))
|
|
|
|
int_coords = ((0.4, 0.4), (0.4, 0.6), (0.6, 0.6), (0.6, 0.4), (0.4, 0.4))
|
|
|
|
poly = Polygon(ext_coords, int_coords)
|
|
|
|
path, args, kwargs = poly.deconstruct()
|
|
|
|
self.assertEqual(path, 'django.contrib.gis.geos.polygon.Polygon')
|
|
|
|
self.assertEqual(args, (ext_coords, int_coords))
|
|
|
|
self.assertEqual(kwargs, {})
|
|
|
|
|
|
|
|
lr = LinearRing((0, 0), (0, 1), (1, 1), (0, 0))
|
|
|
|
path, args, kwargs = lr.deconstruct()
|
|
|
|
self.assertEqual(path, 'django.contrib.gis.geos.linestring.LinearRing')
|
|
|
|
self.assertEqual(args, ((0, 0), (0, 1), (1, 1), (0, 0)))
|
|
|
|
self.assertEqual(kwargs, {})
|
|
|
|
|
|
|
|
mp = MultiPoint(Point(0, 0), Point(1, 1))
|
|
|
|
path, args, kwargs = mp.deconstruct()
|
|
|
|
self.assertEqual(path, 'django.contrib.gis.geos.collections.MultiPoint')
|
|
|
|
self.assertEqual(args, (Point(0, 0), Point(1, 1)))
|
|
|
|
self.assertEqual(kwargs, {})
|
|
|
|
|
|
|
|
ls1 = LineString((0, 0), (1, 1))
|
|
|
|
ls2 = LineString((2, 2), (3, 3))
|
|
|
|
mls = MultiLineString(ls1, ls2)
|
|
|
|
path, args, kwargs = mls.deconstruct()
|
|
|
|
self.assertEqual(path, 'django.contrib.gis.geos.collections.MultiLineString')
|
|
|
|
self.assertEqual(args, (ls1, ls2))
|
|
|
|
self.assertEqual(kwargs, {})
|
|
|
|
|
|
|
|
p1 = Polygon(((0, 0), (0, 1), (1, 1), (0, 0)))
|
|
|
|
p2 = Polygon(((1, 1), (1, 2), (2, 2), (1, 1)))
|
|
|
|
mp = MultiPolygon(p1, p2)
|
|
|
|
path, args, kwargs = mp.deconstruct()
|
|
|
|
self.assertEqual(path, 'django.contrib.gis.geos.collections.MultiPolygon')
|
2017-12-29 04:07:29 +08:00
|
|
|
self.assertEqual(args, (p1, p2))
|
2016-05-11 17:29:16 +08:00
|
|
|
self.assertEqual(kwargs, {})
|
|
|
|
|
|
|
|
poly = Polygon(((0, 0), (0, 1), (1, 1), (0, 0)))
|
|
|
|
gc = GeometryCollection(Point(0, 0), MultiPoint(Point(0, 0), Point(1, 1)), poly)
|
|
|
|
path, args, kwargs = gc.deconstruct()
|
|
|
|
self.assertEqual(path, 'django.contrib.gis.geos.collections.GeometryCollection')
|
|
|
|
self.assertEqual(args, (Point(0, 0), MultiPoint(Point(0, 0), Point(1, 1)), poly))
|
|
|
|
self.assertEqual(kwargs, {})
|
|
|
|
|
2016-11-30 19:33:58 +08:00
|
|
|
def test_subclassing(self):
|
|
|
|
"""
|
|
|
|
GEOSGeometry subclass may itself be subclassed without being forced-cast
|
|
|
|
to the parent class during `__init__`.
|
|
|
|
"""
|
|
|
|
class ExtendedPolygon(Polygon):
|
2017-02-02 00:41:56 +08:00
|
|
|
def __init__(self, *args, data=0, **kwargs):
|
2017-01-21 21:13:44 +08:00
|
|
|
super().__init__(*args, **kwargs)
|
2016-11-30 19:33:58 +08:00
|
|
|
self._data = data
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return "EXT_POLYGON - data: %d - %s" % (self._data, self.wkt)
|
|
|
|
|
|
|
|
ext_poly = ExtendedPolygon(((0, 0), (0, 1), (1, 1), (0, 0)), data=3)
|
|
|
|
self.assertEqual(type(ext_poly), ExtendedPolygon)
|
|
|
|
# ExtendedPolygon.__str__ should be called (instead of Polygon.__str__).
|
|
|
|
self.assertEqual(str(ext_poly), "EXT_POLYGON - data: 3 - POLYGON ((0 0, 0 1, 1 1, 0 0))")
|
2017-03-30 01:17:31 +08:00
|
|
|
self.assertJSONEqual(
|
|
|
|
ext_poly.json,
|
|
|
|
'{"coordinates": [[[0, 0], [0, 1], [1, 1], [0, 0]]], "type": "Polygon"}',
|
|
|
|
)
|
2016-11-30 19:33:58 +08:00
|
|
|
|
2017-02-16 00:14:21 +08:00
|
|
|
def test_geos_version_tuple(self):
|
2017-07-28 01:52:17 +08:00
|
|
|
versions = (
|
|
|
|
(b'3.0.0rc4-CAPI-1.3.3', (3, 0, 0)),
|
|
|
|
(b'3.0.0-CAPI-1.4.1', (3, 0, 0)),
|
|
|
|
(b'3.4.0dev-CAPI-1.8.0', (3, 4, 0)),
|
|
|
|
(b'3.4.0dev-CAPI-1.8.0 r0', (3, 4, 0)),
|
|
|
|
(b'3.6.2-CAPI-1.10.2 4d2925d6', (3, 6, 2)),
|
|
|
|
)
|
|
|
|
for version_string, version_tuple in versions:
|
|
|
|
with self.subTest(version_string=version_string):
|
|
|
|
with mock.patch('django.contrib.gis.geos.libgeos.geos_version', lambda: version_string):
|
|
|
|
self.assertEqual(geos_version_tuple(), version_tuple)
|
2017-02-16 00:14:21 +08:00
|
|
|
|
2016-06-10 14:50:07 +08:00
|
|
|
def test_from_gml(self):
|
|
|
|
self.assertEqual(
|
|
|
|
GEOSGeometry('POINT(0 0)'),
|
|
|
|
GEOSGeometry.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>'
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
2017-07-25 17:11:43 +08:00
|
|
|
def test_from_ewkt(self):
|
|
|
|
self.assertEqual(GEOSGeometry.from_ewkt('SRID=1;POINT(1 1)'), Point(1, 1, srid=1))
|
|
|
|
self.assertEqual(GEOSGeometry.from_ewkt('POINT(1 1)'), Point(1, 1))
|
|
|
|
|
|
|
|
def test_from_ewkt_empty_string(self):
|
|
|
|
msg = 'Expected WKT but got an empty string.'
|
|
|
|
with self.assertRaisesMessage(ValueError, msg):
|
|
|
|
GEOSGeometry.from_ewkt('')
|
|
|
|
with self.assertRaisesMessage(ValueError, msg):
|
|
|
|
GEOSGeometry.from_ewkt('SRID=1;')
|
|
|
|
|
|
|
|
def test_from_ewkt_invalid_srid(self):
|
|
|
|
msg = 'EWKT has invalid SRID part.'
|
|
|
|
with self.assertRaisesMessage(ValueError, msg):
|
|
|
|
GEOSGeometry.from_ewkt('SRUD=1;POINT(1 1)')
|
|
|
|
with self.assertRaisesMessage(ValueError, msg):
|
|
|
|
GEOSGeometry.from_ewkt('SRID=WGS84;POINT(1 1)')
|
|
|
|
|
2017-09-11 21:13:02 +08:00
|
|
|
def test_fromstr_scientific_wkt(self):
|
|
|
|
self.assertEqual(GEOSGeometry('POINT(1.0e-1 1.0e+1)'), Point(.1, 10))
|
|
|
|
|
2016-11-30 20:39:03 +08:00
|
|
|
def test_normalize(self):
|
|
|
|
g = MultiPoint(Point(0, 0), Point(2, 2), Point(1, 1))
|
|
|
|
self.assertIsNone(g.normalize())
|
|
|
|
self.assertTrue(g.equals_exact(MultiPoint(Point(2, 2), Point(1, 1), Point(0, 0))))
|
|
|
|
|
2016-12-02 11:22:04 +08:00
|
|
|
def test_empty_point(self):
|
|
|
|
p = Point(srid=4326)
|
|
|
|
self.assertEqual(p.ogr.ewkt, p.ewkt)
|
|
|
|
|
2016-12-02 16:56:34 +08:00
|
|
|
self.assertEqual(p.transform(2774, clone=True), Point(srid=2774))
|
|
|
|
p.transform(2774)
|
|
|
|
self.assertEqual(p, Point(srid=2774))
|