Accessing the "Query Features" functionality from Python

I’m trying to write a Python code that, from a pair of coordinates, determines the features at that coordinate. (What I mainly need is to determine the country the point is located in, as well as if it is located in a body of water.) I have found the OSMPythonTools library, though the documentation is quite hard to understand. I don’t have any experience with the OSM API.

Thank you very much for your help in advance!

Note that such reverse geocoding can be done locally, see for example GitHub - westnordost/countryboundaries: fast offline reverse country geocoding: find country given a geo position (if for some reason this tool is not fitting, “reverse country geocoding” should find more)

Doing the same with water - that depends whether you would be happy enough about checking is point with ocean or do you want to check even tiny ponds

2 Likes

See here the Reverse geocoding section.
Copying from examples your code should be like this:

from OSMPythonTools.nominatim import Nominatim
nominatim = Nominatim()
Heidelberg = nominatim.query(49.4093582, 8.694724, reverse=True, zoom=3)
country=Heidelberg.displayName()
2 Likes

I think I found a solution for the water question:

from OSMPythonTools.overpass import Overpass

overpass = Overpass()

lat = "46.8445"   # input("Latitude: ")
lon = "17.7789"   # input("Longitude: ")
q = 'is_in(' + str(lat) + ', ' + str(lon) + '); out body;'
result = overpass.query(q)

print("water" in [item.tag('natural') for item in result.elements()])
1 Like

The query features functionality on the web site is just making queries to an overpass server - there is no API to it on www.openstreetmap.org but you can use the overpass API in the same way the web site does.

You can see the code that builds the queries we use, and a long comment describing them in the github repository.

4 Likes