将多个GeoJSON对象与javascript结合使用

问题描述:

是否可以使用JavaScript内置函数合并多个GeoJSON对象?如果不是,我该如何在飞行中做到这一点?将多个GeoJSON对象与javascript结合使用

根据对JSON的建议(例如here),我尝试了geoJSON1 = geoJSON1.concat(geoJSON2),它返回geoJSON1.concat is not a function。 GeoJSON是点要素,是有效的GeoJSON对象。

这可能是一个简单的问题,用“否”作为答案,但我一直未能找到确凿的答案。

例GeoJSONs:

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"} 
     } 
    ] 
} 
+0

在http扫视: //geojson.org/建议你有**对象**而不是**数组**。 – Quentin

+0

您可以添加一些输入样例以及所需输出的样子吗? – kristaps

+0

@Quentin为什么'concat'方法不起作用? @kristaps我添加了一些简单的输入和结果示例。 – Bird

你可以使用阵列扩展语法(在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设有字段是一个数组:

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'} 
+0

这对您的示例起作用,但不幸的是,它似乎没有组合GeoJSON对象,它只返回最后一个对象(在本例中为GeoJSON2)。必须是由于GeoJSONs的结构?感谢您为我展示了一个新功能。 – Bird