将Json格式化为Geojson
function object2Geojson(data) {
var featureCollection = { "type": "FeatureCollection" };
var features = new Array();
for (let i = 0; i < data.length; i++) {
var feature = { "type": "Feature" };
feature.properties = data[i];
var geometry = { "type": "Point" };
geometry.coordinates = [data[i].LON, data[i].LAT];
feature.geometry = geometry;
features.push(feature);
}
featureCollection.features = features;
return featureCollection;
}
源数据
[
{
"LON": 114.42089,
"LAT": 37.369456,
},
{
"LON": 114.427959,
"LAT": 37.369497,
}
]
最终Geojson数据
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {
"LON": 114.42089,
"LAT": 37.369456
},
"geometry": {
"type": "Point",
"coordinates": [
114.42089,
37.369456
]
}
},
{
"type": "Feature",
"properties": {
"LON": 114.427959,
"LAT": 37.369497
},
"geometry": {
"type": "Point",
"coordinates": [
114.427959,
37.369497
]
}
}
]
}