ST_SquareBin takes a geometry column and a numeric bin size and returns a bin column. The output column contains a single square 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 | square |
SQL | ST |
Scala | square |
For more details, go to the GeoAnalytics Engine API reference for square_bin.
Python and SQL Examples
# Example of square_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]]]),),
(Polygon([[[20,0],[30,20],[40,0]]]),),
(Polygon([[[20,30],[25,35],[30,30]]]),)]
df = spark.createDataFrame(data,["polygon"]) \
.withColumn("square_bin", ST.square_bin("polygon", 5)) \
.withColumn("bin_geometry", ST.bin_geometry("square_bin"))
df.select("square_bin").show()
ax = df.st.plot("polygon", facecolor="none", edgecolor="red")
df.st.plot("bin_geometry", ax=ax, facecolor="none", edgecolor="blue")
+---------------+
| square_bin|
+---------------+
| bin#0|
|bin#25769803777|
|bin#21474836486|
+---------------+

# Example of square_bin with a long column as input
from geoanalytics.sql import functions as ST
data = [(0,), (25769803777,), (21474836486,)]
df = spark.createDataFrame(data, ["bin_id"]) \
.withColumn("square_bin", ST.square_bin("bin_id", 5)) \
.withColumn("bin_geometry", ST.bin_geometry("square_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))),
PolygonRow(Polygon(Point(20, 30), Point(25, 35), Point(30, 30))))
val df = spark.createDataFrame(data)
.withColumn("square_bin", ST.squareBin($"polygon", 5))
df.select("square_bin").show()
+---------------+
| square_bin|
+---------------+
| bin#0|
|bin#25769803777|
|bin#21474836486|
+---------------+
import com.esri.geoanalytics.sql.{functions => ST}
case class BinID(binID: String)
val data = Seq(BinID("0"),
BinID("25769803777"),
BinID("21474836486"))
val df = spark.createDataFrame(data)
.withColumn("square_bin", ST.squareBin($"binID", 5))
df.select("square_bin").show()
+---------------+
| square_bin|
+---------------+
| bin#0|
|bin#25769803777|
|bin#21474836486|
+---------------+
Version table
Release | Notes |
---|---|
1.0.0 | Python and SQL functions introduced |
1.5.0 | Scala function introduced |