cpp_customize: Customize a CCH with edge weights

View source: R/cch.R

cpp_customizeR Documentation

Customize a CCH with edge weights

Description

Update a prepared CCH with the edge weights that should be used for shortest path queries. Customization is much cheaper than preparing the CCH topology, so it is the step to repeat when congestion changes travel times.

Usage

cpp_customize(CCH, weights = NULL)

Arguments

CCH

An object generated by cpp_contract with customizable = TRUE.

weights

Optional edge weights. Defaults to the original graph cost. Give a single column name ("dist" or a column imported with makegraph(aux = ...)) to select a stored weight, or one numeric value per edge of CCH$original$data.

Details

The recommended way to customize a CCH is to import the weights up front with makegraph using the aux argument and select them by column name, e.g. cpp_customize(cch, weights = "time"). This is safer than writing a raw vector at query time: the edge table stored in the CCH doubles when the graph was built with directed = FALSE, so a raw vector is easy to get the wrong length or order for, and a mismatch is silently accepted. A raw numeric vector must then hold one value per edge of CCH$original$data, in that exact order.

Value

A customized CCH metric object usable by get_distance_pair, get_distance_matrix and get_aon.

Examples

RcppParallel::setThreadOptions(numThreads = 1)

# Travel time as the main cost, road length as an auxiliary weight
edges <- data.frame(from_vertex = c(0, 0, 1, 1, 2),
                    to_vertex = c(1, 2, 2, 3, 3),
                    cost = c(1, 4, 1, 2, 1))
graph <- makegraph(edges, directed = TRUE,
                   aux = data.frame(length = c(1, 3, 2, 4, 2)))
cch <- cpp_contract(graph, customizable = TRUE)

# First customization with the travel time (the default)
metric_time <- cpp_customize(cch)
get_distance_pair(metric_time, from = c(0, 1), to = c(3, 3))

# Second customization with the road length, selected by column name
metric_length <- cpp_customize(cch, weights = "length")
get_distance_pair(metric_length, from = c(0, 1), to = c(3, 3))

cppRouting documentation built on Nov. 12, 2026, 5:11 p.m.