Skip to content

Commit

Permalink
More robust output process
Browse files Browse the repository at this point in the history
Co-Authored-By: Andrea Giudiceandrea <16253859+agiudiceandrea@users.noreply.github.com>
  • Loading branch information
2 people authored and nyalldawson committed Dec 20, 2022
1 parent d851e7e commit 49244a8
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions python/plugins/grassprovider/ext/r_horizon.py
Expand Up @@ -22,6 +22,7 @@
__copyright__ = '(C) 2017, Médéric Ribreux'

import os
import math


def checkParameterValuesBeforeExecuting(alg, parameters, context):
Expand Down Expand Up @@ -50,26 +51,30 @@ def doubleToBaseName(number, nDecimals):
Format filename, according to GRASS implementation,
based on provided number and number of decimals
"""
number += 0.0001
if nDecimals == 0:
return f'{int(number):03}'
return f'{int(number):03}_{str(number).split(".")[-1].ljust(nDecimals, "0")}'
int_part = int(number)
dec_part = int((number - int_part) * pow(10, nDecimals))
return f'{int_part:03}_{str(dec_part).rjust(nDecimals, "0")}'

# There will be as many outputs as the difference between start and end divided by steps
start = alg.parameterAsDouble(parameters, 'start', context)
end = alg.parameterAsDouble(parameters, 'end', context)
step = alg.parameterAsDouble(parameters, 'step', context)
direction = alg.parameterAsDouble(parameters, 'direction', context)

num = start + direction
first_rad = math.radians(start + direction)
nDecimals = getNumberDecimals(step)
dfr_rad = math.radians(step)
arrayNumInt = int((end - start) / abs(step))

directory = alg.parameterAsString(parameters, 'output', context)
# Needed if output to a temporary directory
os.makedirs(directory, exist_ok=True)
while num < end + direction:
baseName = doubleToBaseName(num, nDecimals)
for k in range(arrayNumInt):
angle_deg = math.degrees(first_rad + dfr_rad * k)
baseName = doubleToBaseName(angle_deg, nDecimals)
grassName = f'output{alg.uniqueSuffix}_{baseName}'
fileName = f'{os.path.join(directory, baseName)}.tif'
alg.exportRasterLayer(grassName, fileName)
# Weird issue was generating weird num like 0.12000000000000001 or 0.27999999999999997 for step = 0.2
num = round(num + step, nDecimals)

0 comments on commit 49244a8

Please sign in to comment.