UrbanAnalyst / dodgr

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

add_nodes_to_graph to dodgr_streetnet_sc #217

Closed diegoteca closed 1 year ago

diegoteca commented 1 year ago

Hi Mark,

Thanks you (and for the other contributors) for this package!.

I have a problem and I don't know if it is for (my) bad interpretation of documentation or if it is a small bug.

Is it possible run the function "add_nodes_to_graph" from a "dodgr_streetnet_sc" object ?

I runned the function "add_nodes_to_graph" to my weight graph (previusly builded with "dodgr_streetnet_sc") and I received this message:

"Error in find_xy_cols(obj) : Unable to determine longitude and latitude columns; perhaps try re-naming columns."

I test this with the hampi example (see reprex below) and this message also appear, so I think that it not a problem with my specific data. Perhaps the above is not a mistake but rather an expected result by design. In this case would be a pity because some functions only work with sc_format inputs (isoverts, times, etc.).

library(dodgr)
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union

# Option with sf ----
# Works

hampi_sf = dodgr_streetnet("hampi india")

hampi_net_sf = weight_streetnet(hampi_sf, 
                               wt_profile = "foot")
#> The following highway types are present in data yet lack corresponding weight_profile values: bus_stop,

dim (hampi_net_sf)
#> [1] 7830   15

verts = dodgr_vertices (hampi_net_sf)
set.seed (2)
npts = 10
xy = data.frame (
          x = min (verts$x) + runif (npts) * diff (range (verts$x)),
          y = min (verts$y) + runif (npts) * diff (range (verts$y))
)

hampi_net_sf_new = add_nodes_to_graph(hampi_net_sf, xy)
dim (hampi_net_sf_new)
#> [1] 7880   15

# Option with silicate ----
# Don't work

hampi_sc = dodgr_streetnet_sc("hampi india")

hampi_net_sc = weight_streetnet(hampi_sc, 
                            wt_profile = "foot",
                             turn_penalty = TRUE,
                            keep_cols = "access",
                             left_side = FALSE)
dim (hampi_net_sc)
#> [1] 7836   17

verts = dodgr_vertices (hampi_net_sc)
set.seed (2)
npts = 10
xy = data.frame (
          x = min (verts$x) + runif (npts) * diff (range (verts$x)),
          y = min (verts$y) + runif (npts) * diff (range (verts$y))
)

hampi_net_sc_new = add_nodes_to_graph (hampi_net_sc, xy)
#> Error in find_xy_cols(obj): Unable to determine longitude and latitude columns; perhaps try re-naming columns.
dim (hampi_net_sc_new)
#> Error in eval(expr, envir, enclos): objeto 'hampi_net_sc_new' no encontrado

# Opcion with silicate (with names modifications) ----
# Don't work
# Because the error message said "try re-naming columns", I tested with the same coluns names that the sf format 

colnames(hampi_net_sc)[1] = "from_id"
colnames(hampi_net_sc)[2] = "to_id"
colnames(hampi_net_sc)[3] = "edge_"
colnames(hampi_net_sc)[4] = "from_lon"
colnames(hampi_net_sc)[5] = "from_lat"
colnames(hampi_net_sc)[6] = "to_lon"
colnames(hampi_net_sc)[7] = "to_lat"
colnames(hampi_net_sc)[9] = "way_id"

graph_street_sc_new <- add_nodes_to_graph (hampi_net_sc, xy)
#> Error in find_xy_cols(obj): Unable to determine longitude and latitude columns; perhaps try re-naming columns.
dim (graph_street_sc_new)
#> Error in eval(expr, envir, enclos): objeto 'graph_street_sc_new' no encontrado

Created on 2023-10-02 with reprex v2.0.2