-
Notifications
You must be signed in to change notification settings - Fork 11
/
Class_pcl_processing.py
296 lines (254 loc) · 8.27 KB
/
Class_pcl_processing.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
'''
Class and functions to process the point clouds
Simon. Filhol, November 2019
'''
import pdal, json, glob, os
class pcl_process(object):
def __init__(self, project_path, ext='ply'):
if not os.path.exists(project_path):
print("ERROR The path " + project_path + " doesn't exists")
return
else:
self.project_path = project_path
os.chdir(self.project_path)
# parameters for point cloud filtering
self.x_offset = 410000 # these values are default for Finse, NORWAY
self.y_offset = 6710000 # these values are default for Finse, NORWAY
# Cropping area
self.crop_xmin = 416100
self.crop_xmax = 416900
self.crop_ymin = 6715900
self.crop_ymax = 6716700
self.crop_zmin = 1200
self.crop_zmax = 1500
# Raster bounding box, default is same as cropping box.
self.raster_xmin = self.crop_xmin
self.raster_xmax = self.crop_xmax
self.raster_ymin = self.crop_ymin
self.raster_ymax = self.crop_ymax
# parameters for conversion to GeoTiff
self.resolution = 1
self.radius = self.resolution * 1.4
self.nodata = -9999
self.gdaldriver = "GTiff"
self.output_type = ["min", "max", "mean", "idw", "count", "stdev"]
def add_ply_pcl(self):
os.chdir(self.project_path)
self.ply_pcl_flist = glob.glob("*.ply")
print("=======================\n PLY point clouds added: ")
for file in self.ply_pcl_flist:
print(file)
print(".......................")
print(str(self.ply_pcl_flist.__len__()) + " point clouds added")
print("=======================")
def add_las_pcl(self):
os.chdir(self.project_path)
self.las_pcl_flist = glob.glob("*.las")
print("=======================\n LAS point clouds added: ")
for file in self.las_pcl_flist:
print(file)
print(".......................")
print(str(self.las_pcl_flist.__len__()) + " point clouds added")
print("=======================")
#@staticmethod
def pipeline_realization(self, pip_json, print_result=True):
try:
# ===============================================
# Pipeline execution
print('start')
pipeline = pdal.Pipeline(pip_json)
print(pipeline)
print("here")
pipeline.validate() # check if our JSON and options were good
print("there")
pipeline.execute()
if print_result:
arrays = pipeline.arrays
metadata = pipeline.metadata
log = pipeline.log
print("\n================")
print("Arrays:")
print(arrays)
print("\n================")
print("Metadata:")
print(metadata)
print("\n================")
print("Log:")
print(log)
print("pdal pipeline finished")
return True
except:
print(" Error !!")
return False
def filter_pcl(self, file_input, file_output, print_result=True):
'''
Function to filter a point cloud: cropping to ROI, removing statistically outliers, saving output to .las format
'''
pip_filter_json = json.dumps(
{
"pipeline":
[
file_input,
{
"type": "filters.python",
"script": "pdal_python_filter.py",
"function": "add_XY_UTM",
"pdalargs": {"x_offset": self.x_offset, "y_offset": self.y_offset}
},
{
"type": "filters.crop",
"bounds": str(([self.crop_xmin, self.crop_xmax], [self.crop_ymin, self.crop_ymax]))
},
{
"type": "filters.range",
"limits": "Z[" + str(self.crop_zmin) + ":" + str(self.crop_zmax) + "]"
},
{
"type": "filters.lof",
"minpts": 20
},
{
"type": "filters.range",
"limits": "LocalOutlierFactor[:1.2]"
},
{
"type": "filters.range",
"limits": "Classification[7:12]"
},
{
"type": "writers.las",
"filename": file_output,
"scale_x": 1,
"scale_y": 1,
"scale_z": 1
}
]
}
)
if print_result:
print(pip_filter_json)
self.pipeline_realization(pip_filter_json, print_result=print_result)
def filter_all_pcl(self, print_result=True):
'''
Function to process all pcl with filter_pcl() function
'''
print("=======================")
for file in self.ply_pcl_flist:
self.filter_pcl(file, file[:-4] + '_clean.las', print_result=print_result)
print(".......................")
print("All PLY files filtered")
print("=======================")
def convert_pcl2dem(self, input_file, output_file, print_result=True):
'''
Function to convert .las point cloud to a raster (.tif)
'''
pip_dem = json.dumps(
{
"pipeline": [
{"type": "readers.las",
"filename": input_file
},
{
"filename": output_file,
"gdaldriver": "GTiff",
"output_type": "all",
"resolution": self.resolution,
"radius": self.radius,
"bounds": str(([self.raster_xmin, self.raster_xmax], [self.raster_ymin, self.raster_ymax])),
"type": "writers.gdal",
"nodata": self.nodata
}
]
})
self.pipeline_realization(pip_dem, print_result=print_result)
def convert_all_pcl2dem(self, print_result=True):
'''
Function to process all pcl with filter_pcl() function
'''
print("=======================")
for file in self.las_pcl_flist:
self.convert_pcl2dem(file, file[:-4] + '_' + str(self.resolution) + 'm.tif', print_result=print_result)
print(".......................")
print("All LAS converted to DEMs")
print("=======================")
def extract_ortho_value(self, input_file, output_file, print_result=True):
'''
Function to convert .las point cloud to a raster (.tif)
'''
pip_ortho = json.dumps(
{
"pipeline":[
{"type": "readers.las",
"filename": input_file
},
{
"type": "filters.python",
"script": "pdal_python_filter.py",
"function": "rgb2value",
"add_dimension": "Value",
"module": "anything"
},
{
"filename": output_file, #file.split('.')[0] + '_' + str(resolution) + 'm_value.tif',
"gdaldriver": "GTiff",
"output_type": "mean",
"dimension" : "Value",
"resolution": self.resolution,
"radius": self.radius,
"bounds": str(([self.raster_xmin, self.raster_xmax], [self.raster_ymin, self.raster_ymax])),
"type": "writers.gdal",
"nodata": self.nodata
}
]
})
self.pipeline_realization(pip_ortho, print_result=print_result)
def extract_all_ortho_value(self, print_result=True):
'''
Function to process all pcl and derive an orthophoto containing the Value (computed from RGB to HSV)
'''
print("=======================")
for file in self.las_pcl_flist:
self.extract_ortho_value(file, file[:-4] + '_' + str(self.resolution) + 'm_value.tif', print_result=print_result)
print(".......................")
print("All LAS converted to Value orthophoto (monochrome)")
print("=======================")
@staticmethod
def custom_pipeline(json_pipeline):
'''
Function to enter a custom made pdal pipeline. Input should be a Json format pipeline following the format compatible with PDAL instructions
'''
return json.dumps(json_pipeline)
def apply_custom_pipeline(self, pipeline, file_list=None, print_result=True):
"""
Function to apply a custom pipeline to
"""
if file_list is None:
file_list = self.las_pcl_flist
print("=======================")
for file in file_list:
self.pipeline_realization(pipeline, print_result=print_result)
print(".......................")
print("Custom pipeline applied to all files")
print("=======================")
if __name__ == "__main__":
# Create a pcl_class object, indication the path to the photo4d project
my_pcl = pcl_process(project_path="path_to_project_folder")
my_pcl.resolution = 1 # set the resolution of the final DEMs
# Set the bounding box the Region of Interest (ROI)
my_pcl.crop_xmin = 416100
my_pcl.crop_xmax = 416900
my_pcl.crop_ymin = 6715900
my_pcl.crop_ymax = 6716700
my_pcl.nodata = -9999
# add path og the .ply point cloud files to the python class
my_pcl.add_ply_pcl()
# filter the point clouds with pdal routine, and save resulting point clouds as .las file
my_pcl.filter_all_pcl()
# add path of the .las files
my_pcl.add_las_pcl()
# conver the .las point clouds to DEMs (geotiff)
my_pcl.convert_all_pcl2dem()
# Extract Value orthophoto from RGB
my_pcl.extract_all_ortho_value()
###########
# Custom processing pdal pipeline