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

check for unprojected data when converting points to boxes #644

Merged
merged 1 commit into from
Apr 1, 2024
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
9 changes: 5 additions & 4 deletions deepforest/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 11 additions & 1 deletion tests/test_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
Loading