Skip to content

Commit

Permalink
feat: update cli for coregistration
Browse files Browse the repository at this point in the history
  • Loading branch information
vschaffn committed Nov 12, 2024
1 parent 51d023c commit 8b0c053
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 21 deletions.
46 changes: 43 additions & 3 deletions xdem/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
import logging
import os

import geoutils

from xdem import ( # noqa
coreg,
dem,
Expand All @@ -8,6 +13,7 @@
terrain,
volume,
)
from xdem.coreg.workflows import dem_coregistration
from xdem.ddem import dDEM # noqa
from xdem.dem import DEM # noqa
from xdem.demcollection import DEMCollection # noqa
Expand All @@ -24,8 +30,42 @@
)


def run(reference_dem: str, dem_to_be_aligned: str, verbose: str) -> None:
def coregister(ref_dem_path: str, tba_dem_path: str) -> None:
"""
Function to compare DEMs
Function to compare and coregister Digital Elevation Models (DEMs).
This function verifies the existence of the provided DEM paths,
loads the reference DEM and the DEM to be aligned, and performs
coregistration. The aligned DEM and an inlier mask are then saved
to disk.
:param ref_dem_path: Path to the reference DEM file.
:param tba_dem_path: Path to the DEM that needs to be aligned to the reference.
:return:
:raises FileNotFoundError: if the reference DEM or the DEM to be aligned does not exist.
"""
print("hello world")
# Verify that both DEM paths exist
if not os.path.exists(ref_dem_path):
raise FileNotFoundError(f"Reference DEM path does not exist: {ref_dem_path}")
if not os.path.exists(tba_dem_path):
raise FileNotFoundError(f"DEM to be aligned path does not exist: {tba_dem_path}")

logging.info("Loading DEMs: %s, %s", ref_dem_path, tba_dem_path)

# Load the reference and secondary DEMs
reference_dem, to_be_aligned_dem = geoutils.raster.load_multiple_rasters([ref_dem_path, tba_dem_path])

# Execute coregistration
logging.info("Starting coregistration...")
coreg_dem, coreg_method, out_stats, inlier_mask = dem_coregistration(
to_be_aligned_dem, reference_dem, "aligned_dem.tiff"
)

# Save outputs
logging.info("Saving aligned DEM and inlier mask...")
inlier_rst = coreg_dem.copy(new_array=inlier_mask)
inlier_rst.save("inlier_mask.tiff")

# Print the coregistration details
print("Coregistration statistics:\n", out_stats)
logging.info("Coregistration completed")
42 changes: 24 additions & 18 deletions xdem/xdem_cli.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import argparse
import logging
from argparse import ArgumentParser

import argcomplete
Expand All @@ -12,20 +13,7 @@ def get_parser() -> ArgumentParser:
:return: parser
"""
parser = argparse.ArgumentParser(
description="Compare Digital Elevation Models",
fromfile_prefix_chars="@",
)

parser.add_argument(
"reference_dem",
help="path to a reference dem",
)

parser.add_argument(
"dem_to_be_aligned",
help="path to a second dem",
)
parser = argparse.ArgumentParser(prog="xdem", description="xDEM command-line interface")

parser.add_argument(
"--loglevel",
Expand All @@ -41,6 +29,13 @@ def get_parser() -> ArgumentParser:
version=f"%(prog)s {xdem.__version__}",
)

subparsers = parser.add_subparsers(title="Subcommands", dest="command")

# Subcommand for coregistration
coregister_parser = subparsers.add_parser("coregister", help="Coregister two DEMs")
coregister_parser.add_argument("reference_dem", help="path to a reference dem")
coregister_parser.add_argument("dem_to_be_aligned", help="path to a second dem")

return parser


Expand All @@ -51,10 +46,21 @@ def main() -> None:
parser = get_parser()
argcomplete.autocomplete(parser)
args = parser.parse_args()
try:
xdem.run(args.reference_dem, args.dem_to_be_aligned, args.loglevel)
except Exception as e:
print(f"Error: {e}")

# Show help if no subcommand is provided
if not args.command:
parser.print_help()
return

# Set the logging configuration
logging.basicConfig(level=args.loglevel)

# Handle coregister subcommand
if args.command == "coregister":
try:
xdem.coregister(args.reference_dem, args.dem_to_be_aligned)
except Exception as e:
print(f"Error: {e}")


if __name__ == "__main__":
Expand Down

0 comments on commit 8b0c053

Please sign in to comment.