-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmodel-calculation.py
260 lines (225 loc) · 8.6 KB
/
model-calculation.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
import os
import pathlib
import json
import subprocess
import shutil
import lithops
from lithops.storage import Storage
import time
import numpy as np
import pdal
from osgeo import gdal
from scipy import ndimage
from posixpath import join as posix_join
DATA_BUCKET = 'cb-geospatial-wildfire'
COMPUTE_BACKEND = 'aws_lambda'
STORAGE_BACKEND = 'aws_s3'
STORAGE_PREFIX = 'aws_s3://'
INPUT_DATA_PREFIX = 'input-las-tiles/'
FCC_WINDOW = 3
FCC_BREAKPOINT = 0.01
# Upload dataset
LOCAL_INPUT_DIR = 'input-las-tiles'
storage = Storage(backend=STORAGE_BACKEND)
bucket_objects = storage.list_keys(bucket=DATA_BUCKET)
st = time.time()
print('\nUploading laz files...')
for file_name in os.listdir(LOCAL_INPUT_DIR):
if file_name not in bucket_objects:
key = os.path.join(INPUT_DATA_PREFIX, file_name)
with open(os.path.join(LOCAL_INPUT_DIR, file_name), 'rb') as file:
data = file.read()
storage.put_object(bucket=DATA_BUCKET, key=key, body=data)
print(f'Upload completed with exit!!!')
# Calculte DEM, DSM and CHM
#print(storage.list_keys(bucket=DATA_BUCKET))
def calculate_models(obj, storage):
# Create temporary file paths
tmp_path_prefix = '/tmp/geo/'
if os.path.exists(tmp_path_prefix):
shutil.rmtree(tmp_path_prefix)
for subpath in ['dsm', 'dem', 'chm', 'aspect', 'slope', 'fcc']:
os.makedirs(os.path.join(tmp_path_prefix, subpath), exist_ok=True)
las_tile_filename = pathlib.Path(obj.key).name
tile_key = pathlib.Path(obj.key).stem
# Save obj to file
data = obj.data_stream.read()
input_file_path = os.path.join(tmp_path_prefix, las_tile_filename)
with open(input_file_path, 'wb') as file:
file.write(data)
# DSM pipeline
dsm_file_path = os.path.join(tmp_path_prefix, 'dsm', tile_key + '.gtiff')
dsm_pipeline_json = {
"pipeline": [
{
"type": "readers.las",
"filename": f"{input_file_path}",
"spatialreference": "EPSG:25830"
},
{
"type": "filters.reprojection",
"in_srs": "EPSG:25830",
"out_srs": "EPSG:25830"
},
{
"type": "filters.outlier",
"method": "radius",
"radius": 1.0,
"min_k": 4
},
{
"type": "filters.range",
# Classification equals 2 (corresponding to noise points in LAS).
"limits": "Classification![7:7]"
},
{
"type": "filters.range",
"limits": "returnnumber[1:1]"
},
{
"type": "writers.gdal",
"gdaldriver": "GTiff",
"nodata": "-9999",
"output_type": "max",
"resolution": 1,
"filename": f"{dsm_file_path}"
}
]
}
dsm_pipeline_json_str = json.dumps(dsm_pipeline_json, indent=4)
pipeline = pdal.Pipeline(dsm_pipeline_json_str)
#pipeline.validate()
#pipeline.loglevel = 8
print('Executing DSM pipeline...')
result = pipeline.execute()
print(result)
# DEM pipeline
dem_file_path = os.path.join(tmp_path_prefix, 'dem', tile_key + '.gtiff')
dem_pipeline_json = {
"pipeline": [
{
"type": "readers.las",
"filename": f"{input_file_path}",
"spatialreference": "EPSG:25830"
},
{
"type": "filters.reprojection",
"in_srs": "EPSG:25830",
"out_srs": "EPSG:25830"
},
{
"type": "filters.assign",
"assignment": "Classification[:]=0"
},
{
"type": "filters.elm"
},
{
"type": "filters.outlier",
"method": "radius",
"radius": 1.0,
"min_k": 4
},
{
"type": "filters.smrf",
"ignore": "Classification[7:7]",
"slope": 0.2,
"window": 16,
"threshold": 0.45,
"scalar": 1.2
},
{
"type": "filters.range",
# Classification equals 2 (corresponding to ground in LAS).
"limits": "Classification[2:2]",
},
{
"type": "writers.gdal",
"gdaldriver": "GTiff",
"nodata": "-9999",
"output_type": "max",
"resolution": 1,
"filename": f"{dem_file_path}"
}
]
}
dem_pipeline_json_str = json.dumps(dem_pipeline_json, indent=4)
pipeline = pdal.Pipeline(dem_pipeline_json_str)
#pipeline.validate() # Check if json options are good
#pipeline.loglevel = 8
print('Executing DEM pipeline...')
result = pipeline.execute()
print(result)
# calculate CHM
chm_file_path = os.path.join(tmp_path_prefix, 'chm', tile_key + '.tiff')
cmd = ['gdal_calc.py', '-A', dem_file_path, '-B', dsm_file_path,
'--calc="B-A"', '--NoDataValue=0', '--outfile', chm_file_path]
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True)
stdout, stderr = p.communicate()
print(stdout, stderr)
# assert p.returncode == 0
# calculate aspect
aspect_file_path = os.path.join(tmp_path_prefix, 'aspect', tile_key + '.tiff')
cmd = ['gdaldem', 'aspect', dem_file_path, aspect_file_path, '-compute_edges']
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True)
stdout, stderr = p.communicate()
print(stdout, stderr)
# assert p.returncode == 0
# calculate slope
slope_file_path = os.path.join(tmp_path_prefix, 'slope', tile_key + '.tiff')
cmd = ['gdaldem', 'slope', dem_file_path, slope_file_path, '-compute_edges']
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True)
stdout, stderr = p.communicate()
print(stdout, stderr)
# assert p.returncode == 0
# calculate FCC
in_ds = gdal.Open(dem_file_path)
rows = in_ds.RasterYSize
cols = in_ds.RasterXSize
in_band = in_ds.GetRasterBand(1)
data = in_band.ReadAsArray(0, 0, cols, rows).astype(np.float64)
data[data > FCC_BREAKPOINT] = 1
data[data <= FCC_BREAKPOINT] = 0
# Computing fraction on the whole raster through a moving window.
def _compute_fraction(array):
nveg = np.sum(array == 1)
total = len(array)
out = (nveg/total)*100
return(out)
TCC = ndimage.generic_filter(data, _compute_fraction, size=FCC_WINDOW)
gtiff_driver = gdal.GetDriverByName("GTiff")
fcc_file_path = os.path.join(tmp_path_prefix, 'fcc', tile_key + '.tiff')
out_ds = gtiff_driver.Create(fcc_file_path, cols, rows, 1, in_band.DataType)
out_ds.SetProjection(in_ds.GetProjection())
out_ds.SetGeoTransform(in_ds.GetGeoTransform())
out_band = out_ds.GetRasterBand(1)
out_band.WriteArray(TCC)
# out_ds.BuildOverviews("Average", [2, 4, 8, 16, 32])
out_ds.FlushCache()
del in_ds, out_ds
outputs = [dsm_file_path, dem_file_path, chm_file_path,
aspect_file_path, slope_file_path, fcc_file_path]
for output_path in outputs:
if os.path.exists(output_path):
with open(output_path, 'rb') as output_file:
data = output_file.read()
cos_key = output_path.replace(tmp_path_prefix, '')
storage.put_object(bucket=DATA_BUCKET, key=cos_key, body=data)
else:
print(f'Failed to upload {output_path}')
out = subprocess.check_output(['find', '/tmp/geo/'])
return out
fexec = lithops.FunctionExecutor()
print(os.path.join(STORAGE_PREFIX, DATA_BUCKET, INPUT_DATA_PREFIX))
fs = fexec.map(calculate_models, posix_join(STORAGE_PREFIX, DATA_BUCKET, INPUT_DATA_PREFIX))
res = fexec.get_result(fs=fs)
elapsed = time.time() - st
for r in res:
print(r.decode('utf-8').strip())
print('---')
stats = [f.stats for f in fexec.futures]
gbxms_price = 0.0000000167
sum_total_time = sum([stat['worker_exec_time'] for stat in stats]) * 1000
price = gbxms_price * sum_total_time * 4 # Price GB/ms * sum of times in ms * 4 GB
print(f'Experiment total price is {round(price, 3)} USD')
print(f'Total time: {round(elapsed, 3} s')