Request#

In this page I will describe how to make different types of requests to geopy.

Geopy documentation.

To use it you need to create a Nominatim object and pass it user_agent. In my opinion you can pass whatever you want as user_agent, I don’t really understand how this option is used.

from geopy.geocoders import Nominatim
geolocator = Nominatim(user_agent = "knowledge")

Arbitrary request#

Using the geocode method, you can query the system address as text.

geolocator.geocode("Minsk", language = "en").raw
{'place_id': 184576951,
 'licence': 'Data © OpenStreetMap contributors, ODbL 1.0. http://osm.org/copyright',
 'osm_type': 'relation',
 'osm_id': 59195,
 'lat': '53.9024716',
 'lon': '27.5618225',
 'class': 'boundary',
 'type': 'administrative',
 'place_rank': 7,
 'importance': 0.6699404177567978,
 'addresstype': 'city',
 'name': 'Minsk',
 'display_name': 'Minsk, Belarus',
 'boundingbox': ['53.7938470', '53.9717897', '27.3740176', '28.0799469']}

Request as coordinates#

Using reverse method you can pass any coordinates you like as tuple and got answer.

geolocator.reverse((43, 23), language = "ru").raw
{'place_id': 82486720,
 'licence': 'Data © OpenStreetMap contributors, ODbL 1.0. http://osm.org/copyright',
 'osm_type': 'way',
 'osm_id': 706822395,
 'lat': '42.999751989667615',
 'lon': '23.000019012462104',
 'class': 'highway',
 'type': 'tertiary',
 'place_rank': 26,
 'importance': 0.10000999999999993,
 'addresstype': 'road',
 'name': '813',
 'display_name': '813, Бондин хан, Туден, Годеч, Софийская область, 2240, Болгария',
 'address': {'road': '813',
  'neighbourhood': 'Бондин хан',
  'village': 'Туден',
  'municipality': 'Годеч',
  'county': 'Софийская область',
  'ISO3166-2-lvl6': 'BG-23',
  'postcode': '2240',
  'country': 'Болгария',
  'country_code': 'bg'},
 'boundingbox': ['42.9936767', '42.9998986', '22.9871460', '23.0008811']}

Location object#

Requests to geopy will return the geopy.location.Location object.

The next cell lists the interesting fields.

location_opject = geolocator.geocode("Minsk", language = "ru")

for field in dir(location_opject): 
    if field[0] != "_": print(field)
address
altitude
latitude
longitude
point
raw

raw field is crusical it contains original answer from api.

location_opject.raw
{'place_id': 152512099,
 'licence': 'Data © OpenStreetMap contributors, ODbL 1.0. http://osm.org/copyright',
 'osm_type': 'relation',
 'osm_id': 59195,
 'lat': '53.9024716',
 'lon': '27.5618225',
 'class': 'boundary',
 'type': 'administrative',
 'place_rank': 7,
 'importance': 0.6699404177567978,
 'addresstype': 'city',
 'name': 'Минск',
 'display_name': 'Минск, Беларусь',
 'boundingbox': ['53.7938470', '53.9717897', '27.3740176', '28.0799469']}