检查点是否在多边形(地图)中

发布于 2024-12-29 13:24:57 字数 1808 浏览 4 评论 0原文

我正在尝试检查一个点是否在多边形中。
目前我尝试使用这个函数

pointInPolygon:function (point,polygon){
    var i;
    var j=polygon.length-1;
    var inPoly=false;
    var lon = point.longitude;
    var lat = point.latitude;
    for (i=0; i<polygon.length; i++)
    {
        if (polygon[i][0]<lon && polygon[j][0]>=lon|| polygon[j][0]<lon && polygon[i][0]>=lon){
            if (polygon[i][0]+(lon-polygon[i][0])/(polygon[j][0]-polygon[i][0])*(polygon[j][1]-polygon[i][1])<lat){
                inPoly=!inPoly;
            }
        }
        j=i;
    }
    return inPoly;
}

...这个函数似乎适用于简单的多边形( http:// jsfiddle.net/zTmr7/3/ )但它对我不起作用...... 这是多边形的示例数据:

polygon: Array[14]
Array[2]
        0: "-120.190625"
        1: "29.6614549946937"
Array[2]
        0: "-116.87275390625"
        1: "32.6320990313992"
Array[2]
        0: "-116.60908203125"
        1: "34.0363970332393"
Array[2]
        0: "-120.89375"
        1: "41.9203747676428"
Array[2]
        0: "-114.74140625"
        1: "45.784484644005"
Array[2]
        0: "-115.971875"
        1: "48.6489780115889"
Array[2]
        0: "-132.758984375"
        1: "59.9891712248332"
Array[2]
        0: "-162.5099609375"
        1: "68.919753529737"
Array[2]
        0: "-168.6623046875"
        1: "68.9828872543805"
Array[2]
        0: "-168.4865234375"
        1: "64.2551601036027"
Array[2]
        0: "-179.874356794357"
        1: "51.0915874974707"
Array[2]
        0: "-179.999916362762"
        1: "13.1823178795562"
Array[2]
        0: "-143.8771484375"
        1: "19.9962034117847"
Array[2]
        0: "-120.190625"
        1: "29.6614549946937"  

也许你可以帮忙......提前感谢

PS。解决方案必须专门针对 Bing 地图或通用解决方案...

I'm trying to check if a point is in polygon.
At the moment I have try with this function

pointInPolygon:function (point,polygon){
    var i;
    var j=polygon.length-1;
    var inPoly=false;
    var lon = point.longitude;
    var lat = point.latitude;
    for (i=0; i<polygon.length; i++)
    {
        if (polygon[i][0]<lon && polygon[j][0]>=lon|| polygon[j][0]<lon && polygon[i][0]>=lon){
            if (polygon[i][0]+(lon-polygon[i][0])/(polygon[j][0]-polygon[i][0])*(polygon[j][1]-polygon[i][1])<lat){
                inPoly=!inPoly;
            }
        }
        j=i;
    }
    return inPoly;
}

... this function is seems to work on simple polygon ( http://jsfiddle.net/zTmr7/3/ ) but it won't work for me...
here is sample data of a polygon:

polygon: Array[14]
Array[2]
        0: "-120.190625"
        1: "29.6614549946937"
Array[2]
        0: "-116.87275390625"
        1: "32.6320990313992"
Array[2]
        0: "-116.60908203125"
        1: "34.0363970332393"
Array[2]
        0: "-120.89375"
        1: "41.9203747676428"
Array[2]
        0: "-114.74140625"
        1: "45.784484644005"
Array[2]
        0: "-115.971875"
        1: "48.6489780115889"
Array[2]
        0: "-132.758984375"
        1: "59.9891712248332"
Array[2]
        0: "-162.5099609375"
        1: "68.919753529737"
Array[2]
        0: "-168.6623046875"
        1: "68.9828872543805"
Array[2]
        0: "-168.4865234375"
        1: "64.2551601036027"
Array[2]
        0: "-179.874356794357"
        1: "51.0915874974707"
Array[2]
        0: "-179.999916362762"
        1: "13.1823178795562"
Array[2]
        0: "-143.8771484375"
        1: "19.9962034117847"
Array[2]
        0: "-120.190625"
        1: "29.6614549946937"  

Maybe you can help... thanks in advance

PS. solution must be especially for Bing maps or universal solution...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

野却迷人 2025-01-05 13:24:57

Google 地图 API 尚未提供检查多边形中的点的方法。经过一番研究后,我偶然发现了射线投射算法,该算法将确定 XY 坐标是否位于绘制的形状内。这将转换为纬度和经度。以下内容扩展了 google.maps.polygon.prototype 以使用此算法。只需在 google.maps 加载后在代码中的某个点包含此代码即可:

google.maps.Polygon.prototype.Contains = function(point) {
  var crossings = 0, path = this.getPath();

  // for each edge
  for (var i=0; i < path.getLength(); i++) {
      var a = path.getAt(i),
          j = i + 1;
      if (j >= path.getLength()) {
          j = 0;
      }
      var b = path.getAt(j);
      if (rayCrossesSegment(point, a, b)) {
        crossings++;
      }
  }

  // odd number of crossings?
  return (crossings % 2 == 1);

  function rayCrossesSegment(point, a, b) {
    var px = point.lng(),
        py = point.lat(),
        ax = a.lng(),
        ay = a.lat(),
        bx = b.lng(),
        by = b.lat();
    if (ay > by) {
        ax = b.lng();
        ay = b.lat();
        bx = a.lng();
        by = a.lat();
    }
    // alter longitude to cater for 180 degree crossings
    if (px < 0) { px += 360 };
    if (ax < 0) { ax += 360 };
    if (bx < 0) { bx += 360 };

    if (py == ay || py == by) py += 0.00000001;
    if ((py > by || py < ay) || (px > Math.max(ax, bx))) return false;
    if (px < Math.min(ax, bx)) return true;

    var red = (ax != bx) ? ((by - ay) / (bx - ax)) : Infinity;
    var blue = (ax != px) ? ((py - ay) / (px - ax)) : Infinity;
    return (blue >= red);
  }
};

这里我们通过定义一个名为“Contains”的函数来扩展 google.maps.Polygon 的功能,该函数可用于确定是否提供了 google.maps.Polygon 中提供的纬度经度函数参数是否在多边形内。在这里,我们利用光线投射算法并使用该算法开发了一个函数。现在做完这么多练习后,我们可以按如下方式检查一个点:

var point = new google.maps.LatLng(52.05249047600099, -0.6097412109375); var 多边形 = new google.maps.Polygon({path:[INSERT_PATH_ARRAY_HERE]}); if (polygon.Contains(point)) { // 点在多边形内部 }

有关完整代码和演示,请访问:http://coun sellbyabhi.blogspot.in/2013/01/google-map-check-whether-point-latlong.html

The Google maps API does not already provide a method for checking points in polygons. After researching a bit I stumbled across the Ray-casting algorithm which will determine if an X-Y coordinate is inside a plotted shape. This will translate to latitude and longitude. The following extends the google.maps.polygon.prototype to use this algorithm. Simply include this code at a point in the code after google.maps has loaded:

google.maps.Polygon.prototype.Contains = function(point) {
  var crossings = 0, path = this.getPath();

  // for each edge
  for (var i=0; i < path.getLength(); i++) {
      var a = path.getAt(i),
          j = i + 1;
      if (j >= path.getLength()) {
          j = 0;
      }
      var b = path.getAt(j);
      if (rayCrossesSegment(point, a, b)) {
        crossings++;
      }
  }

  // odd number of crossings?
  return (crossings % 2 == 1);

  function rayCrossesSegment(point, a, b) {
    var px = point.lng(),
        py = point.lat(),
        ax = a.lng(),
        ay = a.lat(),
        bx = b.lng(),
        by = b.lat();
    if (ay > by) {
        ax = b.lng();
        ay = b.lat();
        bx = a.lng();
        by = a.lat();
    }
    // alter longitude to cater for 180 degree crossings
    if (px < 0) { px += 360 };
    if (ax < 0) { ax += 360 };
    if (bx < 0) { bx += 360 };

    if (py == ay || py == by) py += 0.00000001;
    if ((py > by || py < ay) || (px > Math.max(ax, bx))) return false;
    if (px < Math.min(ax, bx)) return true;

    var red = (ax != bx) ? ((by - ay) / (bx - ax)) : Infinity;
    var blue = (ax != px) ? ((py - ay) / (px - ax)) : Infinity;
    return (blue >= red);
  }
};

Here we have extended the functionality of google.maps.Polygon by defining a function with name ‘Contains’ which can be used to determine whether the latitude longitude provided in function parameter are within the polygon or not. Here we make use of Ray-casting algorithm and developed a function using the same. After doing this much of exercise now, we can check a point as follows:

var point = new google.maps.LatLng(52.05249047600099, -0.6097412109375); var polygon = new google.maps.Polygon({path:[INSERT_PATH_ARRAY_HERE]}); if (polygon.Contains(point)) { // point is inside polygon }

For complete code and demo please go to: http://counsellingbyabhi.blogspot.in/2013/01/google-map-check-whether-point-latlong.html

假装不在乎 2025-01-05 13:24:57

第一个 if 语句看起来不错 - 您正在检查该点的经度是否位于多边形段的经度内。

第二个 if 应该用该点的精确经度来插值线段的截距,并确定该截距是高于还是低于该点。由于一个简单的拼写错误,我认为这不是它正在做的事情。

if (polygon[i][1]+(lon-polygon[i][0])/(polygon[j][0]-polygon[i][0])*(polygon[j][1]-polygon[i][1])<lat){
               ^

您还应该在 polygon[i][0]==polygon[j][0] 时包含一个单独的情况,这样就不会出现被零除的错误。

The first if statement looks good - you're checking to see if the longitude of the point lies within the longitude of the polygon segment.

The second if should be interpolating the intercept of the segment with the exact longitude of the point, and determining if that intercept is above or below the point. I don't think that is what it is doing, due to a simple typo.

if (polygon[i][1]+(lon-polygon[i][0])/(polygon[j][0]-polygon[i][0])*(polygon[j][1]-polygon[i][1])<lat){
               ^

You should also include a separate case when polygon[i][0]==polygon[j][0] so that you don't get a divide-by-zero error.

神爱温柔 2025-01-05 13:24:57

您可以使用我在 github 中镜像的 libkml 变体的克隆: https://github.com/ gigdal/libkml-pointinpolygon

在这个开源作者的帮助下,设计了一个模块,该模块将指示给定点是否在 KML 多边形内部。确保检查 git 源的“libkml-git”分支而不是“master”分支。您感兴趣的类是“pointinpolygon.cc”。它是 C++ 源代码,您可以将其包含在项目中并与项目一起构建。

编辑 - 多边形中的点问题的解决方案与其覆盖的地图无关。

You can use my clone of the libkml variant which I have mirrored in github here: https://github.com/gumdal/libkml-pointinpolygon

With help of the author of this open source, a module is designed which will indicate whether the given point is inside the KML polygon or not. Make sure that you check the branch "libkml-git" and not the "master" branch of the git sources. The class you would be interested in is "pointinpolygon.cc". It is C++ source code which you can include inside your project and build it along with your project.

Edit - The solution for point in polygon problem is independent of what map it is overlayed on.

葬心 2025-01-05 13:24:57

true|false = google.maps.geometry.poly.containsLocation(googlePoint, googlePoly);

true|false = google.maps.geometry.poly.containsLocation(googlePoint, googlePoly);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文