2017-07-18 12 views
0

javascript組み込み関数を使用して複数のGeoJSONオブジェクトをマージすることは可能ですか?もしそうでなければ、どうやってこれを飛行で行うことができますか?複数のGeoJSONオブジェクトをjavascriptと組み合わせる

geoJSON1.concat is not a functionを返すJSON(たとえばhere)の提案に従ってgeoJSON1 = geoJSON1.concat(geoJSON2)を試しました。 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フィールドを備えていますが、配列concat方法を使用することができ、また

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

は配列です。 //geojson.org/は**オブジェクト** **配列**ではないことを示唆しています。 – Quentin

+0

入力がどのようなものか、希望の出力がどのようなものかの例をいくつか追加できますか? – kristaps

+0

@Quentinなぜ 'concat'メソッドが機能しないのですか? @kristaps入力と結果の簡単な例をいくつか追加しました。 – Bird

答えて

2

あなたは、アレイの拡張構文(spread operator...)を使用することができます。

// Given GeoJSON1 GeoJSON2 

var newGeoJSON = { 
    "type" : "FeatureCollection", 
    "features": [... GeoJSON1.features, ... GeoJSON2.features] 
} 

これは関数として一般化することができます

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

私は、あなたが探していることは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

関連する問題