C# SqlGeography.STIntersection()即使在我知道没有交集时也会返回
一点关于应用程序的知识 该应用程序允许用户在bing maps WPF api上绘制和保存多边形 我们感兴趣的代码是查找点是否位于多边形中。下面的函数只是在bing地图上的多边形的C# SqlGeography.STIntersection()即使在我知道没有交集时也会返回,c#,wpf,bing-maps,sqlgeography,C#,Wpf,Bing Maps,Sqlgeography,一点关于应用程序的知识 该应用程序允许用户在bing maps WPF api上绘制和保存多边形 我们感兴趣的代码是查找点是否位于多边形中。下面的函数只是在bing地图上的多边形的LocationCollection中循环,并创建一个SqlGeography对象(OpenGisGeographyType.polygon),它是多边形的一个实例 然后,我们将鼠标单击按纬度和经度转换为sqlgeographytype对象(opengisgographytype.Point),并使用SqlGeogra
LocationCollection
中循环,并创建一个SqlGeography对象(OpenGisGeographyType.polygon)
,它是多边形的一个实例
然后,我们将鼠标单击按纬度和经度转换为sqlgeographytype对象(opengisgographytype.Point)
,并使用SqlGeography.STIntersection查找我们的点是否位于多边形内
如图所示,即使该点位于多边形之外,SqlGeography.STIntersection仍会返回一个交点。(当我根据polygonSearch()函数返回的内容将标签设置为“在交付区域内”或“我们的客户区域”时,您可以在图片中看出这一点
当在多边形内测试位置时,图片中的右侧示例具有预期的结果
图中左侧的示例包含意外的结果-这表明一个点位于多边形内,而它显然不在多边形内
注:
- 我使用SqlGeography(myShape)将形状放在地图上,所以我知道 这个形状是用适当的垂直度建造的
- 我使用SqlGeography(myPoint)将pin码放在地图上,因此我知道
在正确的垂直方向上测试销。
- 这仅在大型多边形上失败
Microsoft.SqlServer.Types.dl
l来创建SqlGeography对象。SQL Express 2008免费提供,可以在C:\Program Files(x86)\Microsoft SQL Server\100\SDK\Assembly
public bool polygonSearch2(LocationCollection points, double lat, double lon)
{
SqlGeography myShape = new SqlGeography();
SqlGeographyBuilder shapeBuilder = new SqlGeographyBuilder();
// here are the verticies for the location collection if you want to hard code and try
//shapeBuilder.BeginFigure(47.4275329011347, -86.8136038458706);
//shapeBuilder.AddLine(36.5102408627967, -86.9680936860962);
//shapeBuilder.AddLine(37.4928909385966, -80.2884061860962);
//shapeBuilder.AddLine(38.7375329179818, -75.7180936860962);
//shapeBuilder.AddLine(48.0932596736361, -83.7161405610962);
//shapeBuilder.AddLine(47.4275329011347, -86.8136038458706);
//shapeBuilder.EndFigure();
//shapeBuilder.EndGeography();
// Here I just loop through my points collection backwards to create the polygon in the SqlGeography object
for (int i = points.Count - 1; i >= 0; i--)
{
if (i == 0)
{
shapeBuilder.AddLine(points[i].Latitude, points[i].Longitude);
shapeBuilder.EndFigure();
shapeBuilder.EndGeography();
continue;
}
if (i == points.Count - 1)
{
shapeBuilder.SetSrid(4326);
shapeBuilder.BeginGeography(OpenGisGeographyType.Polygon);
shapeBuilder.BeginFigure(points[i].Latitude, points[i].Longitude);
continue;
}
else
{
shapeBuilder.AddLine(points[i].Latitude, points[i].Longitude);
}
}
myShape = shapeBuilder.ConstructedGeography;
// Here I am creating a SqlGeography object as a point (user mouse click)
SqlGeography myPoint = new SqlGeography();
SqlGeographyBuilder pointBuilder = new SqlGeographyBuilder();
pointBuilder.SetSrid(4326);
pointBuilder.BeginGeography(OpenGisGeographyType.Point);
// Should pass, which it does
// Lat: lat = 43.682110574649791 , Lon: -79.79005605528323
// Should fail, but it intersects??
// Lat: 43.682108149690094 , Lon: -79.790037277494889
pointBuilder.BeginFigure(lat, lon);
pointBuilder.EndFigure();
pointBuilder.EndGeography();
myPoint = pointBuilder.ConstructedGeography;
SqlGeography result = myShape.STIntersection(myPoint);
if (result.Lat.IsNull)
return false;
else
return true;
}
非常感谢任何帮助,我开始让我的老板对这个问题发疯了>我通过使用
LocationToViewPortpoint
函数将我所有的多边形lat/long转换为屏幕上的点
对象,以及我正在测试的交点,并使用X和Y值而不是lat/long来解决这个问题在我的STIntersects
中,它总是返回true还是仅当它关闭但仍在外部时返回?@ta.speot.is仅当关闭但仍在外部时,这只有在缩放后才会失败。你是说在缩放地图,然后添加一个标记后,返回的结果不正确吗?这并不是说一个pin标记首先在p内部当图像被缩放时,olygon会在多边形外渲染,对吗?@Tim在缩放地图,然后添加标记后,返回错误的结果-这是正确的。看起来它只在缩小时有效,因为计算不需要像percise那样。谢谢您的时间