References
Overview
For Addresses
- Find the lat/long for the address
- Get back a list of lat/longs
- From that, create a set of OR query with a radius objects to find which records are within the radiuses.
- Create a lat/long radius search object for Snowflake
- Should find all records that are within the lat/long radius
- Need to figure out what the column shape to search the records
- Is it a geolocation point data type?
select * from people_view
where
(
lat_long_point in the radius_object1
OR lat_long_point in the radius_object2
OR lat_long_point in the radius_object3
)
From Google
City
Will this be a similar method. We need to find a lat/long radius as well and do a similar query as above
Example
To find records within a geographic radius in Snowflake using latitude and longitude, the
ST_DWITHINfunction is typically used. This function checks if two
GEOGRAPHYobjects are within a specified distance of each other.
Steps to search for records within a geographic radius:
- Ensure
GEOGRAPHYData Type: The latitude and longitude columns in the table should be converted or stored asGEOGRAPHYdata types. This is achieved using theST_POINTfunction.
Code
ALTER TABLE your_table ADD COLUMN geom GEOGRAPHY; UPDATE your_table SET geom = ST_POINT(longitude, latitude);
Use ST_DWITHIN in the WHERE Clause.
Code
SELECT * FROM your_table WHERE ST_DWITHIN(geom, ST_POINT(target_longitude, target_latitude), radius_in_meters);
geom: This is theGEOGRAPHYcolumn in your table representing the location of your records.ST_POINT(target_longitude, target_latitude): This creates aGEOGRAPHYobject for the central point of your search radius.radius_in_meters: This is the desired radius in meters.ST_DWITHINcalculates distances in meters by default when operating onGEOGRAPHYobjects.
Example:
To find all records within a 5-kilometer radius of a specific point (e.g., latitude 34.0522, longitude -118.2437):
Code
SELECT *FROM your_tableWHERE ST_DWITHIN(geom, ST_POINT(-118.2437, 34.0522), 5000); -- 5000 meters = 5 kilometers
Performance Considerations:
- Search Optimization Service:
- Filtering:
For large tables and frequent geospatial queries, enable the Search Optimization Service on the GEOGRAPHY column to significantly improve query performance.
Pre-filter data using other non-geospatial criteria (e.g., date ranges, categories) before applying the ST_DWITHIN function to reduce the dataset size that needs geospatial processing.
Example from ADS Direct View Query
SELECT *
from BETTRDATA.production.PEOPLE_VW
WHERE 1=1
AND (
ST_DWITHIN(GEO_POINT, ST_POINT(36.813701, -76.084809), 10000)
or
ST_DWITHIN(GEO_POINT, ST_POINT(36.813701, -76.084809), 10000)
or
ST_DWITHIN(GEO_POINT, ST_POINT(36.813701, -105.084809), 10000)
)
LIMIT 100
;