TransWikia.com

Remove vertex from geometry (polygon) in OpenLayers 5?

Geographic Information Systems Asked on August 7, 2021

I could able to remove the recent modified vertex of a feature using removePoint method of modify interaction, as well as using deleteCondition event of modify interaction. However, that is not what I am trying to achieve. The usecase for this this problem is as below.

I have modify interaction added on map. Now when user tries to modify a feature over map, I am highlighting the vertices, like this
enter image description here

so that user can directly go to these vertices and then do right click which will result in removal of vertex.

For this, I had to implement the contextmenu to handle the right click over map. But, now the challenge is to remove the received coordinate from the modifying geometry.

I tried to implement it as follows, with simple .splice() method on coordinate array of target geometry

 if(this.modifyInteraction.features_.getLength() && this.modifyInteraction.features_.getArray()[0]){
           //Feature being modified
           let modifyft = this.modifyInteraction.features_.getArray()[0];
           let coord = modifyft.getGeometry().getCoordinates()[0];
           let foundIndex;
           for(var k = 0; k < coord.length; k++){
              if(coord[k][0] == rightClickedVertex.getGeometry().getCoordinates()[0] && 
              coord[k][1] == rightClickedVertex.getGeometry().getCoordinates()[1]){
                 foundIndex = k;
                 break;
              }
           }
           if(foundIndex && foundIndex > -1){
              modifyft.getGeometry().getCoordinates()[0].splice(foundIndex, 1);
              //nothing changes
           }
        }

is there any other way to remove the vertex of geometry being edited?

One Answer

Well, found the workaround quickly, I had to call the setCoordinates() method on the feature geometry I was trying to modify.

So in the above solution I updated just coordinates array and then updated the geometry like below

      if(foundIndex && foundIndex > -1){
        coord.splice(foundIndex, 1);
      }
      // set back
      modifyft.getGeometry().setCoordinates([coord]);

This updates the feature over map by removing the vertex of geometry.

However, I feel this is not a recommended way to alter the geometry as this solution needs to access the features_ array of modifyinteraction which is not recommended.

So if anyone comes up with clean solution then welcome!

Answered by Suraj on August 7, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP