From 3cecf3228926c97a3c643ee9a42924b81498e4a6 Mon Sep 17 00:00:00 2001 From: Jan Ole Zabel Date: Fri, 3 Apr 2020 13:12:41 +0200 Subject: [PATCH] Use shapely.LineString in abstract Line class --- .../example_sqlite_map/primitives.py | 11 ++++++----- openlr_dereferencer/maps/abstract.py | 14 +++++++++----- setup.py | 2 +- 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/openlr_dereferencer/example_sqlite_map/primitives.py b/openlr_dereferencer/example_sqlite_map/primitives.py index 97da48d..5c3727f 100644 --- a/openlr_dereferencer/example_sqlite_map/primitives.py +++ b/openlr_dereferencer/example_sqlite_map/primitives.py @@ -3,6 +3,7 @@ from itertools import chain from typing import Iterable from openlr import Coordinates, FRC, FOW +from shapely.geometry import LineString from ..maps import Line as AbstractLine, Node as AbstractNode # https://epsg.io/4326 @@ -54,11 +55,11 @@ def frc(self) -> FRC: (frc,) = self.map_reader.connection.execute(stmt, (self.line_id,)).fetchone() return FRC(frc) - def coordinates(self) -> Iterable[Coordinates]: - "Yields every point in the path geometry as Coordinates" - for index in range(self.num_points()): - lon, lat = self.point_n(index + 1) - yield Coordinates(lon=lon, lat=lat) + @property + def shape(self) -> LineString: + "Returns the line geometry" + points = [self.point_n(index + 1) for index in range(self.num_points())] + return LineString(points) def distance_to(self, coord) -> float: "Returns the distance of this line to `coord` in meters" diff --git a/openlr_dereferencer/maps/abstract.py b/openlr_dereferencer/maps/abstract.py index b016c29..004f5ba 100644 --- a/openlr_dereferencer/maps/abstract.py +++ b/openlr_dereferencer/maps/abstract.py @@ -1,9 +1,9 @@ """An abstract `MapReader` base class, which must be implemented for each map format to decode location references on.""" from abc import ABC, abstractmethod -from typing import Iterable, Hashable +from typing import Iterable, Hashable, Sequence from openlr import Coordinates, FOW, FRC - +from shapely.geometry import LineString class Line(ABC): "Abstract Line class, modelling a line coming from a map reader" @@ -33,10 +33,14 @@ def frc(self) -> FRC: def fow(self) -> FOW: "Returns the form of way of this line" + @property @abstractmethod - def coordinates(self) -> Iterable[Coordinates]: - """Returns the shape of the line. - Yields GeoCoordinate values.""" + def shape(self) -> LineString: + "Returns the geometric shape as a linestring" + + def coordinates(self) -> Sequence[Coordinates]: + """Returns the shape of the line as list of Coordinates""" + return [Coordinates(*point) for point in self.shape.coords] @property def length(self) -> float: diff --git a/setup.py b/setup.py index 3dc2e82..fcd23d2 100644 --- a/setup.py +++ b/setup.py @@ -23,7 +23,7 @@ 'openlr_dereferencer.maps.a_star', 'openlr_dereferencer.decoding', 'openlr_dereferencer'], - install_requires=["openlr", "geographiclib"], + install_requires=["openlr", "geographiclib", "shapely"], test_suite="tests", python_requires=">=3.6", classifiers=[