From d6aa0dfd1b5cc4e35bbfc2b9c316e82705afba2f Mon Sep 17 00:00:00 2001 From: Hasan Nasrallah <50121708+hasan-nn@users.noreply.github.com> Date: Wed, 13 Dec 2023 16:54:57 +0200 Subject: [PATCH] add conversion.py #correction script to convert instances mask to geodataframe --- data_processing/conversion.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 data_processing/conversion.py diff --git a/data_processing/conversion.py b/data_processing/conversion.py new file mode 100644 index 0000000..59c0339 --- /dev/null +++ b/data_processing/conversion.py @@ -0,0 +1,34 @@ +import rasterio as rio +import geopandas as gpd +from rasterio.features import shapes + +def instance_mask_to_gdf( + instance_mask, + transform = None, + crs=None + ): + """ + Input: + - instance_mask : np.array of shape (H,W), where each instance is labeled by a unique id/number + - transform : geospatial transform of the raster - default is None + - crs : crs of the raster - default is None + Output: + - GeoDataFrame of the shapes projected to the specified crs using the transform + """ + + #transform should be Identity if None is provided + transform = rio.transform.IDENTITY if transform is None else transform + + all_shapes = shapes(instance_mask,mask=None,transform=transform) + data = [ + {'properties' : {'id' : v} , 'geometry' : s} for i,(s,v) in enumerate(all_shapes) if v!=0 + ] + + if len(data) == 0: + ##return empty dataframe + return gpd.GeoDataFrame(columns=['id','geometry'], geometry='geometry',crs=crs) + + gdf = gpd.GeoDataFrame.from_features(data,crs=crs) + gdf = gdf.dissolve(by='id') + + return gdf \ No newline at end of file