Polygons¶

This case study reads information from the gstlearn repository. It is meant to show the possibilities on Polygons.

In [1]:
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.

In [2]:
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)
In [3]:
gp.polygon(poly)
gp.decoration(title="Polygon")
No description has been provided for this image

The current polygon contains a large number of vertices.

In [4]:
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.

In [5]:
grid = gl.DbGrid()
err = grid.reset([4000, 3500], [100, 100], [480000, 6650000])
In [6]:
gp.plot(grid, alpha=0.3)
gp.polygon(poly)
gp.decoration(title="Polygon in its alluvial plain")
No description has been provided for this image
In [ ]:
 

Reduce the complexity of the Polygon

In [7]:
newpoly = poly.reduceComplexity(3000)
newpoly.display(gl.AStringFormat(2))
Polygons
--------
Number of Polygon Sets = 1

PolyElem #1
...........
Number of Vertices = 1123
In [8]:
gp.plot(grid, alpha=0.3)
gp.polygon(newpoly)
gp.polygon(poly, edgecolor="red")
gp.decoration(title="(Simplified) Polygon in its alluvial plain")
No description has been provided for this image

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.

In [9]:
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
In [10]:
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
In [11]:
gp.plot(grid, alpha=0.3)
gp.polygon(newpoly)
gp.polygon(poly, edgecolor="red")
gp.decoration(title="(Simplified) Polygon in its dilated selection")
No description has been provided for this image

New interface allowing to create a Grid directly from a polygon

In [12]:
grid2 = gl.DbGrid.createFromPolygon(newpoly, dx=[1000, 1000])
In [13]:
gp.plot(grid2, alpha=0.3)
gp.polygon(newpoly, edgecolor="red")
gp.decoration(title="Polygon and the newly created Grid")
No description has been provided for this image

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!

In [14]:
ndat = 10
x = np.random.randn(ndat)
y = np.random.randn(ndat)
poly = gl.Polygons.createFromVD(x, y)
In [15]:
gp.polygon(poly, edgecolor="blue")
gp.decoration(title="Polygon generated from vectors of coordinates")
No description has been provided for this image

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.

In [16]:
ndat = 50
poly = gl.Polygons.createFillRandom(50, 0)
In [17]:
gp.polygon(poly, edgecolor="blue")
gp.decoration(title="Circular Polygon")
No description has been provided for this image
In [18]:
ndat = 50
poly = gl.Polygons.createFillRandom(50, 0.2)
In [19]:
gp.polygon(poly, edgecolor="blue")
gp.decoration(title="Polygon generated as an almost circle")
No description has been provided for this image
In [ ]: