0

I need to union polygons of the shapefile using python.

https://i.stack.imgur.com/qY6gD.png

There are some self-intersection inside polygon and my python code always results in error.

import geopandas as gpd
from shapely.geometry import Polygon
from shapely.validation import make_valid
from shapely.ops import cascaded_union
from shapely.validation import explain_validity

pz32 = gpd.read_file("B://_Shp_robocze//test//test.shp")
shp = gpd.geoseries.GeoSeries([geom for geom in pz32.unary_union.geoms])

shp.geometry = shp.apply(lambda row: make_valid(row.geometry) if not row.geometry.is_valid else row.geometry, axis=1)

print(shp)

Errors that console printing

TopologyException: side location conflict at 389425.99965310335 578312.21068473824. This can occur if the input geometry is invalid.
Traceback (most recent call last):
  File "C:\...\PycharmProjects\pythonProject\main.py", line 8, in <module>
    shp = gpd.geoseries.GeoSeries([geom for geom in pz32.unary_union.geoms])
  File "C:\...\PycharmProjects\pythonProject\venv\lib\site-packages\geopandas\base.py", line 800, in unary_union
    return self.geometry.values.unary_union()
  File "C:\...\PycharmProjects\pythonProject\venv\lib\site-packages\geopandas\array.py", line 650, in unary_union
    return vectorized.unary_union(self.data)
  File "C:\...\PycharmProjects\pythonProject\venv\lib\site-packages\geopandas\_vectorized.py", line 1034, in unary_union
    return shapely.ops.unary_union(data)
  File "C:\...\PycharmProjects\pythonProject\venv\lib\site-packages\shapely\ops.py", line 161, in unary_union
    return geom_factory(lgeos.methods['unary_union'](collection))
  File "C:\...\PycharmProjects\pythonProject\venv\lib\site-packages\shapely\geometry\base.py", line 73, in geom_factory
    raise ValueError("No Shapely geometry can be created from null value")
ValueError: No Shapely geometry can be created from null value

How to make this geometry valid ?

1 Answers1

0

This error message indicates the presence of null values:

No Shapely geometry can be created from null value

You must have a None or NaN in your geometry column. This is different from (and possibly in addition to) any issues you may have with self-intersections.

You can search for nulls, e.g. with the following, to diagnose what’s going on:

pz32[pz32.isnull()]

Or just drop the row:

pz32 = pz32[pz32.notnull()]

Shapely can’t just create a geometry from nothing, so it’s asking you to decide what to do with the null value. While geopandas can handle null values, you’re forcing the whole GeometryArray to be converted to a single shapely object by taking the unary_union.

Michael Delgado
  • 13,789
  • 3
  • 29
  • 54