Leaflet.Editable icon indicating copy to clipboard operation
Leaflet.Editable copied to clipboard

Invalid geometries are not removed with stopDrawing()

Open hyperknot opened this issue 9 years ago • 4 comments

At the moment, if the user starts to draw a geometry, and stops while it's not complete, by triggering stopDrawing(), the incomplete feature is saved to the feature layer, no matter if it is valid or invalid. (By invalid I mean having MIN_VERTEX vertices.)

I believe endDrawing() should be removing it, but somehow with stopDrawing(), endDrawing() isn't called.

hyperknot avatar Jan 11 '16 13:01 hyperknot

The following events are triggered when I call editTools.stopDrawing() with a 2 vertex polygon:

  • editable:drawing:cancel
  • editable:shape:delete
  • editable:editing
  • editable:shape:deleted
  • editable:drawing:end

hyperknot avatar Jan 11 '16 13:01 hyperknot

endDrawing only deletes the current drawn shape if invalid, but does not take care of removing the feature from the map. Maybe this can be added in case the feature is without any shape after that, but I'm not even sure it's to be added as default behaviour. We may think at some use cases where someone would want to do something else in such case. I need to think a bit more on that. One way to solve the issue for you is to listen for editable:drawing:cancel event, and control here the state of the feature.

yohanboniface avatar Jan 11 '16 13:01 yohanboniface

So far the best solution I found is to check for e.layer._defaultShape() in editable:drawing:end, as by this time this will be an empty array if the shape was invalid.

m.Lmap.editTools.on('editable:drawing:end', function(e) {
    if (!e.layer._defaultShape().length) {
        console.log('removing layer');
        featuregroup.removeLayer(e.layer);
        return;
    }

    // do rest of processing
});

hyperknot avatar Jan 11 '16 14:01 hyperknot

I just found that Measurable has pretty much the exact same solution implemented: https://github.com/umap-project/Leaflet.Measurable/blob/52d56e758e34e2761be4c8c878abe042eed37c62/Leaflet.Measurable.js#L116

hyperknot avatar Jan 15 '16 02:01 hyperknot