Compare commits
No commits in common. "3e519568dbdd369eb9c17b31ab70285fdf57e916" and "c6670fefc635399dba0be05482b558a99827e94d" have entirely different histories.
3e519568db
...
c6670fefc6
2 changed files with 12 additions and 59 deletions
|
@ -1,64 +1,15 @@
|
||||||
import re
|
import re
|
||||||
import math
|
|
||||||
import shapely
|
import shapely
|
||||||
|
|
||||||
from typing import Self
|
def load_poly_from_file(path: str) -> shapely.Polygon:
|
||||||
|
points = list()
|
||||||
class PointSequence(list):
|
|
||||||
@staticmethod
|
|
||||||
def from_file(path: str) -> Self:
|
|
||||||
ret = PointSequence()
|
|
||||||
|
|
||||||
with open(path, 'r') as fh:
|
with open(path, 'r') as fh:
|
||||||
data = fh.read()
|
data = fh.read()
|
||||||
|
|
||||||
for line in data.split('\n'):
|
for line in data.split('\n'):
|
||||||
if line == '':
|
lat, lon = re.split(', ', line)
|
||||||
continue
|
|
||||||
|
|
||||||
lat, lon = re.split(r'\s*,\s*', line)
|
points.append((float(lon), float(lat)))
|
||||||
|
|
||||||
ret.add(float(lon), float(lat))
|
return shapely.Polygon(points)
|
||||||
|
|
||||||
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,8 +68,10 @@ def parse_coord(coord: str) -> tuple[float, float]:
|
||||||
if lon <= 6100:
|
if lon <= 6100:
|
||||||
lon += 10000
|
lon += 10000
|
||||||
|
|
||||||
return shapely.Point(0.01 * -lon,
|
return (
|
||||||
0.01 * int(coord[0:4]))
|
0.01 * -lon,
|
||||||
|
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