locked
How to handle a basic scenario - determine if GPS coordinates are with in a Square. RRS feed

  • Question

  • I have 4 gps cordinates (LAT/LONG) that represents a square.   I want to find if a GPS coordinate falls with in the square.  I have looked on the internet but I have not found an example that covers this basic scenario.  The closes example I found was locating a point inside a radius.  This would not work for me.  It seems as if this would be one of the first things that are show cased with spatial data types or perhaps spatial data types in SQL Server are not really created for to solve these types of problems.  Either way I need to find a solution hopefully using Microsoft technology. It saves me the hasself of buying other tools.


    Wednesday, July 27, 2011 5:37 PM

Answers

  • Try this:

    -- Create a square from four GPS coordinates.
    -- Polygons must be closed, so repeat the first point again at the end
    -- Coordinates are listed in longitude-latitude order
    DECLARE @square geography = 'POLYGON((0 52, 2 52, 2 54, 0 54, 0 52))';
    
    -- Create a point. Again, listed in longitude-latitude order
    DECLARE @point geography = 'POINT(1 53)';
    
    -- Test whether this point intersects the square
    SELECT @point.STIntersects(@square) AS IsThePointInsideTheSquare;
    -- Result 1: the point is inside the square
    
    -- Try another point
    DECLARE @anotherpoint geography = 'POINT(4 50)';
    SELECT @anotherpoint.STIntersects(@square) AS IsTheOtherPointInsideTheSquare;
    -- Result 0: this point is not inside the square
    


    twitter: @alastaira blog: http://alastaira.wordpress.com/
    • Marked as answer by hotshot Wednesday, July 27, 2011 6:33 PM
    Wednesday, July 27, 2011 5:58 PM
    Answerer

All replies

  • Try this:

    -- Create a square from four GPS coordinates.
    -- Polygons must be closed, so repeat the first point again at the end
    -- Coordinates are listed in longitude-latitude order
    DECLARE @square geography = 'POLYGON((0 52, 2 52, 2 54, 0 54, 0 52))';
    
    -- Create a point. Again, listed in longitude-latitude order
    DECLARE @point geography = 'POINT(1 53)';
    
    -- Test whether this point intersects the square
    SELECT @point.STIntersects(@square) AS IsThePointInsideTheSquare;
    -- Result 1: the point is inside the square
    
    -- Try another point
    DECLARE @anotherpoint geography = 'POINT(4 50)';
    SELECT @anotherpoint.STIntersects(@square) AS IsTheOtherPointInsideTheSquare;
    -- Result 0: this point is not inside the square
    


    twitter: @alastaira blog: http://alastaira.wordpress.com/
    • Marked as answer by hotshot Wednesday, July 27, 2011 6:33 PM
    Wednesday, July 27, 2011 5:58 PM
    Answerer
  • So far I am testing it out with GPS coordinates and it is working.  I will keep testing a few more coordinates. Thank you very much.

    Wednesday, July 27, 2011 6:34 PM