javascript - NodeJS find country by LatLon - Stack Overflow

I have a nodejs server and multiply databases, one for each country. I want to be able to select the co

I have a nodejs server and multiply databases, one for each country.
I want to be able to select the correct country by coordinates (lat/Lon) the user sends as parameter. (e.g myserver/query/lat/lon)
I couldn't find a nodejs package which does that.
I did found ones who filter by country based on ip address but that's not the case here.

Thanks.

I have a nodejs server and multiply databases, one for each country.
I want to be able to select the correct country by coordinates (lat/Lon) the user sends as parameter. (e.g myserver/query/lat/lon)
I couldn't find a nodejs package which does that.
I did found ones who filter by country based on ip address but that's not the case here.

Thanks.

Share Improve this question edited May 17, 2015 at 7:08 Ohad Zadok asked Jun 20, 2014 at 19:29 Ohad ZadokOhad Zadok 3,6302 gold badges23 silver badges27 bronze badges 2
  • mongodb had an example db of thousands of geolocations and has geo features like fencing and distance. it might have just been US cities now that i think of it, but you can at least adapt the code to a more prehensive dataset. – dandavis Commented Jun 20, 2014 at 19:34
  • Thanks but I'm looking for a solution which doesn't involve calling the db (mongo in this solution). – Ohad Zadok Commented Jun 20, 2014 at 21:52
Add a ment  | 

2 Answers 2

Reset to default 4

Here is a package for that: https://github./vkurchatkin/which-country

The API is quite simple:

var wc = require('which-country');
console.log(wc([-100, 40])); // prints "USA"

it uses R-tree under the hood, so it's much faster than linear scan (O(log n), I suppose).

Here is what I did:
I downloaded the kml files given by geofabrik for each country (e.g download.geofabrik.de/africa/cameroon.kml) . Finally I loaded the file into nodejs and used a great package named "point-in-polygon" (https://www.npmjs/package/point-in-polygon).
The code:

var inside = require('point-in-polygon')
var fs = require('fs');
var file = __dirname + '/coutries.json';
var countries_json = JSON.parse(fs.readFileSync(file, 'utf8'));
var get_country = function(lat, lon){
        var c_list = countries_json['countries'];
        for(var num in c_list){
            var country = c_list[num];
            if (inside([lat, lon],country['coordinates'])){
                return country['name'];
            }
        }
    return 'not_found';
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745347200a4623609.html

相关推荐

  • javascript - NodeJS find country by LatLon - Stack Overflow

    I have a nodejs server and multiply databases, one for each country. I want to be able to select the co

    1天前
    50

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信