From b25921f0536f9612b3e4f5f0817d785fd9c4fd99 Mon Sep 17 00:00:00 2001 From: Joris Nettelstroth Date: Tue, 13 Dec 2022 12:17:58 +0100 Subject: [PATCH] Prevent access violation error in unary_union() --- dhnx/gistools/connect_points.py | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/dhnx/gistools/connect_points.py b/dhnx/gistools/connect_points.py index 03c31400..1e1fc7a2 100644 --- a/dhnx/gistools/connect_points.py +++ b/dhnx/gistools/connect_points.py @@ -161,6 +161,30 @@ def create_object_connections(points, lines, tol_distance=1): raise ValueError("The Linestrings must consists of simple lines," " with only two coordinates!") + # Pepare merging all the street lines + all_lines = lines['geometry'] + + # There seems to be a conflict between shapely and pygeos, + # which both use 'geos' internally, if both are installed. + # This can cause + # 'OSError exception: access violation reading 0xFFFFFFFFFFFFFFFF'. + # + # With shapely 1.8.0 and pygeos 0.12.0 it was observed that + # this sometimes even fails without error. In such a case + # mergedlines might only contain a single LineString (one street + # segment) instead of a MultiLineString (the combined network + # of all street segments). This completely messes up the + # following nearest_points(). + # + # Wrapping the argument in 'list()' seems to be a valid workaround. + # It may come with a performance cost, as noted here: + # https://github.com/geopandas/geopandas/issues/1820 + # https://github.com/geopandas/geopandas/issues/2171 + # This issue may disappear when shapely 2.0 is released (then pygeos + # is merged with shapely). + mergedlines = unary_union(list(all_lines)) + # mergedlines = unary_union(all_lines) # TODO Try this with shapely 2.0 + # empty geopandas dataframe for house connections conn_lines = gpd.GeoDataFrame(geometry=[], crs=lines.crs) @@ -169,10 +193,6 @@ def create_object_connections(points, lines, tol_distance=1): house_geo = row['geometry'] - # the same with the original lines - all_lines = lines['geometry'] - mergedlines = unary_union(all_lines) - # new nearest point method ############ ######### n_p = nearest_points(mergedlines, house_geo)[0]