ST_HexBin takes a geometry column and a numeric bin size and returns a bin column. The output column contains a single hexagonal bin for each record in the input column. The specified bin size determines the height of each bin and is in the same units as the input geometry. The centroid of the input geometry is guaranteed to intersect with the bin returned but is not necessarily coincident with the bin center. Use ST_BinGeometry to obtain the geometry of each result bin.
This function can also be called with a long column representing the ID of the bin (see ST_BinId). The bin ID will be cast to a bin column.
Function | Syntax |
---|---|
Python | hex |
SQL | ST |
Scala | hex |
For more details, go to the GeoAnalytics for Microsoft Fabric API reference for hex_bin.
Python and SQL Examples
# Example of hex_bin with a geometry column as input
from geoanalytics_fabric.sql import functions as ST, Polygon
data = [(Polygon([[[0,0],[0,10],[10,10],[10,0],[0,0]]]),),
(Polygon([[[20,0],[30,20],[40,0],[20,0]]]),),
(Polygon([[[20,30],[25,35],[30,30],[20,30]]]),)]
df = spark.createDataFrame(data,["polygon"]) \
.withColumn("hex_bin", ST.hex_bin("polygon", 5)) \
.withColumn("bin_geometry", ST.bin_geometry("hex_bin"))
df.select("hex_bin").show()
ax = df.st.plot("polygon", facecolor="none", edgecolor="red")
df.st.plot("bin_geometry", ax=ax, facecolor="none", edgecolor="blue")
+---------------+
| hex_bin|
+---------------+
| bin#4294967296|
|bin#30064771073|
|bin#25769803782|
+---------------+
# Example of hex_bin with a long column as input
from geoanalytics_fabric.sql import functions as ST
data = [(4294967296,), (30064771073,), (25769803782,)]
df = spark.createDataFrame(data, ["bin_id"]) \
.withColumn("hex_bin", ST.hex_bin("bin_id", 5)) \
.withColumn("bin_geometry", ST.bin_geometry("hex_bin"))
ax = df.st.plot("bin_geometry", facecolor="none", edgecolor="blue")
Scala example
import com.esri.geoanalytics.geometry._
import com.esri.geoanalytics.sql.{functions => ST}
case class PolygonRow(polygon: Polygon)
val data = Seq(PolygonRow(Polygon(Point(0, 0), Point(0, 10), Point(10, 10), Point(10, 0), Point(0, 0))),
PolygonRow(Polygon(Point(20, 0), Point(30, 20), Point(40, 0), Point(20, 0))),
PolygonRow(Polygon(Point(20, 30), Point(25, 35), Point(30, 30), Point(20, 30))))
val df = spark.createDataFrame(data)
.withColumn("hex_bin", ST.hexBin($"polygon", 5))
df.select("hex_bin").show()
+---------------+
| hex_bin|
+---------------+
| bin#4294967296|
|bin#30064771073|
|bin#25769803782|
+---------------+
Version table
Release | Notes |
---|---|
1.0.0-beta | Python, SQL, and Scala functions introduced |