Migrated "polygon" code to shapely

This commit is contained in:
Khopa
2020-12-12 02:31:43 +01:00
parent 7c52ca15f3
commit feed55186f
12 changed files with 21 additions and 26 deletions

View File

@@ -2,8 +2,10 @@ import pickle
from typing import Collection, Optional, Tuple
import logging
from shapely import geometry
Zone = Collection[Tuple[float, float]]
Landmap = Tuple[Collection[Zone], Collection[Zone], Collection[Zone]]
Landmap = Tuple[Collection[geometry.Polygon], Collection[geometry.Polygon], Collection[geometry.Polygon]]
def load_landmap(filename: str) -> Optional[Landmap]:
@@ -15,22 +17,9 @@ def load_landmap(filename: str) -> Optional[Landmap]:
return None
def poly_contains(x, y, poly):
n = len(poly)
inside = False
xints = 0.0
p1x, p1y = poly[0]
for i in range(n+1):
p2x, p2y = poly[i % n]
if y > min(p1y, p2y):
if y <= max(p1y, p2y):
if x <= max(p1x, p2x):
if p1y != p2y:
xints = (y-p1y)*(p2x-p1x)/(p2y-p1y)+p1x
if p1x == p2x or x <= xints:
inside = not inside
p1x, p1y = p2x, p2y
return inside
def poly_contains(x, y, poly:geometry.Polygon):
return poly.contains(geometry.Point(x, y))
def poly_centroid(poly) -> Tuple[float, float]:
x_list = [vertex[0] for vertex in poly]