From b0b7f75d4884a7504eb6ba24c0be0bcc476367cb Mon Sep 17 00:00:00 2001 From: Paul RASCLE Date: Sun, 26 Jul 2020 12:06:49 +0200 Subject: [PATCH] fitShapePointsToMesh: option to split shape to adjust, do not take into account superposed points --- src/HYDROTools/shapesGroups.py | 63 ++++++++++++++++++++++++---------- 1 file changed, 44 insertions(+), 19 deletions(-) diff --git a/src/HYDROTools/shapesGroups.py b/src/HYDROTools/shapesGroups.py index 147a71e8..18dedea5 100644 --- a/src/HYDROTools/shapesGroups.py +++ b/src/HYDROTools/shapesGroups.py @@ -222,14 +222,15 @@ def exploreEdgeGroups(meshFile, outputDirectory="", offsetX=0., offsetY=0.): writeShapePoints(mcMesh, fullGrpName, nodeChains, outputDirectory, offsetX, offsetY) -def fitShapePointsToMesh(freeBorderShapefile, shapefileToAdjust, splitFreeBorder=True): +def fitShapePointsToMesh(freeBorderShapefile, shapefileToAdjust, splitFreeBorder=True, splitShapeAdjusted=True): """ shapeFileToAdjust must be a closed line or polygon crossing freeBorderShapefile in 2 points. - Find in shapeFileToAdjust and in freeBorderShapefile the two closest corresponding points and move the points in shapeFileToAdjust to correspond to the points found in freeBorderShapefile. Split shapeFileToAdjust in two parts (inside or outside freeBorder). If requested, split freeBorderShapefile in two parts. + Find in shapeFileToAdjust and in freeBorderShapefile the two closest corresponding points and move the points in shapeFileToAdjust to correspond to the points found in freeBorderShapefile. Split shapeFileToAdjust in two parts (inside or outside freeBorder). If requested, split freeBorderShapefile in two parts. Same for shapeFileToAdjust. parameters: freeBorderShapefile: a set of free border lines, as generated by the functions freeBordersGroup and exploreEdgeGroups. shapefileToAdjust: a closed line or polygon, supposed to be drawn in qgis to pass as close as possible to the points to be connected, on the free border. splitFreeBorder: boolean default True + splitShapeAdjusted: boolean default True """ print(" === fitShapePointsToMesh", freeBorderShapefile, shapefileToAdjust) @@ -254,7 +255,14 @@ def fitShapePointsToMesh(freeBorderShapefile, shapefileToAdjust, splitFreeBorder shapes = sf.shapes() sfta = sf.shape(0) pdist =[] + x0 = -1.e30 + y0 = -1.e30 for i,p in enumerate(sfta.points): + d = math.sqrt((x0-p[0])*(x0-p[0]) + (y0-p[1])*(y0-p[1])) + x0 = p[0] + y0 = p[1] + if d < 1.e-5: + continue # do not take into account superposed points in shape to adjust dmin = 1.e30 jmin = -1 for j,pfb in enumerate(fbs.points): @@ -273,6 +281,8 @@ def fitShapePointsToMesh(freeBorderShapefile, shapefileToAdjust, splitFreeBorder else: ifb1 = pdist[1][1] ifb2 = pdist[0][1] + print("i1, i2, len(sfta.points)", i1, i2, len(sfta.points)) + print("ifb1, ifb2, len(fbs.points)", ifb1, ifb2, len(fbs.points)) # --- write the adusted shapefile: free border closest points replaced with corresponding points # on the free border. two polylines, one inside the domain, one outside @@ -285,24 +295,39 @@ def fitShapePointsToMesh(freeBorderShapefile, shapefileToAdjust, splitFreeBorder w.shapeType = 3 w.field('name', 'C') - chaincoords = [] - chaincoords.append(fbs.points[ifb1]) - for i in range(i1+1, i2): - chaincoords.append(sfta.points[i]) - chaincoords.append(fbs.points[ifb2]) - w.line([chaincoords]) - w.record(chainName + '_0') - - chaincoords = [] - chaincoords.append(fbs.points[ifb2]) - if i2+1 < len(sfta.points): - for i in range(i2+1, len(sfta.points)): + if splitShapeAdjusted: + chaincoords = [] + chaincoords.append(fbs.points[ifb1]) + for i in range(i1+1, i2): + chaincoords.append(sfta.points[i]) + chaincoords.append(fbs.points[ifb2]) + w.line([chaincoords]) + w.record(chainName + '_0') + + chaincoords = [] + chaincoords.append(fbs.points[ifb2]) + if i2+1 < len(sfta.points): + for i in range(i2+1, len(sfta.points)): + chaincoords.append(sfta.points[i]) + for i in range(i1): chaincoords.append(sfta.points[i]) - for i in range(i1): - chaincoords.append(sfta.points[i]) - chaincoords.append(fbs.points[ifb1]) - w.line([chaincoords]) - w.record(chainName + '_1') + chaincoords.append(fbs.points[ifb1]) + w.line([chaincoords]) + w.record(chainName + '_1') + else: + chaincoords = [] + chaincoords.append(fbs.points[ifb1]) + for i in range(i1+1, i2): + chaincoords.append(sfta.points[i]) + chaincoords.append(fbs.points[ifb2]) + if i2+1 < len(sfta.points): + for i in range(i2+1, len(sfta.points)): + chaincoords.append(sfta.points[i]) + for i in range(i1): + chaincoords.append(sfta.points[i]) + w.line([chaincoords]) + w.record(chainName) + w.close() if splitFreeBorder: -- 2.39.2