Open In App

Get OSM Features Within a Distance of a Point Using Python OSMnx Feature Module

Last Updated : 17 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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

fetaure_from_point_gdf

park details for the first 5 rows

Let’s list all the available feauters (columns) using geodataframe info() method.

Python3
gdf.info()

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

feature_from_point_plot

plot showing park bounding box

We can plot it on a map by using the explore() method from geodataframe.

Python3
gdf.explore()

Output

feature_from_point_map

Park details

Find 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:

feature_from_leisure_map

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:

feature_multiple_point_gdf

features based on multiple tags



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads