From 589efdf69bfd08137574d25e01d94fcdc7a5d353 Mon Sep 17 00:00:00 2001 From: Paul RASCLE Date: Mon, 20 Jul 2020 16:09:45 +0200 Subject: [PATCH] fitShapePointsToMesh: option to split free border shape --- src/HYDROTools/shapesGroups.py | 58 ++++++++++++++++++++-------------- 1 file changed, 34 insertions(+), 24 deletions(-) diff --git a/src/HYDROTools/shapesGroups.py b/src/HYDROTools/shapesGroups.py index 1f3da15a..147a71e8 100644 --- a/src/HYDROTools/shapesGroups.py +++ b/src/HYDROTools/shapesGroups.py @@ -222,10 +222,19 @@ def exploreEdgeGroups(meshFile, outputDirectory="", offsetX=0., offsetY=0.): writeShapePoints(mcMesh, fullGrpName, nodeChains, outputDirectory, offsetX, offsetY) -def fitShapePointsToMesh(freeBorderShapefile, shapefileToAdjust): +def fitShapePointsToMesh(freeBorderShapefile, shapefileToAdjust, splitFreeBorder=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. + 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 + """ print(" === fitShapePointsToMesh", freeBorderShapefile, shapefileToAdjust) # --- find domain freeBorder: bounding box englobing all others + # TODO: This may not be always the case, when there is not a single domain with holes, but several non connected domains. fb = shapefile.Reader(freeBorderShapefile) fbShapes = fb.shapes() @@ -236,7 +245,7 @@ def fitShapePointsToMesh(freeBorderShapefile, shapefileToAdjust): if (bb[0] < maxbbox[0] and bb[1] < maxbbox[1] and bb[2] > maxbbox[2] and bb[3] > maxbbox[3]): maxbbox = bb outerBboxIndex = i - fbs = fbShapes[outerBboxIndex] + fbs = fbShapes[outerBboxIndex] # the domain free border shape # --- find the intersections of the shapefile to adjust and the domain free border: # the closests points (two pairs of points) @@ -296,31 +305,32 @@ def fitShapePointsToMesh(freeBorderShapefile, shapefileToAdjust): w.record(chainName + '_1') w.close() - # write the free border splited in two polylines (cut by the adjusted shapefile) + if splitFreeBorder: + # write the free border split in two polylines (cut by the adjusted shapefile) - a = os.path.splitext(freeBorderShapefile) - freeBorderSplit = a[0] + '_split' + a[1] - chainName = os.path.basename(a[0]) + a = os.path.splitext(freeBorderShapefile) + freeBorderSplit = a[0] + '_split' + a[1] + chainName = os.path.basename(a[0]) - w = shapefile.Writer(freeBorderSplit) - w.shapeType = 3 - w.field('name', 'C') + w = shapefile.Writer(freeBorderSplit) + w.shapeType = 3 + w.field('name', 'C') - if (ifb1 > ifb2): - i = ifb1; ifb1 = ifb2; ifb2 = i + if (ifb1 > ifb2): + i = ifb1; ifb1 = ifb2; ifb2 = i - chaincoords = [] - for i in range(ifb1, ifb2+1): - chaincoords.append(fbs.points[i]) - w.line([chaincoords]) - w.record(chainName + '_0') + chaincoords = [] + for i in range(ifb1, ifb2+1): + chaincoords.append(fbs.points[i]) + w.line([chaincoords]) + w.record(chainName + '_0') - chaincoords = [] - for i in range(ifb2, len(fbs.points)): - chaincoords.append(fbs.points[i]) - for i in range(ifb1+1): - chaincoords.append(fbs.points[i]) - w.line([chaincoords]) - w.record(chainName + '_1') - w.close() + chaincoords = [] + for i in range(ifb2, len(fbs.points)): + chaincoords.append(fbs.points[i]) + for i in range(ifb1+1): + chaincoords.append(fbs.points[i]) + w.line([chaincoords]) + w.record(chainName + '_1') + w.close() -- 2.39.2