Salome HOME
fitShapePointsToMesh improvements: output directory, superposed extremity points
authorPaul RASCLE <paul.rascle@openfields.fr>
Thu, 27 Aug 2020 07:30:21 +0000 (09:30 +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 298b29a04639bd87580c14142c58d7488547689a..8bcdd4d035ec35e1dd04c55f9c4ec38ff5cda7d2 100644 (file)
@@ -223,13 +223,14 @@ def exploreEdgeGroups(meshFile, outputDirectory="", offsetX=0., offsetY=0.):
         writeShapePoints(mcMesh, fullGrpName, nodeChains, outputDirectory, offsetX, offsetY)
 
 
-def fitShapePointsToMesh(freeBorderShapefile, shapefileToAdjust, splitFreeBorder=True, splitShapeAdjusted=True):
+def fitShapePointsToMesh(freeBorderShapefile, shapefileToAdjust, outputDirectory="", 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. 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.
+    outputDirectory: if empty, write the resulting shapefiles in their respective directory, with the suffix '_adj', otherwise write in the outputDirectory.
     splitFreeBorder: boolean default True
     splitShapeAdjusted: boolean default True
     """
@@ -258,12 +259,23 @@ def fitShapePointsToMesh(freeBorderShapefile, shapefileToAdjust, splitFreeBorder
     pdist =[]
     x0 = -1.e30
     y0 = -1.e30
+    discountLastSftaPoint = 0
     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]
+        if i == 0:
+            x00 = p[0] # keep first point
+            y00 = p[1]
+        d = math.sqrt((x0-p[0])*(x0-p[0]) + (y0-p[1])*(y0-p[1])) # distance to previous point
+        x0 = p[0] # keep previous point
         y0 = p[1]
         if d < 1.e-5:
-            continue # do not take into account superposed points in shape to adjust
+            print("two consecutives points of shapefile To adjust are superposed, OK")
+            continue # do not take into account consecutive superposed points in shape to adjust
+        if i == len(sfta.points) -1:
+            d = math.sqrt((x00-p[0])*(x00-p[0]) + (y00-p[1])*(y00-p[1])) # distance between first and last point
+            if d < 1.e-5:
+                discountLastSftaPoint = 1
+                print("last point of shapefile To adjust is superposed to first point, last point discarded, OK")
+                continue # do not take into account last point if superposed to first point, in shape to adjust 
         dmin = 1.e30
         jmin = -1
         for j,pfb in enumerate(fbs.points):
@@ -288,9 +300,11 @@ def fitShapePointsToMesh(freeBorderShapefile, shapefileToAdjust, splitFreeBorder
     # --- write the adusted shapefile: free border closest points replaced with corresponding points
     #     on the free border. two polylines, one inside the domain, one outside
 
-    a = os.path.splitext(shapefileToAdjust)
-    shapefileAdjusted = a[0] + '_adj' + a[1]
-    chainName = os.path.basename(a[0])
+    if outputDirectory == "":
+        outputDirectory = os.path.dirname(shapefileToAdjust)
+    a = os.path.splitext(os.path.basename(shapefileToAdjust))
+    shapefileAdjusted = os.path.join(outputDirectory, a[0] + '_adj' + a[1])
+    chainName = a[0]
 
     w = shapefile.Writer(shapefileAdjusted)
     w.shapeType = 3
@@ -308,7 +322,7 @@ def fitShapePointsToMesh(freeBorderShapefile, shapefileToAdjust, splitFreeBorder
         chaincoords = []
         chaincoords.append(fbs.points[ifb2])
         if i2+1 < len(sfta.points):
-            for i in range(i2+1, len(sfta.points)):
+            for i in range(i2+1, len(sfta.points) -discountLastSftaPoint):
                 chaincoords.append(sfta.points[i])
         for i in range(i1):
             chaincoords.append(sfta.points[i])
@@ -322,7 +336,7 @@ def fitShapePointsToMesh(freeBorderShapefile, shapefileToAdjust, splitFreeBorder
             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)):
+            for i in range(i2+1, len(sfta.points) -discountLastSftaPoint):
                 chaincoords.append(sfta.points[i])
         for i in range(i1):
             chaincoords.append(sfta.points[i])
@@ -334,9 +348,11 @@ def fitShapePointsToMesh(freeBorderShapefile, shapefileToAdjust, splitFreeBorder
     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])
+        if outputDirectory == "":
+            outputDirectory = os.path.dirname(freeBorderShapefile)
+        a = os.path.splitext(os.path.basename(freeBorderShapefile))
+        freeBorderSplit = os.path.join(outputDirectory, a[0] + '_split' + a[1])
+        chainName = a[0]
 
         w = shapefile.Writer(freeBorderSplit)
         w.shapeType = 3