geoinfo
geoinfo

Reputation: 305

Create FeatureCollection from multiple GeoJSON strings

I have a list of features and I want to create a FeatureCollection to group them in one string, the features are from different types (polygons, lines and points).

Here is how they are written :

{
  "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
        ]
      ]
    ]
  }
}

Is there a javascript function that deals with this, i'm stuck on it.

Upvotes: 1

Views: 2634

Answers (1)

cartant
cartant

Reputation: 58410

If you have an array of GeoJSON feature strings, you can create a GeoJSON feature collection string like this:

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

Upvotes: 2

Related Questions