-
Notifications
You must be signed in to change notification settings - Fork 9
/
get_trip_geometry.R
233 lines (179 loc) · 6.29 KB
/
get_trip_geometry.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#' Get trip geometry
#'
#' Returns the geometry of each specified `trip_id`, based either on the
#' `shapes` or the `stop_times` file (or both).
#'
#' @param gtfs A GTFS object.
#' @param trip_id A character vector including the `trip_id`s to have their
#' geometries generated. If `NULL` (the default), the function generates
#' geometries for every `trip_id` in the GTFS.
#' @param file A character vector specifying the file from which geometries
#' should be generated (either one of or both `shapes` and `stop_times`). If
#' `NULL` (the default), the function attemps to generate the geometries from
#' both files, but only raises an error if none of the files exist.
#' @param crs The CRS of the resulting object, either as an EPSG code or as an
#' `crs` object. Defaults to 4326 (WGS 84).
#'
#' @return A `LINESTRING sf`.
#'
#' @section Details:
#' The geometry generation works differently for the two files. In the case of
#' `shapes`, the shape as described in the text file is converted to an `sf`
#' object. For `stop_times` the geometry is the result of linking subsequent
#' stops along a straight line (stops' coordinates are retrieved from the
#' `stops` file). Thus, the resolution of the geometry when generated with
#' `shapes` tends to be much higher than when created with `stop_times`.
#'
#' @examples
#' data_path <- system.file("extdata/spo_gtfs.zip", package = "gtfstools")
#'
#' gtfs <- read_gtfs(data_path)
#'
#' trip_geometry <- get_trip_geometry(gtfs)
#' head(trip_geometry)
#'
#' # the above is identical to
#' trip_geometry <- get_trip_geometry(gtfs, file = c("shapes", "stop_times"))
#' head(trip_geometry)
#'
#' trip_ids <- c("CPTM L07-0", "2002-10-0")
#' trip_geometry <- get_trip_geometry(gtfs, trip_id = trip_ids)
#' trip_geometry
#' plot(trip_geometry["origin_file"])
#'
#' @export
get_trip_geometry <- function(gtfs,
trip_id = NULL,
file = NULL,
crs = 4326) {
checkmate::assert_class(gtfs, "dt_gtfs")
checkmate::assert_character(trip_id, null.ok = TRUE)
checkmate::assert_character(file, null.ok = TRUE)
checkmate::assert(
checkmate::check_numeric(crs),
checkmate::check_class(crs, "crs"),
combine = "or"
)
if (!is.null(file))
checkmate::assert_names(file, subset.of = c("shapes", "stop_times"))
# check if required fields and files exist
if (is.null(file)) {
file <- names(gtfs)
file <- file[file %chin% c("shapes", "stop_times")]
if (identical(file, character(0)))
stop(
"The GTFS object must have either a 'shapes' or a 'stop_times' table."
)
}
if ("shapes" %in% file) {
checkmate::assert(
check_gtfs_field_exists(gtfs, "trips", c("trip_id", "shape_id")),
check_gtfs_field_exists(
gtfs,
"shapes",
c("shape_id", "shape_pt_lat", "shape_pt_lon", "shape_pt_sequence")
),
combine = "and"
)
}
if ("stop_times" %in% file) {
checkmate::assert(
check_gtfs_field_exists(gtfs, "trips", "trip_id"),
check_gtfs_field_exists(
gtfs, "stop_times", c("trip_id", "stop_id", "stop_sequence")
),
check_gtfs_field_exists(
gtfs, "stops", c("stop_id", "stop_lat", "stop_lon")
),
combine = "and"
)
}
# select trip_ids to get geometry of
if (!is.null(trip_id)) {
relevant_trips <- trip_id
} else {
relevant_trips <- unique(gtfs$trips$trip_id)
}
# raise warning if a given 'trip_id' doesn't exist in 'trips'
if (!is.null(trip_id)) {
invalid_trip_id <- trip_id[! trip_id %chin% unique(gtfs$trips$trip_id)]
if (!identical(invalid_trip_id, character(0))) {
warning(
paste0(
"'trips' doesn't contain the following trip_id(s): "),
paste0("'", invalid_trip_id, "'", collapse = ", ")
)
}
}
# create linestrings from shapes
if ("shapes" %in% file) {
# select shape_ids to get geometry of
trips <- gtfs$trips[trip_id %chin% relevant_trips & shape_id != ""]
relevant_shapes <- unique(trips$shape_id)
# generate geometry; the condition for nrow == 0 prevents an sfheaders error
shapes_sf <- gtfs$shapes[shape_id %chin% relevant_shapes]
shapes_sf <- shapes_sf[order(shape_id, shape_pt_sequence)]
if (nrow(shapes_sf) == 0) {
empty_linestring <- sf::st_sfc()
class(empty_linestring)[1] <- "sfc_LINESTRING"
shapes_sf <- sf::st_sf(
shape_id = character(),
geometry = empty_linestring,
stringsAsFactors = FALSE
)
} else {
shapes_sf <- sfheaders::sf_linestring(
shapes_sf,
x = "shape_pt_lon",
y = "shape_pt_lat",
linestring_id = "shape_id"
)
}
shapes_sf <- sf::st_set_crs(shapes_sf, 4326)
shapes_sf <- data.table::setDT(shapes_sf)[trips, on = "shape_id"]
shapes_sf[, origin_file := "shapes"]
shapes_sf <- shapes_sf[, .(trip_id, origin_file, geometry)]
}
# create linestrings from stop_times
if ("stop_times" %in% file) {
# generate geometry; the condition for nrow == 0 prevents an sfheaders error
stop_times_sf <- gtfs$stop_times[trip_id %chin% relevant_trips]
stop_times_sf <- stop_times_sf[order(trip_id, stop_sequence)]
stop_times_sf[
gtfs$stops,
on = "stop_id",
`:=`(stop_lat = i.stop_lat, stop_lon = i.stop_lon)
]
if (nrow(stop_times_sf) == 0) {
empty_linestring <- sf::st_sfc()
class(empty_linestring)[1] <- "sfc_LINESTRING"
stop_times_sf <- sf::st_sf(
trip_id = character(),
geometry = empty_linestring,
stringsAsFactors = FALSE
)
} else {
stop_times_sf <- sfheaders::sf_linestring(
stop_times_sf,
x = "stop_lon",
y = "stop_lat",
linestring_id = "trip_id"
)
}
stop_times_sf <- sf::st_set_crs(stop_times_sf, 4326)
data.table::setDT(stop_times_sf)
stop_times_sf[, origin_file := "stop_times"]
}
# tidy final object
if (length(file) == 2) {
final_sf <- rbind(shapes_sf, stop_times_sf)
} else {
final_sf <- get(paste0(file, "_sf"))
}
final_sf <- final_sf[, .(trip_id, origin_file, geometry)]
final_sf <- sf::st_as_sf(final_sf)
# transform crs from 4326 to the one passed to 'crs'
if (crs != 4326 && crs != sf::st_crs(4326))
final_sf <- sf::st_transform(final_sf, crs)
return(final_sf)
}