1

I have two geojson data sets. One of them is polygon feature "yolbuf" and the other one is a polygon feature collection "polygon". I want to calculate intersections of every feature in polygon feature collection and add these intersected polygon geometries to my map. But I can not calculate intersection. I cerate a loop for every feature in "polygon" feature collection and calculate intersections using turf.js but I receive an error "geojson must be a valid Feature or Geometry Object" for the line where I call turf intersection function. I could not figure it out. Any help would be very helpfull. Here is my code that I tried so far:

 var polygon =
    {
        "type": "FeatureCollection",
        "features": [
            {
                "type": "Feature",
                "properties": {},
                "geometry": {
                    "type": "Polygon",
                    "coordinates": [
                        [
                            [
                                33.23123550240566,
                                39.92248701096464
                            ],
                            [
                                33.23151584974473,
                                39.92228723799442
                            ],
                            [
                                33.23143457753136,
                                39.92220902313792
                            ],
                            [
                                33.2313766303031,
                                39.92214733535532
                            ],
                            [
                                33.23102868523483,
                                39.92232101813538
                            ],
                            [
                                33.23123550240566,
                                39.92248701096464
                            ]
                        ]
                    ]
                }
            },
            {
                "type": "Feature",
                "properties": {},
                "geometry": {
                    "type": "Polygon",
                    "coordinates": [
                        [
                            [
                                33.23123550240566,
                                39.92248701096464
                            ],
                            [
                                33.23151584974473,
                                39.92228723799442
                            ],
                            [
                                33.23143457753136,
                                39.92220902313792
                            ],
                            [
                                33.2313766303031,
                                39.92214733535532
                            ],
                            [
                                33.23102868523483,
                                39.92232101813538
                            ],
                            [
                                33.23123550240566,
                                39.92248701096464
                            ]
                        ]
                    ]
                }
            },
            {
                "type": "Feature",
                "properties": {},
                "geometry": {
                    "type": "Polygon",
                    "coordinates": [
                        [
                            [
                                33.23102868523483,
                                39.92232101813538
                            ],
                            [
                                33.2313766303031,
                                39.92214733535532
                            ],
                            [
                                33.23135637097489,
                                39.92212650848449
                            ],
                            [
                                33.23127259690326,
                                39.92202385548821
                            ],
                            [
                                33.23088430660423,
                                39.92220634322857
                            ],
                            [
                                33.23090957419321,
                                39.9222345005147
                            ],
                            [
                                33.23102868523483,
                                39.92232101813538
                            ]
                        ]
                    ]
                }
            },
            {
                "type": "Feature",
                "properties": {},
                "geometry": {
                    "type": "Polygon",
                    "coordinates": [
                        [
                            [
                                33.23102868523483,
                                39.92232101813538
                            ],
                            [
                                33.2313766303031,
                                39.92214733535532
                            ],
                            [
                                33.23135637097489,
                                39.92212650848449
                            ],
                            [
                                33.23127259690326,
                                39.92202385548821
                            ],
                            [
                                33.23088430660423,
                                39.92220634322857
                            ],
                            [
                                33.23090957419321,
                                39.9222345005147
                            ],
                            [
                                33.23102868523483,
                                39.92232101813538
                            ]
                        ]
                    ]
                }
            },
            {
                "type": "Feature",
                "properties": {},
                "geometry": {
                    "type": "Polygon",
                    "coordinates": [
                        [
                            [
                                33.23106496596528,
                                39.92212143765926
                            ],
                            [
                                33.23093691284885,
                                39.92195591156596
                            ],
                            [
                                33.23075984311377,
                                39.92201316928995
                            ],
                            [
                                33.23079351450731,
                                39.92209278466239
                            ],
                            [
                                33.23084284247113,
                                39.92216013703721
                            ],
                            [
                                33.23088430660423,
                                39.92220634322857
                            ],
                            [
                                33.23106496596528,
                                39.92212143765926
                            ]
                        ]
                    ]
                }
            },
            {
                "type": "Feature",
                "properties": {},
                "geometry": {
                    "type": "Polygon",
                    "coordinates": [
                        [
                            [
                                33.23106496596528,
                                39.92212143765926
                            ],
                            [
                                33.23093691284885,
                                39.92195591156596
                            ],
                            [
                                33.23075984311377,
                                39.92201316928995
                            ],
                            [
                                33.23079351450731,
                                39.92209278466239
                            ],
                            [
                                33.23084284247113,
                                39.92216013703721
                            ],
                            [
                                33.23088430660423,
                                39.92220634322857
                            ],
                            [
                                33.23106496596528,
                                39.92212143765926
                            ]
                        ]
                    ]
                }
            },
            {
                "type": "Feature",
                "properties": {},
                "geometry": {
                    "type": "Polygon",
                    "coordinates": [
                        [
                            [
                                33.23127259690326,
                                39.92202385548821
                            ],
                            [
                                33.23122986841128,
                                39.92197283593117
                            ],
                            [
                                33.23116334430379,
                                39.92188269157665
                            ],
                            [
                                33.23095417047043,
                                39.92195033107802
                            ],
                            [
                                33.23093691284885,
                                39.92195591156596
                            ],
                            [
                                33.23106496596528,
                                39.92212143765926
                            ],
                            [
                                33.23127259690326,
                                39.92202385548821
                            ]
                        ]
                    ]
                }
            },
            {
                "type": "Feature",
                "properties": {},
                "geometry": {
                    "type": "Polygon",
                    "coordinates": [
                        [
                            [
                                33.2296955135001,
                                39.92243952105167
                            ],
                            [
                                33.22980597686984,
                                39.9222961545936
                            ],
                            [
                                33.22960217368549,
                                39.92221433149345
                            ],
                            [
                                33.22945999098032,
                                39.92240277126455
                            ],
                            [
                                33.2296955135001,
                                39.92243952105167
                            ]
                        ]
                    ]
                }
            }
        ]
    };
    var yol = {
    "type": "FeatureCollection",
    "features": [
        {
            "type": "Feature",
            "properties": {
                "genislik": 15
            },
            "geometry": {
                "type": "LineString",
                "coordinates": [
                    [
                        33.2292425071561,
                        39.92113004776218,
                        0
                    ],
                    [
                        33.229473546304,
                        39.92133376987435,
                        0
                    ],
                    [
                        33.22950227387721,
                        39.92138603150682,
                        0
                    ],
                    [
                        33.22960053796574,
                        39.92157730010621,
                        0
                    ],
                    [
                        33.22965706684388,
                        39.92175869708733,
                        0
                    ],
                    [
                        33.22970824225857,
                        39.92193301273533,
                        0
                    ],
                    [
                        33.22974032375962,
                        39.92199270354748,
                        0
                    ],
                    [
                        33.22978752905556,
                        39.92205207615689,
                        0
                    ],
                    [
                        33.22983673278315,
                        39.92210059742482,
                        0
                    ],
                    [
                        33.22990544410934,
                        39.92214802439388,
                        0
                    ],
                    [
                        33.23003228674187,
                        39.92224919395576,
                        0
                    ],
                    [
                        33.23012431533797,
                        39.92230792049544,
                        0
                    ],
                    [
                        33.23021705388681,
                        39.92235584040844,
                        0
                    ],
                    [
                        33.23029337496003,
                        39.92238853644901,
                        0
                    ],
                    [
                        33.23039243416922,
                        39.92242594245815,
                        0
                    ],
                    [
                        33.2304371756197,
                        39.92245401139586,
                        0
                    ],
                    [
                        33.23047032061119,
                        39.92249271442368,
                        0
                    ]
                ]
            }
        }
    ]
};



var myStyle = {
    "color": "#ff7800",
    "weight": 5,
    "opacity": 0.65
};
var myStyle2 = {
    "color": "#483D8B",
    "weight": 5,
    "opacity": 0.65
};
var myStyle3 = {
    "color": "#DC143C",
    "weight": 5,
    "opacity": 0.65
};
var myStyle4 = {
    "color": "#00CED1",
    "weight": 5,
    "opacity": 0.65
};
L.geoJSON(polygon, {
    style: myStyle
}).addTo(mymap);

L.geoJSON(yol, {
    style: myStyle2
}).addTo(mymap);


var road = new L.GeoJSON(yol, {style:myStyle3});
road.addTo(mymap);

var buffered = turf.buffer(road.toGeoJSON(), 0.02, {units: 'kilometers'});

var yolbuf = new L.GeoJSON(buffered, {style:myStyle3});
yolbuf.addTo(mymap);

var parseller = polygon.features;
intersections = [];

for (var i = 0; i < parseller.length; i++ ){
    var parsel = parseller[i];
    var intersection = turf.intersect(parsel, yolbuf);
    if (intersection != null){
        intersections.push(intersection);
    }
}

2 Answers 2

3

Your buffer distance was too small to touch a parcel boundary; This is your code working:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>

<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/leaflet.css" />
<script src="https://unpkg.com/[email protected]/dist/leaflet-src.js"></script>
<script src="https://npmcdn.com/@turf/turf/turf.min.js"></script>
<!-- <script src="turf.js"></script>  -->

    <style>
        #map {
            width: 800px;
            height: 600px;
        }
    </style>

</head>
<body>
<div id="map"></div>
<script>
    var mymap = L.map('map').setView([39.921577, 33.229125 ], 17);  

    var osm=new L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png',{ 
                attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'}).addTo(mymap);
 var thePolygon =
    {
        "type": "FeatureCollection",
        "features": [
            {
                "type": "Feature",
                "properties": {},
                "geometry": {
                    "type": "Polygon",
                    "coordinates": [
                        [
                            [
                                33.23123550240566,
                                39.92248701096464
                            ],
                            [
                                33.23151584974473,
                                39.92228723799442
                            ],
                            [
                                33.23143457753136,
                                39.92220902313792
                            ],
                            [
                                33.2313766303031,
                                39.92214733535532
                            ],
                            [
                                33.23102868523483,
                                39.92232101813538
                            ],
                            [
                                33.23123550240566,
                                39.92248701096464
                            ]
                        ]
                    ]
                }
            },
            {
                "type": "Feature",
                "properties": {},
                "geometry": {
                    "type": "Polygon",
                    "coordinates": [
                        [
                            [
                                33.23123550240566,
                                39.92248701096464
                            ],
                            [
                                33.23151584974473,
                                39.92228723799442
                            ],
                            [
                                33.23143457753136,
                                39.92220902313792
                            ],
                            [
                                33.2313766303031,
                                39.92214733535532
                            ],
                            [
                                33.23102868523483,
                                39.92232101813538
                            ],
                            [
                                33.23123550240566,
                                39.92248701096464
                            ]
                        ]
                    ]
                }
            },
            {
                "type": "Feature",
                "properties": {},
                "geometry": {
                    "type": "Polygon",
                    "coordinates": [
                        [
                            [
                                33.23102868523483,
                                39.92232101813538
                            ],
                            [
                                33.2313766303031,
                                39.92214733535532
                            ],
                            [
                                33.23135637097489,
                                39.92212650848449
                            ],
                            [
                                33.23127259690326,
                                39.92202385548821
                            ],
                            [
                                33.23088430660423,
                                39.92220634322857
                            ],
                            [
                                33.23090957419321,
                                39.9222345005147
                            ],
                            [
                                33.23102868523483,
                                39.92232101813538
                            ]
                        ]
                    ]
                }
            },
            {
                "type": "Feature",
                "properties": {},
                "geometry": {
                    "type": "Polygon",
                    "coordinates": [
                        [
                            [
                                33.23102868523483,
                                39.92232101813538
                            ],
                            [
                                33.2313766303031,
                                39.92214733535532
                            ],
                            [
                                33.23135637097489,
                                39.92212650848449
                            ],
                            [
                                33.23127259690326,
                                39.92202385548821
                            ],
                            [
                                33.23088430660423,
                                39.92220634322857
                            ],
                            [
                                33.23090957419321,
                                39.9222345005147
                            ],
                            [
                                33.23102868523483,
                                39.92232101813538
                            ]
                        ]
                    ]
                }
            },
            {
                "type": "Feature",
                "properties": {},
                "geometry": {
                    "type": "Polygon",
                    "coordinates": [
                        [
                            [
                                33.23106496596528,
                                39.92212143765926
                            ],
                            [
                                33.23093691284885,
                                39.92195591156596
                            ],
                            [
                                33.23075984311377,
                                39.92201316928995
                            ],
                            [
                                33.23079351450731,
                                39.92209278466239
                            ],
                            [
                                33.23084284247113,
                                39.92216013703721
                            ],
                            [
                                33.23088430660423,
                                39.92220634322857
                            ],
                            [
                                33.23106496596528,
                                39.92212143765926
                            ]
                        ]
                    ]
                }
            },
            {
                "type": "Feature",
                "properties": {},
                "geometry": {
                    "type": "Polygon",
                    "coordinates": [
                        [
                            [
                                33.23106496596528,
                                39.92212143765926
                            ],
                            [
                                33.23093691284885,
                                39.92195591156596
                            ],
                            [
                                33.23075984311377,
                                39.92201316928995
                            ],
                            [
                                33.23079351450731,
                                39.92209278466239
                            ],
                            [
                                33.23084284247113,
                                39.92216013703721
                            ],
                            [
                                33.23088430660423,
                                39.92220634322857
                            ],
                            [
                                33.23106496596528,
                                39.92212143765926
                            ]
                        ]
                    ]
                }
            },
            {
                "type": "Feature",
                "properties": {},
                "geometry": {
                    "type": "Polygon",
                    "coordinates": [
                        [
                            [
                                33.23127259690326,
                                39.92202385548821
                            ],
                            [
                                33.23122986841128,
                                39.92197283593117
                            ],
                            [
                                33.23116334430379,
                                39.92188269157665
                            ],
                            [
                                33.23095417047043,
                                39.92195033107802
                            ],
                            [
                                33.23093691284885,
                                39.92195591156596
                            ],
                            [
                                33.23106496596528,
                                39.92212143765926
                            ],
                            [
                                33.23127259690326,
                                39.92202385548821
                            ]
                        ]
                    ]
                }
            },
            {
                "type": "Feature",
                "properties": {},
                "geometry": {
                    "type": "Polygon",
                    "coordinates": [
                        [
                            [
                                33.2296955135001,
                                39.92243952105167
                            ],
                            [
                                33.22980597686984,
                                39.9222961545936
                            ],
                            [
                                33.22960217368549,
                                39.92221433149345
                            ],
                            [
                                33.22945999098032,
                                39.92240277126455
                            ],
                            [
                                33.2296955135001,
                                39.92243952105167
                            ]
                        ]
                    ]
                }
            }
        ]
    };
    var yol = {
    "type": "FeatureCollection",
    "features": [
        {
            "type": "Feature",
            "properties": {
                "genislik": 15
            },
            "geometry": {
                "type": "LineString",
                "coordinates": [
                    [
                        33.2292425071561,
                        39.92113004776218,
                        0
                    ],
                    [
                        33.229473546304,
                        39.92133376987435,
                        0
                    ],
                    [
                        33.22950227387721,
                        39.92138603150682,
                        0
                    ],
                    [
                        33.22960053796574,
                        39.92157730010621,
                        0
                    ],
                    [
                        33.22965706684388,
                        39.92175869708733,
                        0
                    ],
                    [
                        33.22970824225857,
                        39.92193301273533,
                        0
                    ],
                    [
                        33.22974032375962,
                        39.92199270354748,
                        0
                    ],
                    [
                        33.22978752905556,
                        39.92205207615689,
                        0
                    ],
                    [
                        33.22983673278315,
                        39.92210059742482,
                        0
                    ],
                    [
                        33.22990544410934,
                        39.92214802439388,
                        0
                    ],
                    [
                        33.23003228674187,
                        39.92224919395576,
                        0
                    ],
                    [
                        33.23012431533797,
                        39.92230792049544,
                        0
                    ],
                    [
                        33.23021705388681,
                        39.92235584040844,
                        0
                    ],
                    [
                        33.23029337496003,
                        39.92238853644901,
                        0
                    ],
                    [
                        33.23039243416922,
                        39.92242594245815,
                        0
                    ],
                    [
                        33.2304371756197,
                        39.92245401139586,
                        0
                    ],
                    [
                        33.23047032061119,
                        39.92249271442368,
                        0
                    ]
                ]
            }
        }
    ]
};



var myStyle = {
    "color": "#ff7800",
    "weight": 5,
    "opacity": 0.65
};
var myStyle2 = {
    "color": "#483D8B",
    "weight": 5,
    "opacity": 0.65
};
var myStyle3 = {
    "color": "#DC143C",
    "weight": 5,
    "opacity": 0.65
};
var myStyle4 = {
    "color": "#00CED1",
    "weight": 5,
    "opacity": 0.65
};
L.geoJSON(thePolygon, {
    style: myStyle
}).addTo(mymap);

L.geoJSON(yol, {
    style: myStyle2
}).addTo(mymap);

var road = new L.geoJSON(yol, {style:myStyle3});
road.addTo(mymap);

var buffered = turf.buffer(road.toGeoJSON(), .05, {units: 'kilometers'});

var yolbuf = new L.geoJSON(buffered, {style:myStyle3});
yolbuf.addTo(mymap);   /// Will be poly segments if road is segments

var dissolved = turf.dissolve(buffered);  //Makes buffer all one poly
var yolbuf2 = new L.geoJSON(dissolved, {color:"orange"}).addTo(mymap);

var parcels = new L.geoJSON(thePolygon, {color:'red'});
parcels.addTo(mymap);

var parseller = thePolygon.features; 
intersections = [];
polysthatIntersected = [];

for (var i = 0; i < parseller.length; i++ ){
            var thePoly = parseller[i];
            var temp1 = turf.polygon(thePoly.geometry.coordinates);
            var temp2 = turf.polygon(buffered.features[0].geometry.coordinates);  

    var intersection = turf.intersect(temp1, temp2);
    if (intersection != null){
        //only area of overlap
        intersections.push(intersection);
        // Complete poly of the parcel touching buffer
        polysthatIntersected.push(thePoly);
    }
}
// Only intersection
var theIntersection = new L.geoJSON(intersections, {color:'black'}).addTo(mymap);
//only parcel intersected by buffer
var theInterPolys = new L.geoJSON(polysthatIntersected, {color:'green'}).addTo(mymap);

 </script>
 </body>
 </html>
1

Turf takes in GeoJSON objects, not instances of L.GeoJson. Note that foo1 and foo2 are very different:

var foo1 = { type: 'Feature', geometry: ... };
var foo2 = L.geoJson(foo1);

In your specific case, you probably want to use Leaflet's toGeoJson() method onyolbuf, i.e. replace:

var intersection = turf.intersect(parsel, yolbuf);

with

var intersection = turf.intersect(parsel, yolbuf.toGeoJson());

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.