First of all, thank you so much for developing this very useful package!
I am working on calculating a travel time matrix (by car) between each "iris" (essentially neighborhoods) in France. Since the full .pbf file for France was too large for r5r to process (4.5 GB), I first filtered the file following guidelines from conveyal . Then, I implemented a solution where I select a 100 km buffer around each French department, calculate the bounding box, and crop the .pbf file accordingly.
For cropping the .pbf file, I used Osmosis as recommended in FAQ 4 of r5r. However, for several departments I encountered errors when setting up r5. Specifically, I was receiving this error: Error in setup_r5(...) : java.lang.NullPointerException.
According to these issues (1, 2), it seemed to me that this error may occur when ways and relations are excluded during cropping. Therefore, I modified the Osmosis cropping code by including clipIncompleteEntities=yes (as suggested here).
This approach has allowed me to compute the travel time matrix for most departments successfully. However, I am still encountering an issue with two departments (departments code==33 and 60). When setting up r5r using the pbf files related to these departments, I receive the following error:
r5r_core <- setup_r5("D:\\France\\network_60\\bo", verbose = FALSE)
No raster .tif files found. Using elevation = 'NONE'.
Using cached R5 version from C:/Users/Asus/AppData/Local/R/win-library/4.2/r5r/jar/r5-v6.9-all.jar
2024-11-11 17:10:06,587 [main] ERROR c.c.r.s.StreetLayer - Continuing to load but ignoring generalized costs due to exception: java.lang.RuntimeException: All ways are expected to have generalized cost tags. Missing: slope_1:forward
Error in setup_r5(sprintf("D:\\France\\network_%s\\bo", dep_code), verbose = FALSE) :
java.lang.IllegalArgumentException: Invalid number of points in LineString (found 1 - must be 0 or >= 2)
This is the result form running the r5r::r5r_sitrep() function:
Do you have any idea on waht might be causing this issue? I am including my code, and the cropped .pbf file for one of the two departments causing the error.
Thank you in advance for your help!
Here is the full r code:
options(java.parameters = '-Xmx12G')
options(scipen=999)
library(sf)
library(r5r)
library(tidyverse)
library(arrow)
library(fs)
library(accessibility)
library(mapview)
departement_fr <- st_read(here::here("coordinates\\ADMIN-EXPRESS-COG_1-1__SHP__FRA_2018-04-03\\ADMIN-EXPRESS-COG\\1_DONNEES_LIVRAISON_2018-03-28\\ADE-COG_1-1_SHP_LAMB93_FR\\DEPARTEMENT.shp"))
load("data\\iris_adm.Rdata")
load("data\\school_france_ttm.Rdata")
fr_iris <- st_transform(fr_iris, 4326)
fr_iris_tmp <- fr_iris %>%
select(code_iris)
centroids_fr <- as.data.frame(st_coordinates(st_centroid(fr_iris$geometry))) %>%
rename(lon = 1, lat = 2)
centroids_fr <- cbind(centroids_fr, fr_iris_tmp)
# List all unique department codes
unique_departments <- unique(departement_fr$INSEE_DEP)
# Initialize timing for the full process
total_start_time <- Sys.time()
# Initialize a vector to store department codes with errors
error_departments <- c()
for (dep_code in unique_departments ) {
cat(paste0("Processing department: ", dep_code, "\n"))
# Select current department
department_x <- departement_fr %>%
st_transform(4326) %>%
filter(INSEE_DEP == dep_code)
# Create a 100 km buffer around the selected department
buffer_100km <- st_buffer(department_x, dist = 100000) # 100 km
# Find all neighborhoods that intersect with this 100 km buffer
neighborhoods_in_buffer <- fr_iris %>%
st_filter(buffer_100km, .predicate = st_intersects)
# Compute bounding box for area
area_bbox <- st_bbox(neighborhoods_in_buffer)
area_bbox_sf <- st_as_sfc(area_bbox)
# Define paths for temporary files created by Osmosis
folder_path <- sprintf("D:\\France\\network_%s\\", dep_code)
dir.create(folder_path, recursive = TRUE)
smaller_pbf <- sprintf("D:\\France\\network_%s\\smaller_%s.pbf", dep_code,dep_code)
network_dat <- "D:\\France\\network\\network.dat"
network_settings <- "D:\\France\\network\\network_settings.json"
mapdb <- paste0(smaller_pbf, ".mapdb")
mapdb_p <- paste0(smaller_pbf, ".mapdb.p")
large_pbf_path <- "D:\\France\\ttm\\filtered.osm.pbf"
# Run Osmosis command to create a smaller .pbf file
osmosis_cmd <- sprintf("%s --read-pbf %s --bounding-box left=%s bottom=%s right=%s top=%s clipIncompleteEntities=yes --write-pbf %s",
"osmosis",
large_pbf_path,
as.numeric(area_bbox$xmin), as.numeric(area_bbox$ymin),
as.numeric(area_bbox$xmax), as.numeric(area_bbox$ymax),
smaller_pbf)
# Execute the Osmosis command
shell(osmosis_cmd, translate = TRUE)
gc()
# Try to set up r5r core with error handling
r5r_core <- tryCatch({
setup_r5(sprintf("D:\\France\\network_%s\\", dep_code), verbose = FALSE)
}, error = function(e) {
cat(paste0("Error in setting up r5r for department ", dep_code, ": ", e$message, "\n"))
# Append the department code to error_departments vector
error_departments <<- c(error_departments, dep_code)
return(NULL) # Return NULL if there's an error
})
# Skip to the next iteration if setup_r5 failed
if (is.null(r5r_core)) {
files_to_delete <- c(folder_path, smaller_pbf, network_dat, network_settings, mapdb, mapdb_p)
for (file_path in files_to_delete) {
if (file.exists(file_path)) {
file.remove(file_path)
cat(paste0("Deleted temporary file: ", file_path, "\n"))
}
}
next
}
stop_r5(r5r_core)
gc()
files_to_delete <- c(folder_path, smaller_pbf, network_dat, network_settings, mapdb, mapdb_p)
for (file_path in files_to_delete) {
if (file.exists(file_path)) {
file.remove(file_path)
cat(paste0("Deleted temporary file: ", file_path, "\n"))
}
}
gc()
}
total_end_time <- Sys.time()
cat(paste0("Total processing time: ", total_end_time - total_start_time, " seconds.\n"))
Hi @rafapereirabr , I’m sorry to bother you directly. I was wondering if you think this issue is not strictly related to r5r, and if I should seek help elsewhere. Many thanks again!
First of all, thank you so much for developing this very useful package!
I am working on calculating a travel time matrix (by car) between each "iris" (essentially neighborhoods) in France. Since the full .pbf file for France was too large for r5r to process (4.5 GB), I first filtered the file following guidelines from conveyal . Then, I implemented a solution where I select a 100 km buffer around each French department, calculate the bounding box, and crop the .pbf file accordingly.
For cropping the .pbf file, I used Osmosis as recommended in FAQ 4 of r5r. However, for several departments I encountered errors when setting up r5. Specifically, I was receiving this error:
Error in setup_r5(...) : java.lang.NullPointerException
.According to these issues (1, 2), it seemed to me that this error may occur when ways and relations are excluded during cropping. Therefore, I modified the Osmosis cropping code by including
clipIncompleteEntities=yes
(as suggested here).This approach has allowed me to compute the travel time matrix for most departments successfully. However, I am still encountering an issue with two departments (departments code==33 and 60). When setting up r5r using the pbf files related to these departments, I receive the following error:
This is the result form running the
r5r::r5r_sitrep()
function:Do you have any idea on waht might be causing this issue? I am including my code, and the cropped .pbf file for one of the two departments causing the error.
Thank you in advance for your help!
Here is the full r code: