Skip to content

Commit

Permalink
write json in export_gtfs()
Browse files Browse the repository at this point in the history
  • Loading branch information
polettif committed Aug 26, 2024
1 parent d9f29e7 commit 963bf9b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 17 deletions.
40 changes: 23 additions & 17 deletions R/export_gtfs.R
Original file line number Diff line number Diff line change
Expand Up @@ -119,35 +119,41 @@ export_gtfs <- function(gtfs,

if (!quiet) message("Writing text files to ", tmpd)

for (file in files) {
filenames = append_file_ext(files)

filename <- paste0(file, ".txt")
for (filename in filenames) {

file <- remove_file_ext(filename)
filepath <- file.path(tmpd, filename)

if (!quiet) message(" - Writing ", filename)

dt <- gtfs[[file]]
dt <- gtfs[[remove_file_ext(filename)]]

# if 'standard_only' is set to TRUE, remove non-standard fields from 'dt'
# before writing it to disk
if(endsWith(filename, ".geojson")) {
jsonlite::write_json(dt, filepath, pretty = FALSE, auto_unbox = TRUE, digits = 8)
} else {

if (standard_only) {
# if 'standard_only' is set to TRUE, remove non-standard fields from 'dt'
# before writing it to disk

file_cols <- names(dt)
extra_cols <- setdiff(file_cols, names(gtfs_standards[[file]]))
if (standard_only) {

if (!identical(extra_cols, character(0))) dt <- dt[, !..extra_cols]
file_cols <- names(dt)
extra_cols <- setdiff(file_cols, names(gtfs_standards[[file]]))

}
if (!identical(extra_cols, character(0))) dt <- dt[, !..extra_cols]

# print warning message if warning is raised and 'quiet' is FALSE
withCallingHandlers(
data.table::fwrite(dt, filepath, scipen = 999),
warning = function(cnd) {
if (!quiet) message(" - ", conditionMessage(cnd))
}
)

# print warning message if warning is raised and 'quiet' is FALSE
withCallingHandlers(
data.table::fwrite(dt, filepath, scipen = 999),
warning = function(cnd) {
if (!quiet) message(" - ", conditionMessage(cnd))
}
)
}
}

# zip the contents of 'tmpd' to 'path', if as_dir = FALSE
Expand All @@ -161,7 +167,7 @@ export_gtfs <- function(gtfs,

unlink(path, recursive = TRUE)

filepaths <- file.path(tmpd, paste0(files, ".txt"))
filepaths <- file.path(tmpd, filenames)

zip::zip(
path,
Expand Down
10 changes: 10 additions & 0 deletions inst/tinytest/test_export_gtfs.R
Original file line number Diff line number Diff line change
Expand Up @@ -294,3 +294,13 @@ resulting_shapes_content <- readLines(file.path(target_dir, "shapes.txt"))

expect_false(identical(resulting_shapes_content[3], "b,2,41,41,1e+07"))
expect_identical(resulting_shapes_content[3], "b,2,41,41,10000000")

# issue #36 ---------------------------------------------------------------
# re-reading written json files are the same

locations_feed <- import_gtfs(system.file("extdata/locations_feed.zip", package = "gtfsio"))
tmpfile <- tempfile(fileext = ".zip")
export_gtfs(locations_feed, tmpfile)
reimported <- import_gtfs(tmpfile)

expect_equal(reimported, locations_feed)

0 comments on commit 963bf9b

Please sign in to comment.