Compare commits

...

4 commits

Author SHA1 Message Date
3e519568db Implement PointSequence class
Implement PointSequence class to encapsulate functionality for handling
closed and open point sequences
2025-03-22 14:32:55 -04:00
b980943b10 Make parse_coord() return shapely.Point 2025-03-22 14:17:30 -04:00
afad8898c7 Implement line_heading() 2025-03-21 16:29:23 -04:00
e1adfd0059 Fix handling of end of file 2025-03-21 16:29:14 -04:00
2 changed files with 59 additions and 12 deletions

View file

@ -1,15 +1,64 @@
import re
import math
import shapely
def load_poly_from_file(path: str) -> shapely.Polygon:
points = list()
from typing import Self
class PointSequence(list):
@staticmethod
def from_file(path: str) -> Self:
ret = PointSequence()
with open(path, 'r') as fh:
data = fh.read()
for line in data.split('\n'):
lat, lon = re.split(', ', line)
if line == '':
continue
points.append((float(lon), float(lat)))
lat, lon = re.split(r'\s*,\s*', line)
return shapely.Polygon(points)
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)

View file

@ -68,10 +68,8 @@ def parse_coord(coord: str) -> tuple[float, float]:
if lon <= 6100:
lon += 10000
return (
0.01 * -lon,
0.01 * int(coord[0:4])
)
return shapely.Point(0.01 * -lon,
0.01 * int(coord[0:4]))
def each_poly(parts: list[str]):
points = list()