0

I have this C# Method that Determine that a point is inside a polygon or not

C# Method is this:

/// <summary>
/// Determine that a point in inside a polygon or not
/// </summary>
/// <param name="points">Points of Polygon</param>
/// <param name="point">Test Point</param>
/// <returns></returns>    
Public bool IsInside(List<PointF> points,PointF point )
    {
        int i, j,n=points.Count;
        bool c = false;
        for (i = 0, j = n - 1; i < n; j = i++)
        {
            if (((points[i].Y > point.Y) != (points[j].Y > point.Y)) &&
                (point.X <
                 (points[j].X - points[i].X)*(point.Y - points[i].Y)/(points[j].Y - points[i].Y) + points[i].X))
                c = !c;
        }
        return c;
    }

How can i convert this to a SQL Function or StoredProcedure?

7
  • @ a_horse_with_no_name Microsoft SQL SERVER 2016 Commented Sep 14, 2016 at 7:36
  • 1
    create two geometry instances and use STContains Commented Sep 14, 2016 at 7:49
  • An idea: you can pass the list of points as xml data and point as x and y parameters to your function or stored procedure. Inside the stored procedure or function load xml data into table using openxml (in function you would need table variable). Then run select query and put your condition of if block in where clause. Commented Sep 14, 2016 at 7:52
  • @MartinSmith can you write code of it for me? tnx Commented Sep 14, 2016 at 8:51
  • 1
    So where is the polygon you are comparing your point to? Also, if you look at my answer below, you will see how you can convert a latitude and longitude to a spatial point using the geometry::STGeomFromText function. Commented Sep 14, 2016 at 9:50

1 Answer 1

2

If you have all your polygons stored in your SQL Server database as spatial geometry data types, you can use the spatial functions available in SQL Server 2008R2+ of which there are many (Google is your friend here):

declare @g geometry
set @g = geometry::STGeomFromText('POLYGON((-33.229869 -70.891988
                                           ,-33.251124 -70.476616
                                           ,-33.703094 -70.508045
                                           ,-33.693931 -70.891052
                                           ,-33.229869 -70.891988
                                           ))'
                                 ,0)

DECLARE @h geometry;

SET @h = geometry::STGeomFromText('POINT(-33.3906300 -70.5725020)', 0);
SELECT @g.STContains(@h);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.