javascript - 多边形选择 OpenLayers 3

标签 javascript selection openlayers-3

如何使用多边形绘制来选择要素?可以根据示例使用方框选择来完成。

我想知道是否有一种方法可以在创建多边形后触发一个事件来检查与它和其他要素的交叉点。就我而言,我正在 try catch 数据点。

    var select = new ol.interaction.Select();
    map.addInteraction(select);

    var selectedFeatures = select.getFeatures();

    // a DragBox interaction used to select features by drawing boxes
    var dragBox = new ol.interaction.DragBox({
        condition: ol.events.condition.platformModifierKeyOnly
    });

    map.addInteraction(dragBox);

    var infoBox = document.getElementById('info');

    dragBox.on('boxend', function() {
        // features that intersect the box are added to the collection of
        // selected features, and their names are displayed in the "info"
        // div
        var info = [];
        var extent = dragBox.getGeometry().getExtent();
        vectorSource.forEachFeatureIntersectingExtent(extent, function(feature) {
            selectedFeatures.push(feature);
            info.push(feature.get('name'));
        });
        if (info.length > 0) {
            infoBox.innerHTML = info.join(', ');
        }
    });

    // clear selection when drawing a new box and when clicking on the map
    dragBox.on('boxstart', function() {
        selectedFeatures.clear();
        infoBox.innerHTML = ' ';
    });
    map.on('click', function() {
        selectedFeatures.clear();
        infoBox.innerHTML = ' ';
    });

这在开放层 3 中可能吗?

最佳答案

对于此类操作,您需要使用 JSTS 拓扑库或 TURF.js库。 在我个人看来,JSTS 是更完整和优雅的解决方案。获取一些信息 here .目前,作者正在进行更改,即将发布官方 ol3 兼容版本,请随时了解情况。

我会给你一个使用旧版本 JSTS 的例子来完成这项工作。 (检查提供的 fiddle 的外部来源以验证您需要加载的 JSTS lib 文件)。如果您有时间检查是否有最新 JSTS 版本的新链接,如果您有任何消息请告诉我们。

逻辑是这样的:

  1. 创建 3 个矢量图层。一个用于您要查询的图层,一个用于放置您的自由手绘图,另一个用于放置您的亮点。

  2. 创建绘图交互并在其上附加“drawend”事件。

  3. 使用 JSTS 查找与数字化几何相交的几何。

这是您的代码和一个 fiddle让您的生活更轻松。

var waterAreasVecSource = new ol.source.Vector({
    loader: function (extent) {
        $.ajax('http://demo.opengeo.org/geoserver/wfs', {
            type: 'GET',
            data: {
                service: 'WFS',
                version: '1.1.0',
                request: 'GetFeature',
                typename: 'water_areas',
                srsname: 'EPSG:3857',
                bbox: extent.join(',') + ',EPSG:3857'
            }
        }).done(loadFeatures)
            .fail(function () {
        alert("fail loading layer!!!")
        });
    },
    strategy: ol.loadingstrategy.bbox
});

function loadFeatures(response) {
    formatWFS = new ol.format.WFS(),
    waterAreasVecSource.addFeatures(formatWFS.readFeatures(response));
}

var waterAreasVector = new ol.layer.Vector({
    source: waterAreasVecSource,
    style: new ol.style.Style({
        stroke: new ol.style.Stroke({
            color: 'rgba(0, 0, 255, 1.0)',
            width: 2
        })
    })
});
var raster = new ol.layer.Tile({
  source: new ol.source.OSM({})
});

var myDrawSource = new ol.source.Vector({wrapX: false});

var myDrawVector = new ol.layer.Vector({
  source: myDrawSource,
  style: new ol.style.Style({
    fill: new ol.style.Fill({
      color: 'rgba(255, 255, 255, 0.5)'
    }),
    stroke: new ol.style.Stroke({
      color: '#ffcc33',
      width: 2
    }),
    image: new ol.style.Circle({
      radius: 7,
      fill: new ol.style.Fill({
        color: '#ffcc33'
      })
    })
  })
});

var mySelectionsSource = new ol.source.Vector({wrapX: false});

var mySelectionsVector = new ol.layer.Vector({
  source: mySelectionsSource,
  style: new ol.style.Style({
    fill: new ol.style.Fill({
      color: 'rgba(255, 0, 0, 0.5)'
    }),
    stroke: new ol.style.Stroke({
      color: 'rgba(255, 0, 0, 1)',
      width: 2
    }),
    image: new ol.style.Circle({
      radius: 7,
      fill: new ol.style.Fill({
        color: '#ffcc33'
      })
    })
  })
});

var map = new ol.Map({
  layers: [raster, myDrawVector,waterAreasVector,mySelectionsVector],
  target: 'map',
  view: new ol.View({
    center: [-8908887.277395891, 5381918.072437216],
    maxZoom: 19,
    zoom: 12
  })
});


var  draw = new ol.interaction.Draw({
      source: myDrawSource,
      type: "Polygon",
    });

map.addInteraction(draw);

draw.on('drawend',function(e){
myDrawSource.clear();
mySelectionsSource.clear();
var extent = e.feature.getGeometry().getExtent();
var geomA = e.feature.getGeometry();
waterAreasVecSource.forEachFeatureInExtent(extent,function(aa){
console.log("forEachFeatureInExtent",aa.getGeometry());
if (polyIntersectsPoly(geomA,aa.getGeometry()) === true){
mySelectionsSource.addFeature(aa);
}
});
});



/**
* check whether the supplied polygons have any spatial interaction
* @{ol.geometry.Polygon} polygeomA 
* @{ol.geometry.Polygon} polygeomB 
* @returns {Boolean} true||false
*/
function polyIntersectsPoly(polygeomA, polygeomB) {
 var geomA = new jsts.io.GeoJSONReader().read(new ol.format.GeoJSON().writeFeatureObject(
        new ol.Feature({
            geometry: polygeomA
       })
   )
   ).geometry;
var geomB = new jsts.io.GeoJSONReader().read(new ol.format.GeoJSON().writeFeatureObject(
        new ol.Feature({
            geometry: polygeomB
        })
    )
    ).geometry;
return geomA.intersects(geomB);
};

关于javascript - 多边形选择 OpenLayers 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35386689/

相关文章:

javascript - 图像在 osf 层顶部的静态图像层上显示错误

javascript - 如何使用正则表达式删除双空格字符?

javascript - Vuejs - 如何在作用域插槽中的父子之间传递更改

java - Java 中等效的 'nth_element' 函数是什么?

javascript - 选择选择选项时显示选择

Openlayers 3 View setProperties 不填充 map

openlayers-3 - Openlayers 3 map 最初只绘制窗口调整大小

javascript - 一组单选按钮上的 onfocus 事件如何像单个控件一样工作?

javascript - 如何创建一个将参数传递给 REST Web 服务的 Controller

function - Excel 2016 : How to select function from "suggested" dropdown list *by keyboard*?