sqlspatial-query

SQL Spatial Query - Determine Which Polygon A Lat/Long Point Falls Into


Via ArcMap, I imported a Feature Class into my SQL2019 Server. No issues and the polygons are displaying properly in the 'spatial results' tab when I check. Inside that feature class, there are three distinct shapes (Lets call the field tblGeo.AREA).

I have another table with LAT/LNG coordinate points (tblPoint.LAT, tblPoint.LNG).

Using the two tables (tblGeo and tblPoint), how can I determine which AREA field the coordinate falls into (if any)?

tblGeo:

Field Name Field Type Sample
GID INT 1,2,3...
SHAPE GEOMETRY 0x2569... or 0x110F...
GEOAREA VARCHAR(50) Washington, New York,...

tblPoint:

Field Name Field Type Sample
PID INT 1,2,3...
LOCATION VARCHAR(100) White House
LAT DECIMAL(9,6) 38.897957
LNG DECIMAL(9,6) -77.036560

Desired Output

PID Location Lat Lng GeoArea
1 White House 38.897957 -77.036560 Washington
2 Empire State Building 40.748817 -73.985428 New York
... ... ... ...

Solution

  • Sample input and output data would be nice.
    You'll need to convert LAT and LNG to a geometry point.
    Assuming LAT and LNG are DECIMAL(9, 6)...

    select g.name as AreaName
    , p.name as PointName
    
    from tblGeo g
      right outer join tblPoint p on g.AREA.STContains(geometry::Point(p.LAT, p.LNG, 0)) = 1
    

    I could check my work if you provided sample data.