-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgedi_prep.py
270 lines (194 loc) · 8.3 KB
/
gedi_prep.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Feb 17 12:41:46 2021
@author: heatherkay
"""
import geopandas
import os.path
import glob
from rasterstats import zonal_stats
import numpy as np
import pandas as pd
import rsgislib.vectorutils
def tif_join(folderin, rasterin, folderout):
"""
Function to join GEDI gpkg files to tif
Parameters
----------
folderin: string
Filepath for folder contain GEDI files
rasterin: string
Filepath for tif file
folderout: string
Filepath for folder to contain joined files
"""
gedifiles = glob.glob(folderin + '*.gpkg')
raster = rasterin
beams = ['BEAM0000','BEAM0001','BEAM0010','BEAM0011','BEAM0101','BEAM0110',
'BEAM1000','BEAM1011']
stats = 'median'
for file in gedifiles:
name = os.path.splitext(os.path.basename(file))[0]
for beam in beams:
vector = geopandas.read_file(file, layer=beam)
result = zonal_stats(vector, raster, stats=stats, geojson_out=True)
geostats = geopandas.GeoDataFrame.from_features(result)
geostats.to_file(folderout + name + ".gpkg", layer = beam, driver='GPKG')
def rmv_cat(folderin, folderout):
"""
Function to remove unvegetated GEDI footprints based on CCILC, remove
unneccesary columns, remove footprints with 'surface flag' equal to 1,
remove any footprints with rh100 less than 0, and add a column with the
acquisition date extracted from the filename
Parameters
----------
folderin: string
Filepath for folder contain GEDI files
folderout: string
Filepath for folder to contain processed files
"""
gedifiles = glob.glob(folderin + '*.gpkg')
beams = ['BEAM0000','BEAM0001','BEAM0010','BEAM0011','BEAM0101','BEAM0110',
'BEAM1000','BEAM1011']
colNms =['elevation_bin0','elevation_lastbin','height_bin0','height_lastbin',
'shot_number','solar_azimuth','solar_elevation','latitude_bin0',
'latitude_lastbin','longitude_bin0','longitude_lastbin',
'master_frac','master_int','omega','pai','pgap_theta',
'pgap_theta_error','rg','rhog','rhog_error','rhov','rhov_error',
'rossg','rv']
cat=['0.0', '190.0','200.0','202.0', '210.0', '220.0']
column='median'
for file in gedifiles:
name = os.path.splitext(os.path.basename(file))[0]
name_comp = name.split('_')
date = name_comp[2]
for beam in beams:
df = geopandas.read_file(file, layer=beam)
new = df[np.logical_not(df[column].isin(cat))]
if new.empty:
continue
df2 = new[new['surface_flag']==1]
df3 = df2[df2['rh100']>=0]
df3.drop(colNms, 1, inplace = True)
df3['date']= date
df3.to_file(folderout + name + '.gpkg', layer = beam, driver='GPKG')
def shp_join(folderin, shapein, folderout):
"""
Function to join GEDI gpkg files to shp (here wwf ecoregions)
Parameters
----------
folderin: string
Filepath for folder contain GEDI files
wwfin: string
Filepath for shape file
folderout: string
Filepath for folder to contain joined files
"""
gedifiles = glob.glob(folderin + '*.gpkg')
beams = ['BEAM0000','BEAM0001','BEAM0010','BEAM0011','BEAM0101','BEAM0110',
'BEAM1000','BEAM1011']
colNms = ['AREA', 'ECO_NUM', 'ECO_SYM','G200_BIOME', 'G200_NUM',
'G200_REGIO', 'G200_STAT', 'GBL_STAT', 'OBJECTID', 'PERIMETER',
'PER_area', 'PER_area_1', 'PER_area_2','REALM', 'Shape_Area',
'Shape_Leng', 'area_km2']
wwf_layer = geopandas.read_file(shapein)
for file in gedifiles:
name = os.path.splitext(os.path.basename(file))[0]
for beam in beams:
vector = geopandas.read_file(file, layer=beam)
result = geopandas.sjoin(vector, wwf_layer, how="inner", op="within")
geostats = geopandas.GeoDataFrame.from_features(result)
geostats.drop(colNms, 1, inplace = True)
geostats.to_file(folderout + name + ".gpkg", layer = beam, driver='GPKG')
def split_per_eco(folderin, folderout):
"""
Function to split GEDI files per ecoregion
Parameters
----------
folderin: string
Filepath for folder contain GEDI files
folderout: string
Filepath for folder to contain processed files
"""
gedifiles = glob.glob(folderin + '*.gpkg')
beams = ['BEAM0000','BEAM0001','BEAM0010','BEAM0011','BEAM0101','BEAM0110',
'BEAM1000','BEAM1011']
split_col='ECO_ID'
for file in gedifiles:
name = os.path.splitext(os.path.basename(file))[0]
for beam in beams:
dfa = geopandas.read_file(file, layer=beam)
df = dfa.astype({split_col: 'int32'})
ecoNames = list(np.unique(df[split_col]))#get list of unique ecoregions
for eco in ecoNames:
#create new df
df2 = geopandas.GeoDataFrame(df)
ID = str(eco)
df_eco = df2.loc[df2[split_col]==eco]
df_eco.to_file(folderout + name + "_" + ID + ".gpkg", layer = beam, driver='GPKG')
def join_per_eco(folderin, folderout, IDfile):
"""
Function to join GEDI files per ecoregion
Parameters
----------
folderin: string
Filepath for folder contain GEDI files
folderout: string
Filepath for folder to contain processed files
IDfile: string
Filepath for folder containing list of ecoregion IDs
"""
df = pd.read_csv(IDfile)
ecoNms = list(np.unique(df['ID']))
for ecoNm in ecoNms:
ecoNm = ecoNm.astype(str)
fileList = glob.glob(folderin + 'GEDI*_{}.gpkg'.format(ecoNm))#here also need dict ref
rsgislib.vectorutils.mergeVectors2GPKG(fileList, folderout + 'gedi_' +
ecoNm + '.gpkg', lyrName='join', exists=False)
#rsgislib.vectorutils.mergeVectors2GPKGIndLyrs(fileList, folderout + 'gedi_' + ecoNm + '.gpkg')
def eco_tif_join(folderin, rasterin, folderout):
"""
Function to join GEDI gpkg files regrouped per ecoregion to tif
Parameters
----------
folderin: string
Filepath for folder contain GEDI files
rasterin: string
Filepath for tif file
folderout: string
Filepath for folder to contain joined files
"""
gedifiles = glob.glob(folderin + '*.gpkg')
raster = rasterin
beam = 'join'
stats = 'grid'
for file in gedifiles:
name = os.path.splitext(os.path.basename(file))[0]
vector = geopandas.read_file(file, layer=beam)
result = zonal_stats(vector, raster, stats=stats, geojson_out=True)
geostats = geopandas.GeoDataFrame.from_features(result)
geostats.to_file(folderout + name + ".gpkg", layer = beam, driver='GPKG')
def split_per_grid(folderin, folderout):
"""
Function to split GEDI files per ecoregion
Parameters
----------
folderin: string
Filepath for folder contain GEDI files
folderout: string
Filepath for folder to contain processed files
"""
gedifiles = glob.glob(folderin + '*.gpkg')
split_col='grid'
for file in gedifiles:
name = os.path.splitext(os.path.basename(file))[0]
dfa = geopandas.read_file(file, layer='join')
df = dfa.astype({split_col: 'int32'})
ecoNames = list(np.unique(df[split_col]))#get list of unique ecoregions
for eco in ecoNames:
#create new df
df2 = geopandas.GeoDataFrame(df)
ID = str(eco)
df_eco = df2.loc[df2[split_col]==eco]
df_eco.to_file(folderout + name + "_" + ID + ".gpkg", layer = 'join', driver='GPKG')