shapely / shapely/shapely

RuntimeWarning: invalid value encountered in unary_union

Open
#1,840 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

geos
Dominant language
Python
Stars
4.5k
Forks
631
Avg merge
1d 11h
Merged PRs (30d)
10

Description

This problem is part of a bigger problem where I get many many runtime warnings when running my code, but it seems to start here:

Test code (test data, I don't know how to upload a .shp otherwise)

import geopandas as gpd
import shapely

gdf = gpd.GeoDataFrame.from_file("test.gpkg")
for i in range(1, len(gdf)+1): 
    print(i)
    gdf.head(i).geometry.unary_union
overlapping_haz_geom = gdf.geometry.unary_union

The same problems occur when using this version of the code:

gdf = gpd.GeoDataFrame.from_file("test.gpkg")
for i in range(1, len(gdf)+1): 
    print(i)
    i, 
    gdf.iloc[i-1].geometry.is_valid, 
    gdf.iloc[i-1].geometry.is_empty, 
    shapely.unary_union(gdf.head(i).geometry)
overlapping_haz_geom = shapely.unary_union(gdf.geometry)

Expected behavior and actual behavior.

When running the test code, I get a bunch of these runtime warnings starting at i=14:
RuntimeWarning: invalid value encountered in unary_union
return lib.unary_union(collections, **kwargs)

I do not get the same warning when running the last line: overlapping_haz_geom = gdf.geometry.unary_union

However, using exactly the same data in my original code I do get the same warning when running this line: overlapping_haz_geom = gdf.geometry.unary_union

Weirdly enough two of my colleagues have run my test code & data and both did not get any warnings, while I got at least 10. Noteworthy potentially is that they both work on a windows computer and I work on a Mac.

None of the geometries are invalid or empty

Operating system

macOS Monterey v.12.3

Shapely version and provenance

Python=3.11.4
Geopandas =0.13.2
Shapely =2.0.1

Example of test data

Screenshot 2023-06-23 at 16 20 45

Output for test code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
/Users/sophiebuijs/opt/miniconda3/envs/NTL/lib/python3.11/site-packages/shapely/set_operations.py:426: RuntimeWarning: invalid value encountered in unary_union
return lib.unary_union(collections, **kwargs)
/Users/sophiebuijs/opt/miniconda3/envs/NTL/lib/python3.11/site-packages/shapely/set_operations.py:426: RuntimeWarning: invalid value encountered in unary_union
return lib.unary_union(collections, **kwargs)
/Users/sophiebuijs/opt/miniconda3/envs/NTL/lib/python3.11/site-packages/shapely/set_operations.py:426: RuntimeWarning: invalid value encountered in unary_union
return lib.unary_union(collections, **kwargs)
/Users/sophiebuijs/opt/miniconda3/envs/NTL/lib/python3.11/site-packages/shapely/set_operations.py:426: RuntimeWarning: invalid value encountered in unary_union
return lib.unary_union(collections, **kwargs)
/Users/sophiebuijs/opt/miniconda3/envs/NTL/lib/python3.11/site-packages/shapely/set_operations.py:426: RuntimeWarning: invalid value encountered in unary_union
return lib.unary_union(collections, **kwargs)
/Users/sophiebuijs/opt/miniconda3/envs/NTL/lib/python3.11/site-packages/shapely/set_operations.py:426: RuntimeWarning: invalid value encountered in unary_union
return lib.unary_union(collections, **kwargs)
/Users/sophiebuijs/opt/miniconda3/envs/NTL/lib/python3.11/site-packages/shapely/set_operations.py:426: RuntimeWarning: invalid value encountered in unary_union
return lib.unary_union(collections, **kwargs)
/Users/sophiebuijs/opt/miniconda3/envs/NTL/lib/python3.11/site-packages/shapely/set_operations.py:426: RuntimeWarning: invalid value encountered in unary_union
return lib.unary_union(collections, **kwargs)
/Users/sophiebuijs/opt/miniconda3/envs/NTL/lib/python3.11/site-packages/shapely/set_operations.py:426: RuntimeWarning: invalid value encountered in unary_union
return lib.unary_union(collections, **kwargs)
/Users/sophiebuijs/opt/miniconda3/envs/NTL/lib/python3.11/site-packages/shapely/set_operations.py:426: RuntimeWarning: invalid value encountered in unary_union
return lib.unary_union(collections, **kwargs)
15
/Users/sophiebuijs/opt/miniconda3/envs/NTL/lib/python3.11/site-packages/shapely/set_operations.py:426: RuntimeWarning: invalid value encountered in unary_union
return lib.unary_union(collections, **kwargs)
16
/Users/sophiebuijs/opt/miniconda3/envs/NTL/lib/python3.11/site-packages/shapely/set_operations.py:426: RuntimeWarning: invalid value encountered in unary_union
return lib.unary_union(collections, **kwargs)
17
/Users/sophiebuijs/opt/miniconda3/envs/NTL/lib/python3.11/site-packages/shapely/set_operations.py:426: RuntimeWarning: invalid value encountered in unary_union
return lib.unary_union(collections, **kwargs)
18
/Users/sophiebuijs/opt/miniconda3/envs/NTL/lib/python3.11/site-packages/shapely/set_operations.py:426: RuntimeWarning: invalid value encountered in unary_union
return lib.unary_union(collections, **kwargs)
19
/Users/sophiebuijs/opt/miniconda3/envs/NTL/lib/python3.11/site-packages/shapely/set_operations.py:426: RuntimeWarning: invalid value encountered in unary_union
return lib.unary_union(collections, **kwargs)
20
/Users/sophiebuijs/opt/miniconda3/envs/NTL/lib/python3.11/site-packages/shapely/set_operations.py:426: RuntimeWarning: invalid value encountered in unary_union
return lib.unary_union(collections, **kwargs)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at shapely/set_operations.py around line 426 and reproduce the warning with the provided test.gpkg loop, Python 3.11.4, GeoPandas 0.13.2, and Shapely 2.0.1. Compare the macOS and Windows behavior described in the report and investigate why unary_union emits the warning for valid, non-empty geometries. Done means the cause is identified and the warning behavior is fixed or clearly accounted for with verification against the supplied reproduction.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
computer-graphics
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.