从多个GeoJSON字符串创建FeatureCollection

时间:2016-10-04 16:02:34

标签: javascript json 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 个答案:

答案 0 :(得分:1)

如果您有一组GeoJSON要素字符串,则可以创建一个GeoJSON要素集合字符串,如下所示:

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