munterfi / hereR

R package that provides an interface to the HERE REST APIs: Geocoder API, Routing API, Traffic API, Public Transit API and Destination Weather API. Locations and routes are returned as 'sf' objects.
https://munterfi.github.io/hereR/
GNU General Public License v3.0
87 stars 11 forks source link

Feature: Request tolls #151

Closed unbrother closed 2 years ago

unbrother commented 2 years ago

I see that it is possible to request tolls for routes in the API but it would be great to be able to do it with hereR.

munterfi commented 2 years ago

Hi @unbrother, thanks for reporting.

Querying tolls (for individual triplegs) and aggregate them on route level is somewhat complicated. Depending on the country, the toll system is different. For example, in Switzerland you pay an amount once a year for the use of highways (vignette, flat rate). In contrast, tolls for routes in countries with toll stations are clearly defined and recurring. If a route from Switzerland to Italy (a system with toll stations) is queried, the costs for the route cannot simply be added up, since the return would be cheaper due to the already bought vignette in Switzerland:

library(hereR)
zh <- geocode("Zurich, Switzerland")
ge <- geocode("Genova, Italy")
rt <- route(zh, ge, transport_mode = "car", url_only = TRUE)
#> https://router.hereapi.com/v8/routes?apiKey=<YOUR-KEY>&origin=47.377,8.53977&destination=44.41048,8.93917&departureTime=2022-06-27T13:08:52%2B02:00&transportMode=car&alternatives=0&return=tolls

And the response from the HERE Routing API with &return=tolls:

{
   "routes":[
      {
         "id":"29dbb34e-2de7-4e32-8ad3-ec634214caeb",
         "sections":[
            {
               "id":"bf70b11d-f423-459b-a9f5-ae35f59f2be1",
               "type":"vehicle",
               "departure":{
                  "time":"2022-06-27T13:08:52+02:00",
                  "place":{
                     "type":"place",
                     "location":{
                        "lat":47.3770155,
                        "lng":8.539779
                     },
                     "originalLocation":{
                        "lat":47.3769999,
                        "lng":8.5397699
                     }
                  }
               },
               "arrival":{
                  "time":"2022-06-27T18:06:17+02:00",
                  "place":{
                     "type":"place",
                     "location":{
                        "lat":44.41048,
                        "lng":8.9391699
                     },
                     "originalLocation":{
                        "lat":44.41048,
                        "lng":8.93917
                     }
                  }
               },
               "transport":{
                  "mode":"car"
               },
               "tolls":[
                  {
                     "countryCode":"CHE",
                     "tollSystemRef":0,
                     "tollSystem":"VIGNETTE SWITZERLAND",
                     "fares":[
                        {
                           "id":"daf1bde6-e0ad-4da6-8392-1cb974bf1a72",
                           "name":"VIGNETTE SWITZERLAND",
                           "price":{
                              "type":"value",
                              "currency":"CHF",
                              "value":39.0
                           },
                           "reason":"toll",
                           "paymentMethods":[
                              "passSubscription"
                           ],
                           "pass":{
                              "returnJourney":false,
                              "validityPeriod":{
                                 "period":"extendedAnnual"
                              }
                           }
                        }
                     ]
                  },
                  {
                     "countryCode":"ITA",
                     "tollSystemRef":1,
                     "tollSystem":"AUTOSTRADE PER L'ITALIA S.P.A.",
                     "fares":[
                        {
                           "id":"a75e3e4f-d0d8-4ef4-8b19-a307bbf37719",
                           "name":"AUTOSTRADE PER L'ITALIA S.P.A.",
                           "price":{
                              "type":"value",
                              "currency":"EUR",
                              "value":2.3
                           },
                           "reason":"toll",
                           "paymentMethods":[
                              "cash",
                              "bankCard",
                              "creditCard",
                              "transponder",
                              "travelCard"
                           ]
                        }
                     ],
                     "tollCollectionLocations":[
                        {
                           "name":"Grandate",
                           "location":{
                              "lat":45.77395,
                              "lng":9.04929
                           }
                        }
                     ]
                  },
                  {
                     "countryCode":"ITA",
                     "tollSystemRef":1,
                     "tollSystem":"AUTOSTRADE PER L'ITALIA S.P.A.",
                     "fares":[
                        {
                           "id":"5024f9f3-6c50-4a28-aeb9-1109a542447d",
                           "name":"AUTOSTRADE PER L'ITALIA S.P.A.",
                           "price":{
                              "type":"value",
                              "currency":"EUR",
                              "value":3.3
                           },
                           "reason":"toll",
                           "paymentMethods":[
                              "cash",
                              "bankCard",
                              "creditCard",
                              "transponder",
                              "travelCard"
                           ]
                        }
                     ],
                     "tollCollectionLocations":[
                        {
                           "name":"Rho",
                           "location":{
                              "lat":45.54107,
                              "lng":9.05848
                           }
                        }
                     ]
                  },
                  {
                     "countryCode":"ITA",
                     "tollSystemRef":1,
                     "tollSystem":"AUTOSTRADE PER L'ITALIA S.P.A.",
                     "fares":[
                        {
                           "id":"e0e74421-5212-4140-a172-228431f161cf",
                           "name":"AUTOSTRADE PER L'ITALIA S.P.A.",
                           "price":{
                              "type":"value",
                              "currency":"EUR",
                              "value":11.2
                           },
                           "reason":"toll",
                           "paymentMethods":[
                              "cash",
                              "bankCard",
                              "creditCard",
                              "transponder",
                              "travelCard"
                           ]
                        }
                     ],
                     "tollCollectionLocations":[
                        {
                           "name":"Assago",
                           "location":{
                              "lat":45.38928,
                              "lng":9.13855
                           }
                        },
                        {
                           "name":"Genova",
                           "location":{
                              "lat":44.43124,
                              "lng":8.94574
                           }
                        }
                     ]
                  }
               ],
               "tollSystems":[
                  {
                     "id":509,
                     "name":"VIGNETTE SWITZERLAND",
                     "languageCode":"ENG"
                  },
                  {
                     "id":5048,
                     "name":"AUTOSTRADE PER L'ITALIA S.P.A.",
                     "languageCode":"ENG"
                  }
               ]
            }
         ]
      }
   ]
}

As soon as I have some more time, I will work on a proposal to integrate tolls in route queries via the the hereR package.

munterfi commented 2 years ago

Request tolls for routes with transport mode "car", "truck" "taxi" or "bus" is now possible:

Setup, create international route from CH to IT:

library(hereR)
set_verbose(TRUE)

zh <- geocode("Zurich, Switzerland")
#> Sending 1 request(s) with 5 RPS to: 'https://geocode.search.hereapi.com/v1/geocode?...'
#> Received 1 response(s) with total size: 872 bytes
ge <- geocode("Genova, Italy")
#> Sending 1 request(s) with 5 RPS to: 'https://geocode.search.hereapi.com/v1/geocode?...'
#> Received 1 response(s) with total size: 888 bytes

Request route with tolls in the currency of the system locale, including vignettes:

Sys.localeconv()[["int_curr_symbol"]]
#> [1] "USD "
(rt <- route(zh, ge, transport_mode = "car"))
#> Sending 1 request(s) with 10 RPS to: 'https://router.hereapi.com/v8/routes?...'
#> Received 1 response(s) with total size: 64.1 Kb
#> Simple feature collection with 1 feature and 12 fields
#> Geometry type: LINESTRING
#> Dimension:     XYZ
#> Bounding box:  xmin: 8.42371 ymin: 44.41048 xmax: 9.13999 ymax: 47.37781
#> z_range:       zmin: 68 zmax: 1225
#> Geodetic CRS:  WGS 84
#>   id rank section           departure             arrival    type mode distance
#> 1  1    1       1 2022-06-30 21:45:12 2022-07-01 02:31:10 vehicle  car   419278
#>   duration duration_base consumption tolls                       geometry
#> 1    17158         16522    153.6208 58.55 LINESTRING Z (8.539779 47.3...

Request route without vignettes:

(rt <- route(zh, ge, transport_mode = "car", vignettes = FALSE))
#> Sending 1 request(s) with 10 RPS to: 'https://router.hereapi.com/v8/routes?...'
#> Received 1 response(s) with total size: 63.7 Kb
#> Simple feature collection with 1 feature and 12 fields
#> Geometry type: LINESTRING
#> Dimension:     XYZ
#> Bounding box:  xmin: 8.42371 ymin: 44.41048 xmax: 9.13999 ymax: 47.37781
#> z_range:       zmin: 68 zmax: 1225
#> Geodetic CRS:  WGS 84
#>   id rank section           departure             arrival    type mode distance
#> 1  1    1       1 2022-06-30 21:45:13 2022-07-01 02:31:11 vehicle  car   419278
#>   duration duration_base consumption tolls                       geometry
#> 1    17158         16522    153.6208 17.77 LINESTRING Z (8.539779 47.3...

Change system locale (EUR):

Sys.setlocale("LC_ALL", "it_IT.utf8")
#> [1] ""
Sys.localeconv()[["int_curr_symbol"]]
#> [1] "EUR "
(rt <- route(zh, ge, transport_mode = "car"))
#> Sending 1 request(s) with 10 RPS to: 'https://router.hereapi.com/v8/routes?...'
#> Received 1 response(s) with total size: 64.1 Kb
#> Simple feature collection with 1 feature and 12 fields
#> Geometry type: LINESTRING
#> Dimension:     XYZ
#> Bounding box:  xmin: 8.42371 ymin: 44.41048 xmax: 9.13999 ymax: 47.37781
#> z_range:       zmin: 68 zmax: 1225
#> Geodetic CRS:  WGS 84
#>   id rank section           departure             arrival    type mode distance
#> 1  1    1       1 2022-06-30 21:45:13 2022-07-01 02:31:11 vehicle  car   419278
#>   duration duration_base consumption tolls                       geometry
#> 1    17158         16522    153.6208 57.79 LINESTRING Z (8.539779 47.3...

Set currency to mexican peso using set_currency() and therefore take precedence over the system locale:

set_currency("MXN")
(rt <- route(zh, ge, transport_mode = "car"))
#> Sending 1 request(s) with 10 RPS to: 'https://router.hereapi.com/v8/routes?...'
#> Received 1 response(s) with total size: 64.1 Kb
#> Simple feature collection with 1 feature and 12 fields
#> Geometry type: LINESTRING
#> Dimension:     XYZ
#> Bounding box:  xmin: 8.42371 ymin: 44.41048 xmax: 9.13999 ymax: 47.37781
#> z_range:       zmin: 68 zmax: 1225
#> Geodetic CRS:  WGS 84
#>   id rank section           departure             arrival    type mode distance
#> 1  1    1       1 2022-06-30 21:45:14 2022-07-01 02:31:12 vehicle  car   419278
#>   duration duration_base consumption   tolls                       geometry
#> 1    17158         16522    153.6208 1166.28 LINESTRING Z (8.539779 47.3...

Check transport mode without tolls:

(rt <- route(zh, ge, transport_mode = "bicycle"))
#> Sending 1 request(s) with 10 RPS to: 'https://router.hereapi.com/v8/routes?...'
#> Received 1 response(s) with total size: 111.8 Kb
#> Simple feature collection with 17 features and 12 fields
#> Geometry type: LINESTRING
#> Dimension:     XYZ
#> Bounding box:  xmin: 8.53388 ymin: 44.4099 xmax: 9.79494 ymax: 47.37702
#> z_range:       zmin: 50 zmax: 2337
#> Geodetic CRS:  WGS 84
#> First 10 features:
#>    id rank section           departure             arrival       type
#> 1   1    1       1 2022-06-30 21:45:15 2022-07-01 02:02:38    vehicle
#> 2   1    1       2 2022-07-01 02:02:38 2022-07-01 02:07:50 pedestrian
#> 3   1    1       3 2022-07-01 02:07:50 2022-07-01 02:09:05    vehicle
#> 4   1    1       4 2022-07-01 02:09:05 2022-07-01 02:14:49 pedestrian
#> 5   1    1       5 2022-07-01 02:14:49 2022-07-01 06:34:07    vehicle
#> 6   1    1       6 2022-07-01 06:34:07 2022-07-01 06:39:54 pedestrian
#> 7   1    1       7 2022-07-01 06:39:54 2022-07-01 06:41:40    vehicle
#> 8   1    1       8 2022-07-01 06:41:40 2022-07-01 06:53:45 pedestrian
#> 9   1    1       9 2022-07-01 06:53:45 2022-07-01 10:21:44    vehicle
#> 10  1    1      10 2022-07-01 10:21:44 2022-07-01 10:36:40 pedestrian
#>          mode distance duration duration_base consumption tolls
#> 1     bicycle    61752    15443         15443     44.0300     0
#> 2  pedestrian      286      312           312          NA     0
#> 3     bicycle      182       75            75      0.1964     0
#> 4  pedestrian      322      344           344          NA     0
#> 5     bicycle    61856    15558         15558     44.9633     0
#> 6  pedestrian      327      347           347          NA     0
#> 7     bicycle       90      106           106      0.3447     0
#> 8  pedestrian      698      725           725          NA     0
#> 9     bicycle    30092    12479         12479     36.2990     0
#> 10 pedestrian      870      896           896          NA     0
#>                          geometry
#> 1  LINESTRING Z (8.539779 47.3...
#> 2  LINESTRING Z (9.0756 47.099...
#> 3  LINESTRING Z (9.07493 47.10...
#> 4  LINESTRING Z (9.07553 47.10...
#> 5  LINESTRING Z (9.0782 47.105...
#> 6  LINESTRING Z (9.52979 46.84...
#> 7  LINESTRING Z (9.5326 46.845...
#> 8  LINESTRING Z (9.5329 46.844...
#> 9  LINESTRING Z (9.53886 46.84...
#> 10 LINESTRING Z (9.58527 46.63...

Can you check with the development version of the package remotes::install_github("munterfi/hereR@feature/route-tolls")?

Session info ``` r devtools::session_info() #> ─ Session info ─────────────────────────────────────────────────────────────── #> setting value #> version R version 4.2.0 (2022-04-22) #> os macOS Big Sur/Monterey 10.16 #> system x86_64, darwin17.0 #> ui X11 #> language (EN) #> collate en_US.UTF-8 #> ctype en_US.UTF-8 #> tz Europe/Zurich #> date 2022-06-30 #> pandoc 2.18 @ /usr/local/bin/ (via rmarkdown) #> #> ─ Packages ─────────────────────────────────────────────────────────────────── #> package * version date (UTC) lib source #> brio 1.1.3 2021-11-30 [1] CRAN (R 4.2.0) #> cachem 1.0.6 2021-08-19 [1] CRAN (R 4.2.0) #> callr 3.7.0 2021-04-20 [1] CRAN (R 4.2.0) #> class 7.3-20 2022-01-16 [1] CRAN (R 4.2.0) #> classInt 0.4-7 2022-06-10 [1] CRAN (R 4.2.0) #> cli 3.3.0 2022-04-25 [1] CRAN (R 4.2.0) #> crayon 1.5.1 2022-03-26 [1] CRAN (R 4.2.0) #> crul 1.2.0 2021-11-22 [1] CRAN (R 4.2.0) #> curl 4.3.2 2021-06-23 [1] CRAN (R 4.2.0) #> data.table 1.14.2 2021-09-27 [1] CRAN (R 4.2.0) #> DBI 1.1.3 2022-06-18 [1] CRAN (R 4.2.0) #> desc 1.4.1 2022-03-06 [1] CRAN (R 4.2.0) #> devtools 2.4.3 2021-11-30 [1] CRAN (R 4.2.0) #> digest 0.6.29 2021-12-01 [1] CRAN (R 4.2.0) #> dplyr 1.0.9 2022-04-28 [1] CRAN (R 4.2.0) #> e1071 1.7-11 2022-06-07 [1] CRAN (R 4.2.0) #> ellipsis 0.3.2 2021-04-29 [1] CRAN (R 4.2.0) #> evaluate 0.15 2022-02-18 [1] CRAN (R 4.2.0) #> fansi 1.0.3 2022-03-24 [1] CRAN (R 4.2.0) #> fastmap 1.1.0 2021-01-25 [1] CRAN (R 4.2.0) #> flexpolyline 0.2.5 2021-11-19 [1] CRAN (R 4.2.0) #> fs 1.5.2 2021-12-08 [1] CRAN (R 4.2.0) #> generics 0.1.2 2022-01-31 [1] CRAN (R 4.2.0) #> glue 1.6.2 2022-02-24 [1] CRAN (R 4.2.0) #> hereR * 0.8.2.9000 2022-06-30 [1] local #> htmltools 0.5.2 2021-08-25 [1] CRAN (R 4.2.0) #> httpcode 0.3.0 2020-04-10 [1] CRAN (R 4.2.0) #> jsonlite 1.8.0 2022-02-22 [1] CRAN (R 4.2.0) #> KernSmooth 2.23-20 2021-05-03 [1] CRAN (R 4.2.0) #> knitr 1.39 2022-04-26 [1] CRAN (R 4.2.0) #> lifecycle 1.0.1 2021-09-24 [1] CRAN (R 4.2.0) #> magrittr 2.0.3 2022-03-30 [1] CRAN (R 4.2.0) #> memoise 2.0.1 2021-11-26 [1] CRAN (R 4.2.0) #> pillar 1.7.0 2022-02-01 [1] CRAN (R 4.2.0) #> pkgbuild 1.3.1 2021-12-20 [1] CRAN (R 4.2.0) #> pkgconfig 2.0.3 2019-09-22 [1] CRAN (R 4.2.0) #> pkgload 1.2.4 2021-11-30 [1] CRAN (R 4.2.0) #> prettyunits 1.1.1 2020-01-24 [1] CRAN (R 4.2.0) #> processx 3.5.3 2022-03-25 [1] CRAN (R 4.2.0) #> proxy 0.4-27 2022-06-09 [1] CRAN (R 4.2.0) #> ps 1.7.0 2022-04-23 [1] CRAN (R 4.2.0) #> purrr 0.3.4 2020-04-17 [1] CRAN (R 4.2.0) #> R6 2.5.1 2021-08-19 [1] CRAN (R 4.2.0) #> Rcpp 1.0.8.3 2022-03-17 [1] CRAN (R 4.2.0) #> remotes 2.4.2 2021-11-30 [1] CRAN (R 4.2.0) #> rlang 1.0.2 2022-03-04 [1] CRAN (R 4.2.0) #> rmarkdown 2.14 2022-04-25 [1] CRAN (R 4.2.0) #> rprojroot 2.0.3 2022-04-02 [1] CRAN (R 4.2.0) #> rstudioapi 0.13 2020-11-12 [1] CRAN (R 4.2.0) #> sessioninfo 1.2.2 2021-12-06 [1] CRAN (R 4.2.0) #> sf 1.0-7 2022-03-07 [1] CRAN (R 4.2.0) #> stringi 1.7.6 2021-11-29 [1] CRAN (R 4.2.0) #> stringr 1.4.0 2019-02-10 [1] CRAN (R 4.2.0) #> testthat 3.1.4 2022-04-26 [1] CRAN (R 4.2.0) #> tibble 3.1.7 2022-05-03 [1] CRAN (R 4.2.0) #> tidyselect 1.1.2 2022-02-21 [1] CRAN (R 4.2.0) #> units 0.8-0 2022-02-05 [1] CRAN (R 4.2.0) #> usethis 2.1.6 2022-05-25 [1] CRAN (R 4.2.0) #> utf8 1.2.2 2021-07-24 [1] CRAN (R 4.2.0) #> vctrs 0.4.1 2022-04-13 [1] CRAN (R 4.2.0) #> withr 2.5.0 2022-03-03 [1] CRAN (R 4.2.0) #> xfun 0.31 2022-05-10 [1] CRAN (R 4.2.0) #> yaml 2.3.5 2022-02-21 [1] CRAN (R 4.2.0) #> #> [1] /Library/Frameworks/R.framework/Versions/4.2/Resources/library #> #> ────────────────────────────────────────────────────────────────────────────── ```