2

I use the GeoPy library in Python to find the location of countries. I use country names as input, like:

geolocator = Nominatim(user_agent="[email protected]")    
geolocator.geocode("Georgia")

The output: (32.3293809, -83.1137366)

But this is de longitude and latitude of the state Georgia and not the country Georgia.

I tried: geolocator.geocode(country="Georgia"), but that doesn't seem to work; no output.

How do I get the country instead of the state? Am I missing a parameter?

6
  • 2
    Try to use its name in another language. For example, their names are Gürcistan(country), Georgia(state) in Turkish. Commented Nov 11, 2020 at 11:38
  • @KadirŞahbaz This worked thanks! Commented Nov 11, 2020 at 12:13
  • @Taras using country_codes worked too, thanks a lot! Commented Nov 11, 2020 at 12:14
  • @Taras Put your comment in the answer, it might help others with similar problem.
    – TomazicM
    Commented Nov 11, 2020 at 15:52
  • @Taras Thanks again! Commented Nov 12, 2020 at 10:18

3 Answers 3

4

When you try embedding exactly_one=False parameter (by default it is True) into your code, like

from geopy.geocoders import Nominatim

geolocator = Nominatim(user_agent='[email protected]', timeout=10)
location = geolocator.geocode("Georgia", exactly_one=False)

the result will be a list, where you can observe that 10 OSM-features possess a 'Georgia' string in them.

[Location(Georgia, United States of America, (32.3293809, -83.1137366, 0.0)), Location(საქართველო, (41.6809707, 44.0287382, 0.0)), Location(Georgia, Franklin County, Vermont, United States of America, (44.72422295, -73.14704151731551, 0.0)), Location(Georgia, Freehold Township, New Jersey, 07710, United States of America, (40.1873348, -74.2845906, 0.0)), Location(Georgia, Lawrence County, Indiana, United States of America, (38.7100521, -86.5722154, 0.0)), Location(Georgia, Lamar County, Texas, United States of America, (33.7539902, -95.8313566, 0.0)), Location(Georgia, Hartselle, Morgan County, Alabama, 35640, United States of America, (34.453428, -86.9280617, 0.0)), Location(Georgia, Kingman County, Kansas, 67067, United States of America, (37.6277929, -98.008669, 0.0)), Location(Georgia, Assumption Parish, Louisiana, 70372, United States of America, (29.8402049, -90.9884312, 0.0)), Location(Georgia, دبي, United Arab Emirates, (25.20796335, 55.153055200000004, 0.0))]

When geolocator.geocode("Georgia") is used, it results in a first element from the list, i.e. Location(Georgia, United States of America, (32.3293809, -83.1137366, 0.0)).

So, there are several solutions to achieve a country instead of a state.

  1. Using the country_codes="ge" parameter, a list with two-letter country codes can be found in ISO 3166-1 alpha-2

    from geopy.geocoders import Nominatim
    
    geolocator = Nominatim(user_agent='[email protected]', timeout=10)
    location = geolocator.geocode("Georgia", country_codes="ge")
    
  2. Using country name written in a native language, e.g. for Georgia both 'საქართველო' or 'Sakartvelo'

    from geopy.geocoders import Nominatim
    
    geolocator = Nominatim(user_agent='[email protected]', timeout=10)
    location = geolocator.geocode("საქართველო")
    

    or

    from geopy.geocoders import Nominatim
    
    geolocator = Nominatim(user_agent='[email protected]', timeout=10)
    location = geolocator.geocode("Sakartvelo")
    
  3. Using country name written in a different language, e.g. 'Грузія' in Ukrainian. As was mentioned by @Kadir Şahbaz in his comment.

    from geopy.geocoders import Nominatim
    
    geolocator = Nominatim(user_agent='[email protected]', timeout=10)
    location = geolocator.geocode("Грузія")
    

Result of print(location.raw)

{'place_id': 235489648, 'licence': 'Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright', 'osm_type': 'relation', 'osm_id': 28699, 'boundingbox': ['41.0552922', '43.5864294', '39.8844803', '46.7365373'], 'lat': '41.6809707', 'lon': '44.0287382', 'display_name': 'საქართველო', 'class': 'boundary', 'type': 'administrative', 'importance': 0.7384537973566208, 'icon': 'https://nominatim.openstreetmap.org/ui/mapicons//poi_boundary_administrative.p.20.png';}


References:

2

There is also the option to specify a region to search in or even restrict results to. This should be used when ever possible and will be much safer than playing language tricks.

If you look at the documentation of the method, you can see the parameters viewbox and bounded.

viewbox (list or tuple of 2 items of geopy.point.Point or 
        (latitude, longitude) or "%(latitude)s, %(longitude)s".)
        – Coordinates to restrict search within.
        Example: [Point(22, 180), Point(-22, -180)].

bounded (bool) – Restrict the results to only items contained 
        within the bounding view_box.

Right now the documentation is a bit out of date, viewbox is actually just a hint.

So to ask Nominatim to prefer results in the area of Georgia the Country, you could do this:

>>> geolocator.geocode("Georgia", viewbox=((40, 39), (44, 47)))
Location(საქართველო, (41.6809707, 44.0287382, 0.0))

And if you want to make sure that nothing from elsewhere is returned, you would also include the bounded parameter:

>>> geolocator.geocode(
>>>         "Georgia",
>>>         viewbox=((40, 39), (44, 47)), 
>>>         bounded=True
>>> )
Location(საქართველო, (41.6809707, 44.0287382, 0.0))
1

I would use featuretype="country", which returns only countries:

>>> geolocator.geocode("Georgia",featuretype="country")
Location(საქართველო, (41.6808672, 44.0287806, 0.0))

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.