cyipt / actdev

ActDev - Active travel provision and potential in planned and proposed development sites
https://actdev.cyipt.bike
7 stars 3 forks source link

New Sites for "liveable exeter" developments #156

Closed natesheehan closed 3 years ago

natesheehan commented 3 years ago

I have been asked by Exeter City Futures to add 8 new sites to ActDev. The housing developments come from the liveable exeter project.

New developments range from 307 - 5,544 (a whole new town) new houses.

I am currently writing a script to allow new sites to be added (hopefully this could turn into something more dynamic, maybe an r.shiny?) However, I am currently running into issues when building routes (balanced, fast & quiet).

I am unsure if this has something to do with disaggregate_desire_lines or something else I've missed.

error = <simpleError in ldf[i, 1:2]: invalid subscript type 'list'>

Reproducible code: https://gist.github.com/natesheehan/dc02afd6183c255f7b9f329ce36093fb

JSON file (for red cow village): { "type": "FeatureCollection", "features": [{ "type": "Feature", "properties": {}, "geometry": { "type": "Polygon", "coordinates": [ [ [-3.5422325134277344, 50.730238336946485], [-3.5418462753295894, 50.73025871011286], [-3.541733622550964, 50.73022815035997], [-3.5414600372314453, 50.73010251561089], [-3.5406446456909175, 50.73057788856262], [-3.540998697280884, 50.73087669194891], [-3.5410523414611816, 50.73097855630405], [-3.541068434715271, 50.73103627934033], [-3.5411006212234497, 50.731070234034306], [-3.541138172149658, 50.7310804204377], [-3.5415619611740112, 50.73105665215966], [-3.542066216468811, 50.731046465751106], [-3.5424846410751343, 50.73098874272739], [-3.542677760124206, 50.73097176535394], [-3.542715311050415, 50.73086311001816], [-3.5429298877716064, 50.7306491940893], [-3.542865514755249, 50.73049979190884], [-3.542886972427368, 50.7304047175459], [-3.54284405708313, 50.73028247879576], [-3.5425275564193726, 50.73029606089487], [-3.5422325134277344, 50.730238336946485] ] ] } }] }

I'd be greatful @Robinlovelace if we could run through the scenarios-streamlined.r file (and maybe the other scripts too) before WC 12th, if you have a spare 20/30 mins.

mvl22 commented 3 years ago

However, I am currently running into issues when building routes (balanced, fast & quiet).

Have you obtained and specified a CycleStreets API key so that you can access the API service?

See notes at: https://github.com/Robinlovelace/cyclestreets which I presume is ultimately loaded by the code above.

Robinlovelace commented 3 years ago

Great to hear there's demand for new developments! Regarding the specific issue, can you provide a reproducible example to illustrate what is going on? Here is a simple starter for 10 that highlights the need for a CycleStreets API key that @mvl22 mentions as the possible culprit:

remotes::install_github("robinlovelace/cyclestreets")
library(tidyverse)
library(stplanr)
library(pct)
library(tmap)
tmap_mode("view")
nchar(Sys.getenv("CYCLESTREETS")) # check you have API key for routing
# 16
# qtm(pct::pct_regions)
desire_lines = pct::get_pct_lines(region = "devon")
desire_lines_top = desire_lines %>% 
  top_n(n = 20, wt = all) %>% 
  select(all:bicycle)
routes = route(l = desire_lines_top, route_fun = cyclestreets::journey, plan = "fastest")
names(routes)
qtm(routes)

That's a good starter, see the documentation in ?journey on how to get an API key if that is the issue. route_osrm() should still work.

Output from the reproducible example above on my computer shown below.

image

natesheehan commented 3 years ago

Aha! Yes, I expect you are both correct @mvl22 & @Robinlovelace 👏

I have applied for an API key and will keep the thread posted once it's verified 🚀

mvl22 commented 3 years ago

I've pushed that API key request through.

Robin, perhaps the library ought to show an error message if no key is loaded, and calling code ought to check for thrown errors. I'm not sure what error handling is typically done in R so I can't comment on what specific changes should be made, but both of these would be standard good practices.

Robinlovelace commented 3 years ago

Robin, perhaps the library ought to show an error message

Agreed. How is this message?

library(cyclestreets)
Sys.setenv("CYCLESTREETS"="")
# ?journey
from = c(-1.55, 53.80) # geo_code("leeds")
to = c(-1.76, 53.80) # geo_code("bradford uk")
r1 = journey(from, to)
#> Error in journey(from, to): Error: No valid API key was supplied. Please go to https://www.cyclestreets.net/api/apply/ to apply for an API key.

Created on 2021-04-05 by the reprex package (v2.0.0)

natesheehan commented 3 years ago

Thanks, @mvl22 for setting up my API key so swiftly!

@Robinlovelace I am still running into the same issues. See reprex example below:

remotes::install_github("robinlovelace/cyclestreets")

library(tidyverse)
library(stplanr)
library(pct)
library(tmap)
library(cyclestreets)
tmap_mode("view")
#> tmap mode set to interactive viewing
nchar(Sys.getenv("CYCLESTREETS")) # check you have API key for routing
#> [1] 16
desire_lines = pct::get_pct_lines(region = "devon")
desire_lines_top = desire_lines %>% 
  top_n(n = 20, wt = all) %>% 
  select(all:bicycle)

routes = route(l = desire_lines_top, route_fun = cyclestreets::journey, plan = "balanced")
#> Most common output is simpleError
#> These routes failed: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20
#> The first of which was:
#> <simpleError in ldf[i, 1:2]: invalid subscript type 'list'>
#> Returning list
Created on 2021-04-06 by the reprex package (v2.0.0)

I dont think this is an API issue anymore as I can run

from = c(-2.999161, 50.78106) #first desire line from pct devon region
to = c(-2.983265, 50.78106)
r = cyclestreets::journey(from, to, "balanced")
qtm(r)

result: github

are there any other dependencies that might cause this error?

Session Info ``` r devtools::session_info() #> - Session info --------------------------------------------------------------- #> setting value #> version R version 4.0.4 (2021-02-15) #> os Windows 10 x64 #> system x86_64, mingw32 #> ui RTerm #> language (EN) #> collate English_United Kingdom.1252 #> ctype English_United Kingdom.1252 #> tz Europe/London #> date 2021-04-06 #> #> - Packages ------------------------------------------------------------------- #> package * version date lib source #> assertthat 0.2.1 2019-03-21 [1] CRAN (R 4.0.3) #> backports 1.2.1 2020-12-09 [1] CRAN (R 4.0.3) #> cachem 1.0.3 2021-02-04 [1] CRAN (R 4.0.3) #> callr 3.6.0 2021-03-28 [1] CRAN (R 4.0.4) #> cli 2.3.1 2021-02-23 [1] CRAN (R 4.0.4) #> crayon 1.4.1 2021-02-08 [1] CRAN (R 4.0.3) #> desc 1.2.0 2018-05-01 [1] CRAN (R 4.0.3) #> devtools 2.3.2 2020-09-18 [1] CRAN (R 4.0.4) #> digest 0.6.27 2020-10-24 [1] CRAN (R 4.0.3) #> ellipsis 0.3.1 2020-05-15 [1] CRAN (R 4.0.3) #> evaluate 0.14 2019-05-28 [1] CRAN (R 4.0.3) #> fansi 0.4.2 2021-01-15 [1] CRAN (R 4.0.3) #> fastmap 1.1.0 2021-01-25 [1] CRAN (R 4.0.3) #> fs 1.5.0 2020-07-31 [1] CRAN (R 4.0.3) #> glue 1.4.2 2020-08-27 [1] CRAN (R 4.0.3) #> highr 0.8 2019-03-20 [1] CRAN (R 4.0.3) #> htmltools 0.5.1.1 2021-01-22 [1] CRAN (R 4.0.3) #> knitr 1.31 2021-01-27 [1] CRAN (R 4.0.3) #> lifecycle 1.0.0 2021-02-15 [1] CRAN (R 4.0.4) #> magrittr 2.0.1 2020-11-17 [1] CRAN (R 4.0.3) #> memoise 2.0.0 2021-01-26 [1] CRAN (R 4.0.3) #> pillar 1.5.0 2021-02-22 [1] CRAN (R 4.0.4) #> pkgbuild 1.2.0 2020-12-15 [1] CRAN (R 4.0.3) #> pkgconfig 2.0.3 2019-09-22 [1] CRAN (R 4.0.3) #> pkgload 1.1.0 2020-05-29 [1] CRAN (R 4.0.3) #> prettyunits 1.1.1 2020-01-24 [1] CRAN (R 4.0.3) #> processx 3.5.1 2021-04-04 [1] CRAN (R 4.0.5) #> ps 1.5.0 2020-12-05 [1] CRAN (R 4.0.3) #> purrr 0.3.4 2020-04-17 [1] CRAN (R 4.0.3) #> R6 2.5.0 2020-10-28 [1] CRAN (R 4.0.3) #> remotes 2.2.0 2020-07-21 [1] CRAN (R 4.0.3) #> reprex 2.0.0 2021-04-02 [1] CRAN (R 4.0.5) #> rlang 0.4.10 2020-12-30 [1] CRAN (R 4.0.3) #> rmarkdown 2.7 2021-02-19 [1] CRAN (R 4.0.4) #> rprojroot 2.0.2 2020-11-15 [1] CRAN (R 4.0.3) #> sessioninfo 1.1.1 2018-11-05 [1] CRAN (R 4.0.4) #> stringi 1.5.3 2020-09-09 [1] CRAN (R 4.0.3) #> stringr 1.4.0 2019-02-10 [1] CRAN (R 4.0.3) #> styler 1.4.1 2021-03-30 [1] CRAN (R 4.0.4) #> testthat 3.0.2 2021-02-14 [1] CRAN (R 4.0.4) #> tibble 3.0.6 2021-01-29 [1] CRAN (R 4.0.3) #> usethis 2.0.1 2021-02-10 [1] CRAN (R 4.0.4) #> utf8 1.1.4 2018-05-24 [1] CRAN (R 4.0.3) #> vctrs 0.3.6 2020-12-17 [1] CRAN (R 4.0.3) #> withr 2.4.1 2021-01-26 [1] CRAN (R 4.0.3) #> xfun 0.21 2021-02-10 [1] CRAN (R 4.0.3) #> yaml 2.2.1 2020-02-01 [1] CRAN (R 4.0.3) #> #> [1] C:/Users/natha/OneDrive/Documents/R/win-library/4.0 #> [2] C:/Program Files/R/R-4.0.4/library Created on 2021-04-06 by the reprex package (v2.0.0) ```
Robinlovelace commented 3 years ago

Hi @natesheehan yes it looks like something is up with the route() function. It looks the same as an issue that was affecting the Geocomputation with R book's CI build which is based on Docker so it is reproducible, just not on my computer. Not sure what's going on, well worth debugging.

Robinlovelace commented 3 years ago

First thought: could it be the stplanr version? I checked the session info on my computer and it says:

stplanr      * 0.8.1    2020-12-23 [1] Github (ropensci/stplanr@d4d2700)          

Interestingly the stplanr version doesn't appear in the session info in your comment above.

Robinlovelace commented 3 years ago

Tried again on the CRAN version of stplanr and cannot reproduce the issue on my laptop:

 stplanr      * 0.8.1    2021-01-07 [1] CRAN (R 4.0.5)                             
Robinlovelace commented 3 years ago

This is the line of code that is generating the error message in stplanr I think: https://github.com/ropensci/stplanr/blob/master/R/route.R#L132

      single_route <- FUN(ldf[i, 1:2], ldf[i, 3:4], ...)

It seems to me that i here is a list but it should be a numeric (integer) vector of length 1.

Robinlovelace commented 3 years ago

Heads-up @natesheehan I think I've found the cause. Can you try to install the package pbapply and try again? I think this is the issue: https://github.com/ropensci/stplanr/blob/master/R/route.R#L81

natesheehan commented 3 years ago

@Robinlovelace Ayo! You got it! Working perfectly now! Kudos. I will now give it a go with the scenarios-streamlined.R file

Robinlovelace commented 3 years ago

Apologies for this weird issue and thanks for raising it. Imagine it has affected others. Good news: I've created an issue that should be a quick fix. https://github.com/ropensci/stplanr/issues/460

natesheehan commented 3 years ago

Glad to be of help, and thanks for debugging so proficiently 🚀 Good news from me: routes are now made for my new site!

natesheehan commented 3 years ago

A simplified method to add a new site is more or less done. Currently runs fine for scenarios-streamlined.r in build.r. Should get the rest done by AM.

TODO before PR

Robinlovelace commented 3 years ago

Job done I think, great work @natesheehan. One question for @mvl22, should the new site appear in the landing page? The all-sites.GeoJSON is updated and the site is live :tada:

https://actdev.cyipt.bike/exeter-red-cow-village/

natesheehan commented 3 years ago

@joeytalbot could you share with me the emails of those who wanted to add new sites, as we can now do this now. Also, where do the walk_base_rating, cycle_base_rating and drive_base_rating come from? As these are the only fields missing from the new sites

joeytalbot commented 3 years ago

Sorry I didn't mean to delete that!

natesheehan commented 3 years ago

I have added all the livable Exeter sites required for now, going to close this issue.