2016-09-14 24 views
0

我有一个确定的点是多边形内或不什么是平等的SQL函数或StoredProcedure的我的C#方法

C#方法是这样的这个C#方法:

/// <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; 
    } 

我如何转换这到SQL函数或StoredProcedure?

+0

@ a_horse_with_no_name Microsoft SQL SERVER 2016 – Ali7091

+1

创建两个几何实例并使用['STContains'](https://msdn.microsoft.com/en-gb/library/bb933904.aspx) –

+0

一个想法:您可以通过列表的点作为xml数据,并将x和y参数指向您的函数或存储过程。在存储过程或函数内部使用openxml将xml数据加载到表中(在函数中,您需要表变量)。然后运行select查询,并将if条件的条件放在where子句中。 –

回答

2

如果您有存储在SQL Server数据库作为空间geometry数据类型所有的多边形,您可以使用SQL Server 2008 R2 +可用空间的功能,其中有许多(谷歌是在这里你的朋友):

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); 
相关问题