拥有OpenLayers中的多边形的removeLastPoint()函数

普雷斯托

我正在尝试使用removeLastPoint()OpenLayers的功能解决我的应用程序问题问题在于,在移动设备上removeLastPoint()正在按错误的顺序去除点。在桌面模式下,一切正常。

是此问题的解释。

让我在图像上介绍一下这种“以错误的顺序消除点的问题”在实践中是如何工作的。

我们从画一些开始LineString

在此处输入图片说明

现在LineString我们已经从第5点的连接中创建了一个现在,我试图removeLatPoint()通过单击我的调用函数removeButton,就像这样:

removeButton.on('click', () => {
   draw.removeLastPoint(); // this function comes from ol.integration.Draw class
});

现在,我期望得到这样的结果:

在此处输入图片说明

但是我得到了什么?我得到这样的东西:

在此处输入图片说明

在下一步中,我将获得:

在此处输入图片说明

接下来:

在此处输入图片说明

最后,我只会得到第5点。

在此处输入图片说明

问题在于,在OpenLayers removeLastPoint()函数中不是删除最后一个点,而是删除它之前的一个。

这是removeLastPoint()来自的当前功能代码OpenLayers

在此处输入图片说明

问题在于这条线对我来说:coordinates.splice(-2, 1);因为它的工作就像我展示的那样。

在此处输入图片说明

我迫不及待要解决这个问题,所以我写了我的临时解决方案。在我的修复程序中,我在removeLastPoint()自己的函数之前调用,该函数删除了最后一个坐标并复制了它之前的坐标。就像这样:

在此处输入图片说明

There is once difference. I'm using draw.extend(measureFeature); function to duplicate this coordinate before this last, because I have to refresh information which have been saved in private variable this.sketchCoords_ on the OpenLayers site. When I was trying to do it on the other way then this.sketchCoords_ still remembered previously saved state of drawed geometry and instead of call removeLastPoint() for myArray = ['1','2','3','4','4'] then it was calling for myArray = ['1','2','3','4','5']. So I have to use draw.extend() to duplicate current last item and update this.sketchCoords_.

Here is my own fix on this problem:

removeButton.on('click', () => {
    if (this.isMobileDevice) {
        this.removeLastPoint();
    }
    this.draw.removeLastPoint();
}); 

removeLastPoint() {
    let measureFeature = this.sketch;
    measureFeature = measureFeature.clone();
    const geom = measureFeature.getGeometry();

    if (geom instanceof ol.geom.LineString) {
        const coords = geom.getCoordinates();
        let preparedCoords: [number, number][] = new Array();

        if (coords) {
            preparedCoords = coords;
            preparedCoords.splice(-1, 1);
            geom.setCoordinates(preparedCoords);
            measureFeature.setGeometry(geom);

            if (preparedCoords.length > 0) {
                this.draw.extend(measureFeature);
            }
        }
    } else if (geom instanceof ol.geom.Polygon) {

    }
}

My fix is working as I exected. After call removeLastPoint() is removing redundant item and I've got a correct collection.

And here is starting my real problem. Polygons. On polygons removeLastPoint() function doesn't work well too and there we've got this same problem with removing wrong point (not last but before it).

So I'm going to write a similar mechanism to modify polygon coordinates as I wrote for LineString geometries.

But how can I update this.sketchCoords_ by inject my modified coordinates collection if draw.extend() is working only for LineString geometries?

在此处输入图片说明

I haven't idea how can I do it. Class Draw from OpenLayers hasn't any helpful function for polygons which will be change or update current drawing polygon.

Here is full of Draw class.

Have you got any idea how can I update it and inject my modified coordinate collection?

Here is my fiddle, Maybe it'll helpful. I don't know why but on my fiddle sketch is always null so this fiddle is not working well but it able to show my code a little bit.

Presto

Once again I solved the problem which I put by myself into StackOverflow. Awesome!

So let me present the solution.

I said that after modify a polygon I need update this.sketchCoords_ object because without it the removeLastPoint() from OpenLayers will be working on the old coordinates collection.

If draw.extend() is working only for LineString geometries then I had to find another solution how to do it.

And I found it.

The solution was:

  1. writing own class to extend class Draw from OpenLayers from comes this.sketchCoords_ object:

    declare namespace ol { 
       namespace interaction { 
           class ExtendedDrawClass extends ol.interaction.Draw {   
               sketchCoords_?: ol.Coordinate | ol.Coordinate[] | ol.Coordinate[][];
           }
       }
    }
    
  2. and after that just modify this collection in own code. For example just like that:

    var myNewCoordinates = geometry.getCoordinates();
    myNewCoordinates.split(-1,1);
    (<ol.Coordinate[][]>(<ol.interaction.ExtendedDrawClass>this.draw).sketchCoords_) = [myNewCoordinates];
    

And that's all.

当代码从那时开始调用removeLastPoint()函数时,OpenLayers它已经将一个myNewCoordinates集合设置为this.sketchCoords_对象。

我将此解决方案留给有类似问题的人。也许会有所帮助。如果是,请支持我的提问和回答。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在具有多边形的多边形Python中的多边形中打洞

多边形选择OpenLayers 3

C ++:从带有孔的多边形中获取简单多边形的列表

检查点是否在OpenLayers 3中的多边形内

坐标中的多边形

数据框中多变量的多边形函数用法

多边形中的点,简单的4个顶点多边形,没有外部库

如何从openlayers4上的多边形/点的坐标中获取像素

停止在带有openlayers 5.3.0的多边形中添加点

在OpenLayers地图中显示多边形

是否有R函数用于检查指定的GeoJSON对象(多边形或多多边形)是否包含指定点?

检查openlayers中多边形内部的坐标/位置

查找形状文件中的所有相交多边形

MYSQL几何函数多边形

多边形剪裁中的多边形

如何对多边形使用polygonPointTest函数?

在openlayers中,如何从多边形的url中拟合(拉伸)图片?

ggplot中具有孔的多边形的Choropleth贴图

如何将在一行中触摸自己的多边形转换为有效的多边形?

多边形中的点,多个多边形

在graphviz中绘制带有标记顶点的多边形

OpenLayers:绘制多边形和点然后拖动它们

绘制的多边形不显示在 openlayers 中

如何使用 OpenLayers 限制在现有多边形内绘制

Foxall 的 G 函数在 R spatstat 中带有多边形

OpenLayers - 基于多个多边形设置中心和缩放

在 Leaflet 中的多边形中渲染没有孔的重叠多边形

在 openlayers 中绘制多边形后无法获得几何图形

Openlayers - 避免和调整重叠的多边形