I am using Mapbox GL JS and querying a source on a common ID.

var tempId= 55;

var relatedFeatures=map.querySourceFeatures('featureSource', {
    sourceLayer: "sourceLayerId",
    filter: ['in', "FID",tempId]

This returns an array of geojson features. If I pass this directly to turf union like below it throws an error


I can iterate over the array like below and union the features together but this seems unnecessary. Any suggestion for doing this more efficiently?

var featuresJoined=relatedFeatures[1]

You can use your array of features as an argument list (see e.g.: https://stackoverflow.com/a/2856069/8620332), so it would look sth like this:

var featuresJoined = turf.union.apply(this, relatedFeatures);
  • It's better to add links as a hyperlink in answers or questions. – BBG_GIS Nov 7 '17 at 2:10

Your approach of iterating over each feature is a valid approach.

According to http://turfjs.org/docs/#union union doesn't accept an array of features instead each feature must be an argument to union, so if you're targeting ES6 you can use spread syntax.


Otherwise Magnus's answer should also work.

  • Just to clarify - You can merge two OR MORE polygons at a time. Your wording says different from the docs. – its30 Jun 1 '18 at 19:01
  • Thanks @geofu I've updated the answer to mentioned spread syntax. – AndrewHarvey Jun 11 '18 at 3:47
  • This seems wrong, the documentation has been updated on turf/union, which accepts only two polygons. – Drasill Sep 18 '18 at 10:08

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.