Skip to content
Snippets Groups Projects
Commit 69dca56f authored by Martin Schrön's avatar Martin Schrön
Browse files

New feature to plot road types in OsmGraph

parent ed694fb1
No related branches found
No related tags found
No related merge requests found
......@@ -3,12 +3,36 @@ CoRNy exdata.OpenStreetMap
Get Data from OpenStreetMap
"""
import numpy as np
from corny import *
import pandas
from shapely.geometry import Point,Polygon
class OsmGraph:
road_colors = dict(
footway = "C7",
mixed = "black",
path = "C2",
primary = "C3",
service = "C8",
track = "C5",
trunk = "C4",
trunk_link = "C6",
unclassified = "#CCCCCC",
motorway = "red",
motorway_link = "pink",
residential = "C9",
secondary = "C1",
tertiary = "C0",
road = "#888888",
primary_link = "violet",
secondary_link = "plum",
tertiary_link = "thistle",
none = "white"
)
def __init__(self, network="rivers", bbox=None):
self.network = "none"
......@@ -19,7 +43,8 @@ class OsmGraph:
self.bbox = bbox
self.graph = None
self.shape = None
self.data = None
self.road_colors = OsmGraph.road_colors
def download_graph(self, bbox=None, **kwargs):
"""
......@@ -38,79 +63,92 @@ class OsmGraph:
return(self)
def make_shape(self):
def to_data(self):
"""
Convert a graph to a shape
Convert a graph to a data
"""
if self.graph is None:
print("! OsmGraph: missing graph, use download_graph() first.")
return()
self.data = None
# print("! OsmGraph: missing graph, use download_graph() first.")
return(self)
import osmnx as ox
try:
self.shape = ox.graph_to_gdfs(
self.data = ox.graph_to_gdfs(
self.graph,
nodes=False, edges=True,
fill_edge_geometry=True)
except:
self.shape = None
print("! OsmGraph: cannot convert network graph of %s." % self.network)
self.data = None
# print("! OsmGraph: cannot convert network graph of %s." % self.network)
return(self)
def get(self):
graph_kw = dict()
"""
Wrapper to generate river or road data
"""
if self.network == "rivers":
graph_kw = dict(
simplify = False,
truncate_by_edge = True,
retain_all = True,
custom_filter = '["waterway"~"river"]'
)
self.get_rivers()
elif self.network == "roads":
graph_kw = dict(
simplify = True,
truncate_by_edge = True,
retain_all = True
)
self.get_roads()
self.download_graph(self.bbox, **graph_kw)
self.make_shape()
return(self)
def get_rivers(self, bbox):
def get_rivers(self):
"""
Get shape of network graph of rivers
Get data of network graph of rivers
"""
self.download_graph(
bbox,
self.bbox,
simplify = False,
truncate_by_edge = True,
retain_all = True,
custom_filter = '["waterway"~"river"]'
)
self.make_shape()
return(self.shape)
self.to_data()
return(self)
def get_roads(self, bbox):
def get_roads(self):
"""
Get shape of network graph of roads
Get data of network graph of roads
"""
self.download_graph(
bbox,
self.bbox,
simplify = True,
truncate_by_edge = True,
retain_all = True
)
self.make_shape()
return(self.shape)
self.to_data()
if not self.data is None:
self.data["highway_str"] = ""
for i, row in self.data.iterrows():
row = row.copy()
if isinstance(row["highway"], list):
if 'primary' in row["highway"]:
self.data.loc[i, 'highway_str'] = 'primary'
elif 'track' in row["highway"]:
self.data.loc[i, 'highway_str'] = 'track'
else:
self.data.loc[i, 'highway_str'] = 'mixed'
else:
self.data.loc[i, 'highway_str'] = row["highway"]
# Unique list of road categories
self.road_categories = np.unique(self.data["highway_str"].values)
# Set every unknown road category to none
self.road_categories = [r if r in OsmGraph.road_colors else "none" for r in self.road_categories]
return(self)
def plot(self, ax):
"""
Plotting routine for OsmGraphs. Expects ax.
"""
if self.shape is None:
if self.data is None:
print("! OsmGraph %s: Nothing to plot." % self.network)
else:
plot_kw = dict()
......@@ -125,9 +163,29 @@ class OsmGraph:
color="k", alpha=0.3,
lw=1, ls=":")
self.shape.plot(
self.data.plot(
ax=ax, **plot_kw)
def plot_road_types(
self, ax,
legend_kw = dict(bbox_to_anchor=(1.01, 1), loc='upper left', frameon=False)
):
"""
Plotting routine for OsmGraphs road types. Expects ax.
"""
if self.data is None or self.network != "roads":
print("! OsmGraph %s: Nothing to plot." % self.network)
else:
for cat in self.road_categories:
self.data.query("'%s' in highway_str" % cat).plot(
ax=ax,
label=cat,
color=self.road_colors[cat],
lw=1
)
ax.legend(**legend_kw)
#######################################################
##### Automatic OSM Lookup from Overpass API
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment