Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.22 KB | None | 0 0
  1. data(meuse)
  2. coordinates(meuse) <- ~x+y
  3. meuse <- st_as_sf(meuse)
  4.  
  5. m1 <- select(meuse, cadmium)
  6. m2 <- select(meuse, copper)
  7.  
  8. m3 <- bind_rows(m1, m2)
  9. m3$geometry <- c(m1$geometry, m2$geometry)
  10. m3 <- st_as_sf(m3)
  11.  
  12. > head(m3)
  13. Error in .subset2(x, i, exact = exact) :
  14. attempt to select less than one element in get1index
  15.  
  16. > nc1
  17. Simple feature collection with 100 features and 2 fields
  18. geometry type: MULTIPOLYGON
  19. dimension: XY
  20. bbox: xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965
  21. epsg (SRID): 4267
  22. proj4string: +proj=longlat +datum=NAD27 +no_defs
  23. First 20 features:
  24. NWBIR74 BIR79 geometry
  25. 1 10 1364 MULTIPOLYGON(((-81.47275543...
  26. 2 10 542 MULTIPOLYGON(((-81.23989105...
  27. 3 208 3616 MULTIPOLYGON(((-80.45634460...
  28. 4 123 830 MULTIPOLYGON(((-76.00897216...
  29. 5 1066 1606 MULTIPOLYGON(((-77.21766662...
  30. 6 954 1838 MULTIPOLYGON(((-76.74506378...
  31.  
  32. > nc2
  33. Simple feature collection with 100 features and 2 fields
  34. geometry type: MULTIPOLYGON
  35. dimension: XY
  36. bbox: xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965
  37. epsg (SRID): 4267
  38. proj4string: +proj=longlat +datum=NAD27 +no_defs
  39. First 20 features:
  40. SID79 NWBIR79 geometry
  41. 1 0 19 MULTIPOLYGON(((-81.47275543...
  42. 2 3 12 MULTIPOLYGON(((-81.23989105...
  43. 3 6 260 MULTIPOLYGON(((-80.45634460...
  44. 4 2 145 MULTIPOLYGON(((-76.00897216...
  45. 5 3 1197 MULTIPOLYGON(((-77.21766662...
  46.  
  47. vv = bind_rows(data.frame(nc1),data.frame(nc2))
  48. vv$geometry=c(nc1$geometry, nc2$geometry)
  49. vv = st_as_sf(vv)
  50.  
  51. First 20 features:
  52. NWBIR74 BIR79 SID79 NWBIR79 geometry
  53. 1 10 1364 NA NA MULTIPOLYGON(((-81.47275543...
  54. 2 10 542 NA NA MULTIPOLYGON(((-81.23989105...
  55. 3 208 3616 NA NA MULTIPOLYGON(((-80.45634460...
  56. 4 123 830 NA NA MULTIPOLYGON(((-76.00897216...
  57. 5 1066 1606 NA NA MULTIPOLYGON(((-77.21766662...
  58.  
  59. proj4string: +proj=longlat +datum=NAD27 +no_defs
  60. NWBIR74 BIR79 SID79 NWBIR79 geometry
  61. 195 NA NA 4 487 MULTIPOLYGON(((-77.14895629...
  62. 196 NA NA 5 1023 MULTIPOLYGON(((-78.26149749...
  63. 197 NA NA 3 763 MULTIPOLYGON(((-78.02592468...
  64. 198 NA NA 17 1832 MULTIPOLYGON(((-78.65571594...
  65. 199 NA NA 9 2100 MULTIPOLYGON(((-77.96073150...
  66. 200 NA NA 6 841 MULTIPOLYGON(((-78.65571594...
  67.  
  68. x <- mutate(x, JOINID = 1:nrow(x))
  69. y <- mutate(y, JOINID = (nrow(x)+1):(nrowx() + nrow(y)))
  70. left_join(x, y, by = 'JOINID')
  71.  
  72. library(sf)
  73. library(dplyr)
  74.  
  75. bind_rows_sf <- function(...){
  76. sf_list <- rlang::dots_values(...)[[1]]
  77.  
  78. sfg_list_column <- lapply(sf_list, function(sf) sf$geometry[[1]]) %>% st_sfc
  79. df <- lapply(sf_list, function(sf) st_set_geometry(sf, NULL)) %>% bind_rows
  80.  
  81. sf_appended <- st_sf(data.frame(df, geom=sfg_list_column))
  82.  
  83. return(sf_appended)
  84. }
  85.  
  86. sf_1 <- st_sf(data.frame(a=1, geom=st_sfc(st_point(0:1))))
  87. sf_2 <- st_sf(data.frame(a=2, b=4, geom=st_sfc(st_point(1:2))))
  88. sf_3 <- st_sf(data.frame(a=3, b=5, c=6, geom=st_sfc(st_point())))
  89. sf_list <- list(sf_1, sf_2, sf_3)
  90.  
  91. sf_123 <- sf_list %>% bind_rows_sf
  92. sf_123
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement