Loading...

verticapy.sql.geo.split_polygon_n#

verticapy.sql.geo.split_polygon_n(p: str, nbins: int = 100) vDataFrame#

Splits a polygon into (nbins 2) smaller polygons of approximately equal total area. This process is inexact, and the split polygons have approximated edges; greater values for nbins produces more accurate and precise edge approximations.

Parameters#

p: str

String representation of the polygon.

nbins: int, optional

Number of bins used to cut the longitude and the latitude. Split polygons have approximated edges, and greater values for nbins leads to more accurate and precise edge approximations.

Returns#

vDataFrame

output vDataFrame that includes the new polygons.

Examples#

We import verticapy:

import verticapy as vp

Hint

By assigning an alias to verticapy, we mitigate the risk of code collisions with other libraries. This precaution is necessary because verticapy uses commonly known function names like “average” and “median”, which can potentially lead to naming conflicts. The use of an alias ensures that the functions from verticapy are used as intended without interfering with functions from other libraries.

Let’s use the following polygon.

p = 'POLYGON ((121.334030916 31.5081948415, 121.334030917 31.5079167872, 121.333748304 31.5081948413, 121.334030916 31.5081948415))'
poly = vp.vDataFrame({"triangle": [p]})
poly["triangle"].apply("ST_GeomFromText({})")
poly["triangle"].geo_plot(
    color="white",
    edgecolor="black",
)

../_images/sql_geo_functions_split_polygon_n.png

Now, let’s proceed to split the polygon into multiple parts.

from verticapy.sql.geo import split_polygon_n

split_p = split_polygon_n(p)
display(split_p)
123
gid
Integer
100%
🌎
Geometry(2024)
100%
199
2100
3199
4200
5201
6299
7300
8301
9302
10399
11400
12401
13402
14403
15499
16500
17501
18502
19503
20504

Let’s visualize it.

split_p["geom"].geo_plot(
    color="white",
    edgecolor="black",
)

../_images/sql_geo_functions_split_polygon_n_2.png

Note

This function can be employed to partition the space into multiple smaller segments, enabling more precise analysis. It proves particularly useful for use cases such as analyzing density.