【问题标题】:How to get all points inside a polygon in openlayers 4.x?如何在openlayers 4.x中获取多边形内的所有点?
【发布时间】:2018-09-25 13:08:11
【问题描述】:

我想使用 Openlayers 4.x 集成一个功能是我想获取地图上多边形内的所有点。目前我可以得到多边形本身的所有坐标。

但我想要多边形内的所有坐标或点。如果我解释得更多,这意味着我希望地图上的所有点或坐标都被多边形区域包围。

【问题讨论】:

    标签: javascript geometry openlayers


    【解决方案1】:

    在@willsters 回答的基础上,如果已经确定了候选对象并且您只是在寻找点(几何为范围),则可以在相反方向使用 forEachFeatureIntersectingExtent 来查看这些点是否与多边形的几何相交。

    var candidates = [];
    source.forEachFeatureIntersectingExtent(myPolygon.getGeometry().getExtent(),function(feature){
        if (feature.getGeometry().get('type') == 'Point') {
            candidates.push(feature);
        }
    });
    
    var selected = [];
    candidates.forEach(function(candidate){
        source.forEachFeatureIntersectingExtent(candidate.getGeometry().getExtent(),function(feature){
            if (feature === myPolygon) {
                selected.push(candidate);
            }
        });
    });
    

    对于单个坐标点,我认为可以一步完成:

    var selected = [];
    source.forEachFeatureIntersectingExtent(myPolygon.getGeometry().getExtent(),function(feature){
        if (feature.getGeometry().get('type') == 'Point' &&
            myPolygon.getGeometry().intersectsCoordinate(feature.getGeometry().get('coordinates')) {
                candidates.push(selected);
        }
    });
    

    关于像这样划分为单元格,将为包含多边形的 10x10 网格的每个单元格生成 pinpush 点。如果只有部分单元格与多边形相交,则单元格中心的针脚可能位于几何图形之外。

    var extent = myPolygon.getGeometry().getExtent();
    for (var i=extent[0]; i<extent[2]; i+=(extent[2]-extent[0])/10) {
        for (var j=extent[1]; j<extent[3]; j+=(extent[3]-extent[1])/10) {
            var cellExtent = [i,j,i+(extent[2]-extent[0])/10),j+(extent[3]-extent[1])/10];
            source.forEachFeatureIntersectingExtent(cellExtent,function(feature){
                if (feature === myPolygon) {
                   var pinPush = new ol.feature(new ol.geom.Point(ol.extent.getCenter(cellExtent)));
                   source.addFeature(pinPush); 
                }
            });
        }
    }
    

    【讨论】:

    • 嗨,迈克,感谢您的回复。基本上我想绘制一个点网格,或者你可以说我想将多边形内的区域划分为行和列,以便每个单元格都包含一个图钉。你知道如何在 openlayers 4.x 中做什么吗?我认为您的代码示例接近我的要求。谢谢。
    • 嗨@Mike,我对这一行的来源感到困惑:source.forEachFeatureIntersectingExtent() 因为我有openlayers 地图并且地图上只有一个多边形。那么在这种情况下,来源是什么?并且您的嵌套循环似乎无法正常工作,出现一些错误。您能否在 jsfiddle 上添加您的代码。
    • source 是用于包含多边形的矢量图层的源,即layer.getSource() 这不是一个工作示例,我现在已经纠正了两个输入错误。跨度>
    • 如果网格是 x 宽和 y 高,则宽度增量为 (extent[2]-extent[0])/x,高度增量为 (extent[3]-extent[1])/y
    • 您可以轻松地将每个 pinpush 替换为覆盖其整个网格单元的东西 gridPush = new ol.feature(ol.geom.Polygon.fromExtent(cellExtent));但其中一些可能仅部分位于您的多边形内。
    【解决方案2】:

    我经常在 openlayers 旁边包含 turf.js 库,专门用于此类任务。我的大部分几何图形都是 geojson 中的 nativley,因此 turf.js 非常适合。如果你有一个 geojson FeatureCollection。您可以迭代 .features 数组(甚至只是任何[x, y] 点数组)并检查每个数组以查看它是否在您的多边形内。如果有帮助,我可以制作一个有效的小提琴。

    // In this example I'm looking for all features
    // that have AT LEAST ONE point within  
    // the world extent (WGS84)
    const polygon = turf.bboxPolygon([-180, -90, 180, 90])
    myGeoJson.features.forEach(function(feature){
        const points = turf.explode(feature);
        const pointsWithin = turf.pointsWithinPolygon(points, polygon);
        if(pointsWithin.features && pointsWithin.features.length){
            // feature has AT LEAST ONE point inside the polygon
            // I can see what points by iterating the
            // pointsWithin.features array
        }else{
            // feature has ZERO points inside the polgyon
        }
    });
    

    http://turfjs.org/docs#pointsWithinPolygon

    【讨论】:

      【解决方案3】:

      vectorSource.forEachFeatureIntersectingExtent()polygonGeom.getExtent() 一起使用。这将为您提供其中大多数的范围快捷方式。之后,您将需要自己实现点内多边形(网上有很多资源)或使用像https://github.com/bjornharrtell/jsts 这样的库。 OpenLayers 仅提供几何与范围的交集。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-11-30
        • 1970-01-01
        • 2020-06-17
        • 2011-04-19
        • 1970-01-01
        • 1970-01-01
        • 2013-10-03
        相关资源
        最近更新 更多