mocnik-science / osm-python-tools

A library to access OpenStreetMap related services
GNU General Public License v3.0
428 stars 47 forks source link

OSMPythonTools.nominatim output preferred language #67

Open pankus opened 1 year ago

pankus commented 1 year ago

Maybe I didn't understand how to do it, but it would be very useful to be able to pass a language-related parameter in Nominatim. Something similar to: loc = nominatim.query(*coordinates, reverse=True, wkt=True, language='en' )

ikomash commented 1 year ago

@pankus, If you take a look at official Nominatim API page https://nominatim.org/release-docs/latest/api/Search/#language-of-results, you can find this line: accept-language=<browser language string>. According to library source code, Nominatim.query() accepts **kwargs which are connected with request parameters params = kwargs['params'] if 'params' in kwargs else {} allowing you to pass original API parameters. So, you can do something like this:

from OSMPythonTools.nominatim import Nominatim
nominatim = Nominatim()
loc = nominatim.query("United Arab Emirates", params = {'accept-language':'en'})
print(loc.displayName())
# United Arab Emirates

Without params = {'accept-language':'en'} it returns it's local language variant الإمارات العربية المتحد In your case of reverse search, it will also try to return attributes in the desired language (if they exist in OSM database) for methods like loc.address().