I have CSV file which looks like this. I need to convert it into a GeoJSON format to visualize it with Mapbox. I used this reference of what a geojson needs to look like from Mapbox and wrote the following script that could produce a similar file from the data I had:
const fs = require('fs');
const CWD = process.cwd();
const { parse } = require('csv/sync');
const inPath = `${CWD}/src/data/`;
const outPath = `${CWD}/src/data/`;
// Read file
const csv = fs.readFileSync(`${inPath}organizations.csv`, 'utf8');
const json = parse(csv, { columns: true });
const jsonWithId = json.map((el, i) => ({ ...el, id: i + 1 }));
const features = jsonWithId.map(el => ({
type: 'Feature',
properties: el,
geometry: {
type: 'Point',
coordinates: [el.scatterLong, el.scatterLat ]
}
}));
// Create the feature collection
const featureCollection = {
type: 'FeatureCollection',
crs: {
type: 'name',
properties: {
name: 'urn:ogc:def:crs:OGC:1.3:CRS84'
},
},
features: features
};
// Write the GEOJSON file
fs.writeFileSync(`${outPath}organizations.geojson`, JSON.stringify(featureCollection));
This converts my file and produces this geojson which seems to be the correct result. When I try to run it on JSFiddle like here, everything works perfectly. However when I copy the same code into my Svelte website and run it like so:
import { mapbox, key } from './mapbox.js';
import { setContext } from 'svelte';
import geoData from "$data/organizations.geojson"
setContext(key, {
getMap: () => map,
});
// Function load to create a new map and add to div
const load = () => {
map = new mapbox.Map({
container: container,
style: 'mapbox://styles/username/cl4ktlt35001d16mim8rtqh8i',
center: [-103.5917, 40.6699],
zoom: 3
});
map.on('load', () => {
map.addSource('earthquakes', {
type: 'geojson',
data: geoData,
cluster: true,
clusterMaxZoom: 14, // Max zoom to cluster points on
clusterRadius: 50 // Radius of each cluster when clustering points (defaults to 50)
});
// Rest of the code
I get the following error:
500
Parse failure: Unexpected token (1:7)
Contents of line 1: [the entire contents of the file follow]
What does this mean? What is wrong with my file, why does it run fine on JSFiddle but not on my actual website? And how can I fix this?