19

Is there an easy way to get the name of the country on whose territory a given point is located?

I don't care too much about accuracy nor about ambiguities given by political disputes. Just need a good enough approximation.

ibz
  • 44,461
  • 24
  • 70
  • 86
  • 1
    Do you want to do this using an online service or also offline? Also which platform / programming language do you want the answer to related to? – hippietrail Aug 06 '12 at 11:49

7 Answers7

6

EDIT: The Yahoo Where API is no longer available.

Try the Yahoo! APIs.

Latitude and Longitude

Latitude and longitude can be specified for the location parameter. Latitude and longitude may be expressed as decimal degrees or degrees-minutes-seconds, with either leading or trailing directionals or leading signs. If directionals are provided, longitude may appear before latitude. If directionals are not provided, longitude may appear before latitude if it is outside the range -90 to 90. Otherwise, the latitude must appear first. Punctuation marks (commas, degrees, minutes, seconds) are ignored.

Examples: 

•50.3 -120.5
•50.3, -120.5
•-120.5 50.3
•50.3 N 120.5 W
•120.5 W 50.3 N
•50 18 0 -120 30 0
•50 18 0 N 120 30 0 W
•50° 18' 0" N 120° 30' 0" W

The response element will give you the country, as well as many other elements.

Don't forget to send as parameter gflags=R, to make the reverse geocoding. If you want the output in json, send the parameter flags=J as well.

Community
  • 1
  • 1
GalacticJello
  • 11,235
  • 2
  • 25
  • 35
  • The Yahoo! geocode location API does not provide any information for several requests that I've tried, for location which aren't that difficult to look up (nothing in the middle or the ocean or something) so I would be very careful when depending on this. – Guss Jun 23 '11 at 10:57
  • 1
    @Felipe - 50,000 request/day on [PlaceFinder](http://developer.yahoo.com/geo/placefinder/). – GalacticJello Oct 25 '12 at 16:34
  • @GalacticJello in my tests I had only 40,000 per day. – Felipe Oct 30 '12 at 17:24
5

How about using google's reverse geo-encoding service?

Kent Boogaart
  • 175,602
  • 35
  • 392
  • 393
  • 1
    The problem of this is: Use of the Google Geocoding API is subject to a query limit of 2,500 geolocation requests per day. (User of Google Maps API for Business may perform up to 100,000 requests per day.) (...) Note: the Geocoding API may only be used in conjunction with a Google map; geocoding results without displaying them on a map is prohibited. For complete details on allowed usage, consult the Maps API Terms of Service License Restrictions. – Felipe Oct 25 '12 at 16:03
5

Just for the reference, geonames.org also has a nice webservice, but it's rate limited (which was an issue in my case since I had to look up a big batch of coordinates).

Example: http://ws.geonames.org/findNearbyPlaceName?lat=47.3&lng=9

ibz
  • 44,461
  • 24
  • 70
  • 86
  • That saved me, thanks! You need a username to make it work, and `felix` and `nico` work fine. (To other people: ) you have a limit of 2000 per hour, so use them wisely – Nico Oct 29 '14 at 16:49
1

Here's some JavaScript code to get the name of the country from latitude and longitude coordinates:


  1. Using Google's geocoding services [jsfiddle]:

    var lookupCountryWithGoogle = function(lat, lng) { var latlng = new google.maps.LatLng(lat, lng);

    var geoCoder = new google.maps.Geocoder();
    
    geoCoder.geocode({
        location: latlng
    }, function(results, statusCode) {
        var lastResult = results.slice(-1)[0];
    
        if (statusCode == 'OK' && lastResult && 'address_components' in lastResult) {
            alert('coordinates: ' + lat + ', ' + lng + '\n' + 'country: ' + lastResult.address_components.slice(-1)[0].long_name);
        } else {
            alert('coordinates: ' + lat + ', ' + lng + '\n' + 'failed: ' + statusCode);
        }
    });
    

    };

    lookupCountryWithGoogle(43.7534932, 28.5743187); // will succeed lookupCountryWithGoogle(142, 124); // will fail

(You'll need to load Google's JavaScript support for this from a URL such as maps.google.com/maps/api/js?sensor=false&fake=.js)


  1. Using ws.geonames.org [jsfiddle]:

    var lookupCountryWithGeonames = function(lat, lng) {

    $.getJSON('http://ws.geonames.org/countryCode', {
        lat: lat,
        lng: lng,
        type: 'JSON'
    }, function(results) {
        if (results.countryCode) {
            alert('coordinates: ' + lat + ', ' + lng + '\n' + 'country: ' + results.countryName);
        } else {
            alert('coordinates: ' + lat + ', ' + lng + '\n' + 'failed: ' + results.status.value + ' ' + results.status.message);
        }
    });
    

    };

    lookupCountryWithGeonames(43.7534932, 28.5743187); // will succeed

    lookupCountryWithGeonames(142, 124); // will fail ​

hippietrail
  • 15,848
  • 18
  • 99
  • 158
1

You could also look at the services from tinygeocoder.

Another alternative is to download the country maps from natural earth (or another source). Using Geotools you could search whether the point is inside one of the countries.

0

The Geonames API has changed a bit. More info here: https://www.geonames.org/export/web-services.html

A working example from that page:

curl "http://api.geonames.org/countryCode?lat=47.03&lng=10.2&username=demo"
Eponymous
  • 109
  • 8
0

Current answers rely on online APIs, which are subjects to changes, query limits, obsolescence, etc.

For recurrent and/or numerous searches, a fast and easy way is to download the countries shapes in SHP format (Natural Earth seems a recognized source, you can filter countries afterwards if you’re only interested in a particular region), and to check if the point is contained within the boundaries.

This answer may help setting this up with Python.

Skippy le Grand Gourou
  • 6,976
  • 4
  • 60
  • 76