How to check if multiple GeoJSON objects form one complete area?

Viewed 70

I have a Javascript array of GeoJSON objects, which are all by default Polygons. I am currently using Turf.js with intersect to check if two features overlap, but how do I check if 2+ can all form a single cohesive area?

  • There is no upper limit to the number of mini-areas
  • If there are 3 locations for example, location 1 and 3 don't have to intersect each other as long as they both intersect location 2 (i.e. the idea is to be able to form one new polygon at the end)

I have tried something along the lines of the following, but it doesn't seem very efficient

var locationsToCheck = [...]; // Populated with GeoJSON objects

var currLocation = locationsToCheck.pop();
        
while (locationsToCheck.length > 0) {
    var currLocationTwo = locationsToCheck.pop();
    
    var intersection = turf.intersect(currLocation, currLocationTwo);
    
    if (intersection != null) {
        currLocation = intersection;
    } else {
        locationsToCheck.unshift(currLocationTwo);
    }
}

and it ends in an infinite loop if there are areas that never intersect at all. I thought of creating a second array to store the ones that have been checked once already, but that produces the same infinite loop again. What would be the optimal way to achieve this?

Note: The reason I use unshift to insert the area again, is it because it might intersect with another mini-area later in the list

0 Answers
Related