I am very new to creating webmaps with Leaflet and I am trying to save my shapes I draw into my postgis db. I would prefer to be saved as .shp or any default format (i.e. json) with its geometry field. I seek a step-by-step code like a small demo.

the script till now:

var drawnItems = new L.FeatureGroup();

    map.on('draw:created', function (e) {
        var type = e.layerType,
        layer = e.layer;

    polygon_options = {
        allowIntersection: false,
                drawError: {
                    color: '#b00b00',
                    timeout: 1000
                showArea: true,
                metric: false,
                repeatMode: false,
        shapeOptions: {
            stroke: true,
            color: '#6e83f0',
            weight: 4,
            opacity: 0.5,
            fill: true,
            fillColor: null,
            fillOpacity: 0.2,
            clickable: true

    function drawPolygon(){
            var polygonDrawer = new L.Draw.Polygon(map, polygon_options);     
  • Could anyone help with this issue? – Dimitris Floyd Jul 26 '17 at 16:07

So you could use draw:created to capture the layer, convert it to geojson then stringify it to save in your database. I've only done this once and it was dirty but worked.

map.on('draw:created', function (e) {
  var type = e.layerType;
  var layer = e.layer;

  var shape = layer.toGeoJSON()
  var shape_for_db = JSON.stringify(shape);

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.