ST_HexBin

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.

FunctionSyntax
Pythonhex_bin(geometry, bin_size)
SQLST_HexBin(geometry, bin_size)
ScalahexBin(geometry, binSize)

For more details, go to the GeoAnalytics Engine API reference for hex_bin.

Python and SQL Examples

PythonPythonSQL
Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# Example of hex_bin with a geometry column as input
from geoanalytics.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")
Result
Use dark colors for code blocksCopy
1
2
3
4
5
6
7
+---------------+
|        hex_bin|
+---------------+
| bin#4294967296|
|bin#30064771073|
|bin#25769803782|
+---------------+
Plotting example for ST_HexBin
Plotted result for ST_HexBin with a geometry column as input.
PythonPythonSQL
Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
# Example of hex_bin with a long column as input
from geoanalytics.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")
Plotting example for ST_HexBin
Plotted result for ST_HexBin with a long column as input.

Scala example

Scala
Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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()
Result
Use dark colors for code blocksCopy
1
2
3
4
5
6
7
+---------------+
|        hex_bin|
+---------------+
| bin#4294967296|
|bin#30064771073|
|bin#25769803782|
+---------------+

Version table

ReleaseNotes

1.0.0

Python and SQL functions introduced

1.5.0

Scala function introduced

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.