Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Finish the 'Activity models' vignette #76

Closed
3 of 4 tasks
Robinlovelace opened this issue Sep 13, 2021 · 11 comments
Closed
3 of 4 tasks

Finish the 'Activity models' vignette #76

Robinlovelace opened this issue Sep 13, 2021 · 11 comments

Comments

@Robinlovelace
Copy link
Collaborator

Robinlovelace commented Sep 13, 2021

After #23 and #75 I'm pretty happy with how this is looking: https://a-b-street.github.io/abstr/articles/activity.html

It shows a simple example of someone travelling from home to work, and then walking for lunch and leisure, and then cycling back home again.

It imports great into A/B Street 🎉

Peek 2021-09-13 21-22

But I think there are a few unfinished things with this vignette, e.g.:

  • Show an example with more agents, e.g. with the Sao Paulo data produced by @lucasccdias (let me know if you're up for giving that a go Lucas)
  • Add commands in A/B Street to load the data
  • Visualising activity patterns dynamically before A/B Street import, e.g. with ab_animate() and ab_interactive() functions, as discussed with @natesheehan and as touched on here: Blog post on animating trajectories geocompx/geocompkg#29
  • Anything else
@lucasccdias
Copy link
Collaborator

Yes! I will try here, @Robinlovelace .

@natesheehan
Copy link
Collaborator

First initial go at animating in R (Using Leaflet)

https://rpubs.com/nathanaelsheehan/ACT_OD_VIZ

A lot more to do here, but we have the activity moving.

TODO

  • Use determnistic timing
  • Color code by mode

library(osrm)
library(stplanr)
library(sf)

places = tibble::tribble(
  ~name, ~x, ~y,
  "Home", -1.524, 53.819,
  "Work", -1.552, 53.807,
  "Park", -1.560, 53.812,
  "Cafe", -1.556, 53.802
)
places_sf = sf::st_as_sf(places, coords = c("x", "y"), crs = 4326)
plot(places_sf, pch = places$name)
od = tibble::tribble(
  ~o, ~d, ~mode, ~departure, ~person,
  "Home", "Work", "Bike", "08:30", 1,
  "Work", "Park", "Walk", "11:30", 1,
  "Park", "Cafe", "Walk", "12:15", 1,
  "Cafe", "Work", "Walk", "12:45", 1,
  "Work", "Home", "Bike", "17:00", 1
)

#> Calculate trip route from geometry
calculate_routes = function(o,d,name){
  trips = route(
    from = unlist(o),
    to = unlist(d),
    route_fun = osrmRoute,
    returnclass = "sf"
  )

  route_geom = as.data.frame(trips$geometry)
  route_geom = st_as_sf(route_geom)
  route_geom$name = name

  return(route_geom)
}

# calculate robin activity
home_to_work = calculate_routes(o = places_sf$geometry[1], d = places_sf$geometry[2], name = "Robin")
work_to__park = calculate_routes(o = places_sf$geometry[2], d = places_sf$geometry[3],  name = "Robin")
park_to_cafe = calculate_routes(o = places_sf$geometry[3], d = places_sf$geometry[4],  name = "Robin")
cafe_to_work = calculate_routes(o = places_sf$geometry[4], d = places_sf$geometry[2],  name = "Robin")
work_to_home = calculate_routes(o = places_sf$geometry[2], d = places_sf$geometry[1],  name = "Robin")


robin_activity = rbind(home_to_work,work_to__park,park_to_cafe,cafe_to_work,work_to_home)

# calculate a neighbours activity
places_neighbour = tibble::tribble(
  ~name, ~x, ~y,
  "Home", -1.524, 53.822,
  "Work", -1.552, 53.814,
  "Park", -1.560, 53.812,
  "Cafe", -1.556, 53.802
)
places_neighbour_sf = sf::st_as_sf(places_neighbour, coords = c("x", "y"), crs = 4326)

home_to_work_neighbour = calculate_routes(o = places_neighbour_sf$geometry[1], d = places_neighbour_sf$geometry[2], name = "Neighbour")
work_to__park_neighbour = calculate_routes(o = places_neighbour_sf$geometry[2], d = places_neighbour_sf$geometry[3],  name = "Neighbour")
park_to_cafe_neighbour = calculate_routes(o = places_neighbour_sf$geometry[3], d = places_neighbour_sf$geometry[4],  name = "Neighbour")
cafe_to_work_neighbour = calculate_routes(o = places_neighbour_sf$geometry[4], d = places_neighbour_sf$geometry[2],  name = "Neighbour")
work_to_home_neighbour = calculate_routes(o = places_neighbour_sf$geometry[2], d = places_neighbour_sf$geometry[1],  name = "Neighbour")

neighbour_activity = rbind(home_to_work_neighbour,work_to__park_neighbour,park_to_cafe_neighbour, cafe_to_work_neighbour, work_to_home_neighbour)

# bind activities
activity_all = rbind(neighbour_activity,robin_activity)
activity_all$name = as.character(activity_all$name)

# convert data into list format for playback function (note: for the leaflet playback to work a point must be cast + far from ideal but estimates an eprox journey)
activity_cast = st_cast(activity_all, "POINT")
activity_cast = split(activity_cast, f = activity_cast$name)
lapply(1:length(activity_cast), function(x) {
  activity_cast[[x]]$time <<- as.POSIXct(
    seq.POSIXt(Sys.time() - 2500, Sys.time(), length.out = nrow(activity_cast[[x]])))
})

# agent icon
agent = makeIcon(
  iconUrl = "https://www.freepnglogos.com/uploads/circle-png/orange-circle-icons-and-png-31.png",
  iconWidth = 14, iconHeight = 14
)

# plot map
leaflet() %>%
  addTiles() %>%
  addPlayback(data = activity_cast,
              icon = agent,
              options = playbackOptions(radius = 3,
                                        speed = 8,tickLen = 50, tracksLayer = TRUE),
              pathOpts = pathOptions(weight = 8))

@Robinlovelace
Copy link
Collaborator Author

Had a play. This is looking Awesome! Great work Nathanael. One suggestion: allow it to visualise movement without need for routing, e.g. using https://docs.ropensci.org/stplanr/reference/line_segment.html

@Robinlovelace
Copy link
Collaborator Author

image

Robinlovelace added a commit that referenced this issue Sep 15, 2021
@Robinlovelace
Copy link
Collaborator Author

I think this is done. Please review @natesheehan, @lucasccdias and even @dabreegster if you get a chance: https://a-b-street.github.io/abstr/articles/activity.html

Would be great to check that the code is actually reproducible.

There is some duplication in there and I think we should have a bit of a summary of how the different articles fit together on the README but overall think this is more-or-less done...

@Robinlovelace
Copy link
Collaborator Author

This could be split out as a separate issue... Visualising activity patterns dynamically before A/B Street import, e.g. with ab_animate() and ab_interactive() functions, as discussed with @natesheehan and as touched on here: Blog post on animating trajectories geocompx/geocompkg#29

Quite a big one just noticed but looks like progress has already been made.

@lucasccdias
Copy link
Collaborator

Hi, @Robinlovelace . The ./import.sh --raw --map --city=gb/leeds did not work here.

gdal_abstreet

But I was able to download the map using the GUI of the build v0_2_58.

@dabreegster
Copy link
Contributor

You're missing GDAL dependencies: https://a-b-street.github.io/docs/tech/dev/index.html#dependencies

I'm finishing a big refactor to A/B Street's importing tools today: a-b-street/abstreet#753
After that, I'm going to come through and update docs here too. I don't encourage people to compile A/B Street themselves; it's a large barrier, and the binary release will work fine after my PR.

@natesheehan
Copy link
Collaborator

This could be split out as a separate issue... Visualising activity patterns dynamically before A/B Street import, e.g. with ab_animate() and ab_interactive() functions, as discussed with @natesheehan and as touched on here: Blog post on animating trajectories geocompr/geocompkg#29

Quite a big one just noticed but looks like progress has already been made.

I agree, this certainly feels like at least two separate vignettes. My thoughts would be once we have either ab_animate or ab_interactive then we could split these two up. One is an introduction to the multi-trip activity model, with the end showing how to visualize in R, but also outlining the shortcomings of R in comparison to A/B-Street. The second should be the import of multi-modal activity in A/B-Street, building up from a small example to a BIG example.

@natesheehan
Copy link
Collaborator

An activity model video would be a nice addition for #68

@natesheehan
Copy link
Collaborator

I think we can close this issue. IMO we should move ab_animate to a separate repo and possibly integrate a bigger activity model into the vignette at a later stage.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants