Skip to content

Commit

Permalink
Remove dem_blender.py and fit merging logic on CLI. Convert to paths …
Browse files Browse the repository at this point in the history
…to pathlib API #9
  • Loading branch information
hcwinsemius committed Oct 16, 2024
1 parent d8ffa33 commit 5714995
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 61 deletions.
2 changes: 0 additions & 2 deletions dem_blender/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
__version__ = "0.1.0"

from .cli import *
from . import dem_blender

__all__ = [
"dem_blender",
"cli"
]
47 changes: 41 additions & 6 deletions dem_blender/cli/main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import click
import glob
from pathlib import Path
from dem_blender import dem_blender
from typing import Optional

from dem_blender import merge

def print_info(ctx, param, value):
if not value:
Expand All @@ -19,24 +22,56 @@ def print_license(ctx, param, value):
path_opt = click.option(
"-p",
"--path",
type=click.Path(exists=True, resolve_path=True, dir_okay=True, file_okay=False),
type=click.Path(exists=True, resolve_path=True, dir_okay=True, file_okay=False, path_type=Path),
help="Path to directory containing terrain models",
# callback=validate_files_in_dir, # TODO: write a validator that confirms that all files in folder are valid geospatial raster files
required=True
)

nodata_val_opt = click.option(
"-n",
"--nodata",
type=click.INT,
help="Missing value (default: derived from DEM layers)",
required=False
)
overwrite_opt = click.option(
"-o",
"--overwrite",
is_flag=True,
default=False,
help="Force overwriting result if existing"
)
@click.command()
@click.argument(
'OUTPUT',
type=click.Path(resolve_path=True, dir_okay=False, file_okay=True),
type=click.Path(resolve_path=True, dir_okay=False, file_okay=True, path_type=Path),
required=True
)
@path_opt
def merge(
@nodata_val_opt
@overwrite_opt
def cli(
output: Path,
path: Path,
nodata: Optional[int] = None,
overwrite: Optional[bool] = False
):
"""Single CLI command for blending terrain models."""
print(f"Input path: {path}")
print(f"Output path file: {output}")
raise NotImplementedError
print(f"Missing: {nodata}")
if output.exists():
if overwrite:
# remove content
output.unlink()
else:
raise IOError(f"Output path {output} exists. Use -o / --overwrite to force overwriting of file.")
input_dems = list(path.glob("*.tif"))
if len(input_dems) == 0:
print(f"No files with extension .tif found in {path}")
merge.euclidean_merge_dems(
output_dem=output,
input_dems=input_dems,
nodata=nodata,
)

33 changes: 0 additions & 33 deletions dem_blender/dem_blender.py

This file was deleted.

9 changes: 4 additions & 5 deletions dem_blender/euclid.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
import rasterio
import log

from dem_blender import log

try:
# GDAL >= 3.3
Expand All @@ -12,12 +13,10 @@
except:
pass

def compute_euclidean_map(geotiff_path, output_path, overwrite=False):
def compute_euclidean_map(geotiff_path, output_path, overwrite=False, nodata=None):
if not os.path.exists(geotiff_path):
log.ODM_WARNING("Cannot compute euclidean map (file does not exist: %s)" % geotiff_path)
return

nodata = -9999
with rasterio.open(geotiff_path) as f:
nodata = f.nodatavals[0]

Expand All @@ -30,7 +29,7 @@ def compute_euclidean_map(geotiff_path, output_path, overwrite=False):
#if gdal_proximity is not None:
try:
gdal_proximity(['gdal_proximity.py',
geotiff_path, output_path, '-values', str(nodata),
str(geotiff_path), str(output_path), '-values', str(nodata),
'-co', 'TILED=YES',
'-co', 'BIGTIFF=IF_SAFER',
'-co', 'COMPRESS=DEFLATE',
Expand Down
1 change: 0 additions & 1 deletion dem_blender/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import dateutil.parser
import shutil
import multiprocessing
from arghelpers import args_to_dict

if sys.platform == 'win32' or os.getenv('no_ansiesc'):
# No colors on Windows (sorry !) or existing no_ansiesc env variable
Expand Down
45 changes: 32 additions & 13 deletions dem_blender/merge.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import math
import numpy as np
from scipy import ndimage
import os
import rasterio

from pathlib import Path
from rasterio.transform import Affine, rowcol
import euclid
import log
import os
from scipy import ndimage
from typing import List

from dem_blender import euclid
from dem_blender import log

def file_exists(path_file):
return os.path.isfile(path_file)
Expand All @@ -21,21 +24,28 @@ def related_file_path(input_file_path, prefix="", postfix="", replace_base=None)
For example: related_file_path("/path/to/file.ext", "a.", ".b")
--> "/path/to/a.file.b.ext"
"""
path, filename = os.path.split(input_file_path)

path = input_file_path.parent
filename = input_file_path.name
# path = path/to
# filename = file.ext

basename, ext = os.path.splitext(filename)
basename = input_file_path.stem
ext = input_file_path.suffix
# basename = file
# ext = .ext

if replace_base is not None:
basename = replace_base

return os.path.join(path, "{}{}{}{}".format(prefix, basename, postfix, ext))
return path / "{}{}{}{}".format(prefix, basename, postfix, ext)

def euclidean_merge_dems(input_dems, output_dem, creation_options={}, euclidean_map_source=None):
def euclidean_merge_dems(
output_dem: Path,
input_dems: List[Path] = [],
nodata=None,
creation_options={},
euclidean_map_source=None
):
"""
Based on https://github.com/mapbox/rio-merge-rgba
and ideas from Anna Petrasova
Expand All @@ -46,11 +56,12 @@ def euclidean_merge_dems(input_dems, output_dem, creation_options={}, euclidean_
by a weighted average based on such euclidean distance.
"""
inputs = []
tmp_files = []
bounds=None

existing_dems = []
for dem in input_dems:
if not file_exists(dem):
if not dem.exists():
log.ODM_WARNING("%s does not exist. Will skip from merged DEM." % dem)
continue
existing_dems.append(dem)
Expand All @@ -66,9 +77,15 @@ def euclidean_merge_dems(input_dems, output_dem, creation_options={}, euclidean_
profile = first.profile

for dem in existing_dems:
eumap = euclid.compute_euclidean_map(dem, related_file_path(dem, postfix=".euclideand", replace_base=euclidean_map_source), overwrite=False)
if eumap and file_exists(eumap):
eumap = euclid.compute_euclidean_map(
dem,
related_file_path(dem, postfix=".euclideand", replace_base=euclidean_map_source),
nodata=nodata,
overwrite=False
)
if eumap and os.path.isfile(eumap):
inputs.append((dem, eumap))
tmp_files.append(eumap)

log.ODM_INFO("%s valid DEM rasters to merge" % len(inputs))

Expand Down Expand Up @@ -196,5 +213,7 @@ def euclidean_merge_dems(input_dems, output_dem, creation_options={}, euclidean_
dstarr[dstarr == 0.0] = src_nodata

dstrast.write(dstarr, window=dst_window)

# cleanup
for file in tmp_files:
file.unlink()
return output_dem
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ test = [
full = ["dem_blender[extra,test]"]

[project.scripts]
dem_blender = "dem_blender.cli.main:merge"
dem_blender = "dem_blender.cli.main:cli"

[project.urls]
Source = "https://github.com/localdevices/dem_blender"
Expand Down

0 comments on commit 5714995

Please sign in to comment.