From 7eb47fc058d1af843c5b452f6ea874850d55531e Mon Sep 17 00:00:00 2001 From: bw4sz Date: Fri, 29 Mar 2024 16:28:02 -0700 Subject: [PATCH] check for unprojected data when converting points to boxes --- deepforest/utilities.py | 9 +++++---- tests/test_utilities.py | 12 +++++++++++- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/deepforest/utilities.py b/deepforest/utilities.py index 91ca9285..a6cb8300 100644 --- a/deepforest/utilities.py +++ b/deepforest/utilities.py @@ -295,10 +295,11 @@ def shapefile_to_annotations(shapefile, resolution = src.res[0] raster_crs = src.crs - # Check matching the crs - if not gdf.crs.to_string() == raster_crs.to_string(): - raise ValueError("The shapefile crs {} does not match the image crs {}".format( - gdf.crs, src.crs)) + if gdf.crs: + # Check matching the crs + if not gdf.crs.to_string() == raster_crs.to_string(): + raise ValueError("The shapefile crs {} does not match the image crs {}".format( + gdf.crs, src.crs)) # Transform project coordinates to image coordinates df["tile_xmin"] = (df.minx - left) / resolution diff --git a/tests/test_utilities.py b/tests/test_utilities.py index b5a9ea49..3f80332c 100644 --- a/tests/test_utilities.py +++ b/tests/test_utilities.py @@ -60,7 +60,17 @@ def test_shapefile_to_annotations_convert_to_boxes(tmpdir): image_path = get_data("OSBS_029.tif") shp = utilities.shapefile_to_annotations(shapefile="{}/annotations.shp".format(tmpdir), rgb=image_path, savedir=tmpdir, geometry_type="point") assert shp.shape[0] == 2 - + +def test_shapefile_to_annotations_convert_unprojected_to_boxes(tmpdir): + sample_geometry = [geometry.Point(10,20),geometry.Point(20,40)] + labels = ["Tree","Tree"] + df = pd.DataFrame({"geometry":sample_geometry,"label":labels}) + gdf = gpd.GeoDataFrame(df, geometry="geometry") + gdf.to_file("{}/annotations.shp".format(tmpdir)) + image_path = get_data("OSBS_029.png") + shp = utilities.shapefile_to_annotations(shapefile="{}/annotations.shp".format(tmpdir), rgb=image_path, savedir=tmpdir, geometry_type="point") + assert shp.shape[0] == 2 + def test_shapefile_to_annotations(tmpdir): sample_geometry = [geometry.Point(404211.9 + 10,3285102 + 20),geometry.Point(404211.9 + 20,3285102 + 20)] labels = ["Tree","Tree"]