JS和Svelte:错误将JS转换为Geojson-解析故障意外令牌(1:7)
我有CSV文件,看起来像 geojson需要从mapbox 写下以下脚本,可以从我拥有的数据中产生类似的文件:
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));
这将转换我的文件并产生这个geojson 这似乎是正确的结果。当我尝试在jsfiddle上运行它时,例如在这里,一切正常。但是,当我将同一代码复制到我的Svelte网站上时,然后像这样运行:
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
我会收到以下错误:
500
Parse failure: Unexpected token (1:7)
Contents of line 1: [the entire contents of the file follow]
这是什么意思?我的文件有什么问题,为什么它在JSFiddle上运行良好,而在我的实际网站上却没有?我该如何解决?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我假设如何/何时调用
load
是有问题的,因为它在 repl 使用 (不确定是否明智地公开访问权限?/a>像这样的jsfiddle代码,并给出相同的结果 /svelte.dev/tutorial/context-api“ rel =“ nofollow noreferrer”> context-api 使用
mapbox-gl
mapboxgl.js
map.svelte.svelte.svelte.svelte
I'd assume that there's something wrong with how/when you call
load
because it works in a REPL using an action like this with the code from your JSFiddle and giving the same result (not sure if it's wise to make the accessToken public? If not let me know and I delete the REPL)The official tutorial context-api section using
mapbox-gl
mapboxgl.js
Map.svelte