Sunday, 23 October 2016

turf.js point never found inside or within GeoJSON features loop


I have a GeoJSON http://radikal.com.mx/distrito.json and all I am trying to do is find if a turf point that has a longitude and latitude is inside or within that GeoJSON. No matter what point I use I always get false.


This is based on the work from: Bill Chappell


Here is my Javascript:


// FILE: js/get-data.js
$(function() {
var cluster = [];
var features = [];
var point = turf.point([-98.768159, 20.098287]); // Hgo | This should return one TRUE at least
// var point = turf.point([-79.601038, 43.6565353]); // Toronto | This should always be FALSE

var url = 'http://radikal.com.mx/distrito.json';

$.getJSON(url, function(data)
{
features = data.features;

for (var i = 0, len = features.length; i < len; i++)
{
// Get coordinates
var is_inside = turf.inside(point, features[i]);

console.log(is_inside);

if (is_inside) {
console.log('Found it inside');
}
}

console.log(cluster);
});
});


Here is my HTML

























Answer



GeoJSON requires the coordinates to be in WGS-84 (4326) however your coordinates are not in Lat, Long. I'm guessing your using some projection that used meters. You need to re-project your data to WGS-84 then make your GeoJSON file.


You can check your file in http://geojsonlint.com/ or http://geojson.io.


Lat coordinates are never greater then 90, long never greater then 180.


What you have: {"type":"Polygon","coordinates":[[[548716.5712466406,2291522.454105338],[548709.6603264149,2291623.0293460153],[548707.9638823455,2291833.4553844943],


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...