Search code examples
sqlsql-servergps

I would like to select all points where they are outside my polygon


I would like to select all points where they are outside my polygon.

I have a column called gps, like GEOGRAPHY

This field contains latitude and longitude.

I declared the region of my polygon

DECLARE @thePolygon GEOGRAPHY
SET @thePolygon = GEOGRAPHY::STGeomFromText('POLYGON((-78.50932668617881 45.024933647425115, -78.53403351361905 44.9898648154388, -78.48446979547693 44.97239241709962, -78.45973073293072 45.007441690111115, -78.50932668617881 45.024933647425115))', 4269);

now I wanted something like not in, ex.

[![select top 100 gps from MonitoramentosVTR
where gps not in GEOGRAPHY::STIntersection(@thePolygon)][1]][1]

I am using Microsoft SQL Server


Solution

  • Use STIntersects, eg:

    select top 100 gps 
    from MonitoramentosVTR
    where gps.STIntersects(@thePolygon) = 0
    order by ...