rencodingcharacter-encodinggtfsgtfstools

Problem for reading UTF-8 file with gtfstools


I'm trying to open a GTFS file that has UTF-8 encoding, but even though I changed my project's encoding in R to UTF-8, the characters are still truncated. The problem can be seen in the "stop_name" column. I'm using windows 10 and I know there are some encoding issues with R, but I have no idea what it is.

Reproducible example:

install.packages('gtfstools')
library(gtfstools)

# GTFS file directory
data_path <- system.file("extdata", package = "gtfstools")
spo_path <- file.path(data_path, "spo_gtfs.zip")

# read the file
spo_gtfs <- read_gtfs(spo_path)

# Show the stops (problem with encoding)
head(spo_gtfs$stops)

Output: enter image description here

Session info:

> sessionInfo()
R version 4.1.2 (2021-11-01)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18363)

Matrix products: default

locale:
[1] LC_COLLATE=Portuguese_Brazil.1252  LC_CTYPE=Portuguese_Brazil.1252    LC_MONETARY=Portuguese_Brazil.1252
[4] LC_NUMERIC=C                       LC_TIME=Portuguese_Brazil.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
[1] compiler_4.1.2 tools_4.1.2   

Solution

  • You just need to use the encoding parameter on read_gtfs():

    library(gtfstools)
    
    # GTFS file directory
    data_path <- system.file("extdata", package = "gtfstools")
    spo_path <- file.path(data_path, "spo_gtfs.zip")
    
    # read the file
    spo_gtfs <- read_gtfs(spo_path, encoding = "UTF-8")
    
    # Show the stops (problem with encoding)
    head(spo_gtfs$stops)
    #>    stop_id     stop_name stop_desc  stop_lat  stop_lon
    #> 1:   18848      Clínicas           -23.55402 -46.67111
    #> 2:   18849 Vila Madalena           -23.54650 -46.69114
    #> 3:   18850    Consolação           -23.55809 -46.66020
    #> 4:   18851     Conceição           -23.63504 -46.64124
    #> 5:   18852     Jabaquara           -23.64600 -46.64103
    #> 6:   18853     São Judas           -23.62588 -46.64094