Matt Mateo
Matt Mateo

Reputation: 168

Intersect between Polygon and Multipolygon using turfjs

I ran an error using turfjs performing intersect function of turfjs

enter image description here

The drawn layer is "Polygon" while I want it to be intersect in a "MultiPolygon" layer.

Here's the snippet of my code:

$.getJSON("FloodHazard_CRB_100Year.json", function(baha) { 
            //addDataToMap(data, map); 
            console.log(json);
            console.log(baha);
            //check_intersect(baha,json);
            f1 = baha.features;
            f2 = json.features;

            var conflictlist;

            for (var i = 0; i < f1.length; i++) {
                var parcel1 = f1[i];
                for (var j = 0; j <f2.length; j++) {
                    var parcel2 = f2[j];
                    //console.log("Processing",i,j);
                        var conflict = turf.intersect(parcel1, parcel2);
                        if (conflict != null) {
                            conflictlist = conflict;
                        }
                }
            }
            var intersect_style = {
                fillColor: "#ff0000",
                color: "#000",
                opacity: 1,
                weight:0.5,
                fillOpacity: 0.8
            };
            L.geoJson(conflictlist,{
                style: intersect_style
            }).addTo(map);
            console.log(conflictlist);

            //check_intersect(json);
        });

Upvotes: 1

Views: 3715

Answers (1)

ghybs
ghybs

Reputation: 53185

Note first that Turf needs the full GeoJSON Feature objects as arguments, not just their geometry.

Then if my understanding is correct, your 2nd loop is a workaround for the need to pass only Polygons to turf.intersect, and not MultiPolygons?

In that case you would need first to properly convert your MutiPolygon into a collection or array of Features with a Polygon geometry each. Then you can loop on them to perform your intersection.

Upvotes: 1

Related Questions