mocnik-science / osm-python-tools

A library to access OpenStreetMap related services
GNU General Public License v3.0
440 stars 48 forks source link

HTTP Error 400: Bad Request for query that is successful in overpass turbo #58

Closed csmotion closed 2 years ago

csmotion commented 2 years ago

First, let me say that OSMPythonTools is really excellent, the work is much appreciated!

I'm looking for major/minor road intersections per this example: https://gis.stackexchange.com/questions/296278/overpass-api-find-road-intersection-points-with-defined-line

I manually construct my query as follows:

query = '[bbox:%3.6f, %3.6f, %3.6f, %3.6f]; way[highway~\"^(motorway|trunk|primary|secondary|tertiary|(motorway|trunk|primary|secondary)_link)$\"]->.major; way[highway~\"^(unclassified|residential|living_street|service)$\"]->.minor; node(w.major)(w.minor); out body geom;' % (bbox[0], bbox[1], bbox[2], bbox[3])
logger.info(query)

# Query
self.intersections = self.overpass.query(query, timeout=60)

My constructed query is as follows, and successfully runs in overpass turbo, but I get HTTP Error 400: Bad Request when running overpass.query.

[bbox:40.115150, -105.143300, 40.215150, -105.043300]; way[highway~"^(motorway|trunk|primary|secondary|tertiary|(motorway|trunk|primary|secondary)_link)$"]->.major; way[highway~"^(unclassified|residential|living_street|service)$"]->.minor; 
node(w.major)(w.minor); out body geom;

Thoughts?

csmotion commented 2 years ago

Noticed this in the error. The semi-colon between [out:json] and [bbox causes an invalid request in overpass turbo. Removing it results nets a valid request:

WARNING:OSMPythonTools:[overpass] downloading data: [timeout:60][out:json];[bbox:40.115150, -105.143300, 40.215150, -105.043300]; way[highway]->.major; way[highway]->.minor; node(w.major)(w.minor); out body geom; ERROR:OSMPythonTools:The requested data could not be downloaded. HTTP Error 400: Bad Request

I'm still not sure how to place bbox in the manually formed query in a way that yields a valid request.

csmotion commented 2 years ago

Ok, this is my bad. Looks like solution is to stuff into the settings arg of query call like so:

# Construct intersection query
query = 'way[highway]->.major; way[highway]->.minor; node(w.major)(w.minor); out body geom;'
strBbox = "%3.6f, %3.6f, %3.6f, %3.6f" % (bbox[0], bbox[1], bbox[2], bbox[3])
bbox = {'bbox': strBbox}
logger.info(query)

# Query     
self.intersections = self.overpass.query(query, timeout=60, settings=bbox)
logger.info(self.intersections.countNodes())