Does the same thing as gepaf
(which stands for Google Encoded Polyline Algorithm Format) but in a more efficient way, using Rust.
The name is still subject to discussion but "gepafer" can be understood as "gepaf, faster".
Some packages, such as osrm
, need to encode / decode coordinates in order to communicate with the OSRM API.
There are already some R packages that provide this functionality:
- The
gepaf
package is a great package (disclaimer: I'm its co-author with @rcarto), but it is written in pure R and it is relatively slow. - The
googlePolylines
package is fast (it uses C++ under the hood)but, on 10/10/2024, the CRAN announced that the package will be archived on 24/10/2024 if nothing is done by its maintainer.
This package aims to provide the same functionality as gepaf
while being as fast as googlePolylines
, using Rust.
Moreover, it's a great opportunity to learn how to write R packages with Rust.
To do so, gepafer
is using the extendr
Rust crate and the rextendr
R package
(but other alternatives such as savvy
exist and could be considered in the future).
You can install the development version of gepafer
from GitHub with:
# install.packages("remotes")
remotes::install_github("mthh/gepafer")
You will need the Rust toolchain to compile the Rust code. The Minimum Supported Rust Version (MSRV) is 1.61.0.
The package is not yet on CRAN.
This packages aims to be a drop-in replacement for the gepaf
package.
It provides two functions, encode_polyline
and decode_polyline
, that do the same thing as the functions with the same name in the gepaf
package.
The encode_polyline
function takes a data frame with two columns, lat
and lon
, and returns an encoded polyline.
library(gepafer)
coords <- data.frame(lat = c(38.5, 40.7, 43.252), lon = c(-120.2, -120.95, -126.453))
encpoly <- encode_polyline(coords, factor=5)
encpoly
#> [1] "_p~iF~ps|U_ulLnnqC_mqNvxq`@"
The decode_polyline
function takes an encoded polyline and returns a data frame with two columns, lat
and lon
.
library(gepafer)
coords <- decode_polyline(enc_polyline = "_p~iF~ps|U_ulLnnqC_mqNvxq`@", factor=5)
coords
#> lat lon
#> 1 38.500 -120.200
#> 2 40.700 -120.950
#> 3 43.252 -126.453
GPL-3 (as the gepaf
package)