如何在 OpenLayers 5.3.0 中裁剪和显示裁剪的矢量几何图形

How to clip and show clipped vector geometry in OpenLayers 5.3.0

我必须根据 main/restriction 矢量图层裁剪矢量图层。绘制时,如果绘制的部分图层为限制层外,则裁剪限制层外的区域。

示例 1。 我们可以看到,底部边框的一部分在限制之外(紫色层)

我想知道是否可以将特征添加到限制层外的剪辑区域

示例2.去除限制层外的区域(绿色)

我尝试使用 (Turf.js) 库中的 bboxPolygon 函数计算几何。甚至尝试使用 booleanWithin 来检测绘制的多边形是否在限制层之外,但无济于事。

现在,我想知道是否可以在 addfeature 渲染限制层内的剪辑区域时剪辑区域(如示例 2 所示)

这是我用来以某种方式检测区域是否被剪裁或是否在限制层内的代码片段。

// detect if drawn layers is outside restriction layer
vectorDrawLayer.getSource().on('addfeature', (evt) => {
  let feature = evt.feature;
  //let coordinatess = feature.getGeometry().clone().transform('EPSG:3857', 'EPSG:4326').getCoordinates();

  let geojsonFormat = new GeoJSON();

  let firstGeometryObject = {};
  let secondGeometryObject = {};

  if (vectorDrawLayer.getSource().getState() === 'ready') {
    let firstGeometry = vectorDrawLayer.getSource().getFeatures()[0];
    let secondGeometry = restrictionLayer.getSource().getFeatures()[0];

    firstGeometryObject = geojsonFormat.writeFeatureObject(firstGeometry, { dataProjection: 'EPSG:4326', featureProjection: 'EPSG:3857' });

    secondGeometryObject = geojsonFormat.writeFeatureObject(secondGeometry, { dataProjection: 'EPSG:4326', featureProjection: 'EPSG:3857' });
  }

  let isWithin = booleanWithin(firstGeometryObject, secondGeometryObject)
  let clipped = bBoxclip(firstGeometryObject, transformedExtent);

  //console.log(JSON.stringify(firstGeometryObject))
  console.log(clipped.geometry.coordinates);
  console.log(isWithin);
})

更新:

我能够使用 http://turfjs.org/docs/#intersect 仅提取限制层内的多边形,但现在我在仅渲染相交层时遇到问题。

最初,我想删除图层源,然后在获得相交多边形坐标后我想添加新的相交多边形几何体(没有外部区域)但我无法渲染任何东西(似乎我遗漏了什么)

这是代码片段:

  let geojsonFormat = new GeoJSON();
  let firstGeometryObject = {};
  let secondGeometryObject = {};
  let feature;
  if (vectorDrawLayer.getSource().getState() === 'ready') {

    let firstGeometry = vectorDrawLayer.getSource().getFeatures()[0];
    let secondGeometry = restrictionLayer.getSource().getFeatures()[0];


    firstGeometryObject = geojsonFormat.writeFeatureObject(firstGeometry, { dataProjection: 'EPSG:4326', featureProjection: 'EPSG:3857' });

    secondGeometryObject = geojsonFormat.writeFeatureObject(secondGeometry, { dataProjection: 'EPSG:4326', featureProjection: 'EPSG:3857' });

    let intersectTest = intersect(firstGeometryObject, secondGeometryObject);

    let polygon = new Polygon(intersectTest.geometry.coordinates)

    feature = new Feature({
      geometry: polygon,
      name: 'test BlaBla'
    });
    console.log(feature)
    vectorDrawSource.removeFeature(firstGeometry);
    vectorDrawSource.addFeatures(feature);
  }

这里是 link 测试应用程序(更新): https://stackblitz.com/edit/js-vpdnbf

如果您的目标是在多边形内渲染要素,您可以在图层上使用裁剪过滤器。请参阅此示例 https://viglino.github.io/ol-ext/examples/filter/map.filter.crop.html

好的,我知道了。使用 intersect 函数,我通过将转换后的 GeoJSON 对象传递给函数来获得相交的特征对象。

示例:

let secondGeometryObject = secondGeometryObject = geojsonFormat.writeFeatureObject(secondGeometry, { dataProjection: 'EPSG:4326', featureProjection: 'EPSG:3857' });

然后使用相交特征对象的坐标创建新的多边形。

let intersectPolygon = intersect(firstGeometryObject, secondGeometryObject);

let polygon = new Polygon(intersectPolygon.geometry.coordinates)

然后,我转换多边形并获取转换后的坐标 ('EPSG:4326')

let transformedPoly = polygon.clone().transform('EPSG:4326', 'EPSG:3857').getCoordinates();

最后将新获取的坐标设置到事件特征中

evt.feature.getGeometry().setCoordinates(transformedPoly);

或者,我们可以这样使用:

// transforming polygon to epsg:3857
let transformedPoly = polygon.clone().transform('EPSG:4326', 'EPSG:3857');
// set newly transformed polygon to feature
evt.feature.setGeometry(transformedPoly);

这里是固定的解决方案:

如果有人对如何裁剪和获取坐标有更好的想法,请随时 post 回答,我一定会投票 :) 希望对某人有所帮助:)

已更新 link 应用程序位于此处: https://stackblitz.com/edit/js-vpdnbf