spatial morpher of sfnetwork to_spatial_contracted ignore NA vlaues

36 views Asked by At

I have a sfnetwork, where I want to run the spatial morpher to_spatial_contracted. However in my grouping variables I have several NA values. If just run it they will get treated as one group, but I would like to have them as different groups.

 library(sf)
library(sfnetworks)

s1 <- st_multilinestring(list(rbind(c(0,3), c(0,4))))
s2 <- st_multilinestring(list(rbind(c(0,4), c(1,5))))
s3 <- st_multilinestring(list(rbind(c(1,5), c(2,5))))
s4 <- st_multilinestring(list(rbind(c(2,5), c(2.5,5))))
s5 <- st_multilinestring(list(rbind(c(2.7,5), c(4,5))))
s6 <- st_multilinestring(list(rbind(c(4,5), c(4.5,4))))
s7 <- st_multilinestring(list(rbind(c(4.5,4), c(5,4))))

sf_ml <- st_sf(section = 1 ,geometry=st_sfc(list(s1,s2,s3,s4,s5,s6,s7)))
sf_ml$group <- c(1,1,2,NA,NA,2,1)

sf_ml <- st_cast(sf_ml, "LINESTRING")

sf_ml <- as_sfnetwork(sf_ml)
sf_ml <-  sf_ml %>% mutate(group = c(1,1,2,NA,NA,2,1,3,3) )
#works but will group NA values
sf_ml <- sf_ml %>%  activate(nodes) %>% convert(., to_spatial_contracted, group, simplyfy = TRUE, summarise_attributes = list(group = max))


#does not work
sf_ml <- sf_ml %>%  activate(nodes) %>% convert(.  , to_spatial_contracted, group, simplyfy = TRUE, node = which(!is.na(.N()$group)),  summarise_attributes = list(group = max))


#works but I will loose the nodes but i want to keep them
sf_ml <- sf_ml %>% filter(!is.na(group)) %>%   activate(nodes) %>% convert(.,   to_spatial_contracted, group, simplyfy = TRUE,summarise_attributes = list(group = max))
0

There are 0 answers