Skip to content

Commit

Permalink
there was a bug in the rasterio src handler from weecology@e6e2716 (w…
Browse files Browse the repository at this point in the history
  • Loading branch information
bw4sz authored Dec 27, 2023
1 parent 58b7b4f commit 16b0cf0
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
3 changes: 2 additions & 1 deletion deepforest/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,9 +293,10 @@ def shapefile_to_annotations(shapefile,
with rasterio.open(rgb) as src:
left, bottom, right, top = src.bounds
resolution = src.res[0]
raster_crs = src.crs

# Check matching the crs
if not gdf.crs == src.crs:
if not gdf.crs == raster_crs:
raise ValueError("The shapefile crs {} does not match the image crs {}".format(
gdf.crs, src.crs))

Expand Down
11 changes: 11 additions & 0 deletions tests/test_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,18 @@ def test_shapefile_to_annotations(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="bbox")
assert shp.shape[0] == 2

def test_shapefile_to_annotations_incorrect_crs(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:32618")
gdf["geometry"] = [geometry.box(left, bottom, right, top) for left, bottom, right, top in gdf.geometry.buffer(0.5).bounds.values]

gdf.to_file("{}/annotations.shp".format(tmpdir))
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_boxes_to_shapefile_projected(m):
img = get_data("OSBS_029.tif")
r = rio.open(img)
Expand Down

0 comments on commit 16b0cf0

Please sign in to comment.