ipeaGIT / gtfs2gps

Convert GTFS data into a data.table with GPS-like records in R
https://ipeagit.github.io/gtfs2gps/
Other
71 stars 10 forks source link

fread gps inside gps_as_linestring function #153

Closed Joaobazzo closed 4 years ago

Joaobazzo commented 4 years ago

I'm still having some problems with gtfs2gps::write_gtfs, so this issue is not reproducible, but perhaps I can make it clear


# this is my gtfs

> gtfs <- gtfs2gps::read_gtfs("../data-raw/gtfs/bra_spo/bra_spo.zip")
> gtfs <- gtfs2gps::filter_by_shape_id(gtfs,shape_ids = "52843")
> gps <- gtfs2gps::gtfs2gps(gtfs_data = gtfs)
> dim(gps)
# [1] 20760    14
> str(gps$trip_id) # it looks fine so far
# chr [1:20760] "8001-10-1" "8001-10-1" "8001-10-1" "8001-10-1" "8001-10-1" "8001-10-1" "8001-10-1" "8001-10-1" "8001-10-1" ...

# save as we normally write in gtfs2gps::gtfs2gps
> data.table::fwrite(gps,"52843.txt") 

# read as we normally read in gtfs2gps::gps_as_linestring
> gps1 <- data.table::fread("52843.txt") 

# check type
# data.table understands it as IDate format
> str(gps1$trip_id) 
# IDate[1:20760], format: "8001-10-01" "8001-10-01" "8001-10-01" "8001-10-01" "8001-10-01" "8001-10-01" "8001-10-01" "8001-10-01" "8001-10-01" ...

# one workaround is to input colClasses in data.table fread
# so this is supposed to be inside `gtfs2gps::gps_as_linestring`
> gps2 <- data.table::fread("52843.txt", colClasses = list(character=c("trip_id")))
str(gps2$trip_id)
# chr [1:20760] "8001-10-1" "8001-10-1" "8001-10-1" "8001-10-1" "8001-10-1" "8001-10-1" "8001-10-1" "8001-10-1" "8001-10-1" ...

However, perhaps we should make sure all columns have proper types, so might be interesting to force certain column types in all columns.

pedro-andrade-inpe commented 4 years ago

Yes, sometimes I have this problem also. A few days ago I found a bug related to route_id, where the attribute is usually character but it was read as numeric. I had to convert it to character internally (https://github.com/ipeaGIT/gtfs2gps/commit/fb0639ace27db821ff9f9c77ac2d64e045cc080). Perhaps we could always convert all ids to string.