Converts a data.frame into a collection of geometries.
Usage
gm_geometries(
obj,
id_cols,
geometry_cols,
class_attributes = list(),
close = FALSE,
closed_attribute = FALSE
)Arguments
- obj
data.frame
- id_cols
vector of id columns (either integer or string)
- geometry_cols
vector of geometry columns (either integer or string)
- class_attributes
class attributes to assign to each geometry
- close
logical stating if the last row must equal the first row of each geometry
- closed_attribute
logical, if true a 'has_been_closed' attribute is added to each matrix that has been closed.
Examples
df <- data.frame(
id = c(1,1,1,1,1,2,2,2,2,2)
, x = 1:10
, y = 10:1
)
gm_geometries(
df
, id_cols = c(1L)
, geometry_cols = c(2L,3L)
)
#> [[1]]
#> [,1] [,2]
#> [1,] 1 10
#> [2,] 2 9
#> [3,] 3 8
#> [4,] 4 7
#> [5,] 5 6
#>
#> [[2]]
#> [,1] [,2]
#> [1,] 6 5
#> [2,] 7 4
#> [3,] 8 3
#> [4,] 9 2
#> [5,] 10 1
#>
## Adding a class attribute
gm_geometries(
df
, id_cols = c(1)
, geometry_cols = c(2,3)
, list( class = "my_line_object" )
)
#> [[1]]
#> [,1] [,2]
#> [1,] 1 10
#> [2,] 2 9
#> [3,] 3 8
#> [4,] 4 7
#> [5,] 5 6
#> attr(,"class")
#> [1] "my_line_object"
#>
#> [[2]]
#> [,1] [,2]
#> [1,] 6 5
#> [2,] 7 4
#> [3,] 8 3
#> [4,] 9 2
#> [5,] 10 1
#> attr(,"class")
#> [1] "my_line_object"
#>
## Adding a second ID column
df$id1 <- c(1,1,1,2,2,1,1,2,2,3)
gm_geometries(
df
, id_cols = c(1,4)
, geometry_cols = c(2,3)
, list( class = "my_multiline_object" )
)
#> [[1]]
#> [[1]]
#> [,1] [,2]
#> [1,] 1 10
#> [2,] 2 9
#> [3,] 3 8
#>
#> [[2]]
#> [,1] [,2]
#> [1,] 4 7
#> [2,] 5 6
#>
#> attr(,"class")
#> [1] "my_multiline_object"
#>
#> [[2]]
#> [[1]]
#> [,1] [,2]
#> [1,] 6 5
#> [2,] 7 4
#>
#> [[2]]
#> [,1] [,2]
#> [1,] 8 3
#> [2,] 9 2
#>
#> [[3]]
#> [,1] [,2]
#> [1,] 10 1
#>
#> attr(,"class")
#> [1] "my_multiline_object"
#>
## Using character column names
gm_geometries(
df
, id_cols = c("id","id1")
, geometry_cols = c("x","y")
)
#> [[1]]
#> [[1]][[1]]
#> [,1] [,2]
#> [1,] 1 10
#> [2,] 2 9
#> [3,] 3 8
#>
#> [[1]][[2]]
#> [,1] [,2]
#> [1,] 4 7
#> [2,] 5 6
#>
#>
#> [[2]]
#> [[2]][[1]]
#> [,1] [,2]
#> [1,] 6 5
#> [2,] 7 4
#>
#> [[2]][[2]]
#> [,1] [,2]
#> [1,] 8 3
#> [2,] 9 2
#>
#> [[2]][[3]]
#> [,1] [,2]
#> [1,] 10 1
#>
#>
## matrix input
m <- as.matrix( df )
gm_geometries(
m
, id_cols = c("id","id1")
, geometry_cols = c("x","y")
)
#> [[1]]
#> [[1]][[1]]
#> [,1] [,2]
#> [1,] 1 10
#> [2,] 2 9
#> [3,] 3 8
#>
#> [[1]][[2]]
#> [,1] [,2]
#> [1,] 4 7
#> [2,] 5 6
#>
#>
#> [[2]]
#> [[2]][[1]]
#> [,1] [,2]
#> [1,] 6 5
#> [2,] 7 4
#>
#> [[2]][[2]]
#> [,1] [,2]
#> [1,] 8 3
#> [2,] 9 2
#>
#> [[2]][[3]]
#> [,1] [,2]
#> [1,] 10 1
#>
#>
gm_geometries(
m
, id_cols = c(1,4)
, geometry_cols = c(2,3)
)
#> [[1]]
#> [[1]][[1]]
#> [,1] [,2]
#> [1,] 1 10
#> [2,] 2 9
#> [3,] 3 8
#>
#> [[1]][[2]]
#> [,1] [,2]
#> [1,] 4 7
#> [2,] 5 6
#>
#>
#> [[2]]
#> [[2]][[1]]
#> [,1] [,2]
#> [1,] 6 5
#> [2,] 7 4
#>
#> [[2]][[2]]
#> [,1] [,2]
#> [1,] 8 3
#> [2,] 9 2
#>
#> [[2]][[3]]
#> [,1] [,2]
#> [1,] 10 1
#>
#>
## use close to make the last row the same as the first row
df <- data.frame(
id = c(1,1,1,1)
, x = c(1,1,2,2)
, y = c(1,2,2,1)
)
gm_geometries(
df
, id_cols = "id"
, geometry_cols = c("x","y")
, close = TRUE
)
#> [[1]]
#> [,1] [,2]
#> [1,] 1 1
#> [2,] 1 2
#> [3,] 2 2
#> [4,] 2 1
#> [5,] 1 1
#>