Salome HOME
fitShapePointsToMesh: option to split free border shape
authorPaul RASCLE <paul.rascle@openfields.fr>
Mon, 20 Jul 2020 14:09:45 +0000 (16:09 +0200)
committerYOANN AUDOUIN <B61570@dsp0851742.postes.calibre.edf.fr>
Fri, 30 Oct 2020 16:06:22 +0000 (17:06 +0100)
src/HYDROTools/shapesGroups.py

index 1f3da15a05c379d76571f1e5f0959d3699bccd75..147a71e838d1fd4a5ca87cf9909b776a8d10c4c2 100644 (file)
@@ -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()