Skip to contents

Builds a per-edge penalty column from the edge highway class: each edge gets factor * weight, where factor comes from the named penalties vector (classes not listed get 0). The column quantifies how much of a route runs on penalized road types, in the units of weight.

Usage

ox_add_edge_penalties(g, penalties, weight = "length", name = "penalty")

Arguments

g

An osm_graph.

penalties

Named numeric vector of highway = factor pairs; each edge's penalty is factor * weight. Classes not listed (and edges with no highway) get 0.

weight

Edge column the factors multiply. Default "length".

name

Name of the new edge column. Default "penalty".

Value

The osm_graph with the penalty edge column added.

Details

Two ways to use it:

  • Scalarized routing — add the penalty onto the routing weight (g$edges$w <- g$edges$length + g$edges$penalty) and route on w with ox_shortest_path(): an ordinary shortest path that trades length against preference at a fixed rate.

  • Constrained / Pareto routing — pass the column as the resource of ox_route_constrained() or ox_route_pareto(), e.g. "fastest route with at most 500 m on residential streets" (penalties = c(residential = 1), budget = 500).

Examples

g <- example_osm_graph()
g <- ox_add_edge_penalties(g, c(residential = 1))
head(g$edges$penalty)
#> [1] 100 100 100 100 100 100