Skip to content
This repository has been archived by the owner on Jun 10, 2022. It is now read-only.

Add function to convert gtfs object to sp (similar to gtfs2sldf()) #24

Closed
dantonnoriega opened this issue Sep 30, 2016 · 8 comments
Closed

Comments

@dantonnoriega
Copy link
Collaborator

Suggestion: explore options for converting gtfs data classes into spatial objects such as SpatialLinesDataFrame.

Note: stplanr has a basic function for this: gtfs2sldf().

@mdsumner
Copy link
Contributor

mdsumner commented Jan 6, 2017

sf will be a better target now, sp has a subset of what sf can store. I'm happy to explore this, I've got my sf skillz pretty well sorted.

@dantonnoriega
Copy link
Collaborator Author

dantonnoriega commented Jan 6, 2017 via email

@mdsumner
Copy link
Contributor

mdsumner commented Jan 6, 2017

One way is to use the re-compose function spbabel::sp, which is looking for the generic names "object_", "branch_", "order_", "x_", "y_" which correspond to your shape_id (one branch per object), shape_pt_sequence, shape_pt_lon and shape_pt_lat.

library(spbabel)
sp_line <- sp(transmute(NYC$shapes_df, object_ = shape_id, branch_ = shape_id, order_ = shape_pt_sequence, x_ = shape_pt_lon, y_ = shape_pt_lat), crs = "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs")
sp_line$shape_id <- unique(NYC$shapes_df$shape_id)
sp_line$rownumber_ <- NULL

That can be converted to sf using sf::st_as_sf, and more directly the dev version of spbabel has stubs for the sf() function, that I haven't implemented :)

But, for that shapes_df it's more or less like this:

library(sf)
library(dplyr)
sf_line <- distinct(NYC$shapes_df, shape_id)
sf_line[["geometry"]] <- st_sfc(lapply(split(NYC$shapes_df, NYC$shapes_df$shape_id), function(x) st_linestring(as.matrix(x[order(x$shape_pt_sequence), c("shape_pt_lon", "shape_pt_lat")]))), crs = "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs")
sf_line <- st_as_sf(sf_line)
## make shape_id a factor so plot works (but watch out for ggplot2::geom_sf coming soon)
sf_line$shape_id <- factor(sf_line$shape_id)

I hope that helps, I'm happy to go deeper if you need, but I kind of need to hear what work flows you'll want. I am interested, I've been watching the progress being made a Sydney Transit recently!

You can't really have a line with properties on the vertices or on the segments, though you can have Z and M but they have to be floating point (same as x, y) so you can't store arbitrary stuff there. In the past I've used the trip package as a really primitive way of doing that, and recently I've worked on more abstract forms in spbabel and rangl, but I can't quite pin it down yet.

You can easily break these lines into vertices and segments and store them right in a dataframe, and grouping and recombining them in sf world is pretty good. Feel free to ping me with your ideas about what would be really helpful and I'll try to do it. It's reasonably easy to merge in elevation and time and plot these things in 3- and 4-D and I'm very interested to start doing that with examples like this.

@tbuckl
Copy link
Contributor

tbuckl commented Jun 25, 2017

@mdsumner thanks for those snippets! @dantonnoriega thanks for this excellent package! as you can see above, i'm poking around with questions like this. let me know if you have any questions.

@dantonnoriega
Copy link
Collaborator Author

Added @tombuckley gtfs routes to sf function. @mdsumner, once the release is live, would you mind checking out the results? I'm not familiar enough with sf data to understand the benefit.

@mdsumner
Copy link
Contributor

I can do that. One thing I learnt just now is to ensure lower level classes in sp don't have named lists which is a side effect of some approaches I have used. Fwiw running mapview on a sprint object is a good way to visualize it, check for basic sense.

@dantonnoriega dantonnoriega reopened this Jul 25, 2017
@dantonnoriega
Copy link
Collaborator Author

Awesome! Thanks!

@mdsumner
Copy link
Contributor

It definitely looks fine to me, @mpadge will have stronger insights about the best ways to manage the conversions, but I'd say that can be put off.

These functions (in convert_gtfs.R) definitely need examples and stronger title and description slots. They could be bundled together in one Rd.

@maelle maelle closed this as completed Jun 10, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants