hypertidy/silicate

sfheaders constructor

mdsumner opened this issue · 2 comments

Just me learnin'

library(silicate)
library(sf)
sc <- PATH(inlandwaters)


## here we expand full table in one go, it's faster but may scale better if
## joining each path back to the vertex table
system.time({
  sf1 <- sc$path %>% inner_join(sc$path_link_vertex, "path_") %>% 
  transmute(multipolygon_id = as.integer(factor(object_)), 
            polygon_id = as.integer(factor(path_)), vertex_) %>% 
  inner_join(sc$vertex, "vertex_") %>% 
  transmute(x = x_, y = y_, multipolygon_id, polygon_id) %>% 
  sfc_multipolygon(multipolygon_id = "multipolygon_id", polygon_id = "polygon_id") %>% st_zm()
})

plot(sf1, col = rainbow(6))

FYI, sf_() functions are now implemented; The only caveat at the moment is the object needs to be sorted first to retain the correct id values.

system.time({
  sf1 <- sc$path %>% inner_join(sc$path_link_vertex, "path_") %>% 
    transmute(multipolygon_id = as.integer(factor(object_)), 
              polygon_id = as.integer(factor(path_)), vertex_) %>% 
    inner_join(sc$vertex, "vertex_") %>% 
    #arrange(multipolygon_id, polygon_id) %>%     ## probably needed
    #transmute(x = x_, y = y_, multipolygon_id, polygon_id) %>% 
    sf_multipolygon(x = "x_", y = "y_", multipolygon_id = "multipolygon_id", polygon_id = "polygon_id") %>% st_zm()
})

plot(sf1, col = rainbow(6))

Something something for the long awaited general constructor:

build-helper branch: https://github.com/mdsumner/sfheaders/blob/build-helper/R/sf_build_helper.R

  • makes robust to names(df) == x,y,multipoint_id,multipolygon_id
df <- data.frame(
  ml_id = c(1,1,1,1,1,1,1,1,2,2,2,2,2)
  , l_id = c(1,1,1,2,2,3,3,3,1,1,1,2,2)
  , lon = rnorm(13)
  , lat = rnorm(13)
  , zz = rnorm(13)
  , mm = rnorm(13)
)

library(dplyr)
df %>% transmute(x = lon, y = lat, m = mm, 
                 multilinestring_id = ml_id, 
                 linestring_id = l_id) %>% to_sf()

[1] "MULTILINESTRING"
[1] "XYM"
  id
1  1
2  2
                                                                                                                                                                                                                                                                                                    geometry
1 0.61963987, 0.11224327, 0.64566518, 0.12233031, 1.65269752, 0.67847771, 1.85544648, -0.54576171, 0.60228944, 1.54736628, 0.68739131, -1.27781841, -0.18173460, -2.00502488, -1.03697337, 0.05077725, -0.07291741, -0.33053971, -1.28162614, 0.65140647, -1.65032123, -0.51335996, -2.31118245, -2.30453526
2                                                                                                                                   0.1216577, 0.5552702, 0.4773798, -0.9760897, 1.2971763, 1.0136283, -1.1285004, 0.9403342, 1.9043115, -0.8785659, 1.4213589, -1.8107020, -0.4950868, 1.6528056, 1.1824897