2016-10-04 98 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