1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
|
import unittest
from shapely import wkt
from shapely.geometry import shape
from shapely.geometry.linestring import LineString
from shapely.geometry.multilinestring import MultiLineString
from shapely.geometry.multipoint import MultiPoint
from shapely.geometry.multipolygon import MultiPolygon
from shapely.geometry.polygon import LinearRing, Polygon
class GeoThing:
def __init__(self, d):
self.__geo_interface__ = d
class GeoInterfaceTestCase(unittest.TestCase):
def test_geointerface(self):
# Convert a dictionary
d = {"type": "Point", "coordinates": (0.0, 0.0)}
geom = shape(d)
assert geom.geom_type == "Point"
assert tuple(geom.coords) == ((0.0, 0.0),)
# Convert an object that implements the geo protocol
geom = None
thing = GeoThing({"type": "Point", "coordinates": (0.0, 0.0)})
geom = shape(thing)
assert geom.geom_type == "Point"
assert tuple(geom.coords) == ((0.0, 0.0),)
# Check line string
geom = shape({"type": "LineString", "coordinates": ((-1.0, -1.0), (1.0, 1.0))})
assert isinstance(geom, LineString)
assert tuple(geom.coords) == ((-1.0, -1.0), (1.0, 1.0))
# Check linearring
geom = shape(
{
"type": "LinearRing",
"coordinates": (
(0.0, 0.0),
(0.0, 1.0),
(1.0, 1.0),
(2.0, -1.0),
(0.0, 0.0),
),
}
)
assert isinstance(geom, LinearRing)
assert tuple(geom.coords) == (
(0.0, 0.0),
(0.0, 1.0),
(1.0, 1.0),
(2.0, -1.0),
(0.0, 0.0),
)
# polygon
geom = shape(
{
"type": "Polygon",
"coordinates": (
((0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (2.0, -1.0), (0.0, 0.0)),
((0.1, 0.1), (0.1, 0.2), (0.2, 0.2), (0.2, 0.1), (0.1, 0.1)),
),
}
)
assert isinstance(geom, Polygon)
assert tuple(geom.exterior.coords) == (
(0.0, 0.0),
(0.0, 1.0),
(1.0, 1.0),
(2.0, -1.0),
(0.0, 0.0),
)
assert len(geom.interiors) == 1
# multi point
geom = shape({"type": "MultiPoint", "coordinates": ((1.0, 2.0), (3.0, 4.0))})
assert isinstance(geom, MultiPoint)
assert len(geom.geoms) == 2
# multi line string
geom = shape(
{"type": "MultiLineString", "coordinates": (((0.0, 0.0), (1.0, 2.0)),)}
)
assert isinstance(geom, MultiLineString)
assert len(geom.geoms) == 1
# multi polygon
geom = shape(
{
"type": "MultiPolygon",
"coordinates": [
(
((0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (1.0, 0.0), (0.0, 0.0)),
((0.1, 0.1), (0.1, 0.2), (0.2, 0.2), (0.2, 0.1), (0.1, 0.1)),
)
],
}
)
assert isinstance(geom, MultiPolygon)
assert len(geom.geoms) == 1
def test_empty_wkt_polygon():
"""Confirm fix for issue #450"""
g = wkt.loads("POLYGON EMPTY")
assert g.__geo_interface__["type"] == "Polygon"
assert g.__geo_interface__["coordinates"] == ()
def test_empty_polygon():
"""Confirm fix for issue #450"""
g = Polygon()
assert g.__geo_interface__["type"] == "Polygon"
assert g.__geo_interface__["coordinates"] == ()
|