Get OSM Features Within a Distance of a Point Using Python OSMnx Feature Module
Last Updated :
17 Apr, 2024
In this article, we will see how we can get open street map features within a distance of a point (latitude-longitude) using the OSMnx feature module in Python.
Syntax of osmnx.features.features_from_point() Function
The function creates a GeoDataFrame of OSM features within some distance of a point N, S, E, and W. Below is the syntax:
osmnx.features.features_from_point(center_point, tags, dist=1000)
Parameters:
- center_point (tuple) – the (lat, lon) center point around which to get the features
- tags (dict) – Dict of tags used for finding elements in the selected area.
- dist (numeric) – distance in meters
Returns: gdf
Return Type: geopandas.GeoDataFrame
OSM Features Within a Distance of Point Using OSMnx Feature Module
Below are some approaches by which we can find OSM features within a distance of point using OSMnx feature module in Python:
Find the Park Details (under Leisure) From the Open Street Map
Here, we get the park details within 1000 m of a point. In the below code, 'leisure' is the main tag (key) and 'park' is the subtag (value). The code as follows:
Python3
import osmnx as ox
# latitude-longitude point
center_point = (33.299896, -111.831638)
# osm tag
tags = {"leisure": "park"}
# retrieve feature from point
gdf = ox.features_from_point(center_point, tags, dist=1000)
# list first 5 rows from geodataframe
gdf.head(5)
Output
park details for the first 5 rows Let's list all the available feauters (columns) using geodataframe info() method.
Python3
Output
<class 'geopandas.geodataframe.GeoDataFrame'>
MultiIndex: 15 entries, ('way', 493099632) to ('way', 1125075333)
Data columns (total 17 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 nodes 15 non-null object
1 leisure 15 non-null object
2 name 5 non-null object
3 operator 2 non-null object
4 operator:type 2 non-null object
5 operator:wikidata 2 non-null object
6 geometry 15 non-null geometry
7 addr:city 2 non-null object
8 addr:housenumber 2 non-null object
9 addr:postcode 2 non-null object
10 addr:state 2 non-null object
11 addr:street 2 non-null object
12 description 1 non-null object
13 source 2 non-null object
14 wikidata 2 non-null object
15 website 1 non-null object
16 gnis:feature_id 1 non-null object
dtypes: geometry(1), object(16)
memory usage: 2.9+ KB
Plotting the OSM Features Using plot_footprints() Method
In this step, we will plot the OSM features using plot_footprints() method by passing the geodataframe inside it.
Python3
fig, ax = ox.plot_footprints(gdf, figsize=(3, 3))
Output
plot showing park bounding box We can plot it on a map by using the explore() method from geodataframe.
Python3
Output
Park detailsFind the Leisure Details From the Open Street Map
Apart from 'park' there are many other subtags under 'leisure' key such as swimming_area, sports_center, garden etc. To get the entire features under leisure key, we just need to mention the tag as
tags = {"leisure": True}
Let's look at the below code:
Python3
import osmnx as ox
center_point = (33.299896, -111.831638)
tags = {"leisure": True}
# feature from point
gdf = ox.features_from_point(center_point, tags, dist=1000)
# display it on map
gdf.explore()
Output:
leisure details Get Multiple Map Features from Open Street Map
Let's try multiple tags. We can try the below tag
tags = {'historic':True,
'natural':['grassland','tree_row'],
'landuse':'religious'}
In the below code, it lists the entire historic details since we set 'historic' tag as true; In case of 'natural' tag, OSMnx fetches details based on 'grassland' and 'tree_row' subtags. Similary the 'religious' subtag details from 'landuse' tag.
Python3
import osmnx as ox
# latitude-longitude point
center_point = (33.299896, -111.831638)
# osm tag
tags = {'historic': True,
'natural': ['grassland', 'tree_row'],
'landuse': 'religious'}
# retrieve features from point
gdf = ox.features_from_point(center_point, tags, dist=1000)
# display specific columns from geodataframe
gdf[['geometry', 'name', 'historic', 'landuse', 'religion', 'natural']]
Output:
features based on multiple tags
Similar Reads
Get OSM Features Within a Distance of Address Using OSMnx Feature Module OpenStreetMap provides physical features on the ground (e.g ., roads, buildings, etc.) using tags attached to its basic data structure (nodes, ways, and relations). Each tag describes a geographic attribute of the feature for the specific node, way, or relation. In this article, we will see how we c
6 min read
Find the Nearest Node to a Point Using OSMnx Distance Module Nearest neighbor search algorithms are crucial for robust navigation. OSMnx makes use of different types of nearest neighbor search algorithms for the projected and unprotected graph. In this article, we will see how we can find the nearest node to a point using OSMnx distance module in Python. Synt
7 min read
Find the Nearest Edge to a Point Using Python OSMnx Distance Module OSMnx distance module can find the nearest edge to a point using the nearest_edges functionality. In this article, we will see how to find the nearest edge to a point using the OSMnx distance Module in Python. Syntax of osmnx.distance.nearest_edges() FunctionHere, the below function uses an R-tree s
5 min read
Calculate Euclidean Distance Using Python OSMnx Distance Module Euclidean space is defined as the line segment length between two points. The distance can be calculated using the coordinate points and the Pythagoras theorem. In this article, we will see how to calculate Euclidean distances between Points Using the OSMnx distance module. Syntax of osmnx.distance.
4 min read
How to print all files within a directory using Python? The OS module is one of the most popular Python modules for automating the systems calls and operations of an operating system. With a rich set of methods and an easy-to-use API, the OS module is one of the standard packages and comes pre-installed with Python. In this article, we will learn how to
3 min read