-
Notifications
You must be signed in to change notification settings - Fork 48
/
geojson_polygon.py
78 lines (63 loc) · 2.66 KB
/
geojson_polygon.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
from shapely.geometry import LinearRing
from .geojson import Geojson
class GeojsonPolygon(Geojson):
"""
The Python representation of a GeoJSON polygon or multipolygon feature.
A GeojsonPolygon instance has a geometry and properties.
"""
def __init__(self, id=None, feature_properties=None, feature_geometry=None, is_multi_geom=False):
super().__init__(id, feature_properties, feature_geometry)
self.is_multi_geom = is_multi_geom
def parse_geojson(self, properties, is_roof=False, color_attribute=('NONE', 'numeric')):
super().parse_geojson(properties, is_roof, color_attribute)
if self.is_multi_geom:
exterior_ring = self.get_clockwise_polygon(
self.feature_geometry["coordinates"][0][0]
)
interior_rings = [
int_ring[:-1]
for int_ring in self.feature_geometry["coordinates"][0][1:]
]
if is_roof:
self.adjust_height(exterior_ring, self.height)
for int_ring in interior_rings:
self.adjust_height(int_ring, self.height)
else:
exterior_ring = self.get_clockwise_polygon(
self.feature_geometry["coordinates"][0]
)
interior_rings = [
int_ring[:-1]
for int_ring in self.feature_geometry["coordinates"][1:]
]
if is_roof:
for coord in exterior_ring:
self.adjust_height(coord, self.height)
for coord in interior_rings:
self.adjust_height(coord, self.height)
self.exterior_ring = exterior_ring
self.interior_rings = interior_rings
z_name = properties[properties.index("z") + 1]
self.set_z(exterior_ring, z_name)
for int_ring in interior_rings:
self.set_z(int_ring, z_name)
return True
def get_clockwise_polygon(self, polygon):
"""
Return a clockwise polygon without the last point (the last point is the same as the first one).
:param polygon: a list of points
:return: a list of points
"""
if LinearRing(polygon).is_ccw:
return polygon[:-1][::-1]
else:
return polygon[:-1]
def adjust_height(self, ring, height):
"""
Decreases the Z coordinate of each point in a ring by a specified height.
Parameters:
- ring: A list of coordinates, with each coordinate being a list of three numbers [x, y, z].
- height: The amount to subtract from the Z coordinate .
"""
for coord in ring:
coord[2] -= height