Skip to content
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

Bug fix, crs matching #591

Merged
merged 1 commit into from
Dec 27, 2023
Merged
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
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
Loading