2

I've been playing around with turf.js and I'd like to use turf.intersect to intersect two datasets. My raw data are two polygon shape files so I converted them to geojson. I then tried to use turf.intersect on the two geojson files but it fails as the geojson contain FeatureCollections while turf.intersect expects Polygons.

Is there a workaround to this?

Right now, I'm thinking of iterate over the polygons in the first geojson file one by one and then checking if it intersects with any of the polygons of the second geojson. I'd then just combine the results into another geojson file.

Is there a faster way?

  • 1
    It seems turf-intersect originally supported FeatureCollections, but they removed that support in November 2014. Unclear why that decision was made. – nmpeterson Feb 23 '15 at 16:05
  • I'm curious as well. Most files in the wild are FeatureCollections. An intersect that can only work with Polygons isn't terribly useful or maybe it's just me. Maybe the code got too complex. – R.K. Feb 23 '15 at 16:19
  • 1
    Ah, I received some clarification: "The first implementation was only taking the first feature out of the collection. This was causing unwanted surprises." So true FeatureCollection support was never implemented. – nmpeterson Feb 25 '15 at 20:14
  • Thanks for the info! Do you have your own workaround? :-) – R.K. Feb 26 '15 at 0:20
2

I use the following code to find the intersection between two networks as polylines, I turn them in GeoJSON and then calculate the intersections with turf, iterating every feature of the collection.

var map = L.map('map').setView([37.501010429493284, 85.80322265625], 7);

L.tileLayer('https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token=pk.eyJ1IjoibWFwYm94IiwiYSI6ImNpandmbXliNDBjZWd2M2x6bDk3c2ZtOTkifQ._QA7i5Mpkd_m30IGElHziw', {
    maxZoom: 18,
    attribution: 'Map data &copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, ' +
        '<a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, ' +
        'Imagery © <a href="http://mapbox.com">Mapbox</a>',
    id: 'mapbox.light'
}).addTo(map);



var fc1Layer = L.geoJson(fc1);
var fc2Layer = L.geoJson(fc2);

//ejemplo intersección entre una misma red|
//fc2Layer.addTo(map);
//showIntersects(fc2, fc2);

//ejemplo intersección entre dos redes diferentes
fc1Layer.addTo(map);
fc2Layer.addTo(map);
showIntersects(fc1, fc2);



/*fc1 y fc2 son features collections*/
function showIntersects(fc1, fc2){
    fc1.features.forEach(function(layer1){
        fc2.features.forEach(function(layer2){
            var intersection = turf.intersect(layer1, layer2);
            if(intersection!=undefined){
                var interLayer=L.geoJson(intersection).addTo(map);
                arreglo.push(interLayer);
            }
        });
    });
}

for(var i=0; i<arreglo.length;i++){
    document.write("intersecto 1: "+arreglo[i]+"<br />");
}
1

Not quite the most elegant solution but here's what I did so far.

Load the two geojson files

var dataset1 = JSON.parse(fs.readFileSync('./d1.geojson', 'utf8'));
var dataset2 = JSON.parse(fs.readFileSync('./d2.geojson', 'utf8'));

Get features

f1 = dataset1.features
f2 = dataset2.features

Once the features are loaded, it's time to iterate over them.

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.push(conflict);
            }
    }
}

Create FeatureCollections from intersect results with valid geometries:

var intersectiontest = turf.featurecollection(conflictlist);
fs.writeFile( "intersection.geojson", JSON.stringify( intersectiontest ), "utf8" );

And there you have it, getting the intersection of two FeatureCollections. If anybody has a more elegant solution, please do post your own answer or give your feedback in the comments.

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.