Convert a Spatial DataFrame to SpatialRDD
Use WherobotsDB DataFrame-RDD Adapter to convert a DataFrame to an SpatialRDD.- Python
- Scala
- Java
Write a Spatial Range Query
A spatial range query takes as input a range query window and an SpatialRDD and returns all geometries that have specified relationship with the query window. Assume you now have an SpatialRDD (typed or generic). You can use the following code to issue an Spatial Range Query on it. ==spatialPredicate== can be set toSpatialPredicate.INTERSECTS to return all geometries intersect with query window. Supported spatial predicates are:
CONTAINS: geometry is completely inside the query windowINTERSECTS: geometry have at least one point in common with the query windowWITHIN: geometry is completely within the query window (no touching edges)COVERS: query window has no point outside of the geometryCOVERED_BY: geometry has no point outside of the query windowOVERLAPS: geometry and the query window spatially overlapCROSSES: geometry and the query window spatially crossTOUCHES: the only points shared between geometry and the query window are on the boundary of geometry and the query windowEQUALS: geometry and the query window are spatially equal
Spatial range query is equivalent with a SELECT query with spatial predicate as search condition in Spatial SQL. An example query is as follows:
- Python
- Scala
- Java
WherobotsDB Python users: Please use RangeQueryRaw from the same module if you want to avoid jvm python serde while converting to Spatial DataFrame. It takes the same parameters as RangeQuery but returns reference to jvm rdd which can be converted to dataframe without python - jvm serde using Adapter.
Range query window
Besides the rectangle (Envelope) type range query window, WherobotsDB range query window can be Point/Polygon/LineString. The code to create a point, linestring (4 vertices) and polygon (4 vertices) is as follows:- Python
- Scala
- Java
A Shapely geometry can be used as a query window. To create shapely geometries, please follow the Shapely official docs.
Use spatial indexes
WherobotsDB provides two types of spatial indexes, Quad-Tree and R-Tree. Once you specify an index type, WherobotsDB will build a local tree index on each of the SpatialRDD partition. To utilize a spatial index in a spatial range query, use the following code:- Python
- Scala
- Java
Output format
- Python
- Scala/Java
The output format of the spatial range query is another RDD which consists of GeoData objects.SpatialRangeQuery result can be used as RDD with map or other spark RDD functions. Also it can be used as
Python objects when using collect method.
Example:Or transformed to GeoPandas GeoDataFrame
Write a Spatial KNN Query
A spatial K Nearnest Neighbor query takes as input a K, a query point and an SpatialRDD and finds the K geometries in the RDD which are the closest to he query point. Assume you now have an SpatialRDD (typed or generic). You can use the following code to issue an Spatial KNN Query on it.- Python
- Scala
- Java
Spatial KNN query that returns 5 Nearest Neighbors is equal to the following statement in Spatial SQL
Query center geometry
Besides the Point type, WherobotsDB KNN query center can be Polygon and LineString.- Python
- Scala/Java
To create Polygon or Linestring object please follow Shapely official docs
Use spatial indexes
To utilize a spatial index in a spatial KNN query, use the following code:- Python
- Scala
- Java
Output format
- Python
- Scala/Java
The output format of the spatial KNN query is a list of GeoData objects. The list has K GeoData objects.Example:
Write a Spatial Join Query
A spatial join query takes as input two Spatial RDD A and B. For each geometry in A, finds the geometries (from B) covered/intersected by it. A and B can be any geometry type and are not necessary to have the same geometry type. Assume you now have two SpatialRDDs (typed or generic). You can use the following code to issue an Spatial Join Query on them.- Python
- Scala
- Java
Spatial join query is equal to the following query in Spatial SQL:
Use spatial partitioning
Sedona spatial partitioning method can significantly speed up the join query. Three spatial partitioning methods are available: KDB-Tree, Quad-Tree and R-Tree. Two SpatialRDD must be partitioned by the same way. If you first partition SpatialRDD A, then you must use the partitioner of A to partition B.- Python
- Scala/Java
- Python
- Scala/Java
Use spatial indexes
To utilize a spatial index in a spatial join query, use the following code:- Python
- Scala
- Java
Output format
- Python
- Scala/Java
Result for this query is RDD which holds two GeoData objects within list of lists.
Example:It is possible to do some RDD operation on result data ex. Getting polygon centroid.
- spatialJoin
- DistanceJoinQueryFlat
- SpatialJoinQueryFlat
Write a Distance Join Query
A distance join query takes as input two Spatial RDD A and B and a distance. For each geometry in A, finds the geometries (from B) are within the given distance to it. A and B can be any geometry type and are not necessary to have the same geometry type. You can transform your CRS using Spatial SQLST_Transform.
If you don’t want to transform your data and are ok with sacrificing the query accuracy, you can use an approximate degree value for distance. Please use this calculator.
Assume you now have two SpatialRDDs (typed or generic). You can use the following code to issue an Distance Join Query on them.
- Python
- Scala/Java
The output format of the distance join query is a list of lists. Each list contains two GeoData objects.
Example:It is possible to do some RDD operation on result data ex. Getting polygon centroid.
COVERED_BY and INTERSECTS as spatial predicates. The rest part of the join query is same as the spatial join query.
The details of spatial partitioning in join query is here.
The details of using spatial indexes in join query is here.
The output format of the distance join query is here.
Distance join query is equal to the following query in Spatial SQL:
Convert SpatialRDD to Spatial DataFrame
Use WherobotsDB DataFrame-RDD Adapter to convert a DataFrame to an SpatialRDD.- Python
- Scala
- Java
- Scala
Convert SpatialPairRDD to Spatial DataFrame
PairRDD is the result of a spatial join query or distance join query. Spatial SQL DataFrame-RDD Adapter can convert the result to a DataFrame. But you need to provide the name of other attributes.- Python
- Scala
- Java
- Python
- Scala
- Java
- Scala

