Skip to content

Commit

Permalink
Use shapely.LineString in abstract Line class
Browse files Browse the repository at this point in the history
  • Loading branch information
UgnilJoZ committed Apr 3, 2020
1 parent 6569590 commit 3cecf32
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
11 changes: 6 additions & 5 deletions openlr_dereferencer/example_sqlite_map/primitives.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down
14 changes: 9 additions & 5 deletions openlr_dereferencer/maps/abstract.py
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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=[
Expand Down

0 comments on commit 3cecf32

Please sign in to comment.