forked from qwc-services/qwc-feature-info-service
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
32 lines (30 loc) · 1.31 KB
/
utils.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
import math
def geom_center(geomtype, coordinates):
if geomtype.startswith("Multi"):
return geom_center(geomtype[5:], coordinates[0])
elif geomtype == "Point":
return coordinates
elif geomtype == "LineString":
cumlengths = [0]
for i in range(1, len(coordinates)):
dx = coordinates[i][0] - coordinates[i - 1][0]
dy = coordinates[i][1] - coordinates[i - 1][1]
cumlengths.append(cumlengths[i - 1] + math.sqrt(dx * dx + dy * dy))
halflen = 0.5 * cumlengths[-1]
for i in range(1, len(cumlengths)):
if cumlengths[i] > halflen:
mu = (halflen - cumlengths[i - 1]) / (cumlengths[i] - cumlengths[i - 1])
return [
coordinates[i - 1][0] + mu * (coordinates[i][0] - coordinates[i - 1][0]),
coordinates[i - 1][1] + mu * (coordinates[i][1] - coordinates[i - 1][1])
]
elif geomtype == "Polygon":
ring = coordinates[0]
area = 0
cx = cy = 0
for i in range(0, len(ring) - 1):
t = ring[i][0] * ring[i + 1][1] - ring[i + 1][0] * ring[i][1]
area += t
cx += (ring[i][0] + ring[i + 1][0]) * t
cy += (ring[i][1] + ring[i + 1][1]) * t
return [cx / (3 * area), cy / (3 * area)]