Leaflet-Geoman – Checking Validity of Polygon Drawn with Leaflet-Geoman Plugin

invalid-layerleafletleaflet-plugins

We are using Leaflet.js to allow our users to draw polygons and save them to their GIS server. We do this by converting the geometry from Leaflet into WFS format and making an Insert Transaction via WFS.

We are having problems where users are drawing polygons that intersect themselves and saving them. These are then showing up as invalid in PostGIS.

Is there any way to check for validity in JavaScript before we insert them?

We are using Leaflet.Geoman for drawing:

map.on('pm:create', function (e) {
  var layerToDraw = e.layer;            
  //want to check here whether the layerToDraw is valid
  features.addLayer(layerToDraw);
});

Best Answer

If you look at the docs for Leaflet-Geoman at https://github.com/geoman-io/leaflet-geoman#draw-mode you'll see there is allowSelfIntersection option that can prevent drawing kinked polygons.

All you have to do is to set a global option:

map.pm.setGlobalOptions({
  allowSelfIntersection: false
});