Search code examples
pythonopenstreetmaposmnx

Filter OpenStreetMap edges on `Surface` type


I'm accessing OpenStreetMap data using osmnx, using:

import osmnx as ox

graph = ox.graph_from_place('Bennekom')
nodes, edges = ox.graph_to_gdfs(graph)

I know from openstreetmap.org/edit that all (?) street features have an attribute Surface, which can be Unpaved, Asphalt, Gravel, et cetera. However, that info is not included in the GeoDataFrames, so I cannot filter or select certain surface types. Is that somehow possible?


Solution

  • You need to include the surface key in the useful_tags_way :

    import osmnx as ox
    
    ox.settings.useful_tags_way = ["surface"] # << add this line
    
    graph = ox.graph_from_place("Bennekom")
    nodes, edges = ox.graph_to_gdfs(graph)
    

    NB: You might need to explode the surface column in order to get all matches when filtering.

    print(
        edges.iloc[:, :-1]
        .sample(frac=1, random_state=111)
        .to_string(max_rows=10, max_cols=5, index=False)
    )
    
                      osmid                  surface  oneway reversed  length
      [1213295165, 6920271] [paving_stones, asphalt]   False     True  34.747
                    6920805                  asphalt   False     True  69.793
                  189224820                  asphalt   False     True  21.313
                  701436450                      NaN   False    False  14.168
                    6920602            paving_stones   False    False  67.469
                        ...                      ...     ...      ...     ...
    [1281296947, 604729524]                  asphalt   False     True  92.612
                  173209511                      NaN   False     True  84.315
                  604729512                      NaN   False     True 157.071
                  857498475                      NaN   False    False  17.064
                  365986402                      NaN   False     True  14.977