Skip to content

include shared_paths function #73

@mattijn

Description

@mattijn

Firstly, congrats on this package! It looks much promising!

I noticed that the pygeos.shared_paths() function was written out of the package in #53. It would be great to have it back!

I tried to include the removed code again and got it somehow working (see code change), see below for some tests. Not sure how to continue or which output/dimension is looked after to make the function reappear.

The tests I did:

from shapely import geometry
import pygeos
import numpy as np
from IPython.display import SVG, display
def svg_style(lines):
    svg_lines = lines._repr_svg_()
    svg_lines = svg_lines.replace('stroke="#66cc99"', 'stroke="orange"', 1)
    svg_lines = svg_lines.replace('stroke-width="0.0432"', 'stroke-width="0.1"', 1)
    svg_lines = svg_lines.replace('opacity="0.8"', 'opacity="0.4"', 1)

    display(SVG(svg_lines))    
# prepare some linestrings
g1 = geometry.LineString([(0, 0), (1, 0), (1, 1), (0, 1), (0, 0)])
g2 = geometry.LineString([(1, 0), (2, 0), (2, 1), (1, 1), (1, 0)])
g3 = geometry.LineString([(0, 0), (2, 0), (2, 1), (0, 1), (0, 0)])
# shared paths in opposite direction of both inputs
lines_opposite_direction = geometry.MultiLineString([g1, g2])

# orange is g1
# green is g2
svg_style(lines_opposite_direction)

image

# shared paths in same direction of both inputs
lines_same_direction = geometry.MultiLineString([g1,g3])

# orange is g1
# green is g3
svg_style(lines_same_direction)

image

# convert to pygeos
pyg1 = pygeos.from_wkt(g1.wkt)
pyg2 = pygeos.from_wkt(g2.wkt)
pyg3 = pygeos.from_wkt(g3.wkt)
# only contain shared paths in backward direction
pygeos.shared_paths(pyg1, pyg2)
<pygeos.Geometry GEOMETRYCOLLECTION (MULTILINESTRING EMPTY, MULTILINESTRING ((1 0, 1 1)))>
# only contain shared paths in forward direction
pygeos.shared_paths(pyg1, pyg3)
<pygeos.Geometry GEOMETRYCOLLECTION (MULTILINESTRING ((0 0, 1 0), (1 1, 0 1), (0 1, 0 0)), MULTILINESTRING EMPTY)>
# prepare as Numpy array
arr_g1g1 = np.array([pyg1, pyg1])
arr_g2g3 = np.array([pyg2, pyg3])
# compute shared_paths using arrays
arr_shared_paths = pygeos.shared_paths(arr_g1g1, arr_g2g3)
arr_shared_paths
array([<pygeos.Geometry GEOMETRYCOLLECTION (MULTILINESTRING EMPTY, MULTILINESTRING ((1 0, 1 1)))>,
       <pygeos.Geometry GEOMETRYCOLLECTION (MULTILINESTRING ((0 0, 1 0), (1 1, 0 1), (0 1, 0 0)), MULTILINESTRING EMPTY)>],
      dtype=object)
arr_shared_paths[0]
<pygeos.Geometry GEOMETRYCOLLECTION (MULTILINESTRING EMPTY, MULTILINESTRING ((1 0, 1 1)))>
# try to get the 2nd item from the geometry collection [MULTILINESTRING ((1 0, 1 1))]
arr_shared_paths[0][1]
---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

<ipython-input-12-e427a0a9c0f9> in <module>
      1 # try to get the 2nd item from the geometry collection [MULTILINESTRING ((1 0, 1 1))]
----> 2 arr_shared_paths[0][1]


TypeError: 'pygeos.lib.GEOSGeometry' object is not subscriptable
# try to get number of geometries (should be 2)
pygeos.get_num_geometries(arr_shared_paths[0])
2
# try to get the 2nd item from the geometry collection
bw_shared_ls = pygeos.get_geometry(arr_shared_paths[0], 1)
bw_shared_ls
<pygeos.Geometry MULTILINESTRING ((1 0, 1 1))>
pygeos.get_coordinates(bw_shared_ls)
array([[1., 0.],
       [1., 1.]])
# try to get the 1st (empty) item from the geometry collection
fw_shared_ls = pygeos.get_geometry(arr_shared_paths[0], 0)
fw_shared_ls
<pygeos.Geometry MULTILINESTRING EMPTY>
pygeos.get_coordinates(fw_shared_ls)
array([], shape=(0, 2), dtype=float64)
# compare with shapely:
from shapely.ops import shared_paths
combs = [[g1, g2], [g1, g3]]
%%timeit
list_shared_paths = []
for comb in combs:
    list_shared_paths.append(shared_paths(comb[0], comb[1]))
208 µs ± 1.37 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
%%timeit
arr_shared_paths = pygeos.shared_paths(arr_g1g1, arr_g2g3)
185 µs ± 863 ns per loop (mean ± std. dev. of 7 runs, 1000 loops each)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions