Description
withSpatialSearchColumns
is an implicit method which adds columns to a
dataset using a spatial search function that returns an com.mapinfo.midev.data.ICursor. A
ICursor containing multiple features will be exploded and will result in multiple rows in
the output.
This method can be used to extends the functionality of a Dataset to better facilitate Spatial Searches. For example, finding all the POIs within half of a mile of each of your customers.
Syntax
withSpatialSearchColumns(cols: TraversableOnce[Column], searchFunction:AnyRef,
attributeHandler: AttributeHandler = defaultAttributeHandler,
includeEmptySearchResults: Boolean = true,
schema: TraversableOnce[StructField] = null): DataFrame
Parameters
Parameter | Type | Description |
---|---|---|
cols | TraversableOnce[Column] |
Sequence of columns to pass to the search function supplied. Number of columns must equal numbers of searchFunction's parameters. |
searchFunction | AnyRef | User defined search function to execute for each row in the dataset. Must return an ICursor, or null if a search cannot be executed for given inputs. |
attributeHandler | AttributeHandler |
Attribute handler to use for converting types. Optional. Default value = defaultAttributeHandler |
includeEmptySearchResult | Boolean | If true, then an empty ICursor will keep the original input row, and the new columns will be null. If false, then an empty ICursor will result in the row not appearing in the outputted DataFrame. Optional. Default value = true |
schema | Column | Specify structFields representing fields returned from the supplied searchFunction. If a schema is not supplied, then one will be automatically determined from the ICursor. Consider specifying a schema if it is possible for entire dataset to result in a null ICursor. Optional. Default value = null |
Return Values
Return Type | Description |
---|---|
DataFrame | A new DataFrame containing all columns from the original Dataset along with columns returned form the search function. |
Examples
This example returns a dataframe that is the result of a features returned by the specified
spatial search function. Following example works as point-in-polygon operation. For every
row in input dataset containing longitude and latitude, it applies
contains search function and checks whether point lies within the
polygon table and then searches for outpurt fields Name
and
ZIP
in polygon table and appends those in output dataframe.
val contains: (Double, Double) => ICursor = (longitude: Double, latitude: Double) => {
val point: Point = new Point(SpatialInfo.create(CoordSysConstants.longLatWGS84), new DirectPosition(longitude, latitude))
val filter: GeometryFilter = new GeometryFilter(geom, GeometryOperator.CONTAINS, point)
val filterSearch: FilterSearch = new FilterSearch(“Name, ZIP”, filter, null)
table.search(filterSearch)
}
val outputDF: DataFrame = inputDF.withSpatialSearchColumns(
Seq(col(longitude).cast(DoubleType), col(latitude).cast(DoubleType)),
contains)
Example showing passing includeEmptySearchResults
as
false
. This will not return the input rows which do not lie inside the
polygon in output:
val contains: (Double, Double) => ICursor = (longitude: Double, latitude: Double) => {
val point: Point = new Point(SpatialInfo.create(CoordSysConstants.longLatWGS84), new DirectPosition(longitude, latitude))
val filter: GeometryFilter = new GeometryFilter(geom, GeometryOperator.CONTAINS, point)
val filterSearch: FilterSearch = new FilterSearch(“Name, ZIP”, filter, null)
table.search(filterSearch)
}
val outputDF: DataFrame = inputDF.withSpatialSearchColumns(
Seq(col(longitude).cast(DoubleType), col(latitude).cast(DoubleType)),
contains, includeEmptySearchResults = false)