23.3. Spark JTS

The Spark JTS module provides a set of User Defined Functions (UDFs) and User Defined Types (UDTs) that enable executing SQL queries in spark that perform geospatial operations on geospatial data types.

GeoMesa SparkSQL support builds upon the DataSet/DataFrame API present in the Spark SQL module to provide geospatial capabilities. This includes custom geospatial data types and functions, the ability to create a DataFrame from a GeoTools DataStore, and optimizations to improve SQL query performance.

This functionality is located in the geomesa-spark/geomesa-spark-jts module:

  // version, etc.

23.3.1. Example

The following is a Scala example of loading a DataFrame with user defined types:

Notice how the initial schema does not have a UserDefinedType, but after applying our User Defined Functions to the appropriate columns, we are left with a data frame with geospatial column types.

It is also possible to construct a DataFrame from a list of geospatial objects:

import spark.implicits._
val point = new GeometryFactory().createPoint(new Coordinate(3.4, 5.6))
val df = Seq(point).toDF("point")

23.3.2. Configuration

To enable this behavior, import org.locationtech.geomesa.spark.jts._, create a SparkSession` and call ``.withJTS on it. This will register the UDFs and UDTs as well as some catalyst optimizations for these operations. Alternatively you can call initJTS(SQLContext).

import org.apache.spark.sql.SparkSession
import org.apache.spark.sql.SQLContext
import org.locationtech.geomesa.spark.jts._

val spark: SparkSession = SparkSession.builder() // ... initialize spark session

23.3.3. Geospatial User-defined Types and Functions

The Spark JTS module takes several classes representing geometry objects (as described by the OGC OpenGIS Simple feature access common architecture specification and implemented by the Java Topology Suite) and registers them as user-defined types (UDTs) in SparkSQL. For example the Geometry class is registered as GeometryUDT. The following types are registered:

  • GeometryUDT
  • PointUDT
  • LineStringUDT
  • PolygonUDT
  • MultiPointUDT
  • MultiLineStringUDT
  • MultiPolygonUDT
  • GeometryCollectionUDT

Spark JTS also implements a subset of the functions described in the OGC OpenGIS Simple feature access SQL option specification as SparkSQL user-defined functions (UDFs). These include functions for creating geometries, accessing properties of geometries, casting Geometry objects to more specific subclasses, outputting geometries in other formats, measuring spatial relationships between geometries, and processing geometries.

For example, the following SQL query

select * from chicago where st_contains(st_makeBBOX(0.0, 0.0, 90.0, 90.0), geom)

uses two UDFs–st_contains and st_makeBBOX–to find the rows in the chicago DataFrame where column geom is contained within the specified bounding box.

The UDFs are also exposed for use with the DataFrame API, meaning the above example is also achievable with the following code:

import org.locationtech.geomesa.spark.jts._
import spark.implicits. _
chicagoDF.where(st_contains(st_makeBBOX(0.0, 0.0, 90.0, 90.0), $"geom"))

A complete list of the implemented UDFs is given in the next section (SparkSQL Functions).

23.3.4. Building

This module can be built and used independently of GeoMesa with the following command:

$ mvn install -pl geomesa-spark/geomesa-spark-jts