I have a polygon structure in an sql2005 db as described below.
CREATE TABLE [dbo].[Polygons](
[PolygonID] [int] IDENTITY(1,1) NOT NULL,
[PolygonName] [varchar](255) NOT NULL,
[PolygonColor] [varchar](7) NOT NULL,
[PolygonRuleID] [int] NOT NULL)
CREATE TABLE [dbo].[Polylines](
[LineID] [int] IDENTITY(1,1) NOT NULL,
[LineX1] [float] NOT NULL,
[LineY1] [float] NOT NULL,
[LineX2] [float] NOT NULL,
[LineY2] [float] NOT NULL,
[PolygonID] [int] NOT NULL
)
Now I retrieve whole lines to application and put all to hit testing function.
public static bool PointInPolygon(float pointX, float pointY, PolylineCollection polygon)
{
int nvert = polygon.Count();
int i, j = 0;
bool c = false;
for (i = 0, j = nvert - 1; i < nvert; j = i++)
{
if (((polygon[i].LineY1 > pointY) != (polygon[j].LineY1 > pointY)) &&
(pointX < (polygon[j].LineX1 - polygon[i].LineX1) * (pointY - polygon[i].LineY1) / (polygon[j].LineY1 - polygon[i].LineY1) + polygon[i].LineX1))
c = !c;
}
return c;
}
But I need to move this function to sql server. But Sql 2005 doesn’t have native spatial functions and I dont want to use any extra spatial functionality libraries. How can I port this function to T-SQL? 🙂 Or anyone have different solution to PointInPolygon check?
Thanks
SQL Server 2005 allows you to write native functions for the CLR that can execute server side. You can read the MSDN intro Using CLR Integration in SQL Server 2005. This should allow you to have your function implemented as an addition to sql server and run at native speeds.