首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >即使我知道没有交集,SqlGeography.STIntersection()也会返回

即使我知道没有交集,SqlGeography.STIntersection()也会返回
EN

Stack Overflow用户
提问于 2013-07-03 21:06:31
回答 1查看 1.2K关注 0票数 3

关于申请的一点;

该应用程序允许用户绘制多边形并将多边形保存到bing地图WPF api上。

我们感兴趣的代码之一是找出一个点是否在多边形中的位置。下面的函数简单地循环遍历bing映射上多边形的LocationCollection,并创建一个SqlGeography object (OpenGisGeographyType.Polygon),它是多边形的一个实例。

然后根据纬度和经度将鼠标单击转换为SqlGeography object (OpenGisGeographyType.Point),并使用SqlGeography.STIntersection查找我们的点是否位于多边形内。

如图所示,即使点在多边形之外,SqlGeography.STIntersection仍然返回一个交点。(您可以在图片中说明这一点,因为我根据返回的polygonSearch()函数将标签设置为“交付区域内”或"Customer“。

当在多边形内测试位置时,图片中的正确示例具有预期的结果。

图片中的左边示例包含意外的结果--这表明一个点在多边形内,而它显然不是!

备注:

  • 我使用SqlGeography (myShape)把形状放在地图上,所以我知道形状是用适当的眩晕来构造的。
  • 我使用SqlGeography (myPoint)把引脚放在地图上,所以我知道这个引脚是在正确的眩晕状态下测试的。
    • 这只在大多边形上失败。

下面我给出在内存中创建多边形的代码,并将鼠标单击事件转换为lat、经度。(我已经在注释中包含了多边形眩晕,这样就可以不用必应api来查看它,只需用上面的注释替换for循环),尽管您需要引用Microsoft.SqlServer.Types.dll来创建SqlGeography对象。它与SQL 2008免费,可以在C:\Program Files (x86)\Microsoft SQL Server\100\SDK\Assemblies中找到。

代码语言:javascript
复制
 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;



}

任何帮助都是非常感谢的,我已经开始为这个问题而发疯了,>.<。

这和SRID有关系吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-07-06 05:38:53

我通过使用Point函数将屏幕上的所有多边形lat / long转换为一个LocationToViewPortpoint对象,以及我正在测试的交叉点,并在STIntersects中使用X和Y值而不是lat / long来解决这个问题。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17458117

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档