Skip to content
Open
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
29 changes: 22 additions & 7 deletions src/deepforest/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,13 +224,27 @@ def shapefile_to_annotations(
convert_point: bool = False,
label: str | None = None,
) -> gpd.GeoDataFrame:
raise DeprecationWarning(
"shapefile_to_annotations is deprecated, use read_file instead"
warnings.warn(
"shapefile_to_annotations is deprecated, use read_file instead",
DeprecationWarning,
stacklevel=2,
)

if buffer_size is not None and convert_point:
raise DeprecationWarning(
"buffer_size argument is deprecated, use convert_point_to_bbox instead"
warnings.warn(
"buffer_size argument is deprecated, use convert_point_to_bbox instead",
DeprecationWarning,
stacklevel=2,
)

if isinstance(shapefile, str):
shapefile_path = shapefile
shapefile = gpd.read_file(shapefile_path)
shapefile = DeepForest_DataFrame(shapefile)
shapefile = __assign_image_path__(shapefile, image_path=rgb)
shapefile = __check_and_assign_label__(shapefile, label=label)
shapefile = __assign_root_dir__(
input=shapefile_path, gdf=shapefile, root_dir=root_dir
)

return __shapefile_to_annotations__(shapefile)
Expand All @@ -247,12 +261,13 @@ def __assign_image_path__(gdf, image_path: str) -> str:
pass
else:
if "image_path" in gdf.columns:
existing_image_path = gdf.image_path.unique()[0]
if len(existing_image_path) > 1:
existing_image_paths = gdf.image_path.unique()
if len(existing_image_paths) > 1:
warnings.warn(
f"Multiple image_paths found in dataframe: {existing_image_path}, overriding and assigning {image_path} to all rows!",
f"Multiple image_paths found in dataframe: {existing_image_paths}, overriding and assigning {image_path} to all rows!",
stacklevel=2,
)
existing_image_path = existing_image_paths[0]
if existing_image_path != image_path:
warnings.warn(
f"Image path {existing_image_path} found in dataframe, overriding and assigning {image_path} to all rows!",
Expand Down
41 changes: 41 additions & 0 deletions tests/test_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,47 @@ def test_read_file_shapefile_without_image_path(tmp_path):
assert "label" in result.columns
assert hasattr(result, "root_dir")

def test_shapefile_to_annotations_deprecation_warning(tmp_path):
"""shapefile_to_annotations should emit DeprecationWarning, not raise it."""
import warnings
sample_geometry = [geometry.Point(404211.9 + 10, 3285102 + 20)]
labels = ["Tree"]
df = pd.DataFrame({"geometry": sample_geometry, "label": labels})
gdf = gpd.GeoDataFrame(df, geometry="geometry", crs="EPSG:32617")
gdf["geometry"] = [geometry.box(left, bottom, right, top) for left, bottom, right, top in
gdf.geometry.buffer(0.5).bounds.values]
gdf["image_path"] = os.path.basename(get_data("OSBS_029.tif"))
shp_path = str(tmp_path / "annotations.shp")
gdf.to_file(shp_path)

with pytest.warns(DeprecationWarning, match="shapefile_to_annotations is deprecated"):
result = utilities.shapefile_to_annotations(shp_path, root_dir=os.path.dirname(get_data("OSBS_029.tif")))
assert result is not None

def test_assign_image_path_warns_on_override():
"""__assign_image_path__ should warn when overriding an existing image_path."""
import warnings
sample_geometry = [geometry.box(0, 0, 1, 1)]
df = pd.DataFrame({"geometry": sample_geometry, "label": ["Tree"], "image_path": ["old_image.tif"]})
gdf = gpd.GeoDataFrame(df, geometry="geometry")

with pytest.warns(UserWarning, match="overriding and assigning"):
utilities.__assign_image_path__(gdf, image_path="new_image.tif")

def test_assign_image_path_warns_multiple_paths():
"""__assign_image_path__ should warn when multiple image_paths exist."""
import warnings
sample_geometry = [geometry.box(0, 0, 1, 1), geometry.box(2, 2, 3, 3)]
df = pd.DataFrame({
"geometry": sample_geometry,
"label": ["Tree", "Tree"],
"image_path": ["image_a.tif", "image_b.tif"]
})
gdf = gpd.GeoDataFrame(df, geometry="geometry")

with pytest.warns(UserWarning, match="Multiple image_paths found"):
utilities.__assign_image_path__(gdf, image_path="override.tif")

def test_shapefile_to_annotations_invalid_epsg(tmp_path):
sample_geometry = [geometry.Point(404211.9 + 10, 3285102 + 20), geometry.Point(404211.9 + 20, 3285102 + 20)]
labels = ["Tree", "Tree"]
Expand Down
Loading