0

The following code allows for loading local shapefiles, GeoJSON, KML and GPX, but items properties are not the same. Only shapefiles will return its properties with a bindpopup of global properties.

// base leaflet

var map = L.map('map').setView([51.505, -0.09], 13);

L.tileLayer('https://tile.openstreetmap.org/{z}/{x}/{y}.png', {
    maxZoom: 19,
    attribution: '&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>'
}).addTo(map);

// declarations for handling resulting data

var geo2 = L.featureGroup({}, {
        onEachFeature: function popUp(f, l) {
            var out = [];
            if (f.properties){
            for(var key in f.properties){
            out.push(key + ": " + f.properties[key]);
            }
            l.bindPopup(out.join("<br />"));
            }
        }
    })
    // .bindPopup('Hello world!')
    // .on('click', function() { alert('Clicked on a group!'); })
    .addTo(map);

var geo = L.geoJson({
    features: []
}, {
    // onEachFeature: function popUp(f, l) {
    //   var out = [];
    //    if (f.properties){
    //    for(var key in f.properties){
    //      out.push(key + ": " + f.properties[key]);
    //    }
    //    l.bindPopup(out.join("<br />"));
    //  }
    // }
});

var geojsono;
var loadedfile;

// hidden file input

let filesender = document.createElement('input');
filesender.setAttribute("type", "file");
filesender.setAttribute("id", "filesender");
filesender.setAttribute("style", "display:none");
filesender.setAttribute("accept", ".zip, .geojson, .json, .gpx, .kml, application/gpx+xml,application/gpx,application/json,application/vnd.google-earth.kml+xml,application/vnd.google-earth.kml");

document.getElementById('map').appendChild(filesender);

 var btn_loadfile = L.easyButton('fa-folder', function(btn, map) {
    function_loadfile();
  }).addTo(map);

  function loadfile() {
    document.getElementById('filesender').click();
    filesender.onchange = () => {
        let file = filesender.files[0],
            name = file.name,
            ext = name.split('.').pop(),
            filename = name.substr(0, name.lastIndexOf('.')) || name

        if (ext != 'zip') { //gpx, kml, geojson
            reader = new FileReader();
            reader.readAsText(file);
            reader.onload = L.Util.bind((f) => {
                let data = f.target.result,
                    geojsono = ['gpx', 'kml'].indexOf(ext) !== -1 ? toGeoJSON[ext](new DOMParser().parseFromString(data, "text/xml")) : JSON.parse(data);
                loadedfile = L.geoJSON(geojsono);
                geo2.addLayer(loadedfile);
            })
        } else {
            var data;
            reader = new FileReader();
            reader.readAsArrayBuffer(file);
            reader.onload = L.Util.bind((f) => {
                data = f.target.result,
                    geojsono = ['zip'].indexOf(ext) !== -1 ? shp(data) : JSON.parse(data);
                //  geo.addData(geojsono.outcome);
            });
            reader.onloadend = L.Util.bind(() => {
                geo.addData(geojsono.outcome);
                geo2.addLayer(geo);

            });
        };
    };
  };

I would like that all other file type (GeoJSON, KML, GPX) behave the same as shapefile or the contrary; that at the end they have the same contents at the same place - so I can do aditional stuff: exports, and showing the bindPopup.

The name attribute for a shapefile in plain text is found in geo2._layers[651]._layers[650].feature.properties.name

and for a geojson and presumably the others: geo2._layers[631]._layers[632].feature.properties.name

I can't see a difference, but nor a result.

4
  • What about any feedback to answer of your previous question: gis.stackexchange.com/questions/467448/…?
    – TomazicM
    Nov 1, 2023 at 9:10
  • @TomazicM sorry I felt lost in the big amount of stuff that I have to fix,but I'll be looking at that right away...
    – Vincent Dc
    Nov 1, 2023 at 10:58
  • I'm still dealing with polygons and watching this too today
    – Vincent Dc
    Nov 1, 2023 at 11:07
  • 2
    Without samples from your KML, GPX and GeoJSON files and code for your convert procedure toGeoJSON it's impossible to say why it does not work for you. Please edit your question and add missing info.
    – TomazicM
    Nov 1, 2023 at 13:51

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.