我的 C# 方法的 SQL 函数或存储过程是什么
What is Equal SQL Function or StoredProcedure for my C# Method
我有这个 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 函数或存储过程?
如果您将所有多边形作为空间 geometry
数据类型存储在 SQL 服务器数据库中,则可以使用 SQL Server 2008R2+ 中可用的空间函数,其中有很多(Google 是你的朋友):
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);
我有这个 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 函数或存储过程?
如果您将所有多边形作为空间 geometry
数据类型存储在 SQL 服务器数据库中,则可以使用 SQL Server 2008R2+ 中可用的空间函数,其中有很多(Google 是你的朋友):
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);