【问题标题】:How to clip and show clipped vector geometry in OpenLayers 5.3.0如何在 OpenLayers 5.3.0 中剪裁和显示剪裁的矢量几何图形
【发布时间】:2019-08-12 13:31:48
【问题描述】:

我必须根据主/限制矢量图层来裁剪矢量图层。绘制时,如果绘制图层的某些部分在限制层之外,则剪裁在限制层之外的区域。

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

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

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

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

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

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

// 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 仅提取限制层内的多边形,但现在我在仅渲染相交层时遇到问题。

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

这里是代码sn-p:

  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);
  }

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

【问题讨论】:

    标签: openlayers openlayers-5 turfjs


    【解决方案1】:

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

    【讨论】:

    • 这也适用于矢量图层吗?除了裁剪之外,我还需要裁剪特征几何。尽管如此,还是感谢您的时间和精力,但不幸的是,我相信这个答案不能解决我的问题:)
    【解决方案2】:

    好的,我想通了。使用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);
    

    这里是固定的解决方案:

    如果有人对如何剪辑和获取坐标有更好的想法,请随时发布答案,我一定会赞成 :) 希望它可以帮助某人:)

    更新的应用链接在这里: https://stackblitz.com/edit/js-vpdnbf

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-18
      • 2015-02-02
      • 2013-10-05
      相关资源
      最近更新 更多