luukvdmeer / sfnetworks

Tidy Geospatial Networks in R
https://luukvdmeer.github.io/sfnetworks/
Other
347 stars 20 forks source link

agr attributes are lost when creating an sfnetwork object #123

Closed agila5 closed 3 years ago

agila5 commented 3 years ago

Describe the bug I think that the agr attributes are lost when converting sf objects to sfnetworks. This is clearly not a big deal but I think it means that some warning messages that are returned by the spatial morphers are irrelevant. See below for an example.

Reproducible example

# load packages
library(sf)
#> Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1
library(sfnetworks)
library(tidygraph)

# fake data
fake_sf <- st_sf(
  data.frame(type = 1), 
  geometry = st_sfc(
    st_linestring(rbind(c(0, 0), c(1, 1))), 
    crs = 4326
  ), 
  agr = "constant"
)

If I convert sf to sfnetworks and then back to sf, the agr component is lost.

as_sfnetwork(fake_sf, directed = FALSE) %>% 
  activate("edges") %>% 
  st_as_sf()
#> Simple feature collection with 1 feature and 3 fields
#> geometry type:  LINESTRING
#> dimension:      XY
#> bbox:           xmin: 0 ymin: 0 xmax: 1 ymax: 1
#> geographic CRS: WGS 84
#> # A tibble: 1 x 4
#>    from    to  type         geometry
#>   <int> <int> <dbl> <LINESTRING [°]>
#> 1     1     2     1       (0 0, 1 1)

I think that means that the following warning message will always be raised

fake_sf %>% 
  as_sfnetwork(directed = FALSE) %>% 
  convert(to_spatial_subdivision)
#> Warning: to_spatial_subdivision assumes attributes are constant over geometries
#> # A sfnetwork with 2 nodes and 1 edges
#> #
#> # CRS:  EPSG:4326 
#> #
#> # An unrooted tree with spatially explicit edges
#> #
#> # Node Data:     2 x 2 (active)
#> # Geometry type: POINT
#> # Dimension:     XY
#> # Bounding box:  xmin: 0 ymin: 0 xmax: 1 ymax: 1
#>      geometry .tidygraph_node_index
#>   <POINT [°]>                 <int>
#> 1       (0 0)                     1
#> 2       (1 1)                     2
#> #
#> # Edge Data:     1 x 5
#> # Geometry type: LINESTRING
#> # Dimension:     XY
#> # Bounding box:  xmin: 0 ymin: 0 xmax: 1 ymax: 1
#>    from    to  type         geometry .tidygraph_edge_index
#>   <int> <int> <dbl> <LINESTRING [°]>                 <int>
#> 1     1     2     1       (0 0, 1 1)                     1

Created on 2021-02-10 by the reprex package (v0.3.0)

Expected behavior Preserve agr components.

R Session Info

Session info ``` r devtools::session_info() #> - Session info --------------------------------------------------------------- #> setting value #> version R version 3.6.3 (2020-02-29) #> os Windows 10 x64 #> system x86_64, mingw32 #> ui RTerm #> language (EN) #> collate Italian_Italy.1252 #> ctype Italian_Italy.1252 #> tz Europe/Berlin #> date 2021-02-10 #> #> - Packages ------------------------------------------------------------------- #> package * version date lib source #> assertthat 0.2.1 2019-03-21 [1] CRAN (R 3.6.0) #> callr 3.5.1 2020-10-13 [1] CRAN (R 3.6.3) #> class 7.3-17 2020-04-26 [1] CRAN (R 3.6.3) #> classInt 0.4-3 2020-04-07 [1] CRAN (R 3.6.3) #> cli 2.3.0 2021-01-31 [1] CRAN (R 3.6.3) #> colorspace 2.0-0 2020-11-11 [1] CRAN (R 3.6.3) #> crayon 1.4.1 2021-02-08 [1] CRAN (R 3.6.3) #> DBI 1.1.1 2021-01-15 [1] CRAN (R 3.6.3) #> desc 1.2.0 2018-05-01 [1] CRAN (R 3.6.0) #> devtools 2.3.2 2020-09-18 [1] CRAN (R 3.6.3) #> digest 0.6.27 2020-10-24 [1] CRAN (R 3.6.3) #> dplyr 1.0.4 2021-02-02 [1] CRAN (R 3.6.3) #> e1071 1.7-4 2020-10-14 [1] CRAN (R 3.6.3) #> ellipsis 0.3.1 2020-05-15 [1] CRAN (R 3.6.3) #> evaluate 0.14 2019-05-28 [1] CRAN (R 3.6.0) #> fansi 0.4.2 2021-01-15 [1] CRAN (R 3.6.3) #> fs 1.5.0 2020-07-31 [1] CRAN (R 3.6.3) #> generics 0.1.0 2020-10-31 [1] CRAN (R 3.6.3) #> ggplot2 3.3.2 2020-06-19 [1] CRAN (R 3.6.3) #> glue 1.4.2 2020-08-27 [1] CRAN (R 3.6.3) #> gtable 0.3.0 2019-03-25 [1] CRAN (R 3.6.0) #> highr 0.8 2019-03-20 [1] CRAN (R 3.6.0) #> htmltools 0.5.0 2020-06-16 [1] CRAN (R 3.6.3) #> igraph 1.2.6 2020-10-06 [1] CRAN (R 3.6.3) #> KernSmooth 2.23-18 2020-10-29 [1] CRAN (R 3.6.3) #> knitr 1.30 2020-09-22 [1] CRAN (R 3.6.3) #> lifecycle 0.2.0 2020-03-06 [1] CRAN (R 3.6.2) #> lwgeom 0.2-5 2020-06-12 [1] CRAN (R 3.6.3) #> magrittr 2.0.1 2020-11-17 [1] CRAN (R 3.6.3) #> memoise 1.1.0 2017-04-21 [1] CRAN (R 3.6.0) #> munsell 0.5.0 2018-06-12 [1] CRAN (R 3.6.0) #> pillar 1.4.7 2020-11-20 [1] CRAN (R 3.6.3) #> pkgbuild 1.1.0 2020-07-13 [1] CRAN (R 3.6.3) #> pkgconfig 2.0.3 2019-09-22 [1] CRAN (R 3.6.1) #> pkgload 1.1.0 2020-05-29 [1] CRAN (R 3.6.3) #> prettyunits 1.1.1 2020-01-24 [1] CRAN (R 3.6.2) #> processx 3.4.4 2020-09-03 [1] CRAN (R 3.6.3) #> ps 1.4.0 2020-10-07 [1] CRAN (R 3.6.3) #> purrr 0.3.4 2020-04-17 [1] CRAN (R 3.6.3) #> R6 2.5.0 2020-10-28 [1] CRAN (R 3.6.3) #> Rcpp 1.0.6 2021-01-15 [1] CRAN (R 3.6.3) #> remotes 2.2.0 2020-07-21 [1] CRAN (R 3.6.3) #> rlang 0.4.10 2020-12-30 [1] CRAN (R 3.6.3) #> rmarkdown 2.5 2020-10-21 [1] CRAN (R 3.6.3) #> rprojroot 2.0.2 2020-11-15 [1] CRAN (R 3.6.3) #> scales 1.1.1 2020-05-11 [1] CRAN (R 3.6.3) #> sessioninfo 1.1.1 2018-11-05 [1] CRAN (R 3.6.0) #> sf * 0.9-7 2021-01-06 [1] CRAN (R 3.6.3) #> sfheaders 0.4.0 2020-12-01 [1] CRAN (R 3.6.3) #> sfnetworks * 0.5.0 2021-02-10 [1] Github (luukvdmeer/sfnetworks@c064180) #> stringi 1.5.3 2020-09-09 [1] CRAN (R 3.6.3) #> stringr 1.4.0 2019-02-10 [1] CRAN (R 3.6.0) #> testthat 3.0.0 2020-10-31 [1] CRAN (R 3.6.3) #> tibble 3.0.6 2021-01-29 [1] CRAN (R 3.6.3) #> tidygraph * 1.2.0 2020-05-12 [1] CRAN (R 3.6.3) #> tidyr 1.1.2 2020-08-27 [1] CRAN (R 3.6.3) #> tidyselect 1.1.0 2020-05-11 [1] CRAN (R 3.6.3) #> units 0.6-7 2020-06-13 [1] CRAN (R 3.6.3) #> usethis 2.0.0 2020-12-10 [1] CRAN (R 3.6.3) #> utf8 1.1.4 2018-05-24 [1] CRAN (R 3.6.0) #> vctrs 0.3.6 2020-12-17 [1] CRAN (R 3.6.3) #> withr 2.3.0 2020-09-22 [1] CRAN (R 3.6.3) #> xfun 0.19 2020-10-30 [1] CRAN (R 3.6.3) #> yaml 2.2.1 2020-02-01 [1] CRAN (R 3.6.2) #> #> [1] C:/Users/Utente/Documents/R/win-library/3.6 #> [2] C:/Program Files/R/R-3.6.3/library ```
luukvdmeer commented 3 years ago

Hmm, I see now that indeed the agr of the edges is not preserved (the agr of the nodes is). This seems to be because we have to convert the edges to a data frame before being able to create a tbl_graph out of it.

Another issue by the way is that the "assumes atributes are constant" is always given, no matter what the agr values are. We probably should do a check before throwing the error.

luukvdmeer commented 3 years ago

The edge agr should be preserved now. Also, the "assume constant" warning is only raised when there is at least one edge attribute not marked as constant (note that the from and to columns are not regarded as edge attributes in that check).

# load packages
library(sf)
#> Linking to GEOS 3.9.0, GDAL 3.2.0, PROJ 7.2.0
library(sfnetworks)
library(tidygraph)
#> 
#> Attaching package: 'tidygraph'
#> The following object is masked from 'package:stats':
#> 
#>     filter

# fake data
fake_sf <- st_sf(
  data.frame(type = 1), 
  geometry = st_sfc(
    st_linestring(rbind(c(0, 0), c(1, 1))), 
    crs = 4326
  ), 
  agr = "constant"
)

net = as_sfnetwork(fake_sf, directed = FALSE)

st_agr(net, "edges")
#>     from       to     type 
#>     <NA>     <NA> constant 
#> Levels: constant aggregate identity

convert(net, to_spatial_subdivision)
#> # A sfnetwork with 2 nodes and 1 edges
#> #
#> # CRS:  EPSG:4326 
#> #
#> # An unrooted tree with spatially explicit edges
#> #
#> # Node Data:     2 x 2 (active)
#> # Geometry type: POINT
#> # Dimension:     XY
#> # Bounding box:  xmin: 0 ymin: 0 xmax: 1 ymax: 1
#>      geometry .tidygraph_node_index
#>   <POINT [°]>                 <int>
#> 1       (0 0)                     1
#> 2       (1 1)                     2
#> #
#> # Edge Data:     1 x 5
#> # Geometry type: LINESTRING
#> # Dimension:     XY
#> # Bounding box:  xmin: 0 ymin: 0 xmax: 1 ymax: 1
#>    from    to  type         geometry .tidygraph_edge_index
#>   <int> <int> <dbl> <LINESTRING [°]>                 <int>
#> 1     1     2     1       (0 0, 1 1)                     1

Created on 2021-03-03 by the reprex package (v0.3.0)