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

sum_network_routes results differ between sf and sp data #249

Closed
Robinlovelace opened this issue Apr 10, 2018 · 9 comments
Closed

sum_network_routes results differ between sf and sp data #249

Robinlovelace opened this issue Apr 10, 2018 · 9 comments

Comments

@Robinlovelace
Copy link
Member

Identified here: https://github.com/Robinlovelace/geocomp-tranport-workshop

If

ways_sln = SpatialLinesNetwork(ways_sp)

is replaced by

ways_sln = SpatialLinesNetwork(ways)

It produces an incorrect result.

@mpadge
Copy link
Member

mpadge commented Apr 11, 2018

If ways_sp is constructed as as (ways, "Spatial"), then I get the same values

ways_sp <- as (ways, "Spatial")
ways_sln2 <- SpatialLinesNetwork (ways_sp)
identical (slot (ways_sln, "g"), slot (ways_sln2, "g")) # FALSE, but only coz of igraph hash
identical (slot (ways_sln, "nb"), slot (ways_sln2, "nb")) # TRUE
identical (slot (ways_sln, "weightfield"), slot (ways_sln2, "weightfield")) # TRUE

looks good to me

@Robinlovelace
Copy link
Member Author

Can you reproduce this plot? The results suggest it's not a big issue, something linked to the creation of the sf object I think which should be relatively straightforward to fix, hopefully. Even better: it's not a bug and only affect me (I'm using the dev version of splanr but cannot think of changes that could have introduced this regression):

library(stplanr)
from = "Jena Paradies"
to = "JenTower"
(from_coords = geo_code(from))
#> [1] 11.58766 50.92493
(to_coords = geo_code(to))
#> [1] 11.58465 50.92883
u = "https://github.com/Robinlovelace/geocomp-tranport-workshop/raw/master/ways.geojson"
if (!file.exists("ways.geojson")) {
  download.file(url = u, destfile = "ways.geojson")
}
ways = sf::read_sf("ways.geojson")
ways_sp = as(ways, "Spatial")

ways_sln = SpatialLinesNetwork(ways_sp)
from_sln = find_network_nodes(ways_sln, from_coords[1], from_coords[2])
to_sln = find_network_nodes(ways_sln, to_coords[1], to_coords[2])
r_local = sum_network_routes(ways_sln, from_sln, to_sln, "length", combinations = F)

ways_sln = SpatialLinesNetwork(ways)
#> Linking to GEOS 3.5.1, GDAL 2.2.2, proj.4 4.9.2
from_sln = find_network_nodes(ways_sln, from_coords[1], from_coords[2])
to_sln = find_network_nodes(ways_sln, to_coords[1], to_coords[2])
r_local_sf = sum_network_routes(ways_sln, from_sln, to_sln, "length", combinations = F)

plot(r_local, lwd = 5)
plot(r_local_sf$geometry, add = TRUE, col = "red")

Session info
devtools::session_info()
#> Session info -------------------------------------------------------------
#>  setting  value                       
#>  version  R version 3.4.4 (2018-03-15)
#>  system   x86_64, linux-gnu           
#>  ui       X11                         
#>  language en_GB:en                    
#>  collate  en_GB.UTF-8                 
#>  tz       Europe/London               
#>  date     2018-04-11
#> Packages -----------------------------------------------------------------
#>  package     * version    date       source                           
#>  assertthat    0.2.0      2017-04-11 cran (@0.2.0)                    
#>  backports     1.1.2      2017-12-13 CRAN (R 3.4.3)                   
#>  base        * 3.4.4      2018-03-16 local                            
#>  bindr         0.1.1      2018-03-13 CRAN (R 3.4.4)                   
#>  bindrcpp    * 0.2.2      2018-03-29 CRAN (R 3.4.4)                   
#>  class         7.3-14     2015-08-30 CRAN (R 3.4.0)                   
#>  classInt      0.1-24     2017-04-16 cran (@0.1-24)                   
#>  compiler      3.4.4      2018-03-16 local                            
#>  curl          3.2        2018-03-28 CRAN (R 3.4.4)                   
#>  datasets    * 3.4.4      2018-03-16 local                            
#>  DBI           0.8        2018-03-02 cran (@0.8)                      
#>  devtools      1.13.5     2018-02-18 CRAN (R 3.4.4)                   
#>  digest        0.6.15     2018-01-28 CRAN (R 3.4.3)                   
#>  dplyr         0.7.4      2017-09-28 CRAN (R 3.4.3)                   
#>  e1071         1.6-8      2017-02-02 cran (@1.6-8)                    
#>  evaluate      0.10.1     2017-06-24 CRAN (R 3.4.1)                   
#>  foreign       0.8-69     2017-06-21 CRAN (R 3.4.0)                   
#>  formatR       1.5        2017-04-25 CRAN (R 3.4.1)                   
#>  geosphere     1.5-7      2017-11-05 CRAN (R 3.4.2)                   
#>  glue          1.2.0      2017-10-29 CRAN (R 3.4.2)                   
#>  graphics    * 3.4.4      2018-03-16 local                            
#>  grDevices   * 3.4.4      2018-03-16 local                            
#>  grid          3.4.4      2018-03-16 local                            
#>  htmltools     0.3.6      2017-04-28 cran (@0.3.6)                    
#>  httr          1.3.1      2017-08-20 cran (@1.3.1)                    
#>  igraph        1.2.1      2018-03-10 cran (@1.2.1)                    
#>  jsonlite      1.5        2017-06-01 cran (@1.5)                      
#>  knitr         1.20       2018-02-20 cran (@1.20)                     
#>  lattice       0.20-35    2017-03-25 CRAN (R 3.3.3)                   
#>  lwgeom        0.1-5      2018-03-20 Github (r-spatial/lwgeom@389090c)
#>  magrittr      1.5        2014-11-22 CRAN (R 3.3.2)                   
#>  maptools      0.9-2      2017-03-25 cran (@0.9-2)                    
#>  memoise       1.1.0      2017-04-21 CRAN (R 3.4.1)                   
#>  methods     * 3.4.4      2018-03-16 local                            
#>  mime          0.5        2016-07-07 CRAN (R 3.3.2)                   
#>  openxlsx      4.0.17     2017-03-23 CRAN (R 3.4.1)                   
#>  pillar        1.2.1      2018-02-27 CRAN (R 3.4.4)                   
#>  pkgconfig     2.0.1      2017-03-21 cran (@2.0.1)                    
#>  R.methodsS3   1.7.1      2016-02-16 cran (@1.7.1)                    
#>  R.oo          1.21.0     2016-11-01 cran (@1.21.0)                   
#>  R.utils       2.6.0      2017-11-05 CRAN (R 3.4.2)                   
#>  R6            2.2.2      2017-06-17 cran (@2.2.2)                    
#>  raster        2.6-7      2017-11-13 CRAN (R 3.4.2)                   
#>  Rcpp          0.12.16    2018-03-13 CRAN (R 3.4.4)                   
#>  rgdal         1.2-18     2018-03-17 CRAN (R 3.4.4)                   
#>  rgeos         0.3-26     2017-10-31 cran (@0.3-26)                   
#>  rlang         0.2.0.9001 2018-04-10 Github (r-lib/rlang@70d2d40)     
#>  rmarkdown     1.9        2018-03-01 CRAN (R 3.4.4)                   
#>  rprojroot     1.3-2      2018-01-03 CRAN (R 3.4.3)                   
#>  sf          * 0.6-2      2018-04-09 Github (r-spatial/sf@b7de234)    
#>  sp            1.2-7      2018-01-19 cran (@1.2-7)                    
#>  stats       * 3.4.4      2018-03-16 local                            
#>  stplanr     * 0.2.3.9000 2018-04-10 Github (ropensci/stplanr@4b8abda)
#>  stringi       1.1.7      2018-03-12 CRAN (R 3.4.4)                   
#>  stringr       1.3.0      2018-02-19 cran (@1.3.0)                    
#>  tibble        1.4.2      2018-01-22 cran (@1.4.2)                    
#>  tools         3.4.4      2018-03-16 local                            
#>  udunits2      0.13       2016-11-17 cran (@0.13)                     
#>  units         0.5-1      2018-01-08 cran (@0.5-1)                    
#>  utils       * 3.4.4      2018-03-16 local                            
#>  withr         2.1.2      2018-04-07 Github (jimhester/withr@79d7b0d) 
#>  xml2          1.2.0      2018-01-24 CRAN (R 3.4.3)                   
#>  yaml          2.1.18     2018-03-08 CRAN (R 3.4.4)

@Robinlovelace
Copy link
Member Author

Heads-up @cmcaine this may be something you're interested in (discussed Friday last week).

@cmcaine
Copy link
Contributor

cmcaine commented May 1, 2018 via email

@mem48
Copy link
Collaborator

mem48 commented May 24, 2018

The problem seems to be related to tidyverse/dplyr#3279 and is happending in lines 555 to 571

sldf <- dplyr::bind_rows(
        lapply(
          1:length(routecoords),
          function(x){
            as.data.frame(routecoords[[x]]) %>%
              dplyr::mutate(linenum = x)
          })
      ) %>%
      sf::st_as_sf(
        coords = utils::head(colnames(.),-2),
        crs = sf::st_crs(sln@sl)$epsg
      ) %>%
      dplyr::group_by(.data$linenum) %>%
      dplyr::summarise() %>%
      sf::st_cast("LINESTRING") %>%
      dplyr::bind_cols(routedata) %>%
      dplyr::select(-.data$linenum)

The solution would be to go straight from the dataframe of coordiantes to a matrix of cooridnates and then a LINESTRING rather than goin via POINTS

@Robinlovelace
Copy link
Member Author

On a related note, this also fails:

SLN_sf2 = SpatialLinesNetwork(routes_fast_sf) 
shortpath <- sum_network_routes(SLN_sf2, 1, 2, sumvars = "length") 

@richardellison
Copy link
Collaborator

Sorry for being slow on this! Definitely has to do with the issue that @mem48 refers to. I will need to fix it.

@Robinlovelace
Copy link
Member Author

Hi @richardellison thanks for confirming. As discussed with @mem48 (who works with me here in Leeds) I've given it a good go but have failed to fix it so far. I suspect it's a regression silently introduced by dplyr that I only discovered with the above example. Any ideas towards a solution? Can give it another go but suspect, as the author of the function, you're better qualified to succeed than me!

richardellison added a commit to richardellison/stplanr that referenced this issue May 26, 2018
@richardellison
Copy link
Collaborator

Adding do_union to summarise() appears to have fixed this. I don't think this is a regression, just that I must never have tested it on routes that did not have only increasing x-coordinates.

plot_149_example

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

5 participants