-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from geoaigroup/hasan-nn-patch-4
add conversion.py #correction
- Loading branch information
Showing
1 changed file
with
34 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |