将多个GeoJSON对象与javascript相结合



是否可以使用javascript内置函数合并多个GeoJSON对象?如果没有,我该如何即时执行此操作?

我根据 JSON 的建议尝试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"}
}
]
}

您可以使用数组扩展语法(展开运算符...(。

// 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 要素字段是一个数组:

function concatGeoJSON(g1, g2){
return { 
"type" : "FeatureCollection",
"features": g1.features.concat(g2.features)
}
}

我认为你要找的是Object.assign 函数。在您的情况下,这是您会做的。

var GeoJSON1 = { 'bat': 'baz'}
var GeoJSON2 = { 'foo':'bar'}
var both = {};
Object.assign(both, GeoJSON1, GeoJSON2);
console.log(both);
// Object {bat: 'baz', foo:'bar'}

最新更新