Leaflet Geometry Management
A Leaflet Plugin For Creating And Editing Geometry Layers in Leaflet 1.0.
Draw, Edit, Drag, Cut and Snap Features.
In the name "leaflet.pm" the "pm" stands for Polygon Management. At the time, this plugin only supported polygons. Now you can edit Markers, Polylines, Polygons, Circles, Rectangles, LayerGroups, GeoJSON, MultiPolygons, MultiLineStrings and more are coming.
Demo (click here)
Why another geometry editing plugin?
As leaflet.draw development seemed to came to a halt and I needed support for
leaflet 1.0 I created this plugin myself due to a lack of alternatives.
As we are always using the latest leaflet version in a big production app, I will
(have to) keep this plugin constantly developed.
Getting Started
Install via npm
npm install leaflet.pm --save
Install Manually
Download
leaflet.pm.css
and
leaflet.pm.min.js
and include them in your project.
Include via CDN
CSS
JS
Include as ES6 Module
;;
Include as CommonJS Module
;;
Documentation
Init Leaflet.PM
Just include leaflet.pm.min.js
right after Leaflet. It initializes itself. If
you want certain layers to be ignored by leaflet.pm, pass pmIgnore: true
to
their options when creating them. Example:
L;
Leaflet.PM Toolbar
This plugin comes with an optional toolbar to give you buttons to use the various features.
// define toolbar optionsvar options = position: 'topleft' // toolbar position, options are 'topleft', 'topright', 'bottomleft', 'bottomright' drawMarker: true // adds button to draw markers drawPolyline: true // adds button to draw a polyline drawRectangle: true // adds button to draw a rectangle drawPolygon: true // adds button to draw a polygon drawCircle: true // adds button to draw a cricle cutPolygon: true // adds button to cut a hole in a polygon editMode: true // adds button to toggle edit mode for all layers removalMode: true // adds a button to remove layers; // add leaflet.pm controls to the mapmappm;
If no options are passed, all buttons will be shown.
If you are wondering how e.g. the drawPolygon
button will enable drawing mode
with specific options, here it is: Simply enable drawing mode programatically,
pass it your options and disable it again. The options will persist, even when
the mode is enabled/disabled via the toolbar.
Example:
// make markers not snappable during marker drawmappm;mappm; // let polygons finish their shape on double clickmappm;mappm;
All available options are specified in the Drawing Mode Section below
Drawing Mode
Use Drawing Mode on a map like this
// optional options for line style during draw. These are the defaultsvar options = // snapping snappable: true snapDistance: 20 // self intersection allowSelfIntersection: true // the lines between coordinates/markers templineStyle: color: 'red' // the line from the last marker to the mouse cursor hintlineStyle: color: 'red' dashArray: 5 5 // show a marker at the cursor cursorMarker: false // finish drawing on double click // DEPRECATED: use finishOn: 'dblclick' instead finishOnDoubleClick: false // specify type of layer event to finish the drawn shape // example events: 'mouseout', 'dblclick', 'contextmenu' // List: http://leafletjs.com/reference-1.2.0.html#interactive-layer-click finishOn: 'contextmenu' // custom marker style (only for Marker draw) markerStyle: opacity: 05 draggable: true ; // enable drawing mode for shape - e.g. Poly, Line, etcmappm;mappm;mappm;mappm;mappm; // get array of all available shapesmappmDraw; // listen to when drawing mode gets enabledmap; // disable drawing modemappm; // listen to when drawing mode gets disabledmap; // listen to when a new layer is createdmap; // listen to vertexes being added to the workingLayer (works only on polylines & polygons)map; // listen to the center of a circle being addedmap; // listen to when the center of a circle is movedmap;
Creating Holes or Cutting a Polygon
Enable drawing for the shape "Cut" to draw a polygon that gets subtracted from all underlying polygons. This way you can create holes, cut polygons in half or remove parts of it.
Important: the cutted layer will be replaced, not updated. Listen to the
pm:cut
event to update your layer references in your code. The pm:cut
event
will provide you with the old/removed/cut layer and returns the resulting
layer(s) that is/are added to the map.
// recommended options (used when enabled via toolbar)var options = snappable: false cursorMarker: false ; // enable cuttingmappmDrawCut; // disable cuttingmappmDrawCut; // toggle cuttingmappmDrawCut; // listen to when a specific layer gets cutlayer; // listen to when any layer on the map gets cutmap;
Edit Mode
Use Edit Mode for a layer like this:
var polygonLayer = L; // optional optionsvar options = // makes the layer draggable draggable: true // makes the vertices snappable to other layers // temporarily disable snapping during drag by pressing ALT snappable: true // distance in pixels that needs to be undercut to trigger snapping // default: 30 snapDistance: 30 // self intersection allowed? allowSelfIntersection: true // disable the removal of markers/vertexes via right click preventMarkerRemoval: false // disable the possibility to edit vertexes preventVertexEdit: false; // enable edit modepolygonLayerpm;markerpm; // disable edit modepolygonLayerpm; // toggle edit modepolygonLayerpm; // check if edit mode is enabledpolygonLayerpm; // returns true/false // listen to changespolygonLayer;polygonLayer;polygonLayer;polygonLayer; // listen to when vertexes are being added or removed from the layerpolygonLayer;polygonLayer; // listen to when a marker of a polygon-vertex is being draggedpolygonLayer;polygonLayer; // listen to when snapping occurs// pm:snap and pm:unsnap are, in addition to the layer, also fired on the markers of the polygon// if you'd need it for some advanced behaviourpolygonLayer;polygonLayer; // if allowSelfIntersection is false: listen to when a self-intersection is detected// e.intersection includes a geoJSON of the intersectionpolygonLayer; // toggle global edit mode (edit mode for all layers on the map)mappm; // listen to when global edit mode is toggledmap; // check self intersectionpolygonLayerpm; // true/false
Removal Mode
// toggle global removal modemappm; // listen to removal of layers that are NOT ignored and NOT helpers by leaflet.pmmap;
Customize Style
In order to change the style of the lines during draw, pass these options to the
enableDraw()
function.
// optional options for line style during draw. These are the defaultsvar options = // the lines between coordinates/markers templineStyle: color: 'red' // the line from the last marker to the mouse cursor hintlineStyle: color: 'red' dashArray: 5 5 ; // enable drawing mode for shape - e.g. Poly, Line, Circle, etcmappm;
To customize the style of the drawn layer with leaflet options, you can either
pass the options to enableDraw
:
// optional options for line style during draw. These are the defaultsvar options = templineStyle: {} hintlineStyle: {} pathOptions: // add leaflet options for polylines/polygons color: 'orange' fillColor: 'green' ; // enable drawing mode for shape - e.g. Poly or Linemappm;
or set the options generally:
mappm;
Feature Request
I'm adopting the Issue Management of lodash which means, feature requests get the "Feature Request" Label and then get closed. You can upvote to existing feature requests (or create new ones). Upvotes make me see how much a feature is requested and prioritize their implementation. Please see the existing Feature Requests here and upvote if you want them to be implemented.
Credit
As I never built a leaflet plugin before, I looked heavily into the code of leaflet.draw to find out how to do stuff. So don't be surprised to see some familiar code.
The icons used for the toolbar are CC-BY Glyphicons.
I also took a hard look at the great L.GeometryUtil for some of my helper functions.