I'm considering implementing a WFS client that, each time the map's view area is moved, fetches the features within that bounding box, and combines them with features previously fetched. Much of the time there will be features downloaded that already exist in the "previously fetched" dataset, so these need to be not duplicated.

Is there a Turf operation to merge two FeatureCollections in this way? I can't see such a thing. union combines two different polygons into a new one. combine combines Point/LineString/Polygon features into MultiPoint/MultiLineString/MultiPolygon.


Unless there's something more complicated that I can't see, couldn't you simply use regular javascript to concatenate the two features array and then build a new FeatureCollection from there? As in :



Edit to reflect changes in the question and the comment regarding duplicated features :

Considering you don't want to keep both versions of an existing feature, and that they share an id, doesn't the solution become a simple case of filtering features that already exist from the new data? Or maybe is it again too naïve?

Assuming a was generated on the first call and b is the newer data, and id is the property you want to filter on (on two lines to simplify readability).

const existingFeatureIds = a.features.map(feature => feature.properties.id);
turf.featureCollection(a.features.concat(b.features.filter(feature => !existingFeatureIds.includes(feature.properties.id))));
  • Oh, I'm assuming there will be a lot of overlap between a.features and b.features. That is, features that exist within both, that can be detected because they share an id or something. – Steve Bennett Jul 10 '18 at 6:04

In case someone is looking for a faster solution, you can create an empty object, and keep inserting pairs of id and feature and convert the object to an array before calling turf.featureCollection.

And of course, since it is an object, the duplicated feature gets replaced by the new one. No need to worry about duplication.

let featuresObj = {};
for (let featureCollection of [a,b]){
  for (let feature of featureCollection){
    const id = feature.properties.id;
    featuresObj[id] = feature;
const resultFeatures = Object.values(featuresObj);
const result = turf.featureCollection(resultFeatures);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.