-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_shapely_geojson.py
91 lines (72 loc) · 2.87 KB
/
test_shapely_geojson.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import io
import pytest
from shapely.geometry import Point
from shapely_geojson import dump, dumps, Feature, FeatureCollection
def test_dump():
fp = io.StringIO()
point = Point(1, 1)
geojson1 = '{\n "type": "Point",\n "coordinates": [\n 1.0,\n 1.0\n ]\n}'
geojson2 = '{\n "coordinates": [\n 1.0,\n 1.0\n ],\n "type": "Point"\n}'
with fp:
dump(point, fp, indent=1)
assert fp.getvalue() == geojson1 or fp.getvalue() == geojson2
def test_dumps():
point = Point(1, 1)
geojson1 = '{\n "type": "Point",\n "coordinates": [\n 1.0,\n 1.0\n ]\n}'
geojson2 = '{\n "coordinates": [\n 1.0,\n 1.0\n ],\n "type": "Point"\n}'
dumped = dumps(point, indent=1)
assert dumped == geojson1 or dumped == geojson2
class TestFeature():
def test_invalid_properties(self):
with pytest.raises(ValueError):
Feature(Point(), properties=[])
def test_default_properties(self):
feature = Feature(Point())
assert feature.properties == {}
def test_invalid_geometry(self):
with pytest.raises(ValueError):
Feature([0, 1])
def test_geo_interface(self):
feature = Feature(Point(1, 1), properties={'key': 'value'})
geo_interface = {
'type': 'Feature',
'geometry': {
'type': 'Point',
'coordinates': (1.0, 1.0),
},
'properties': {
'key': 'value'
},
}
assert feature.__geo_interface__ == geo_interface
def test_equality(self):
one = Feature(Point(1, 1))
another = Feature(Point(1, 1))
assert one == another
class TestFeatureCollection():
def test_is_iterable(self):
features = [Feature(Point(0, 0)), Feature(Point(0, 1))]
collection = FeatureCollection(features)
for collection_item, feature in zip(collection, features):
assert collection_item == feature
def test_geometries_iterator(self):
points = [Point(0, 0), Point(0, 1)]
features = list(map(Feature, points))
collection = FeatureCollection(features)
geometry_point_pairs = zip(collection.geometries_iterator(), points)
for geometry, point in geometry_point_pairs:
assert geometry == point
def test_geometry_conversion(self):
points = [Point(0, 0), Point(0, 1)]
features = list(map(Feature, points))
collection = FeatureCollection(points)
for collection_item, feature in zip(collection, features):
assert collection_item == feature
def test_invalid_features(self):
with pytest.raises(ValueError):
FeatureCollection([[1, 2]])
def test_equality(self):
features = [Feature(Point(0, 0)), Feature(Point(0, 1))]
one = FeatureCollection(features)
another = FeatureCollection(features)
assert one == another