Finding Record By Address of location lat/long point

References

Geospatial data types | Snowflake DocumentationGeospatial data types | Snowflake Documentation

Overview

For Addresses

  1. Find the lat/long for the address
    1. Get back a list of lat/longs
    2. From that, create a set of OR query with a radius objects to find which records are within the radiuses.
  2. Create a lat/long radius search object for Snowflake
  3. Should find all records that are within the lat/long radius
    1. Need to figure out what the column shape to search the records
      1. 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_DWITHIN

function is typically used. This function checks if two

GEOGRAPHY

objects are within a specified distance of each other.

Steps to search for records within a geographic radius:

  • Ensure GEOGRAPHY Data Type: The latitude and longitude columns in the table should be converted or stored as GEOGRAPHY data types. This is achieved using the ST_POINT function.

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 the GEOGRAPHY column in your table representing the location of your records.
  • ST_POINT(target_longitude, target_latitude): This creates a GEOGRAPHY object for the central point of your search radius.
  • radius_in_meters: This is the desired radius in meters. ST_DWITHIN calculates distances in meters by default when operating on GEOGRAPHY objects.

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:
  • For large tables and frequent geospatial queries, enable the Search Optimization Service on the GEOGRAPHY column to significantly improve query performance.

  • Filtering:
  • 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
;