Search code examples
openstreetmapoverpass-apiosmnx

OSMnx: Creating Custom Queries with Alternative Infrastructures


I'm new to OSMnx and Overpass queries in general. I am trying to understand the correct way to write custom queries when working with non-street infrastructure types.

Specifically, I am trying to understand why this query works

import osmnx as ox

my_custom_filter = '["railway"~"disused"]' 

G = ox.graph_from_point((51.5073509,-0.1277583), 
                      distance = 10000,
                      distance_type = 'bbox', 
                      infrastructure = 'way["railway]',
                      network_type = 'none',
                      custom_filter = my_custom_filter
                       )

But this one wields a bad request error:

import osmnx as ox

my_custom_filter = '["railway"~"disused"]' 

G = ox.graph_from_point((51.5073509,-0.1277583), 
                      distance = 10000,
                      distance_type = 'bbox', 
                      infrastructure = 'way["railway~"rail"]',
                      network_type = 'none',
                      custom_filter = my_custom_filter
                       )

Notice the difference is simply that I specifying rail as the type of railway in the latter query.

See the OSM Railway Guide here.

If anyone can point me to any resources which would help me further understand how to construct custom filters - particularly custom filters with more than one filter, that would be excellent also. For example, what would be the correct syntax to add an additional customer filter.


Solution

  • You were just missing a " in your argument. This works:

    import osmnx as ox
    ox.config(log_console=True, use_cache=True)
    point = (51.5073509,-0.1277583)
    dist = 10000
    dt = 'bbox'
    cf = '["railway"~"disused"]' 
    G = ox.graph_from_point(point, dist=dist, dist_type=dt, custom_filter=cf)
    

    But it produces an EmptyOverpassResponse error as there is nothing that matches your query in that search area. You will get a graph however if you change it to this for example:

    cf = '["railway"!~"disused"]' 
    G = ox.graph_from_point(point, dist=dist, dist_type=dt, custom_filter=cf)