是否可以使用javascript内置函数合并多个GeoJSON对象?如果没有,我该如何进行呢?
我根据JSON(例如here)的建议尝试了geoJSON1 = geoJSON1.concat(geoJSON2)
,该方法返回geoJSON1.concat is not a function
。 GeoJSON是点要素,并且是有效的GeoJSON对象。
这可能是一个简单的问题,答案为“否”,但我一直无法找到结论性的答案。
示例GeoJSON:
GeoJSON1 = { "type" : "FeatureCollection",
"features" : [
{ "type" : "Feature",
"id" : 1,
"geometry" : {
"type" : "Point",
"coordinates" : ["-119.6165333","34.35935"]},
"properties" : { "video" : "S105SC_Tape13o.noaudio.mpg", "video_second" : "0", "time" : "19:26:58", "date" : "2005-08-26"}
},
{ "type" : "Feature",
"id" : 2,
"geometry" : {
"type" : "Point",
"coordinates" : ["-119.6165167","34.35935"]},
"properties" : { "video" : "S105SC_Tape13o.noaudio.mpg", "video_second" : "2", "time" : "19:27:00", "date" : "2005-08-26"}
}
]
}
GeoJSON2 = { "type" : "FeatureCollection",
"features" : [
{ "type" : "Feature",
"id" : 27,
"geometry" : {
"type" : "Point",
"coordinates" : ["-119.61635","34.3593833"]},
"properties" : { "video" : "S105SC_Tape13o.noaudio.mpg", "video_second" : "55", "time" : "19:27:53", "date" : "2005-08-26"}
},
{ "type" : "Feature",
"id" : 28,
"geometry" : {
"type" : "Point",
"coordinates" : ["-119.6163333","34.3594"]},
"properties" : { "video" : "S105SC_Tape13o.noaudio.mpg", "video_second" : "56", "time" : "19:27:54", "date" : "2005-08-26"}
}
]
}
所需的结果(具有原始文档所有功能的单个GeoJSON):
newGeoJSON = { "type" : "FeatureCollection",
"features" : [
{ "type" : "Feature",
"id" : 1,
"geometry" : {
"type" : "Point",
"coordinates" : ["-119.6165333","34.35935"]},
"properties" : { "video" : "S105SC_Tape13o.noaudio.mpg", "video_second" : "0", "time" : "19:26:58", "date" : "2005-08-26"}
},
{ "type" : "Feature",
"id" : 2,
"geometry" : {
"type" : "Point",
"coordinates" : ["-119.6165167","34.35935"]},
"properties" : { "video" : "S105SC_Tape13o.noaudio.mpg", "video_second" : "2", "time" : "19:27:00", "date" : "2005-08-26"}
},
{ "type" : "Feature",
"id" : 27,
"geometry" : {
"type" : "Point",
"coordinates" : ["-119.61635","34.3593833"]},
"properties" : { "video" : "S105SC_Tape13o.noaudio.mpg", "video_second" : "55", "time" : "19:27:53", "date" : "2005-08-26"}
},
{ "type" : "Feature",
"id" : 28,
"geometry" : {
"type" : "Point",
"coordinates" : ["-119.6163333","34.3594"]},
"properties" : { "video" : "S105SC_Tape13o.noaudio.mpg", "video_second" : "56", "time" : "19:27:54", "date" : "2005-08-26"}
}
]
}
最佳答案
您可以使用数组扩展语法(spread operator ...
)。
// Given GeoJSON1 GeoJSON2
var newGeoJSON = {
"type" : "FeatureCollection",
"features": [... GeoJSON1.features, ... GeoJSON2.features]
}
这可以概括为一个函数:
function concatGeoJSON(g1, g2){
return {
"type" : "FeatureCollection",
"features": [... g1.features, ... g2.features]
}
}
另外,您可以使用
concat
数组方法,因为GeoJSON features字段是一个数组:function concatGeoJSON(g1, g2){
return {
"type" : "FeatureCollection",
"features": g1.features.concat(g2.features)
}
}