Skip to content

Commit e895c31

Browse files
awirbfso42
authored andcommitted
add check for aimec new coordinate system
remove unused imports add pytest remove unused return variable
1 parent 1c121c0 commit e895c31

File tree

3 files changed

+95
-2
lines changed

3 files changed

+95
-2
lines changed

avaframe/ana3AIMEC/aimecTools.py

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
import numpy as np
88
import pandas as pd
99
import copy
10-
10+
import shapely as shp
11+
import itertools as iterT
1112

1213
# Local imports
1314
import avaframe.in2Trans.shpConversion as shpConv
@@ -21,7 +22,6 @@
2122
import avaframe.out3Plot.outAIMEC as outAimec
2223
import avaframe.out3Plot.plotUtils as pU
2324

24-
2525
# create local logger
2626
log = logging.getLogger(__name__)
2727

@@ -522,6 +522,9 @@ def makeDomainTransfo(pathDict, dem, refCellSize, cfgSetup):
522522
# get z coordinate of the center line
523523
rasterTransfo, _ = geoTrans.projectOnRaster(dem, rasterTransfo)
524524

525+
# check if any coordinate lines of new coordinate system are overlapping
526+
_ = checkOverlapDBXY(rasterTransfo)
527+
525528
# add surface parallel coordinate (sParallel)
526529
rasterTransfo = addSurfaceParalleCoord(rasterTransfo)
527530

@@ -1837,3 +1840,41 @@ def analyzeDiffsRunoutLines(cfgSetup, runoutLine, refDataTransformed, resAnalysi
18371840
log.info('For reference data type %s, runout line comparison is not available' % refLine['type'])
18381841

18391842
return resAnalysisDF
1843+
1844+
1845+
def checkOverlapDBXY(rasterTransfo):
1846+
"""check if x, y coordinates of new coordinate system overlap
1847+
1848+
Parameters
1849+
-----------
1850+
rasterTransfo: dict
1851+
dict with gridx, gridy locations of new coordinates in old coordinate system
1852+
1853+
"""
1854+
1855+
x = rasterTransfo["gridx"]
1856+
y = rasterTransfo["gridy"]
1857+
1858+
# create lineStrings from coordinate points
1859+
multiLine = []
1860+
for i in range(x.shape[1]):
1861+
lineArray = np.zeros((x.shape[0], 2))
1862+
lineArray[:, 0] = x[:, i]
1863+
lineArray[:, 1] = y[:, i]
1864+
multiLine.append(shp.LineString(lineArray))
1865+
1866+
# check for intersections of the individual lines in the multiline
1867+
for line1, line2 in iterT.combinations(multiLine, 2):
1868+
if line1.crosses(line2):
1869+
intersectionPoints = line1.intersection(line2)
1870+
if isinstance(intersectionPoints, shp.Point) or isinstance(intersectionPoints, shp.MultiPoint):
1871+
message = (
1872+
"New coordinate system has overlaps - first intersection at %s. "
1873+
"The provided path has too much curvature. "
1874+
"Try: (1) smoothing the path, (2) reducing the domain width, or (3) using fewer points."
1875+
% intersectionPoints
1876+
)
1877+
log.error(message)
1878+
raise AssertionError(message)
1879+
1880+
return True

avaframe/in3Utils/geoTrans.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1128,6 +1128,9 @@ def path2domain(xyPath, rasterTransfo):
11281128
DBYl = np.array((y + w * np.sin(d)))
11291129
DBYr = np.array((y + w * np.sin(d + math.pi)))
11301130

1131+
# check if left or right domain boundary line is selfintersecting
1132+
checkDBOverlap(DBXl, DBXr, DBYl, DBYr)
1133+
11311134
rasterTransfo["DBXl"] = DBXl
11321135
rasterTransfo["DBXr"] = DBXr
11331136
rasterTransfo["DBYl"] = DBYl
@@ -2094,3 +2097,31 @@ def cellsAffectedLine(header, pointsXY, typePoints):
20942097
mask[ind, ind2] = 1.0
20952098

20962099
return mask, xx, yy
2100+
2101+
2102+
def checkDBOverlap(DBXl, DBXr, DBYl, DBYr):
2103+
"""check if lines spanned by DBX and DBY do intersect themselves; if selfintersecting line error
2104+
2105+
Parameters
2106+
-----------
2107+
DBXl, DBXr, DBYl, DBYr: numpy nd array
2108+
coordinates of lines
2109+
2110+
"""
2111+
2112+
# create left and right domain boundar lineString
2113+
DBr = np.zeros((len(DBXr), 2))
2114+
DBr[:, 0] = DBXr
2115+
DBr[:, 1] = DBYr
2116+
DBrLine = shp.LineString(DBr)
2117+
2118+
DBl = np.zeros((len(DBXl), 2))
2119+
DBl[:, 0] = DBXl
2120+
DBl[:, 1] = DBYl
2121+
DBlLine = shp.LineString(DBl)
2122+
2123+
# check if either of the left or right domain boundary lineString is selfintersecting
2124+
if not DBrLine.is_simple or not DBlLine.is_simple:
2125+
message = "Domain transformation not applicable for given line - curvature of provided line would lead to folding"
2126+
log.error(message)
2127+
raise AssertionError(message)

avaframe/tests/test_aimecTools.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,3 +314,24 @@ def test_computeRunoutLine(tmp_path):
314314
assert ("sRunout" in runoutLine.keys()) is False
315315
assert ("lRunout" in runoutLine.keys()) is False
316316
assert ("xRunout" in runoutLine.keys()) is False
317+
318+
319+
def test_checkOverlapDBXY():
320+
"""check if lines along coordinate grid do intersect"""
321+
322+
# setup required input
323+
x1 = np.arange(0, 10, 1)
324+
y1 = np.arange(2, 10, 1)
325+
X, Y = np.meshgrid(x1, y1)
326+
rasterTransfo = {"gridx": X, "gridy": Y}
327+
328+
flagOverlap = aT.checkOverlapDBXY(rasterTransfo)
329+
330+
assert flagOverlap is True
331+
332+
X[:, 0] = np.arange(0, 8, 1)
333+
rasterTransfo = {"gridx": X, "gridy": Y}
334+
335+
with pytest.raises(AssertionError) as e:
336+
assert aT.checkOverlapDBXY(rasterTransfo)
337+
assert "New coordinate system has overlaps - first intersection at POINT (1 3)." in str(e.value)

0 commit comments

Comments
 (0)