lazer.utils¶
General utility functions that are not specific to lidar processing workflows.
LazerParser (ArgumentParser)
¶
error(self, message)
¶
error(message: string)
Prints a usage message incorporating the message to stderr and exits.
If you override this in a subclass, it should not return -- it should either exit or raise an exception.
Source code in lazer/utils.py
def error(self, message):
self.print_help()
sys.stderr.write(f"\nerror: {message}\n")
sys.exit(2)
add_raster_metadata(file_path, custom_metadata)
¶
Adds custom metadata tags to a raster's header.
Source code in lazer/utils.py
def add_raster_metadata(file_path: str, custom_metadata: dict) -> None:
"""Adds custom metadata tags to a raster's header."""
ref = gdal.Open(file_path, gdal.GA_Update)
metadata = ref.GetMetadata_Dict()
for key, value in custom_metadata.items():
metadata[key] = value
ref.SetMetadata(metadata)
ref.FlushCache()
ref = None
input_files_to_list(input_files)
¶
Parses a list of input file arguments, which may include wildcards, into a single list.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_files |
list |
list of file paths passed at the command line. |
required |
Returns:
Type | Description |
---|---|
list |
a single list with each wildcard entry expanded. |
Source code in lazer/utils.py
def input_files_to_list(input_files: list) -> list:
"""Parses a list of input file arguments, which may include wildcards, into a single list.
Args:
input_files: list of file paths passed at the command line.
Returns:
a single list with each wildcard entry expanded.
"""
output_list = []
for input_arg in input_files:
search_result = glob(input_arg)
output_list += search_result
return output_list
input_files_to_lof(input_files, lof_path)
¶
Parses input file arguments and writes to a lastools 'list of files' file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_files |
list |
list of file paths passed at the command line. |
required |
lof_path |
str |
output text file with one file path per line. |
required |
Source code in lazer/utils.py
def input_files_to_lof(input_files: list, lof_path: str) -> None:
"""Parses input file arguments and writes to a lastools 'list of files' file.
Args:
input_files: list of file paths passed at the command line.
lof_path: output text file with one file path per line.
"""
list_to_lof(input_files_to_list(input_files), lof_path)
list_to_lof(file_list, lof_path)
¶
Writes a list of file paths to a lastools "list of files" file.
Source code in lazer/utils.py
def list_to_lof(file_list: list, lof_path: str) -> None:
"""Writes a list of file paths to a lastools "list of files" file."""
with open(lof_path, "w+") as f:
[f.write(f"{filename}\n") for filename in file_list]
read_file_list(file_path)
¶
Reads an ascii file returns a list containing each line's entry.
Source code in lazer/utils.py
def read_file_list(file_path: str) -> list:
"""Reads an ascii file returns a list containing each line's entry."""
with open(file_path, "r") as handler:
contents = handler.read()
return contents.strip().split("\n")
run_command_line(command)
¶
Run a shell command via subproces and assert no error.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
command |
str |
the shell command string to run. |
required |
Returns:
Type | Description |
---|---|
str |
stdout from completed process. |
Source code in lazer/utils.py
def run_command_line(command: str) -> str:
"""Run a shell command via subproces and assert no error.
Args:
command: the shell command string to run.
Returns:
stdout from completed process.
"""
_logger.debug("Running command: {}".format(command))
completed = subprocess.run(
shlex.split(command), stdout=subprocess.PIPE, stderr=subprocess.PIPE
)
if completed.stderr:
message = "\n\n".join(
[
command,
f"stdout: {completed.stdout.decode('utf-8')}",
f"stderr: {completed.stderr.decode('utf-8')}",
]
)
assert completed.returncode == 0, message
return completed.stdout.decode("utf-8")