Skip to content

Commit

Permalink
49 rastertools meilleure gestion des exceptions et problemes de confi…
Browse files Browse the repository at this point in the history
…guration du radius (#1)

* commit last branch changes from gitlab repo

* add with_max_radius argument to define a max radius value

* another way to include max radius

* forgotten fix

* Replace self.radius as self.max_radius + gitignore

* change self.max_radius to self.radius and radius to optimal_radius

* minor fix + use of self.radius as a paramter for better understanding
  • Loading branch information
erblanm authored May 30, 2024
1 parent fd7a552 commit 3003ed1
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 23 deletions.
24 changes: 24 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Python #
*.py[cod]
*$py.class

# Distribution / packaging
.Python build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
*.whl
*.egg-info/
.installed.cfg
*.egg
*.manifest
*.spec

2 changes: 1 addition & 1 deletion docs/cli/hillshade.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ computes the shadows of the ground surface (buildings, trees, etc.).
$ rastertools hillshade --help
usage: rastertools hillshade [-h] --elevation ELEVATION --azimuth AZIMUTH
[--radius RADIUS] --resolution RESOLUTION
[--radius RADIUS] --resolution RESOLUTION
[-o OUTPUT] [-ws WINDOW_SIZE]
[-p {none,edge,maximum,mean,median,minimum,reflect,symmetric,wrap}]
inputs [inputs ...]
Expand Down
2 changes: 1 addition & 1 deletion src/eolab/rastertools/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,4 @@ def with_bands_arguments(parser):
'--all',
dest="all_bands",
action="store_true",
help="Compute all bands")
help="Compute all bands")
46 changes: 25 additions & 21 deletions src/eolab/rastertools/hillshade.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,27 +105,31 @@ def process_file(self, inputfile: str) -> List[str]:
outdir = Path(self.outputdir)
output_image = outdir.joinpath(f"{utils.get_basename(inputfile)}-hillshade.tif")

if self.radius is None:
# compute the radius from data range
# radius represents the max distance of buildings that can create a hillshade
# considering the sun elevation.
wmax = None
wmin = None
with rasterio.open(inputfile) as src:
if src.count != 1:
raise ValueError("Invalid input file, it must contain a single band.")
for ji, window in src.block_windows(1):
data = src.read(1, masked=True, window=window)
wmax = max(wmax, float(data.max())) if wmax is not None else float(data.max())
wmin = min(wmin, float(data.min())) if wmin is not None else float(data.min())

delta = int((wmax - wmin) / self.resolution)
radius = int(delta / np.tan(np.radians(self.elevation)))
# compute the radius from data range
# radius represents the max distance of buildings that can create a hillshade
# considering the sun elevation.
wmax = None
wmin = None
with rasterio.open(inputfile) as src:
if src.count != 1:
raise ValueError("Invalid input file, it must contain a single band.")
for ji, window in src.block_windows(1):
data = src.read(1, masked=True, window=window)
wmax = max(wmax, float(data.max())) if wmax is not None else float(data.max())
wmin = min(wmin, float(data.min())) if wmin is not None else float(data.min())

delta = int((wmax - wmin) / self.resolution)
optimal_radius = int(delta / np.tan(np.radians(self.elevation)))

if self.radius is None or optimal_radius <= self.radius:
self.radius = optimal_radius
else:
radius = self.radius
_logger.warning(f"The optimal radius value is {optimal_radius} exceeding {self.radius} threshold. "
f"Oversized radius affects computation time and so radius is set to {self.radius}. "
"Result may miss some shadow pixels.")

if radius >= min(self.window_size) / 2:
raise ValueError(f"The radius (option --radius, value={radius}) must be strictly "
if self.radius >= min(self.window_size) / 2:
raise ValueError(f"The radius (option --radius, value={self.radius}) must be strictly "
"less than half the size of the window (option --window_size, "
f"value={min(self.window_size)})")

Expand All @@ -143,15 +147,15 @@ def process_file(self, inputfile: str) -> List[str]:
"elevation": self.elevation,
"azimuth": self.azimuth,
"resolution": self.resolution,
"radius": radius
"radius": self.radius
}
hillshade.configure(hillshade_conf)

# Run the hillshade processing
compute_sliding(
inputfile, output_image, hillshade,
window_size=self.window_size,
window_overlap=radius,
window_overlap=self.radius,
pad_mode=self.pad_mode)

return [output_image.as_posix()]

0 comments on commit 3003ed1

Please sign in to comment.