Polygons¶
This case study reads information from the gstlearn repository. It is meant to show the possibilities on Polygons.
import gstlearn as gl
import gstlearn.plot as gp
import gstlearn.document as gdoc
import numpy as np
gdoc.setNoScroll()
We read the contents of the Polygon definition from a CSV file.
filename = gdoc.loadData("Alluvial", "Seine_Alluvial.csv")
csv = gl.CSVformat.createStandard(gl.ECSV.FRENCH, flagHeader=False, naString="9999")
poly = gl.Polygons.createFromCSV(filename, csv, False)
gp.polygon(poly)
gp.decoration(title="Polygon")
The current polygon contains a large number of vertices.
poly.display(gl.AStringFormat(2))
Polygons -------- Number of Polygon Sets = 1 PolyElem #1 ........... Number of Vertices = 30301
We define a rotated grid which contains the whole alluvial plain.
grid = gl.DbGrid()
err = grid.reset([4000, 3500], [100, 100], [480000, 6650000])
gp.plot(grid, alpha=0.3)
gp.polygon(poly)
gp.decoration(title="Polygon in its alluvial plain")
Reduce the complexity of the Polygon
newpoly = poly.reduceComplexity(3000)
newpoly.display(gl.AStringFormat(2))
Polygons -------- Number of Polygon Sets = 1 PolyElem #1 ........... Number of Vertices = 1123
gp.plot(grid, alpha=0.3)
gp.polygon(newpoly)
gp.polygon(poly, edgecolor="red")
gp.decoration(title="(Simplified) Polygon in its alluvial plain")
Use the simplified polygon to perform a coarse selection. At the same time, dilate this selected area in order to recover the initial space as much as possible.
err = gl.db_polygon(
grid, newpoly, namconv=gl.NamingConvention("Coarse", True, True, True, gl.ELoc.SEL)
)
print(f"Number of active grid nodes = {grid.getNSample(True)}")
Number of active grid nodes = 315868
grid.setLocator("Coarse", gl.ELoc.Z)
err = grid.morpho(gl.EMorpho.DILATION, option=1, radius=[30, 30])
grid.setLocator("Morpho*", gl.ELoc.SEL)
print(f"Number of active grid nodes = {grid.getNSample(True)}")
Number of active grid nodes = 1593571
gp.plot(grid, alpha=0.3)
gp.polygon(newpoly)
gp.polygon(poly, edgecolor="red")
gp.decoration(title="(Simplified) Polygon in its dilated selection")
New interface allowing to create a Grid directly from a polygon
grid2 = gl.DbGrid.createFromPolygon(newpoly, dx=[1000, 1000])
gp.plot(grid2, alpha=0.3)
gp.polygon(newpoly, edgecolor="red")
gp.decoration(title="Polygon and the newly created Grid")
Shortcuts for defining a simple Polygon¶
From a set of coordinates defined as two vectors of coordinates. Note that in the foolowing example, no attention is carried on the shape of the generated polygon: it will certainly not be convex for example!
ndat = 10
x = np.random.randn(ndat)
y = np.random.randn(ndat)
poly = gl.Polygons.createFromVD(x, y)
gp.polygon(poly, edgecolor="blue")
gp.decoration(title="Polygon generated from vectors of coordinates")
In the next case, the simple polygon is generated from a set of vertices spread around the figure centered. The distance between the center and each vertex can be constant (we then obtain a circle) or randomized.
ndat = 50
poly = gl.Polygons.createFillRandom(50, 0)
gp.polygon(poly, edgecolor="blue")
gp.decoration(title="Circular Polygon")
ndat = 50
poly = gl.Polygons.createFillRandom(50, 0.2)
gp.polygon(poly, edgecolor="blue")
gp.decoration(title="Polygon generated as an almost circle")