Search code examples
leafletpapaparse

Papa Parse - array to markers


This should be a really quick and easy one. Sorry if there is a simple solution. I've read through the documentation but can't figure it out.

I've parsed a CSV using papa-parse.

enter image description here

 var lyrHouses = Papa.parse('src/Houses.csv', {
                header: true,
                download: true,
                dynamicTyping: true,
                skipEmptyLines: true,
                complete: function(results) {
                    console.log(results.data);
                }
            });

I would like to manipulate the array further.

How do I..

  1. Create markers from the array.
  2. Perform further analysis via Turf on said array. ie. I know how to use Turf but do I need to transform the array to L.geoJson?

I thought this would be easy but after an hour, I can't figure out how to use the array properly.


Solution

  • Iterate through your results and create GeoJSON features/markers from your array.

     housesGeoJSON = {
      "type": "FeatureCollection",
      "features": [ ]
    }
    
    var lyrHouses = Papa.parse('src/Houses.csv', {
                    header: true,
                    download: true,
                    dynamicTyping: true,
                    skipEmptyLines: true,
                    complete: function(results) {
                        results.data.forEach((house) => {
                            feature = {
                                "type": "Feature",
                                "geometry": {
                                  "type": "Point",
                                  "coordinates": [house.Longitude, house.Latitude]
                                },
                                "properties": {
                                  "Location": house.Location
                                }
                              }
                              marker = L.geoJSON(feature).addTo(map)
                              // Create geojson of all markers push feature to the declared houses geoJSON
                              housesGeoJSON.features.push(feature)
                        })
                    }
                });