Skip to content
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

merscope reader remove invalid polygons #207

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
4 changes: 3 additions & 1 deletion src/spatialdata_io/readers/merscope.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import xarray
from dask import array as da
from dask_image.imread import imread
from shapely.geometry import MultiPolygon
from spatialdata import SpatialData
from spatialdata._logging import logger
from spatialdata.models import Image2DModel, PointsModel, ShapesModel, TableModel
Expand Down Expand Up @@ -311,7 +312,8 @@ def _get_polygons(boundaries_path: Path, transformations: dict[str, BaseTransfor
geo_df = geopandas.read_parquet(boundaries_path)
geo_df = geo_df.rename_geometry("geometry")
geo_df = geo_df[geo_df[MerscopeKeys.Z_INDEX] == 0] # Avoid duplicate boundaries on all z-levels
geo_df.geometry = geo_df.geometry.map(lambda x: x.geoms[0]) # The MultiPolygons contain only one polygon
geo_df = geo_df[geo_df.geometry.is_valid] # Remove invalid geometries
geo_df.geometry = geo_df.geometry.map(lambda x: MultiPolygon(x.geoms))
geo_df.index = geo_df[MerscopeKeys.METADATA_CELL_KEY].astype(str)

return ShapesModel.parse(geo_df, transformations=transformations)
Expand Down
Loading