1

To modify line geometry on OpenLayers(3-5), we use ol.interaction.Modify. What I want to do is, enable modify any points on line, except start / end points.

But just using ol.interaction.Modify normally, we can't prevent modifying start / end points. How to prevent it?

3

It is possible using more recent versions of OpenLayers. This will work in version 4.6.5

  var pixelTolerance = 10;
  var modify = new ol.interaction.Modify({
    source: vector.getSource(),
    pixelTolerance: pixelTolerance,
    condition: function(e){
      var f = this.getMap().getFeaturesAtPixel(e.pixel,{
        hitTolerance: pixelTolerance,
        layerFilter: function(candidate) {
          return candidate === vector;
        }
      });
      if (f && f[0].getGeometry().getType() == 'LineString') {
        var coordinates = f[0].getGeometry().getCoordinates();
        var p0 = e.pixel;
        var p1 = this.getMap().getPixelFromCoordinate(coordinates[0]);
        var dx = p0[0]-p1[0];
        var dy = p0[1]-p1[1];
        if (Math.sqrt(dx*dx+dy*dy) <= pixelTolerance) {
          return false;
        }
        var p1 = this.getMap().getPixelFromCoordinate(coordinates.slice(-1)[0]);
        var dx = p0[0]-p1[0];
        var dy = p0[1]-p1[1];
        if (Math.sqrt(dx*dx+dy*dy) <= pixelTolerance) {
          return false;
        }
      }
      return true;
    }
  });
  • Thank you! I'll try.. – kochizufan Jun 17 at 22:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.