-
Notifications
You must be signed in to change notification settings - Fork 0
/
BM1_AddDSMToVector.py
55 lines (41 loc) · 1.35 KB
/
BM1_AddDSMToVector.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# Building Height Estimation
# Prepare input data ... Add height to building footprint
# Thepchai Srinoi
# Department of Survey Engineering
import geopandas as gpd
import rasterio as rio
from rasterstats import zonal_stats
import pandas as pd
import numpy as np
from tqdm import tqdm
# https://gis.stackexchange.com/questions/433246/zonal-statistics-spatial-join-raster-to-polygon-in-python
# Building Footprint
zones = "building_inbound.gpkg"
# Raster data
values = "DSM_MEA.tif"
#########################################################################
# Processing
# Import Geodataframe ... convert to UTM Zone 47
print('import geodataframe')
gdf = gpd.read_file(zones)
gdf = gdf.to_crs(32647)
#gdf = gdf.iloc[0:10]
print( gdf )
# Import DSM
print('import raster')
img = rio.open(values)
b_count = img.count
#import pdb; pdb.set_trace()
# Zonal Statistics ... Median Add Height to Vector Data
stats = gpd.GeoDataFrame(zonal_stats(gdf, values, stats=["median"]
, band= 1))
print('add to dataframe')
gdf['_median'] = stats
# Calculate nDSM
print('Calculate Height above assumed ground')
gdf['height'] = gdf['_median'] - 30
print( gdf )
# Export Data
print('output')
gdf.to_file("building_inbound_DSM.gpkg", driver='GPKG')
import pdb; pdb.set_trace()