GS_Distance_Within(sg1,sg2,dist)

Returns 1 (true) if the two spatial objects are within the specified distance of one another.

Parameters

Parameter Type Description
sg1 SphericalGeography The spatial location of the first location defined in spherical geography
sg2 SphericalGeography The spatial location of the second location defined in spherical geography
dist double The distance (in meters) that the points should be within each other

Returns

boolean

Example

SELECT * 
FROM geospock.default.events event 
JOIN geospock.default.pois poi 
ON GS_Distance_Within(to_spherical_geography(ST_Point(poi.longitude, poi.latitude)), to_spherical_geography(ST_Point(event.longitude, event.latitude)), 10);

Usage

For example, to define a geofence using GS_Distance_Within(), your query will look like this:

SELECT * 
FROM geospock.default.events event 
JOIN geospock.default.pois poi 
ON GS_Distance_Within(to_spherical_geography(ST_Point(poi.longitude, poi.latitude)), to_spherical_geography(ST_Point(event.longitude, event.latitude)), 10);

This query uses the GeoSpock custom function GS_Distance_Within which has been optimized to run spatial join queries based on the distance (in meters) between two points using spherical geography.

If you are using a Hive table to provide the locations of the POIs, the latitude and longitude fields in that table must be of type double, decimal or float/real.

Alternatively, you could use the other optimized spatial join function, gs_great_circle_distance_within, instead:

SELECT *
FROM geospock.default.events event
JOIN geospock.default.pois poi
ON gs_great_circle_distance_within(poi.longitude, poi.latitude, event.longitude, event.latitude, 10);

See GS_great_circle_distance_within(lon1,lat1,lon2,lat2,dist) for more information.