Skip to content

Commit

Permalink
Reintroduce shared_paths (#77)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattijn authored and caspervdw committed Dec 20, 2019
1 parent 02899c8 commit 736faa2
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
27 changes: 26 additions & 1 deletion pygeos/linear.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from . import lib
from . import Geometry # NOQA

__all__ = ["line_interpolate_point", "line_locate_point", "line_merge"]
__all__ = ["line_interpolate_point", "line_locate_point", "line_merge", "shared_paths"]


def line_interpolate_point(line, distance, normalize=False):
Expand Down Expand Up @@ -87,3 +87,28 @@ def line_merge(line):
<pygeos.Geometry GEOMETRYCOLLECTION EMPTY>
"""
return lib.line_merge(line)


def shared_paths(geom1, geom2):
"""Returns the shared paths between geom1 and geom2.
Both geometries should be linestrings or arrays of linestrings.
A geometrycollection or array of geometrycollections is returned
with each geometrycollection two elements. The first element is a
multilinestring containing shared paths with the same direction
for both inputs. The second element is a multilinestring containing
shared paths with the opposite direction for the two inputs.
Parameters
----------
geom1 : Geometry or array_like
geom2 : Geometry or array_like
Examples
--------
>>> geom1 = Geometry("LINESTRING (0 0, 1 0, 1 1, 0 1, 0 0)")
>>> geom2 = Geometry("LINESTRING (1 0, 2 0, 2 1, 1 1, 1 0)")
>>> shared_paths(geom1, geom2)
<pygeos.Geometry GEOMETRYCOLLECTION (MULTILINESTRING EMPTY, MULTILINESTRING ((1 0, 1 1)))>
"""
return lib.shared_paths(geom1, geom2)
13 changes: 13 additions & 0 deletions pygeos/test/test_linear.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,16 @@ def test_line_merge_geom_array():
actual = pygeos.line_merge([line_string, multi_line_string])
assert pygeos.equals(actual[0], line_string)
assert pygeos.equals(actual[1], multi_line_string)


def test_shared_paths_linestring():
g1 = pygeos.linestrings([(0, 0), (1, 0), (1, 1)])
g2 = pygeos.linestrings([(0, 0), (1, 0)])
actual1 = pygeos.shared_paths(g1, g2)
assert pygeos.equals(pygeos.get_geometry(actual1, 0), g2)


def test_shared_paths_none():
assert pygeos.shared_paths(line_string, None) is None
assert pygeos.shared_paths(None, line_string) is None
assert pygeos.shared_paths(None, None) is None
2 changes: 2 additions & 0 deletions src/ufuncs.c
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,7 @@ static void *intersection_data[1] = {GEOSIntersection_r};
static void *difference_data[1] = {GEOSDifference_r};
static void *symmetric_difference_data[1] = {GEOSSymDifference_r};
static void *union_data[1] = {GEOSUnion_r};
static void *shared_paths_data[1] = {GEOSSharedPaths_r};
typedef void *FuncGEOS_YY_Y(void *context, void *a, void *b);
static char YY_Y_dtypes[3] = {NPY_OBJECT, NPY_OBJECT, NPY_OBJECT};
static void YY_Y_func(char **args, npy_intp *dimensions,
Expand Down Expand Up @@ -1399,6 +1400,7 @@ int init_ufuncs(PyObject *m, PyObject *d)
DEFINE_YY_Y (difference);
DEFINE_YY_Y (symmetric_difference);
DEFINE_YY_Y (union);
DEFINE_YY_Y (shared_paths);

DEFINE_Y_d (get_x);
DEFINE_Y_d (get_y);
Expand Down

0 comments on commit 736faa2

Please sign in to comment.