constructs sfg POLYGON object

sfg_polygon(
  obj,
  x = NULL,
  y = NULL,
  z = NULL,
  m = NULL,
  linestring_id = NULL,
  close = TRUE
)

Arguments

obj

matrix or data.frame

x

x geometry column

y

y geometry column

z

z geometry column

m

m geometry column

linestring_id

column of ids for lines (within polygons)

close

logical indicating whether polygons should be closed. If TRUE, all polygons will be checked and force closed if possible

Value

sfg object of POLYGON geometry

Examples


sfg_polygon( matrix( 1:24, ncol = 2 ) )
#> [[1]]
#>       [,1] [,2]
#>  [1,]    1   13
#>  [2,]    2   14
#>  [3,]    3   15
#>  [4,]    4   16
#>  [5,]    5   17
#>  [6,]    6   18
#>  [7,]    7   19
#>  [8,]    8   20
#>  [9,]    9   21
#> [10,]   10   22
#> [11,]   11   23
#> [12,]   12   24
#> [13,]    1   13
#> 
#> attr(,"class")
#> [1] "XY"      "POLYGON" "sfg"    
sfg_polygon( matrix( 1:24, ncol = 3 ) )
#> [[1]]
#>       [,1] [,2] [,3]
#>  [1,]    1    9   17
#>  [2,]    2   10   18
#>  [3,]    3   11   19
#>  [4,]    4   12   20
#>  [5,]    5   13   21
#>  [6,]    6   14   22
#>  [7,]    7   15   23
#>  [8,]    8   16   24
#>  [9,]    1    9   17
#> 
#> attr(,"class")
#> [1] "XYZ"     "POLYGON" "sfg"    
sfg_polygon( matrix( 1:24, ncol = 4 ) )
#> [[1]]
#>      [,1] [,2] [,3] [,4]
#> [1,]    1    7   13   19
#> [2,]    2    8   14   20
#> [3,]    3    9   15   21
#> [4,]    4   10   16   22
#> [5,]    5   11   17   23
#> [6,]    6   12   18   24
#> [7,]    1    7   13   19
#> 
#> attr(,"class")
#> [1] "XYZM"    "POLYGON" "sfg"    

## different lines
m <- cbind( matrix( 1:24, ncol = 2 ), c(rep(1, 6), rep(2, 6) ) )
sfg_polygon( obj = m, x = 1, y = 2, linestring_id = 3 )
#> [[1]]
#>      [,1] [,2]
#> [1,]    1   13
#> [2,]    2   14
#> [3,]    3   15
#> [4,]    4   16
#> [5,]    5   17
#> [6,]    6   18
#> [7,]    1   13
#> 
#> [[2]]
#>      [,1] [,2]
#> [1,]    7   19
#> [2,]    8   20
#> [3,]    9   21
#> [4,]   10   22
#> [5,]   11   23
#> [6,]   12   24
#> [7,]    7   19
#> 
#> attr(,"class")
#> [1] "XY"      "POLYGON" "sfg"    

## just specifying linestring_id will use all others as the geometries
sfg_polygon( obj = m, linestring_id = 3 )
#> [[1]]
#>      [,1] [,2]
#> [1,]    1   13
#> [2,]    2   14
#> [3,]    3   15
#> [4,]    4   16
#> [5,]    5   17
#> [6,]    6   18
#> [7,]    1   13
#> 
#> [[2]]
#>      [,1] [,2]
#> [1,]    7   19
#> [2,]    8   20
#> [3,]    9   21
#> [4,]   10   22
#> [5,]   11   23
#> [6,]   12   24
#> [7,]    7   19
#> 
#> attr(,"class")
#> [1] "XY"      "POLYGON" "sfg"    

df <- data.frame( x = 1:12, y = 1:12, z = 13:24, id = c(rep(1,6), rep(2,6)))
sfg_polygon( df, x = "x", y = "y" )
#> [[1]]
#>       [,1] [,2]
#>  [1,]    1    1
#>  [2,]    2    2
#>  [3,]    3    3
#>  [4,]    4    4
#>  [5,]    5    5
#>  [6,]    6    6
#>  [7,]    7    7
#>  [8,]    8    8
#>  [9,]    9    9
#> [10,]   10   10
#> [11,]   11   11
#> [12,]   12   12
#> [13,]    1    1
#> 
#> attr(,"class")
#> [1] "XY"      "POLYGON" "sfg"    
sfg_polygon( df, x = "x", y = "y", linestring_id = "id" )
#> [[1]]
#>      [,1] [,2]
#> [1,]    1    1
#> [2,]    2    2
#> [3,]    3    3
#> [4,]    4    4
#> [5,]    5    5
#> [6,]    6    6
#> [7,]    1    1
#> 
#> [[2]]
#>      [,1] [,2]
#> [1,]    7    7
#> [2,]    8    8
#> [3,]    9    9
#> [4,]   10   10
#> [5,]   11   11
#> [6,]   12   12
#> [7,]    7    7
#> 
#> attr(,"class")
#> [1] "XY"      "POLYGON" "sfg"    

sfg_polygon( df, linestring_id = "id" )
#> [[1]]
#>      [,1] [,2] [,3]
#> [1,]    1    1   13
#> [2,]    2    2   14
#> [3,]    3    3   15
#> [4,]    4    4   16
#> [5,]    5    5   17
#> [6,]    6    6   18
#> [7,]    1    1   13
#> 
#> [[2]]
#>      [,1] [,2] [,3]
#> [1,]    7    7   19
#> [2,]    8    8   20
#> [3,]    9    9   21
#> [4,]   10   10   22
#> [5,]   11   11   23
#> [6,]   12   12   24
#> [7,]    7    7   19
#> 
#> attr(,"class")
#> [1] "XYZ"     "POLYGON" "sfg"