Friday 13 January 2017

javascript - Reading through GeoJSON using Turf.js?


I have a simple app that takes a coordinate and determines what GeoJSON polygons it falls inside, then I take the polygon data and put it into a table. I have working code that uses turfjs and Leafletjs. however my task is to do this without a map.


I'm tripping over the part of the code that reads the GeoJSOn and "foreach" poly


var cluster = [];

var point1 = turf.point([-73.79896352764388,42.81585623837577], { });//x,y

$.getJSON(url, function(data){
//// replace from here
geojsonLayer = new L.GeoJSON(data,{style: style, onEachFeature: onEachFeature});
map.addLayer(geojsonLayer);
map.fitBounds(geojsonLayer.getBounds(), {padding: [50, 50]});

geojsonLayer.eachLayer(function (layer) {
///// replace to to here....


//$.each(data, function(layer){ // thought I was on the right track but...

var isInside = turf.inside(point1,layer.feature);

if(isInside){
cluster.push(layer.feature);
}

});


createTable(cluster);

});

Any ideas?



Answer



Got it, create a JS object and load it with features from the GeoJSON file. Next loop through the object passing it through the turf function inside.


var cluster = [];
var features=[];

var point1 = turf.point([-73.798963,42.815856], { });//x,y

$.getJSON(url, function(data){

features = data.features;

for (var i = 0, len = features.length; i < len; i++) {

var isInside = turf.inside(point1,features[i]);


if(isInside){

cluster.push(features[i]);

}
}

createTable(cluster); //Create table of attributes

});

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...