Samuel
Samuel

Reputation: 231

Coloring edges in OSMnx graph based on edge attribute

I want to create a map of the roads within a country, and color the edges based on their "highway" attribute, so that motorways are yellow, trunk green, etc...

However, when following the osmnx example files and attempting to replicate, i receive the following error message: Input:

ec = ox.plot.get_edge_colors_by_attr(graph, attr='highway', cmap='plasma_r')

Output:

TypeError: '<=' not supported between instances of 'str' and 'list'

I'm assuming this is because "highway" is not a numeric variable? This is the code I currently have for the graph

    graph = ox.io.load_graphml("graph.graphml")
    nodes, streets = ox.graph_to_gdfs(graph)
    streets.head()

Output:


       osmid  oneway lanes  ref    highway    junction    length                                           geometry              name maxspeed bridge tunnel access width service           u           v  key
0  659557392    True     1  410  secondary  roundabout    48.672  LINESTRING (-21.93067 64.05665, -21.93067 64.0...               NaN      NaN    NaN    NaN    NaN   NaN     NaN  6175252481  6175252453    0
1  659557393   False     2  410  secondary         NaN   132.007  LINESTRING (-21.93067 64.05665, -21.93057 64.0...   Kaldárselsvegur      NaN    NaN    NaN    NaN   NaN     NaN  6175252481  6275284224    0
2   48547677    True   NaN  430  secondary         NaN   237.337  LINESTRING (-21.72904 64.13621, -21.72959 64.1...     Skyggnisbraut       50    NaN    NaN    NaN   NaN     NaN  5070446594   616709938    0
3  160506796   False   NaN  430  secondary         NaN  2892.051  LINESTRING (-21.72904 64.13621, -21.72848 64.1...  Úlfarsfellsvegur       70    NaN    NaN    NaN   NaN     NaN  5070446594    56620274    0
4  157591872    True     2   41      trunk  roundabout    47.075  LINESTRING (-21.93736 64.06693, -21.93730 64.0...        Hlíðartorg       60    NaN    NaN    NaN   NaN     NaN    12886026    12885866    0

Upvotes: 3

Views: 1914

Answers (1)

gboeing
gboeing

Reputation: 6442

I'm assuming this is because "highway" is not a numeric variable?

Yes. As you can see in the OSMnx docs, the ox.plot.get_edge_colors_by_attr function expects the attr argument to be the "name of a numerical edge attribute." In your example, it's not numeric. Instead, you can use the ox.plot.get_colors function to get one color for each highway type in the graph, then get a list of colors for the edges based on each's highway type:

import osmnx as ox
import pandas as pd
ox.config(use_cache=True, log_console=True)

G = ox.graph_from_place('Piedmont, CA, USA', network_type='drive')

# get one color for each highway type in the graph
edges = ox.graph_to_gdfs(G, nodes=False)
edge_types = edges['highway'].value_counts()
color_list = ox.plot.get_colors(n=len(edge_types), cmap='plasma_r')
color_mapper = pd.Series(color_list, index=edge_types.index).to_dict()

# get the color for each edge based on its highway type
ec = [color_mapper[d['highway']] for u, v, k, d in G.edges(keys=True, data=True)]
fig, ax = ox.plot_graph(G, edge_color=ec)

Upvotes: 3

Related Questions