NOTICE: help.openstreetmap.org is no longer in use from 1st March 2024. Please use the OpenStreetMap Community Forum

I constructed a python query to look for all the bus routes passing by a given box. However, I need to speed up the query as much as possible. I only need the lat/lon coordinates of each node representing the bus stops and the name of the routes that they belong to. My current code:

import requests

overpass_url = "http://overpass-api.de/api/interpreter"

bbox = [48.87542724909715, 2.1707384550740683, 48.88884184835508, 2.1821696229817267]
bbox_str = '('+str(bbox)[1:-1]+')'

overpass_query = '''
[out:json];
(
relation'''+bbox_str+'''['route'='bus'];
);
out body;
>;
out skel;
'''

response = requests.get(overpass_url, params={'data': overpass_query})
data = response.json()

asked 16 Feb '21, 17:51

fragodec's gravatar image

fragodec
16225
accept rate: 0%

edited 16 Feb '21, 17:53


I don't know if it is more efficient and/or faster but you could search for every bus stop in a given bounding box and then recurse back to the relations that have that stop as member:

[out:json][timeout:25];

// query part for: “highway=bus_stop”
node["highway"="bus_stop"]({{bbox}})->.stops;

foreach.stops ->.s(

  //get the route (stop must be a member!)
  rel(bn.s)[type=route]->.r;

  //produce output
  make stop name = s.u(t["name"]),
  lat = s.u(lat()), 
  lon = s.u(lon()),
  lines = r.set(t["ref"]);

  //print
  out meta;     
);
permanent link

answered 20 Feb '21, 21:26

MarcoR's gravatar image

MarcoR
6873820
accept rate: 23%

Thank you for your answer. This works fine but unfortunately it is slower than my code (with the examples I tried).

(22 Feb '21, 11:17) fragodec

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×147
×78
×39
×9
×9

question asked: 16 Feb '21, 17:51

question was seen: 1,649 times

last updated: 22 Feb '21, 11:18

NOTICE: help.openstreetmap.org is no longer in use from 1st March 2024. Please use the OpenStreetMap Community Forum