-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshapely_geojson.py
101 lines (80 loc) · 2.62 KB
/
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
92
93
94
95
96
97
98
99
100
101
import json
from shapely.geometry import mapping
from shapely.geometry.base import BaseGeometry
class Feature():
def __init__(self, geometry, properties=None):
self.geometry = geometry
self.properties = properties
@property
def geometry(self):
return self._geometry
@geometry.setter
def geometry(self, geometry):
if not isinstance(geometry, BaseGeometry):
raise ValueError('geometry must be a shapely geometry.')
self._geometry = geometry
@property
def properties(self):
return self._properties
@properties.setter
def properties(self, properties):
if properties is None:
self._properties = {}
elif isinstance(properties, dict):
self._properties = properties
else:
raise ValueError('properties must be a dict.')
@property
def __geo_interface__(self):
return {
'type': 'Feature',
'geometry': self.geometry.__geo_interface__,
'properties': self.properties,
}
def __eq__(self, other):
return self.__geo_interface__ == other.__geo_interface__
class FeatureCollection():
def __init__(self, objects):
self.features = objects
@property
def features(self):
return self._features
@features.setter
def features(self, objects):
all_are_features = all(
isinstance(feature, Feature)
for feature in objects
)
if all_are_features:
self._features = objects
else:
try:
self._features = [
Feature(geometry)
for geometry in objects
]
except ValueError:
raise ValueError(
'features can be either a Feature or shapely geometry.')
def __iter__(self):
return iter(self.features)
def geometries_iterator(self):
for feature in self.features:
yield feature.geometry
@property
def __geo_interface__(self):
return {
'type': 'FeatureCollection',
'features': [
feature.__geo_interface__
for feature in self.features
],
}
def __eq__(self, other):
return self.__geo_interface__ == other.__geo_interface__
def dump(obj, fp, *args, **kwargs):
"""Dump shapely geometry object :obj: to a file :fp:."""
json.dump(mapping(obj), fp, *args, **kwargs)
def dumps(obj, *args, **kwargs):
"""Dump shapely geometry object :obj: to a string."""
return json.dumps(mapping(obj), *args, **kwargs)