Search code examples
javascriptdictionaryleafletgeojson

Export leaflet map to geojson


Is it possible to export geojson from leaflet to save the map state?

I want to store the markers, zoom & map center to load it later.

There is plenty of ways to load geojson on leaflet, but I can't figure out any option to export the map to geojson...


Solution

  • There's no "out-of-the-box" option to export all the markers on the map to GeoJSON but it's something you can do easily do yourself. Leaflet's L.Marker has a toGeoJSON method:

    Returns a GeoJSON representation of the marker (GeoJSON Point Feature).

    http://leafletjs.com/reference.html#marker-togeojson

    For example:

    // Create a marker
    var marker = new L.Marker([0, 0]);
    // Get the GeoJSON object
    var geojson = marker.toGeoJSON();
    // Log to console
    console.log(geojson);
    

    Will output to your console:

    {
        "type":"Feature",
        "properties":{},
        "geometry":{
            "type":"Point",
            "coordinates":[0,0]
        }
    }
    

    If you want to store all the markers added to your map in a GeoJSON collection, you could do something like this:

    // Adding some markers to the map
    var markerA = new L.Marker([0, 0]).addTo(map),
        markerB = new L.Marker([1, 1]).addTo(map),
        markerC = new L.Marker([2, 2]).addTo(map),
        markerD = new L.Marker([3, 3]).addTo(map);
    
    // Create an empty GeoJSON collection
    var collection = {
        "type": "FeatureCollection",
        "features": []
    };
    
    // Iterate the layers of the map
    map.eachLayer(function (layer) {
        // Check if layer is a marker
        if (layer instanceof L.Marker) {
            // Create GeoJSON object from marker
            var geojson = layer.toGeoJSON();
            // Push GeoJSON object to collection
            collection.features.push(geojson);
        }
    });
    
    // Log GeoJSON collection to console
    console.log(collection);
    

    Will output to your console:

    {
        "type":"FeatureCollection",
        "features":[{
            "type":"Feature",
            "properties":{},
            "geometry":{
                "type":"Point",
                "coordinates":[0,0]
            }
        },{
            "type":"Feature",
            "properties":{},
            "geometry":{
                "type":"Point",
                "coordinates":[1,1]
            }
        },{
            "type":"Feature",
            "properties":{},
            "geometry":{
                "type":"Point",
                "coordinates":[2,2]
            }
        },{
            "type":"Feature",
            "properties":{},
            "geometry":{
                "type":"Point",
                "coordinates":[3,3]
            }
        }]
    }
    

    Edit: However, as the QP found out, if you're able to put your markers into a L.LayerGroup, L.FeatureGroup or L.GeoJSON layer you can just use it's toGeoJSON method which returns a GeoJSON featurecollection:

    Returns a GeoJSON representation of the layer group (GeoJSON FeatureCollection).

    http://leafletjs.com/reference.html#layergroup-togeojson

    If you want to store the map's current bounds (center and zoom) you could simply add it to the collection doing this:

    var bounds = map.getBounds();
    
    var collection = {
        "type": "FeatureCollection",
        "bbox": [[
            bounds.getSouthWest().lng,
            bounds.getSouthWest().lat
        ], [
            bounds.getNorthEast().lng,
            bounds.getNorthEast().lat
        ]],
        "features": []
    };
    

    You can later restore it by using the bbox member in conjunction with L.Map's setBounds method. That's it. You could send it to the server or download it via dataurl whatever you like. Hope that helps, good luck.