Here is a script to turn your csv into a keyed object as suggested in the comments:
function csvJSON(csv){
var lines=csv.split("\n");
var result = {};
var headers=lines[0].split(",");
// start at 1 to skip the header row
for(var i=1;i<lines.length;i++){
var obj = {};
var currentline=lines[i].split(",");
var key = currentline[0];
// start at 1 to skip the first column, which is your key
for(var j=1;j<headers.length;j++){
obj[headers[j]] = currentline[j];
}
result[key] = obj;
}
// return a js object
return result;
}
// read your csv; will likely need a way to get this from a file in your application; just for demonstration here:
var csv = "name,detail1,detail2,detail3,detail4,detail5\nWard 01,23,45,10,54,30\nWard 02,78,98,14,20,53"
// construct the lookup object from the csv
var csvLookup = csvJSON(csv);
With this in place, when constructing your popups you can do a simple lookup by key to get the value(s) of interest:
function onEachFeature(feature, layer) {
// lookup the value from the csv for this polygon feature
// assuming here key is in feature.properties.name = 'Ward01'
var ward = feature.properties.name; // or whatever it really is
var detail = csvLookup[ward]["detail1"];
layer.bindPopup("<p> Detail1 for " + ward + ": " + detail + "</p>");
}
For geoJson you use mixed case as in my JSFiddle example which worked:
http://jsfiddle.net/megalithic/zuz4urb7/
But the CSV plugin lowers the case of all the variables - but by no means obvious so I thought I should follow up with the working code - CSV users be aware!
Working map
http://www.megalithic.co.uk/leaflet_megalith_map.html
working code
var bankias = L.geoCsv(null, {
onEachFeature: function (feature, layer) {
var popup = '';
popup += feature.properties.name +'<br>'+feature.properties.type + '<br>' + feature.properties.colour + '<br>';
if (feature.properties.condition == -1) {popup += 'Condition:Destroyed<br>';}
if (feature.properties.condition > 0) {popup += 'Condition:'+feature.properties.condition +'<br>';}
if (feature.properties.ambience > 0) {popup += 'Ambience:'+feature.properties.ambience +'<br>';}
if (feature.properties.access > 0) {popup += 'Access:'+feature.properties.access +'<br>';}
if (feature.properties.accuracy > 0) {popup += 'Accuracy:'+feature.properties.accuracy +'<br>';}
popup += '<a target="_blank" href="article.php?sid='+feature.properties.sid+'">Link to site</a>';
layer.bindPopup(popup);
},
pointToLayer: function (feature, latlng) {
return L.marker(latlng, {
icon:L.icon({
// modify icon
iconUrl: 'images/mapic/'+feature.properties.icon+'.gif',
iconSize: [20,20],
})
});
},
firstLineTitles: true
});
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: