-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_geointerface.py
74 lines (61 loc) · 2.76 KB
/
test_geointerface.py
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
from . import unittest
from shapely.geometry import asShape
from shapely.geometry.multipoint import MultiPointAdapter
from shapely.geometry.linestring import LineStringAdapter
from shapely.geometry.multilinestring import MultiLineStringAdapter
from shapely.geometry.polygon import PolygonAdapter
from shapely.geometry.multipolygon import MultiPolygonAdapter
class GeoThing(object):
def __init__(self, d):
self.__geo_interface__ = d
class GeoInterfaceTestCase(unittest.TestCase):
def test_geointerface(self):
# Adapt a dictionary
d = {"type": "Point", "coordinates": (0.0, 0.0)}
shape = asShape(d)
self.assertEqual(shape.geom_type, 'Point')
self.assertEqual(tuple(shape.coords), ((0.0, 0.0),))
# Adapt an object that implements the geo protocol
shape = None
thing = GeoThing({"type": "Point", "coordinates": (0.0, 0.0)})
shape = asShape(thing)
self.assertEqual(shape.geom_type, 'Point')
self.assertEqual(tuple(shape.coords), ((0.0, 0.0),))
# Check line string
shape = asShape(
{'type': 'LineString', 'coordinates': ((-1.0, -1.0), (1.0, 1.0))})
self.assertIsInstance(shape, LineStringAdapter)
self.assertEqual(tuple(shape.coords), ((-1.0, -1.0), (1.0, 1.0)))
# polygon
shape = asShape(
{'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)))}
)
self.assertIsInstance(shape, PolygonAdapter)
self.assertEqual(
tuple(shape.exterior.coords),
((0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (2.0, -1.0), (0.0, 0.0)))
self.assertEqual(len(shape.interiors), 1)
# multi point
shape = asShape({'type': 'MultiPoint',
'coordinates': ((1.0, 2.0), (3.0, 4.0))})
self.assertIsInstance(shape, MultiPointAdapter)
self.assertEqual(len(shape.geoms), 2)
# multi line string
shape = asShape({'type': 'MultiLineString',
'coordinates': (((0.0, 0.0), (1.0, 2.0)),)})
self.assertIsInstance(shape, MultiLineStringAdapter)
self.assertEqual(len(shape.geoms), 1)
# multi polygon
shape = asShape(
{'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))
)]})
self.assertIsInstance(shape, MultiPolygonAdapter)
self.assertEqual(len(shape.geoms), 1)
def test_suite():
return unittest.TestLoader().loadTestsFromTestCase(GeoInterfaceTestCase)