UrbanAnalyst / dodgr

Distances on Directed Graphs in R
https://urbananalyst.github.io/dodgr/
127 stars 16 forks source link

Bug with dodgr_centrality #146

Closed agila5 closed 3 years ago

agila5 commented 3 years ago

Hi everyone. I was working on a project related to some functionalities in dodgr (@mpadge the code for the RJ paper), and I think I found a bug in dodgr_centrality. I'm not 100% sure what's going on (so maybe it's not dodgr's fault) and the example is quite convoluted (but I couldn't make it simpler, sorry).

# packages
library(osmextract) 
library(dodgr)
library(sf)
#> Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1
library(tmap)

You can install osmextract with remotes::install_github("ITSLeeds/osmextract"). The following code is used to download West Yorkshire data from Geofabrik and filter the data using a 5km square buffer centered at Chapeltown (Leeds).

chapeltown_center <- st_sfc(st_point(c(430964.5, 435700.3)), crs = 27700)
circular_buffer <- st_buffer(chapeltown_center, units::set_units(5, "km")) %>% 
  st_transform(4326)

my_vectortranslate_options <- c(
  "-f", "GPKG", # output format
  "-overwrite", 
  "-lco", "GEOMETRY_NAME=geometry", # geometry name

  # -spat define a spatial bounding box
  "-spat", st_bbox(circular_buffer), 

  # "regular" query
  "-where", "highway IN (
  'motorway', 'motorway_link', 
  'trunk', 'trunk_link', 
  'primary', 'primary_link', 
  'secondary', 'secondary_link',
  'tertiary', 'tertiary_link', 
  'unclassified', 'service'
  )", 

  # layer
  "lines"
)

chapeltown_leeds_key_roads <- oe_get(
  place = "West Yorkshire", 
  quiet = FALSE, 
  vectortranslate_options = my_vectortranslate_options
)
#> The input place was matched with: West Yorkshire
#> The chosen file was already detected in the download directory. Skip downloading.
#> Start with the vectortranslate operations on the input file!
#> 0...10...20...30...40...50...60...70...80...90...100 - done.
#> Finished the vectortranslate operations on the input file!
#> Reading layer `lines' from data source `C:\Users\Utente\Documents\osm_data\geofabrik_west-yorkshire-latest.gpkg' using driver `GPKG'
#> Simple feature collection with 10842 features and 9 fields
#> geometry type:  LINESTRING
#> dimension:      XY
#> bbox:           xmin: -1.61514 ymin: 53.76481 xmax: -1.421095 ymax: 53.875
#> geographic CRS: WGS 84

Prepare dodgr data

street_network_dodgr <- weight_streetnet(chapeltown_leeds_key_roads, wt_profile = "bicycle")

# edge betweenness: 
dodgr_edges_betweennes <- dodgr_centrality(street_network_dodgr)

# and I think the result is quite reasonable: 
tm_shape(dodgr_to_sf(dodgr_edges_betweennes)) + 
  tm_lines(col = "darkgrey", lwd = "centrality", legend.lwd.show = FALSE, scale = 10)


# vertices betweenness: 
dodgr_vertexes_betweennes <- dodgr_centrality(street_network_dodgr, edges = FALSE)
#> Error in `$<-.data.frame`(`*tmp*`, "centrality", value = c(111456, 468230, : replacement has 14334 rows, data has 14335

Created on 2020-11-02 by the reprex package (v0.3.0)

If you want I can share the object used to reproduce the error. Again, sorry but I couldn't make the reprex simpler (and, for example, i cannot replicate the bug if I exclude unclassified and service highways)

EDIT: I cannot replicate the problem using osmdata + dodgr and the same bbox.

mpadge commented 3 years ago

Thanks @agila5, that was indeed a bug. The above commit should have fixed it.

agila5 commented 3 years ago

Now it works perfectly, thank you very much.