We use Mapbox GL JS 1.12.0 in our Vue.js project. I need to create intersection objects if I have 2 or more polygons. But, I'm able to do it with 2 polygons only. The code looks as following:
import * as turf from '@turf/turf';
export function createIntersection(features) {
// features = 3 polygons on the screen bellow
const intersection = turf.intersect(...features);
return intersection;
}
The screenshots:
As you can see there is only 1 intersection object created.
How to do it with more than 2 polygons?
turf.intersect() can only intersect 2 polygons.
If you want to intersect multiple polygons you can intersect each polygon with each other polygon with turf.intersect() and then combine the results using turf.combine().
Here is some example code:
const polygonA = ...;
const polygonB = ...;
const polygonC = ...;
const allIntersections = {
type: 'FeatureCollection',
features: [
turf.intersect(polygonA, polygonB),
turf.intersect(polygonA, polygonC),
turf.intersect(polygonB, polygonC),
],
};
const combinedIntersection = turf.combine(allIntersections);