Skip to content

ENH: add on_invalid parameter to read_dataframe #422

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
# CHANGELOG

## 0.9.0 (yyyy-mm-dd)

### Improvements

- Add `on_invalid` parameter to `read_dataframe` (#422).

## 0.8.1 (yyyy-mm-dd)

### Bug fixes

- Fix bug preventing reading from file paths containing hashes in `read_dataframe` (#412)
- Fix bug preventing reading from file paths containing hashes in `read_dataframe` (#412).

### Packaging

Expand Down
19 changes: 14 additions & 5 deletions pyogrio/geopandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ def read_dataframe(
sql_dialect=None,
fid_as_index=False,
use_arrow=None,
on_invalid="raise",
arrow_to_pandas_kwargs=None,
**kwargs,
):
Expand Down Expand Up @@ -197,6 +198,15 @@ def read_dataframe(
installed). When enabled, this provides a further speed-up.
Defaults to False, but this default can also be globally overridden
by setting the ``PYOGRIO_USE_ARROW=1`` environment variable.
on_invalid : str, optional (default: "raise")

- **raise**: an exception will be raised if a WKB input geometry is
invalid.
- **warn**: a warning will be raised and invalid WKB geometries will be
returned as ``None``.
- **ignore**: invalid WKB geometries will be returned as ``None``
without a warning.

arrow_to_pandas_kwargs : dict, optional (default: None)
When `use_arrow` is True, these kwargs will be passed to the `to_pandas`_
call for the arrow to pandas conversion.
Expand Down Expand Up @@ -234,7 +244,6 @@ def read_dataframe(

import pandas as pd
import geopandas as gp
from geopandas.array import from_wkb
import shapely # if geopandas is present, shapely is expected to be present

path_or_buffer = _stringify_path(path_or_buffer)
Expand Down Expand Up @@ -292,10 +301,10 @@ def read_dataframe(
if PANDAS_GE_15 and wkb_values.dtype != object:
# for example ArrowDtype will otherwise create numpy array with pd.NA
wkb_values = wkb_values.to_numpy(na_value=None)
df["geometry"] = from_wkb(wkb_values, crs=meta["crs"])
df["geometry"] = shapely.from_wkb(wkb_values, on_invalid=on_invalid)
if force_2d:
df["geometry"] = shapely.force_2d(df["geometry"])
return gp.GeoDataFrame(df, geometry="geometry")
return gp.GeoDataFrame(df, geometry="geometry", crs=meta["crs"])
else:
return df

Expand All @@ -315,9 +324,9 @@ def read_dataframe(
if geometry is None or not read_geometry:
return df

geometry = from_wkb(geometry, crs=meta["crs"])
geometry = shapely.from_wkb(geometry, on_invalid=on_invalid)

return gp.GeoDataFrame(df, geometry=geometry)
return gp.GeoDataFrame(df, geometry=geometry, crs=meta["crs"])


# TODO: handle index properly
Expand Down
Binary file not shown.
31 changes: 31 additions & 0 deletions pyogrio/tests/test_geopandas_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -1641,6 +1641,37 @@ def test_write_geometry_z_types_auto(
assert_geodataframe_equal(gdf, result_gdf)


@pytest.mark.parametrize(
"on_invalid, message",
[
(
"warn",
"Invalid WKB: geometry is returned as None. IllegalArgumentException: "
"Invalid number of points in LinearRing found 2 - must be 0 or >=",
),
("raise", "Invalid number of points in LinearRing found 2 - must be 0 or >="),
("ignore", None),
],
)
def test_read_invalid_shp(data_dir, use_arrow, on_invalid, message):
if on_invalid == "raise":
handler = pytest.raises(shapely.errors.GEOSException, match=message)
elif on_invalid == "warn":
handler = pytest.warns(match=message)
elif on_invalid == "ignore":
handler = contextlib.nullcontext()
else:
raise ValueError(f"unknown value for on_invalid: {on_invalid}")

with handler:
df = read_dataframe(
data_dir / "poly_not_enough_points.shp.zip",
use_arrow=use_arrow,
on_invalid=on_invalid,
)
df.geometry.isnull().all()


def test_read_multisurface(data_dir, use_arrow):
if use_arrow:
with pytest.raises(shapely.errors.GEOSException):
Expand Down