ST_H3Bin takes a geometry column and a numeric H3 resolution and returns a bin column. The output column contains a single H3 bin at the specified resolution for each record in the input column. H3 is a hierarchical geospatial indexing system that partitions the world sphere into hexagonal and pentagonal tiles. H3 supports 16 resolutions ranging from 0 to 15, where 0 results in the largest bins and 15 the smallest. For detailed information on the number of bins and average bin areas at every resolution, refer to the H3 Tables of Cell Statistics Across Resolutions.
With ST_H3Bin, 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.
ST_H3Bin requires the spatial reference to be set to World Geodetic System 1984 (EPSG:4326). If the input geometry is in a different spatial reference, the function automatically transforms the geometry into World Geodetic System 1984. To learn more about spatial references, see Coordinate systems and transformations.
Function | Syntax |
---|---|
Python | h3 |
SQL | ST |
Scala | h3 |
For more details, go to the GeoAnalytics for Microsoft Fabric API reference for h3_bin.
Python and SQL Examples
from geoanalytics_fabric.sql import functions as ST
data = [
("POINT (-117.05 34.22)",),
("LINESTRING (-116.89 33.96, -116.71 34.01, -116.66 34.08)", ),
("POLYGON ((-117.27 34.05, -117.22 33.91, -116.96 33.64, -116.66 33.71, -117.08 33.91, -117.16 34.09, -117.27 34.05))", )
]
df = spark.createDataFrame(data, ["wkt"])\
.withColumn("geometry", ST.geom_from_text("wkt", srid=4326))\
.withColumn("bin", ST.bin_geometry(ST.h3_bin("geometry", 6)))
ax = df.st.plot("geometry", facecolor="none", edgecolor="red")
df.st.plot("bin", ax=ax, facecolor="none", edgecolor="blue")
Scala Example
import com.esri.geoanalytics.sql.{functions => ST}
import org.apache.spark.sql.{functions => F}
case class GeometryRow(wkt: String)
val data = Seq(GeometryRow("POINT (-117.05 34.22)"),
GeometryRow("LINESTRING (-116.89 33.96, -116.71 34.01, -116.66 34.08)"),
GeometryRow("POLYGON ((-117.27 34.05, -117.22 33.91, -116.96 33.64, -116.66 33.71, -117.08 33.91, -117.16 34.09, -117.27 34.05))"))
val df = spark.createDataFrame(data)
.withColumn("geometry", ST.geomFromText($"wkt", F.lit(4326)))
.withColumn("h3_bin", ST.h3Bin($"geometry", 6))
df.select("h3_bin").show(truncate = false)
+----------------------+
|h3_bin |
+----------------------+
|bin#604214655010734079|
|bin#604214642662703103|
|bin#604214632193720319|
+----------------------+
Version table
Release | Notes |
---|---|
1.0.0-beta | Python, SQL, and Scala functions introduced |