2016-10-04 142 views
0

我有一個功能列表,我想創建一個FeatureCollection將它們分組在一個字符串中,這些功能來自不同類型(多邊形,線條和點)。從多個GeoJSON字符串創建FeatureCollection

這裏是他們是如何寫的:

{ 
    "type": "Feature", 
    "properties": {}, 
    "geometry": { 
    "type": "LineString", 
    "coordinates": [ 
     [ 
     -31.640625, 
     15.623036831528264 
     ], 
     [ 
     -2.8125, 
     -14.264383087562637 
     ], 
     [ 
     -22.5, 
     -30.751277776257812 
     ], 
     [ 
     -30.937499999999996, 
     -38.54816542304657 
     ] 
    ] 
    } 
} 

{ 
    "type": "Feature", 
    "properties": {}, 
    "geometry": { 
    "type": "Polygon", 
    "coordinates": [ 
     [ 
     [ 
      -24.960937499999996, 
      29.84064389983441 
     ], 
     [ 
      -2.109375, 
      21.616579336740603 
     ], 
     [ 
      2.4609375, 
      43.068887774169625 
     ], 
     [ 
      -31.289062500000004, 
      49.38237278700955 
     ], 
     [ 
      -24.960937499999996, 
      29.84064389983441 
     ] 
     ] 
    ] 
    } 
} 

是否有一個JavaScript函數,這個交易,我很卡就可以了。

回答

1

如果你有GeoJSON的特徵字符串數組,你可以創建一個GeoJSON的要素集合的字符串是這樣的:

var features = [ 
    '{ "type": "Feature", "properties": {}, ... }', 
    '{ "type": "Feature", "properties": {}, ... }' 
]; 
var collection = JSON.stringify({ 
    features: features.map(JSON.parse), 
    type: 'FeatureCollection' 
}); 
+0

不,我不具備的特徵字符串數組,他們localted在數據庫: '{「type」:「Feature」,「properties」:{},...}''{「type」:「Feature」,「properties」:{},...}' – geoinfo

+0

在這種情況下,我想我必須把它們放在一個數組中,你有什麼想法嗎? – geoinfo