[GIS] Displaying data from csv (or other file format) onto GeoJSON popup

geojsonleaflet

I'm trying to display data from another file such as a csv into a GeoJSON popup window. So far I know I can match the data using a nested for loop with a common id between the two files, but I'm wondering if it's even possible to display this data in the GeoJSON popup window. This is all done with a leaflet map as well.

My reason for displaying data from another file is that it needs to be updated frequently which is easier to do on a csv.

Best Answer

Short answer: yes.

Better answer: I think you should get your process that's writing the CSV to write JSON, if not GeoJSON. That will be easier.

If you do keep your attributes and GeoJSON separate, I'd look at putting the loading of the CSV and attributes in an 'oneachfeature' function, like in this example:

function onEachFeature(feature, layer) {
    // does this feature have a property named popupContent?
    if (feature.properties && feature.properties.popupContent) {
        layer.bindPopup(feature.properties.popupContent);
    }
}

var geojsonFeature = {
    "type": "Feature",
    "properties": {
        "name": "Coors Field",
        "amenity": "Baseball Stadium",
        "popupContent": "This is where the Rockies play!"
    },
    "geometry": {
        "type": "Point",
        "coordinates": [-104.99404, 39.75621]
    }
};

L.geoJson(geojsonFeature, {
    onEachFeature: onEachFeature
}).addTo(map);
Related Question