Querying POIS from OSM using Python

John R. Ballesteros
3 min readOct 21, 2023

--

Some days ago I needed to know the existing schools in Florida, fortunately, in one of my previous posts I delved into how to download roads from OSM, for a certain region of interest:

https://medium.com/@jrballesteros/no-more-gis-digitizing-3fa03335f8be.

However, this time the OSMNX, another available API to query data from Open Street Map (OSM) will be used. The following is the description of the steps to query, download and display on a map the up to date poi information available in OSM.

First of all download Python, my suggestion is to use miniconda, you can find the details in quite a simple way in this link:

https://docs.conda.io/projects/miniconda/en/latest/miniconda-install.html

After that, create a Python environment by opening Anaconda Prompt (miniconda3) , activate it and install OSMNX and other important libraries like: pandas, geopandas, folium and jupyter notebook, like shown in the image below.

Figure 1. Creating an environment and installing libraries with miniconda

Open jupyter notebook and create a new one using Python 3 ipykernel. First import all the libraries that will be used.

import osmnx as ox
import folium
import geopandas

Define a map bounding box, which are the extreme geographical coordinates (WGS84) of the specific region of interest.

# The bounding box
north = 30.910093
south = 25.00042
east = -79.450636
west = -85.855045

POIS in OSM are called amenities, so pick the category you need, ie school, church, restaurant, etc


tags = {"amenity": 'school'} # (dict) – Dict of tags used for finding objects in the selected area: pois.
mypois = ox.features.features_from_bbox(north, south, east, west, tags) # the table
print(len(mypois))
mypois.head(5)

# pick the fields of interest
mypois = mypois[["amenity", "name", "geometry"]]

# create a geodataframe in a specific CRS and save the result into a GeoJson file
gdf = geopandas.GeoDataFrame(mypois, crs="EPSG:4326")
gdf.set_geometry('geometry', crs={'init': 'epsg:4326'})
gdf.to_file('schools.geojson', driver='GeoJSON')

Let’s visualize the results in a map, for that, the folium library come into place.

import folium

# create a map centered in specific lat lon, with a medium zoom and a map base
mymap = folium.Map([28.064443537329, -81.5057858485094],zoom_start=6.5,tiles='cartodbpositron')

# add latlon coordinates to the map
folium.LatLngPopup().add_to(mymap)

# add the previously saved GeoJson to the map
myGeoJson = 'schools.geojson'
folium.GeoJson(myGeoJson).add_to(mymap)

# save and display the map
mymap.save("FloridaSchools.html")

mymap

See the results…

Figure 2. html map of schools in the Florida State from OSM

Conclusion

OSMNX library allows users to query and download pois from OSM for a specific region with few lines of code. The results can be visualized and shared in a html map.

Support me

Enjoying my work? Show your support with Buy me a coffee, a simple way for you to encourage me and others to write. If you feel like it, just click the next link and I will enjoy a cup of coffee!

--

--

John R. Ballesteros

Ph.D Informatics, Assoc. Professor of the UN, Med. Colombia. GenAi, Consultant & Researcher AI & GIS, Serial Interpreneur Navione Drone Services Co, Gisco Maps