locked
How do I check whether a point in the polygon or not RRS feed

  • Question

  • How do I check whether a point in the polygon or not

    and how do I check if the point is on the boundary?

    Thanks for your kind reply!

    Thursday, May 19, 2011 7:01 AM

Answers

  • To test whether a point and a polygon have any intersection at all, use STIntersects().

    To test whether a point is wholly contained inside a polygon, use STContains(), and to test whether it lies on the boundary use STBoundary().

    DECLARE @polygon geometry = 'POLYGON((0 0, 8 0, 4 4, 0 0))';
    DECLARE @point1 geometry = 'POINT(2 1)'; -- Inside Polygon
    DECLARE @point2 geometry = 'POINT(5 3)'; -- On Boundary of Polygon
    DECLARE @point3 geometry = 'POINT(5 10)'; -- Outside Polygon
    
    SELECT
     @polygon.STIntersects(@point1) AS PolygonIntersectsPoint1,
     @polygon.STIntersects(@point2) AS PolygonIntersectsPoint2,
     @polygon.STIntersects(@point3) AS PolygonIntersectsPoint3,
     @polygon.STContains(@point1) AS PolygonContainsPoint1,
     @polygon.STContains(@point2) AS PolygonContainsPoint2,
     @polygon.STContains(@point3) AS PolygonContainsPoint3,
     @polygon.STBoundary().STIntersects(@point1) AS PolygonBoundaryIntersectsPoint1,
     @polygon.STBoundary().STIntersects(@point2) AS PolygonBoundaryIntersectsPoint2,
     @polygon.STBoundary().STIntersects(@point3) AS PolygonBoundaryIntersectsPoint3;
    
    

     


    twitter: @alastaira blog: http://alastaira.wordpress.com/
    • Marked as answer by Stephanie Lv Saturday, May 21, 2011 1:40 AM
    Thursday, May 19, 2011 12:19 PM
    Answerer

All replies

  • To test whether a point and a polygon have any intersection at all, use STIntersects().

    To test whether a point is wholly contained inside a polygon, use STContains(), and to test whether it lies on the boundary use STBoundary().

    DECLARE @polygon geometry = 'POLYGON((0 0, 8 0, 4 4, 0 0))';
    DECLARE @point1 geometry = 'POINT(2 1)'; -- Inside Polygon
    DECLARE @point2 geometry = 'POINT(5 3)'; -- On Boundary of Polygon
    DECLARE @point3 geometry = 'POINT(5 10)'; -- Outside Polygon
    
    SELECT
     @polygon.STIntersects(@point1) AS PolygonIntersectsPoint1,
     @polygon.STIntersects(@point2) AS PolygonIntersectsPoint2,
     @polygon.STIntersects(@point3) AS PolygonIntersectsPoint3,
     @polygon.STContains(@point1) AS PolygonContainsPoint1,
     @polygon.STContains(@point2) AS PolygonContainsPoint2,
     @polygon.STContains(@point3) AS PolygonContainsPoint3,
     @polygon.STBoundary().STIntersects(@point1) AS PolygonBoundaryIntersectsPoint1,
     @polygon.STBoundary().STIntersects(@point2) AS PolygonBoundaryIntersectsPoint2,
     @polygon.STBoundary().STIntersects(@point3) AS PolygonBoundaryIntersectsPoint3;
    
    

     


    twitter: @alastaira blog: http://alastaira.wordpress.com/
    • Marked as answer by Stephanie Lv Saturday, May 21, 2011 1:40 AM
    Thursday, May 19, 2011 12:19 PM
    Answerer
  • Thanks a lot guru!
    Friday, May 20, 2011 12:23 AM