如何将来自服务器的普通地理json转换为geoJson? [英] How do you convert normal geographic json coming from server into geoJson?
本文介绍了如何将来自服务器的普通地理json转换为geoJson?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一台服务器,其服务器具有标准json格式的地理数据,我需要将其更改为geojson格式,以便Mapbox可以读取它.你如何做到的?
I have a server with geographic data in normal json format, which I need to change into geojson format so Mapbox can read it. How do you do this?
例如,您如何转换它:
[
{
"id": 0,
"name": "Hotel",
"icon": "Sleep",
"address": "SampleStreet 34",
"latitude": 12,
"longitude": 55
}
]
对此:
{
"type": "FeatureCollection",
"features": [
{
"id": 0,
"type": "Feature",
"properties": {
"placeID": 0,
"name": "Hotel",
"icon": "sleep",
"addressFormatted": "SampleStreet 34"
},
"geometry": {
"type": "Point",
"coordinates": [
12,
55
]
}
}
推荐答案
如果您使用ES6,则类似的方法可能对您有用,如果您可以直接使用生成的对象,则可能不需要JSON.stringify
.
If you use ES6 then something like this might work for you, JSON.stringify
might not be needed if you can use the resulting object directly.
const data = [
{
id: "0",
name: "Hotel",
icon: "Sleep",
address: "SampleStreet 34",
latitude: "12",
longitude: "55"
},
{
id: "1",
name: "Landmark",
icon: "Star",
address: "SampleStreet 1234",
latitude: "99",
longitude: "100"
}
];
const geojson = {
type: "FeatureCollection",
features: data.map(item => {
return {
id: item.id,
type: "Feature",
properties: {
placeID: item.id,
name: item.name,
icon: item.icon,
addressFormatted: item.address
},
geometry: {
type: "Point",
coordinates: [item.latitude, item.longitude]
}
};
})
};
console.log(geojson);
console.log(JSON.stringify(geojson));
这篇关于如何将来自服务器的普通地理json转换为geoJson?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文