-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_output.py
40 lines (32 loc) · 1.29 KB
/
generate_output.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import utils
import geopandas as gp
import os
import pathlib
import rasterio
import pandas as pd
BASE_DIR = utils.BASE_DIR
def generate_output():
# Create empty dataFrame
column_names = ["image", "region_num", "geometry"]
# column_names = [str , numpy.int64, shapely.geometry.polygon.Polygon]
gdf = gp.GeoDataFrame(columns = column_names)
regions = sorted([name for name in os.listdir(BASE_DIR / "big_predictions")])
for region in regions:
for mask_path in os.listdir(BASE_DIR / "big_predictions" / region):
if 'part' not in mask_path:
whole_mask_path = BASE_DIR / "big_predictions" / region / mask_path
mask = rasterio.open(whole_mask_path)
polygons_list = utils.mask_to_polygons(mask)
for polygon in polygons_list:
new_row = {'image': mask_path,'region_num': region, 'geometry': polygon}
gdf.loc[len(gdf)] = new_row
mask.close()
gdf = gdf.set_crs(3857)
# Filter small lakes
areas = gdf.area
gdf['Area'] = areas
gdf_filt = gdf[gdf['Area']>= 100000]
gdf_filt.drop('Area', inplace=True, axis=1)
gdf_filt.to_file('lake_polygons_test.gpkg', driver='GPKG')
if __name__ == "__main__":
generate_output()