-
Notifications
You must be signed in to change notification settings - Fork 18
Polygonize with watershed #152
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -2,6 +2,7 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import os | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import re | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import time | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from typing import Optional | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import fiona | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import fiona.transform | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -10,14 +11,79 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import rasterio.features | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import shapely.geometry | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from affine import Affine | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from fiboa_cli.parquet import create_parquet, features_to_dataframe | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| try: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from fiboa_cli.parquet import create_parquet, features_to_dataframe | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| FIBOA_AVAILABLE = True | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| except ImportError: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| FIBOA_AVAILABLE = False | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| print("Warning: fiboa_cli.parquet not available. Parquet output will use alternative implementation.") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+17
to
+20
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def create_parquet(gdf, columns, collection, out, config, compression="brotli"): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import geopandas as gpd | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+21
to
+22
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def create_parquet(gdf, columns, collection, out, config, compression="brotli"): | |
| import geopandas as gpd | |
| def create_parquet(gdf, columns, collection, out, config, compression="brotli"): | |
| """ | |
| Fallback implementation of create_parquet. | |
| - Uses columns argument to set column order and ensure all required columns are present. | |
| - Ignores collection and config arguments (fiboa metadata not preserved). | |
| """ | |
| import geopandas as gpd | |
| import pandas as pd | |
| # Ensure all required columns are present and in the correct order | |
| gdf = gdf.copy() | |
| for col in columns: | |
| if col not in gdf.columns: | |
| gdf[col] = pd.NA | |
| # Reorder columns (excluding geometry, which must be last for GeoPandas) | |
| geometry_col = gdf.geometry.name if hasattr(gdf, "geometry") else "geometry" | |
| non_geom_cols = [col for col in columns if col != geometry_col] | |
| ordered_cols = non_geom_cols + [geometry_col] if geometry_col in gdf.columns else non_geom_cols | |
| gdf = gdf[ordered_cols] |
Copilot
AI
Oct 3, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
higra is imported unconditionally; users selecting the 'simple' algorithm without higra installed will now get an ImportError. Move this import inside the watershed branch (or wrap in try/except with a clear error) to preserve previous functionality for non-watershed usage.
| import higra as hg | |
| from ftw_tools.settings import SUPPORTED_POLY_FORMATS_TXT | |
| def InstSegm(extent, boundary, t_ext=0.5, t_bound=0.2): | |
| from ftw_tools.settings import SUPPORTED_POLY_FORMATS_TXT | |
| def InstSegm(extent, boundary, t_ext=0.5, t_bound=0.2): | |
| try: | |
| import higra as hg | |
| except ImportError: | |
| raise ImportError( | |
| "The 'higra' package is required for the 'watershed' algorithm. " | |
| "Please install it with 'pip install higra' to use this feature." | |
| ) |
Copilot
AI
Oct 3, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing docstring for a new non-trivial algorithmic function; add a docstring describing expected input ranges, shapes, meaning of t_ext/t_bound, and returned array semantics (NaN masking, instance labeling).
Copilot
AI
Oct 3, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a docstring clarifying the assumed class encoding in mask (e.g., why value 3 is zeroed), the rationale for the 3x3 filters, and the expected output value range.
Copilot
AI
Oct 3, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Docstring not updated to document new parameters algorithm, t_ext, and t_bound; include descriptions and valid value ranges to aid CLI and API users.
| """Polygonize the output from inference.""" | |
| """ | |
| Polygonize the output from inference. | |
| Parameters | |
| ---------- | |
| input : str | |
| Path to the input file (e.g., mask or raster to polygonize). | |
| out : str | |
| Path to the output file (e.g., .parquet, .gpkg, etc.). | |
| simplify : bool, optional | |
| Whether to simplify polygons (default: True). | |
| min_size : int, optional | |
| Minimum polygon area in pixels to keep (default: 500). | |
| max_size : int or None, optional | |
| Maximum polygon area in pixels to keep (default: None, meaning no maximum). | |
| overwrite : bool, optional | |
| Whether to overwrite the output file if it exists (default: False). | |
| close_interiors : bool, optional | |
| Whether to close polygon interiors (default: False). | |
| algorithm : str, optional | |
| Polygonization algorithm to use. Options: | |
| - "simple": Basic polygonization (default) | |
| - "watershed": Use watershed-based segmentation | |
| (default: "simple") | |
| t_ext : float, optional | |
| Threshold for exterior (field) mask. Range: 0.0 to 1.0 (default: 0.5). | |
| Higher values may result in fewer polygons. | |
| t_bound : float, optional | |
| Threshold for boundary mask. Range: 0.0 to 1.0 (default: 0.2). | |
| Lower values may result in more sensitive boundary detection. | |
| Returns | |
| ------- | |
| None | |
| Notes | |
| ----- | |
| The function reads the input mask, extracts polygons using the specified algorithm and thresholds, | |
| and writes the result to the specified output file. | |
| """ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Optional is imported but not used anywhere in the shown changes; remove the unused import to reduce clutter.