2016-09-14 56 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); 
相關問題