Skip to content

Commit

Permalink
Merge pull request #695 from weecology/issue_694
Browse files Browse the repository at this point in the history
raise error on wgs84 boxes for shapefile to annotation see issue #694
  • Loading branch information
henrykironde authored Jun 23, 2024
2 parents 8889e46 + 9a69d2a commit a16b146
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
5 changes: 5 additions & 0 deletions deepforest/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,11 @@ def shapefile_to_annotations(shapefile,
raster_crs = src.crs

if gdf.crs:
# If epsg is 4326, then the buffer size is in degrees, not meters, see https://github.com/weecology/DeepForest/issues/694
if gdf.crs.to_string() == "EPSG:4326":
raise ValueError(
"The shapefile crs is in degrees. This function works for UTM and meter based crs only. see https://github.com/weecology/DeepForest/issues/694")

# Check matching the crs
if not gdf.crs.to_string() == raster_crs.to_string():
raise ValueError(
Expand Down
11 changes: 11 additions & 0 deletions tests/test_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,17 @@ def test_shapefile_to_annotations_convert_unprojected_to_boxes(tmpdir):
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_invalid_epsg(tmpdir):
sample_geometry = [geometry.Point(404211.9 + 10, 3285102 + 20), geometry.Point(404211.9 + 20, 3285102 + 20)]
labels = ["Tree", "Tree"]
df = pd.DataFrame({"geometry": sample_geometry, "label": labels})
gdf = gpd.GeoDataFrame(df, geometry="geometry", crs="EPSG:4326")
gdf.to_file("{}/annotations.shp".format(tmpdir))
assert gdf.crs.to_string() == "EPSG:4326"
image_path = get_data("OSBS_029.tif")
with pytest.raises(ValueError):
shp = utilities.shapefile_to_annotations(shapefile="{}/annotations.shp".format(tmpdir), rgb=image_path, savedir=tmpdir, geometry_type="bbox")

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"]
Expand Down

0 comments on commit a16b146

Please sign in to comment.