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
turf.union(relatedFeatures)
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]
if(relatedFeatures.length>1){
$.each(relatedFeatures,function(i,item){
if(i>1){
featuresJoined=turf.union(featuresJoined,item)
}
})
}
Best Answer
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 tounion
, so if you're targeting ES6 you can use spread syntax.Otherwise Magnus's answer should also work.