-
Notifications
You must be signed in to change notification settings - Fork 1
/
las_add_buffer.py
283 lines (238 loc) · 11 KB
/
las_add_buffer.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
import argparse
import logging
import os
import tempfile
from functools import wraps
from pathlib import Path
from typing import Callable, List
import pdal
from pdaltools.las_info import (
get_buffered_bounds_from_filename,
get_writer_parameters_from_reader_metadata,
)
from pdaltools.las_merge import create_list
from pdaltools.las_remove_dimensions import remove_dimensions_from_las
ORIGINAL_TILE_TAG = "is_in_original"
def create_las_with_buffer(
input_dir: str,
tile_filename: str,
output_filename: str,
buffer_width: int = 100,
spatial_ref: str = "EPSG:2154",
tile_width: int = 1000,
tile_coord_scale: int = 1000,
tag_original_tile: bool = False,
):
"""Merge lidar tiles around the queried tile and crop them in order to add a buffer
to the tile (usually 100m).
Args:
input_dir (str): directory of pointclouds (where you look for neighbors)
tile_filename (str): full path to the queried LIDAR tile
output_filename (str): full path to the saved cropped tile
buffer_width (int, optional): width of the border to add to the tile (in meters).
Defaults to 100.
spatial_ref (_type_, optional): Spatial reference to use to override the one from input las.
Defaults to "EPSG:2154".
tile_width (int, optional): width of tiles in meters. Defaults to 1000.
tile_coord_scale (int, optional): scale used in the filename to describe coordinates
in meters. Defaults to 1000.
tag_original_tile (bool, optional): if true, add a new "is_in_original" dimension
to the output las, equal to 1 on points that belong to the original tile, 0 on points
that belong to the added buffer. Defaults to False.
"""
bounds = get_buffered_bounds_from_filename(
tile_filename, buffer_width=buffer_width, tile_width=tile_width, tile_coord_scale=tile_coord_scale
)
logging.debug(f"Add buffer of size {buffer_width} to tile.")
las_merge_and_crop(
input_dir,
tile_filename,
bounds,
output_filename,
spatial_ref,
tile_width=tile_width,
tile_coord_scale=tile_coord_scale,
tag_original_tile=tag_original_tile,
)
def las_merge_and_crop(
input_dir: str,
tile_filename: str,
bounds: List,
output_filename: str,
spatial_ref: str = "EPSG:2154",
tile_width=1000,
tile_coord_scale=1000,
tag_original_tile: bool = False,
):
"""Merge and crop las in a single pipeline (for buffer addition)
For performance reasons, instead of using a pipeline that reads all files, merge them and
then crop to the desired bbox, what is done is:
- For each file:
- read it
- crop it according to the bounds
- optionally add a dimension to differentiate points from the central pointscloud
from those added as a buffer
- keep the crop in memory
- delete the pipeline object to release the memory taken by the las reader
- Merge the already cropped data
Args:
input_dir (str): directory of pointclouds (where you look for neighbors)
tile_filename (str): full path to the queried LIDAR tile
bounds (List): 2D bounding box to crop to : provided as ([xmin, xmax], [ymin, ymax])
output_filename (str): full path to the saved cropped tile
spatial_ref (str, optional): spatial reference for the writer. Defaults to "EPSG:2154".
tile_width (int, optional): width of tiles in meters (usually 1000m). Defaults to 1000.
tile_coord_scale (int, optional): scale used in the filename to describe coordinates in meters.
Defaults to 1000.
tag_original_tile (bool, optional): if true, add a new "is_in_original" dimension
to the output las, equal to 1 on points that belong to the original tile, 0 on points
that belong to the added buffer. Defaults to False.
Raises:
ValueError: if the list of tiles to merge is empty
"""
# List files to merge
files_to_merge = create_list(input_dir, tile_filename, tile_width, tile_coord_scale)
central_file = files_to_merge[-1]
if len(files_to_merge) > 0:
# Read and crop each file
crops = []
for f in files_to_merge:
pipeline = pdal.Pipeline()
pipeline |= pdal.Reader.las(filename=f, override_srs=spatial_ref)
if tag_original_tile:
pipeline |= pdal.Filter.ferry(dimensions=f"=>{ORIGINAL_TILE_TAG}")
pipeline |= pdal.Filter.assign(value=f"{ORIGINAL_TILE_TAG}={int(f == central_file)}")
pipeline |= pdal.Filter.crop(bounds=str(bounds))
pipeline.execute()
if len(pipeline.arrays[0]) == 0:
logging.warning(f"File {f} ignored in merge/crop: No points in crop bounding box")
else:
crops.append(pipeline.arrays[0])
if f == central_file:
# Retrieve metadata before the pipeline is deleted
metadata = pipeline.metadata
del pipeline
params = get_writer_parameters_from_reader_metadata(metadata, a_srs=spatial_ref)
# Merge
pipeline = pdal.Filter.merge().pipeline(*crops)
# Write
pipeline |= pdal.Writer(filename=output_filename, forward="all", **params)
logging.info(pipeline.toJSON())
pipeline.execute()
else:
raise ValueError("List of valid tiles is empty : stop processing")
pass
def remove_points_from_buffer(input_file: str, output_file: str):
"""Remove the points that were added as a buffer to a las file using the "is_in_original"
dimension that has been added by create_las_with_buffer
Limitation: if any point has been added to the point cloud after adding the buffer, it
won't be preserved by this operation (only points from the original file are kept)
Args:
input_file (str): path to the input file containing the "is_in_original" dimension
output_file (str): path to the output_file
"""
with tempfile.NamedTemporaryFile(suffix="_with_additional_dim.las") as tmp_las:
pipeline = pdal.Pipeline() | pdal.Reader.las(input_file)
pipeline |= pdal.Filter.range(limits=f"{ORIGINAL_TILE_TAG}[1:1]")
pipeline |= pdal.Writer.las(filename=tmp_las.name, forward="all", extra_dims="all")
pipeline.execute()
remove_dimensions_from_las(tmp_las.name, dimensions=[ORIGINAL_TILE_TAG], output_las=output_file)
def run_on_buffered_las(
buffer_width: int, spatial_ref: str, tile_width: int = 1000, tile_coord_scale: int = 1000
) -> Callable:
"""Decorator to apply a function that takes a las/laz as input and returns a las/laz output
on an input with an additional buffer, then remove the buffer points from the output
The first argument of the decorated function must be an input path
The second argument of the decorated function must be an output path
The buffer is added by merging lidar tiles around the queried tile and crop them based
on their filenames.
Limitation: if any point has been added to the point cloud by the decorated function, it
won't be preserved by this operation (only points from the original file are kept)
Args:
buffer_width (int): width of the border to add to the tile (in meters)
spatial_ref (str): spatial reference for the writer. Example: "EPSG:2154".
tile_width (int, optional): width of tiles in meters (usually 1000m). Defaults to 1000.
tile_coord_scale (int, optional): scale used in the filename to describe coordinates in meters.
Defaults to 1000.
Raises:
FileNotFoundError: when the first argument of the decorated function is not an existing
file
FileNotFoundError: when the second argument of the decorated function is not a path
with an existing parent folder
Returns:
Callable: decorated function
"""
"""Decorator to run a function that takes a las as input and returns a las output
on a las with an additional buffer, then remove the buffer points from the buffer points
"""
def decorator(func):
@wraps(func)
def wrapper(*args, **kwargs):
input_file = args[0]
output_file = args[1]
if not Path(input_file).is_file():
raise FileNotFoundError(
f"File {args[0]} not found. The first argument of a function decorated by "
"'run_on_buffered_las' is expected to be the path to an existing input file."
)
if not Path(output_file).parent.is_dir():
raise FileNotFoundError(
f"Parent folder for file {args[1]} not found. The second argument of a function "
"decorated by 'run_on_buffered_las' is expected to be the path to an output "
"file in an existing folder"
)
with (
tempfile.NamedTemporaryFile(suffix="_buffered_input.laz", dir=".") as buf_in,
tempfile.NamedTemporaryFile(suffix="_buffered_output.laz", dir=".") as buf_out,
):
create_las_with_buffer(
Path(input_file).parent,
input_file,
buf_in.name,
buffer_width=buffer_width,
spatial_ref=spatial_ref,
tile_width=tile_width,
tile_coord_scale=tile_coord_scale,
tag_original_tile=True,
)
func(buf_in.name, buf_out.name, *args[2:], **kwargs)
remove_points_from_buffer(buf_out.name, output_file)
return
return wrapper
return decorator
def parse_args():
parser = argparse.ArgumentParser("Add a buffer to a las tile by stitching with its neighbors")
parser.add_argument(
"--input_dir",
"-i",
type=str,
required=True,
help="Path to the the folder containing the tile to which you want to add buffer"
+ "as well as its neighbors tiles",
)
parser.add_argument(
"--tile_filename", "-f", type=str, required=True, help="Filename of the input tile (basename only)"
)
parser.add_argument("--output_dir", "-o", type=str, required=True, help="Directory folder for saving the outputs")
parser.add_argument(
"--buffer_width",
"-b",
default=100,
type=int,
help="Width (in meter) for the buffer that is added to the tile before interpolation "
+ "(to prevent artefacts)",
)
# Optional parameters
parser.add_argument(
"--spatial_reference", default="EPSG:2154", help="Spatial reference to use to override the one from input las."
)
return parser.parse_args()
if __name__ == "__main__":
args = parse_args()
create_las_with_buffer(
input_dir=args.input_dir,
tile_filename=os.path.join(args.input_dir, args.tile_filename),
output_filename=os.path.join(args.output_dir, args.tile_filename),
buffer_width=args.buffer_width,
spatial_ref=args.spatial_reference,
)