python - OSMNX Intersect Two Tags - Stack Overflow

admin2025-05-01  1

I try to create a visualization about the 'Number Of Cafes Within A 200 M Radius Of The Transjakarta Route' using OSMNX. I do my work here.

But I have some questions here:

  1. Yes, I plot the Transjakarta route, plot the cafe, inside the map of Jakarta. But I want to remove all the cafe's plots that are far away from Transjakarta's line. I just want to show the cafe that intersects with Transjakarta's line because the radius range is only 200m from Transjakarta's line. How can I do that?
  2. How can I show the legend title?

Thank you.

I try to create a visualization about the 'Number Of Cafes Within A 200 M Radius Of The Transjakarta Route' using OSMNX. I do my work here.

But I have some questions here:

  1. Yes, I plot the Transjakarta route, plot the cafe, inside the map of Jakarta. But I want to remove all the cafe's plots that are far away from Transjakarta's line. I just want to show the cafe that intersects with Transjakarta's line because the radius range is only 200m from Transjakarta's line. How can I do that?
  2. How can I show the legend title?

Thank you.

Share Improve this question asked Jan 2 at 22:25 lokalhangattlokalhangatt 797 bronze badges 1
  • Please provide a minimal reproducible code snippet here directly in your post. – gboeing Commented Jan 3 at 15:55
Add a comment  | 

1 Answer 1

Reset to default 1

I want to remove all the cafe's plots that are far away from Transjakarta's line. I just want to show the cafe that intersects with Transjakarta's line because the radius range is only 200m from Transjakarta's line. How can I do that?

You just need to intersect your cafes' points with a 200-meter buffer around your transit lines.

import osmnx as ox

# get the transit lines near some point
point = (34.05, -118.25)
tags = {"railway": ["light_rail", "subway"]}
gdf_rail = ox.features.features_from_point(point, tags, dist=1500)
bbox = gdf_rail.union_all().envelope
gdf_rail = ox.projection.project_gdf(gdf_rail)

# get the cafes within 200 meters of the transit lines
tags = {"amenity": "cafe"}
gdf_poi = ox.features.features_from_polygon(bbox, tags).to_crs(gdf_rail.crs)
gdf_poi = gdf_poi[gdf_poi.intersects(gdf_rail.union_all().buffer(200))]
gdf_poi.shape

# plot the transit lines and nearby cafes
ax = gdf_rail.plot()
ax = gdf_poi.plot(ax=ax)

How can I show the legend title?

Previously answered at Title for matplotlib legend

转载请注明原文地址:http://anycun.com/QandA/1746094226a91582.html