Skip to content

Commit

Permalink
Replace class with inherits
Browse files Browse the repository at this point in the history
  • Loading branch information
mem48 committed Aug 14, 2023
1 parent 98efe8f commit d796a29
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 22 deletions.
10 changes: 5 additions & 5 deletions R/gtfs_interpolate_times.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@
gtfs_interpolate_times <- function(gtfs, ncores = 1){
stop_times <- gtfs$stop_times

if(class(stop_times$arrival_time) != "Period"){
if(!inherits(stop_times$arrival_time, "Period")){
stop_times$arrival_time <- lubridate::hms(stop_times$arrival_time)
}

if(class(stop_times$departure_time) != "Period"){
if(!inherits(stop_times$departure_time, "Period")){
stop_times$departure_time <- lubridate::hms(stop_times$departure_time)
}

if(class(stop_times$stop_sequence) == "character"){
if(inherits(stop_times$stop_sequence, "character")){
stop_times$stop_sequence <- as.integer(stop_times$stop_sequence)
}

Expand Down Expand Up @@ -110,12 +110,12 @@ stops_interpolate <- function(x){

# Needed because rbindlist doesn't work with periods for some reason
arrival_time <- try(period2gtfs(x$arrival_time), silent = TRUE)
if("try-error" %in% class(arrival_time)){
if(inherits(arrival_time, "try-error")){
stop("conversion of times failed for tripID: ",unique(x$trip_id))
}
x$arrival_time <- arrival_time
departure_time <- try(period2gtfs(x$departure_time), silent = TRUE)
if("try-error" %in% class(departure_time)){
if(inherits(departure_time, "try-error")){
stop("conversion of times failed for tripID: ",unique(x$trip_id))
}
x$departure_time <- departure_time
Expand Down
12 changes: 6 additions & 6 deletions R/gtfs_to_sf.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
gtfs_stops_sf <- function(gtfs){
stops <- gtfs$stops

if(class(stops$stop_lon) != "numeric"){
if(!inherits(stops$stop_lon, "numeric")){
message("Converting stop_lon to numeric")
stops$stop_lon <- as.numeric(stops$stop_lon)
}

if(class(stops$stop_lat) != "numeric"){
if(!inherits(stops$stop_lat, "numeric")){
message("Converting stop_lat to numeric")
stops$stop_lat <- as.numeric(stops$stop_lat)
}
Expand Down Expand Up @@ -42,11 +42,11 @@ gtfs_trips_sf <- function(gtfs){
stops <- gtfs$stops
stops <- stops[,c("stop_id","stop_lon","stop_lat")]

if(class(stops$stop_lon) != "numeric"){
if(!inherits(stops$stop_lon, "numeric")){
stops$stop_lon <- as.numeric(stops$stop_lon)
}

if(class(stops$stop_lat) != "numeric"){
if(!inherits(stops$stop_lat, "numeric")){
stops$stop_lat <- as.numeric(stops$stop_lat)
}

Expand Down Expand Up @@ -85,11 +85,11 @@ gtfs_routes_sf <- function(gtfs){
stops <- gtfs$stops
stops <- stops[,c("stop_id","stop_lon","stop_lat")]

if(class(stops$stop_lon) != "numeric"){
if(!inherits(stops$stop_lon, "numeric")){
stops$stop_lon <- as.numeric(stops$stop_lon)
}

if(class(stops$stop_lat) != "numeric"){
if(!inherits(stops$stop_lat, "numeric")){
stops$stop_lat <- as.numeric(stops$stop_lat)
}

Expand Down
8 changes: 4 additions & 4 deletions R/import_OperatingProfile.R
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ import_OperatingProfile <- function(OperatingProfile) {

# Check for children
sdo_check <- try(xml2::xml_child(ServicedDaysOfOperation), silent = TRUE)
if(class(sdo_check) == "try-error"){
if(inherits(sdo_check, "try-error")){
ServicedDaysOfOperation <- NULL
}
sndo_check <- try(xml2::xml_child(ServicedDaysOfNonOperation), silent = TRUE)
if(class(sdo_check) == "try-error"){
if(inherits(sdo_check, "try-error")){
ServicedDaysOfOperation <- NULL
}

Expand Down Expand Up @@ -224,14 +224,14 @@ import_OperatingProfile <- function(OperatingProfile) {
stringsAsFactors = FALSE
)

if(class(ServicedDaysOfOperation) == "data.frame"){
if(inherits(ServicedDaysOfOperation, "data.frame")){
res <- cbind(res, ServicedDaysOfOperation)
} else {
res$ServicedDaysOfOperation <- NA
res$ServicedDaysOfOperationType <- NA
}

if(class(ServicedDaysOfNonOperation) == "data.frame"){
if(inherits(ServicedDaysOfNonOperation, "data.frame")){
res <- cbind(res, ServicedDaysOfNonOperation)
} else {
res$ServicedDaysOfNonOperation <- NA
Expand Down
14 changes: 7 additions & 7 deletions R/write_gtfs.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,24 @@ gtfs_write <- function(gtfs,

#Format Dates

if(class(gtfs$calendar$start_date) == "Date"){
if(inherits(gtfs$calendar$start_date, "Date")){
gtfs$calendar$start_date <- format(gtfs$calendar$start_date, "%Y%m%d")
}

if(class(gtfs$calendar$end_date) == "Date"){
if(inherits(gtfs$calendar$end_date, "Date")){
gtfs$calendar$end_date <- format(gtfs$calendar$end_date, "%Y%m%d")
}

if(class(gtfs$calendar_dates$date) == "Date"){
if(inherits(gtfs$calendar_dates$date, "Date")){
gtfs$calendar_dates$date <- format(gtfs$calendar_dates$date, "%Y%m%d")
}

#Format times
if(class(gtfs$stop_times$arrival_time) == "Period"){
if(inherits(gtfs$stop_times$arrival_time, "Period")){
gtfs$stop_times$arrival_time <- period2gtfs(gtfs$stop_times$arrival_time)
}

if(class(gtfs$stop_times$departure_time) == "Period"){
if(inherits(gtfs$stop_times$departure_time, "Period")){
gtfs$stop_times$departure_time <- period2gtfs(gtfs$stop_times$departure_time)
}

Expand Down Expand Up @@ -87,7 +87,7 @@ gtfs_write <- function(gtfs,
#'
stripCommas <- function(df) {
df[] <- lapply(df, function(x) {
if (class(x) == "character") {
if (inherits(x, "character")) {
if(!all(validUTF8(x))){
Encoding(x) <- "latin1"
x <- enc2utf8(x)
Expand All @@ -109,7 +109,7 @@ stripCommas <- function(df) {
#'
stripTabs <- function(df, stripNewline) {
df[] <- lapply(df, function(x) {
if (class(x) == "character") {
if (inherits(x, "character")) {
if(!all(validUTF8(x))){
Encoding(x) <- "latin1"
x <- enc2utf8(x)
Expand Down

0 comments on commit d796a29

Please sign in to comment.