diff --git a/lib/xmet/geo.py b/lib/xmet/geo.py index b7ffa66..784ec90 100644 --- a/lib/xmet/geo.py +++ b/lib/xmet/geo.py @@ -1,4 +1,5 @@ import re +import enum import math import shapely @@ -14,6 +15,11 @@ def each_intermediate_point(p1: shapely.Point, elif typeof is shapely.MultiLineString: pass +class PointDirection(enum.Enum): + EQUAL = 0 + LEFT = enum.auto() + RIGHT = enum.auto() + class PointSequence(list): linestring: shapely.LineString polygon: shapely.Polygon @@ -73,6 +79,23 @@ class PointSequence(list): return indices[0][0] + def index_distance(self, i1: int, i2: int) -> int: + """ + Returns the index distance of i1 relative to i2, and whether i1 is + considered left of, equal to, or right of i2. + """ + count = len(self) + value = count - ((i1 - i2) % count) + + if value == 0: + direction = PointDirection.EQUAL + elif value > count / 2: + direction = PointDirection.RIGHT + else: + direction = PointDirection.LEFT + + return value, direction + class PolygonBuilder(): point_first: shapely.Point point_last: shapely.Point