Compare commits
4 commits
c6670fefc6
...
3e519568db
Author | SHA1 | Date | |
---|---|---|---|
3e519568db | |||
b980943b10 | |||
afad8898c7 | |||
e1adfd0059 |
2 changed files with 59 additions and 12 deletions
|
@ -1,15 +1,64 @@
|
||||||
import re
|
import re
|
||||||
|
import math
|
||||||
import shapely
|
import shapely
|
||||||
|
|
||||||
def load_poly_from_file(path: str) -> shapely.Polygon:
|
from typing import Self
|
||||||
points = list()
|
|
||||||
|
|
||||||
with open(path, 'r') as fh:
|
class PointSequence(list):
|
||||||
data = fh.read()
|
@staticmethod
|
||||||
|
def from_file(path: str) -> Self:
|
||||||
|
ret = PointSequence()
|
||||||
|
|
||||||
for line in data.split('\n'):
|
with open(path, 'r') as fh:
|
||||||
lat, lon = re.split(', ', line)
|
data = fh.read()
|
||||||
|
|
||||||
points.append((float(lon), float(lat)))
|
for line in data.split('\n'):
|
||||||
|
if line == '':
|
||||||
|
continue
|
||||||
|
|
||||||
return shapely.Polygon(points)
|
lat, lon = re.split(r'\s*,\s*', line)
|
||||||
|
|
||||||
|
ret.add(float(lon), float(lat))
|
||||||
|
|
||||||
|
return ret
|
||||||
|
|
||||||
|
def __init__(self, points: list=None):
|
||||||
|
super().__init__()
|
||||||
|
|
||||||
|
self.poly = None
|
||||||
|
|
||||||
|
for point in points:
|
||||||
|
typeof = type(point)
|
||||||
|
|
||||||
|
if typeof is tuple:
|
||||||
|
self.add(*point)
|
||||||
|
elif typeof is shapely.Point:
|
||||||
|
self.append(point)
|
||||||
|
|
||||||
|
def add(self, lon: float, lat: float):
|
||||||
|
self.append(shapely.Point(lon, lat))
|
||||||
|
|
||||||
|
def is_closed(self) -> bool:
|
||||||
|
return self[-1] == self[0]
|
||||||
|
|
||||||
|
def close(self):
|
||||||
|
if self[-1] != self[0]:
|
||||||
|
self.append(self[0])
|
||||||
|
|
||||||
|
self.poly = shapely.Polygon(self)
|
||||||
|
|
||||||
|
def nearest_index(self, point: shapely.Point) -> int:
|
||||||
|
indices = list()
|
||||||
|
|
||||||
|
for i in range(0, len(self)):
|
||||||
|
indices.append((i, self[i].distance(point)))
|
||||||
|
|
||||||
|
indices.sort(key=lambda i: i[1])
|
||||||
|
|
||||||
|
return indices[0][0]
|
||||||
|
|
||||||
|
def line_heading(p1: shapely.Point, p2: shapely.Point) -> float:
|
||||||
|
dx = p2.x - p1.x
|
||||||
|
dy = p2.y - p1.y
|
||||||
|
|
||||||
|
return math.atan2(dy, dx)
|
||||||
|
|
|
@ -68,10 +68,8 @@ def parse_coord(coord: str) -> tuple[float, float]:
|
||||||
if lon <= 6100:
|
if lon <= 6100:
|
||||||
lon += 10000
|
lon += 10000
|
||||||
|
|
||||||
return (
|
return shapely.Point(0.01 * -lon,
|
||||||
0.01 * -lon,
|
0.01 * int(coord[0:4]))
|
||||||
0.01 * int(coord[0:4])
|
|
||||||
)
|
|
||||||
|
|
||||||
def each_poly(parts: list[str]):
|
def each_poly(parts: list[str]):
|
||||||
points = list()
|
points = list()
|
||||||
|
|
Loading…
Add table
Reference in a new issue