ropensci / opentripplanner

An R package to set up and use OpenTripPlanner (OTP) as a local or remote multimodal trip planner.
https://docs.ropensci.org/opentripplanner
GNU General Public License v3.0
79 stars 20 forks source link

[Feature Request] OPTv2 support - partially already implemented for isochrones #114

Open e-kotov opened 8 months ago

e-kotov commented 8 months ago

Hello! I have made some changes in the package to support the OTPv2 experimental (and very limited) isochrones API for my work.

I tried to make minimal changes, though, at the moment, they do seem more like a hack rather than a proper v2 implementation. Specifically, I fixed the jar download function to work with GitHub releases of OTP, adjusted the code for otp_isochrone() to form correct request urls for the v2 API and to provide some more warnings on the state of the v2 API. Overview of the changes are found here: https://github.com/ropensci/opentripplanner/compare/master...e-kotov:opentripplanner:otp_v2

My tests did not show any breakage of the v1 functionality. Let me know if you would like to merge these changes to the main branch (i will then make a pull request) and if you would like to add more of the v2 support into the package - therefore, I might work on adding those and submitting pull requests.

You can check how it works with V2 with the following code:

# I am using locally extracted Amazon Java downloaded from
# https://docs.aws.amazon.com/corretto/latest/corretto-17-ug/downloads-list.html for OTPv2
# and
# https://docs.aws.amazon.com/corretto/latest/corretto-8-ug/downloads-list.html for OTPv1
# to avoid installing multiple Javas on my system
Sys.setenv(JAVA_HOME = "bin/macos-aarch64/corretto-17-0-9-8-1//")
Sys.getenv("JAVA_HOME")
#> [1] "bin/macos-aarch64/corretto-17-0-9-8-1//"
system2("which", "java")
#> /usr/bin/java
system2("java", "-version")
# > openjdk version "17.0.9" 2023-10-17 LTS
# > OpenJDK Runtime Environment Corretto-17.0.9.8.1 (build 17.0.9+8-LTS)
# > OpenJDK 64-Bit Server VM Corretto-17.0.9.8.1 (build 17.0.9+8-LTS, mixed mode, sharing)

remotes::install_github("e-kotov/opentripplanner@otp_v2")
#> Skipping install of 'opentripplanner' from a github remote, the SHA1 (53e53e8b) has not changed since last install.
#>   Use `force = TRUE` to force installation
library(opentripplanner)
path_data <- file.path(tempdir(), "OTP")
dir.create(path_data)
path_otp <- otp_dl_jar(url = "https://github.com/opentripplanner/OpenTripPlanner/releases/download", version = "v2.4.0", file_name = "otp-2.4.0-shaded.jar")
#> Using cached version from /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/opentripplanner/jar/otp-2.4.0-shaded.jar

otp_dl_demo(path_data)
#> The demo data will be saved to /var/folders/gb/t5zr5rn15sldqybrmqbyh6y80000gn/T//RtmpweRpfD/OTP

log1 <- otp_build_graph(otp = path_otp, dir = path_data)
#> You have the correct version of Java for OTP 2.2+
#> Basic checks completed, building graph, this may take a few minutes
#> The graph will be saved to /var/folders/gb/t5zr5rn15sldqybrmqbyh6y80000gn/T//RtmpweRpfD/OTP/graphs/default ava:232) Graph building took 7s.

otp_conf <- otp_make_config("otp", version = 2)
otp_conf$otpFeatures$SandboxAPITravelTime <- TRUE
otp_write_config(otp_conf, dir = path_data)

log2 <- otp_setup(otp = path_otp, dir = path_data, port = 9080, securePort = 9081, open_browser = FALSE, wait = TRUE)
#> You have the correct version of Java for OTP 2.2+
#> OTP is loading and may take a while to be useable
#> Router http://localhost:9080/otp/routers/default exists
#> OTP is ready to use Go to localhost:9080 in your browser to view the OTP

otpcon <- otp_connect(hostname = "localhost", port = 9080, timezone = "Europe/London")
#> Router http://localhost:9080/otp/routers/default exists
location <- c(-1.15949,50.72831)

isos <- otp_isochrone(otpcon = otpcon, fromPlace = location, mode = c("WALK"))
#> OTP v2.2+ experimentaly supports isochrones, see https://docs.opentripplanner.org/en/v2.4.0/sandbox/TravelTime/
#> Warning in otp_isochrone(otpcon = otpcon, fromPlace = location, mode =
#> c("WALK")): Walking-only isochrones are not supported by OTP v2. You can only
#> use "WALK,TRANSIT". When set to "WALK" OTPv2 defaults to "WALK,TRANSIT". See
#> https://docs.opentripplanner.org/en/v2.4.0/sandbox/TravelTime/
#> 2024-01-05 14:29:48.859115 sending 1 isochrone requests using 9 threads
#> Done in 0 mins
plot(sf::st_geometry(isos))

isos <- otp_isochrone(otpcon = otpcon, fromPlace = location, mode = c("WALK", "TRANSIT"))
#> OTP v2.2+ experimentaly supports isochrones, see https://docs.opentripplanner.org/en/v2.4.0/sandbox/TravelTime/
#> 2024-01-05 14:29:49.477481 sending 1 isochrone requests using 9 threads
#> Done in 0 mins
plot(sf::st_geometry(isos))

Created on 2024-01-05 with reprex v2.0.2

mem48 commented 8 months ago

Sounds useful an something I'd thought about myself. A full PR would be welcome.

mem48 commented 8 months ago

You may also need to update the config validation functions to accept the new options