oemof / DHNx

District heating system optimisation and simulation models
MIT License
27 stars 12 forks source link

Access violation error in unary_union() with shapely and pygeos installed #107

Closed jnettels closed 1 year ago

jnettels commented 1 year ago

The function create_object_connections() uses the shapely unary_union() operation.

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 when unary_union() is used.

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).

I have linked a PR that fixes the issue, with commit b25921f0536f9612b3e4f5f0817d785fd9c4fd99.

badf58030858c69363d4744ab046d8a2ef4855ed The PR also addresses "UserWarning: CRS not set for some of the concatenation inputs." that can show up with geopandas 1.12.x. It is fixed by explicitly setting the crs on all concat inputs.

e571d35b108cf321dbe8392eb8a7458a064fa5e0 This also implements a small convenience function check_duplicate_geometries() within process_geometry(), for testing the input GeoDataFrame for duplicate geometries and plotting them. These can cause nasty errors and can be hard to find.