constructs an sf of POLYGON objects
Usage
sf_polygon(
obj = NULL,
x = NULL,
y = NULL,
z = NULL,
m = NULL,
polygon_id = NULL,
linestring_id = NULL,
close = TRUE,
keep = FALSE,
list_columns = NULL
)Arguments
- obj
sorted matrix or data.frame
- x
x geometry column
- y
y geometry column
- z
z geometry column
- m
m geometry column
- polygon_id
column of ids for polygons
- 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- keep
logical indicating if the non-geometry and non-id columns should be kept. if TRUE you must supply the geometry and id columns, and only the first row of each geometry is kept. See Keeping Properties.
- list_columns
vector of column names to turn into a list.
notes
sfheaders functions do not perform any validity checks on the geometries. Nor do they set Coordinate Reference Systems, EPSG, PROJ4 or precision attributes.
The data.frame and matrices you send into the sfheader functions must be ordered.
Keeping Properties
Setting keep = TRUE will retain any columns not specified as a
coordinate (x, y, z, m) or an id (e.g., linestring_id, polygon_id) of the input obj.
You can use list_columns to specify which of the properties will be turned into
a list, thus keeping all the values in the column. For columns not specified in list_columns,
only the first row of the column is kept
The sf_* functions assume the input obj is a long data.frame / matrix,
where any properties are repeated down the table for the same geometry.
Examples
m <- matrix(c(0,0,0,0,1,1), ncol = 2 )
sf_polygon( m )
#> id geometry
#> 1 1 0, 0, 0, 0, 0, 1, 1, 0
m <- matrix(c(0,0,0,0,0,1,0,1,1,1,2,2,1,2,3,1,3,4), ncol = 3, byrow = TRUE)
sf_polygon( obj = m )
#> id geometry
#> 1 1 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 2, 2, 3, 0, 0, 1, 1, 2, 3, 4, 0
sf_polygon( obj = m, polygon_id = 1 )
#> id geometry
#> 1 0 0, 0, 1, 0, 0, 1, 1, 0
#> 2 1 2, 2, 3, 2, 2, 3, 4, 2
sf_polygon( obj = m, linestring_id = 1 )
#> id geometry
#> 1 1 0, 0, 1, 0, 0, 1, 1, 0, 2, 2, 3, 2, 2, 3, 4, 2
sf_polygon( obj = m, linestring_id = 1, polygon_id = 1 )
#> id geometry
#> 1 0 0, 0, 1, 0, 0, 1, 1, 0
#> 2 1 2, 2, 3, 2, 2, 3, 4, 2
sf_polygon( obj = m, x = 2, y = 3 )
#> id geometry
#> 1 1 0, 0, 1, 2, 2, 3, 0, 0, 1, 1, 2, 3, 4, 0
sf_polygon( obj = m, x = 1, y = 2, z = 3 )
#> id geometry
#> 1 1 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 2, 2, 3, 0, 0, 1, 1, 2, 3, 4, 0
sf_polygon( obj = m, x = 2, y = 3, linestring_id = 1, polygon_id = 1 )
#> id geometry
#> 1 0 0, 0, 1, 0, 0, 1, 1, 0
#> 2 1 2, 2, 3, 2, 2, 3, 4, 2
df <- data.frame(
ml_id = c(1,1,1,1,1,1,1,1,1,2,2,2,2,2,2)
, l_id = c(1,1,1,2,2,2,3,3,3,1,1,1,2,2,2)
, x = rnorm(15)
, y = rnorm(15)
, z = rnorm(15)
, m = rnorm(15)
)
sf_polygon( obj = df, x = "x", y = "y")
#> id
#> 1 1
#> geometry
#> 1 0.24940178, 1.07283825, 2.03936926, 0.44945378, 1.39181405, 0.42656655, 0.10758399, 0.02229473, 0.60361101, -0.26265057, -0.52826408, 0.19214942, -1.14619967, 0.84618466, 0.08171963, 0.24940178, -1.30511701, -0.94491206, 0.45434159, -0.85520250, -0.28689522, 0.89496163, 0.06730444, -0.16267634, -0.82731017, 1.87650562, 0.76644020, 0.97995670, 1.32178099, -1.11971083, 0.51459982, -1.30511701
sf_polygon( obj = df, x = "x", y = "y", z = "z")
#> id
#> 1 1
#> geometry
#> 1 0.24940178, 1.07283825, 2.03936926, 0.44945378, 1.39181405, 0.42656655, 0.10758399, 0.02229473, 0.60361101, -0.26265057, -0.52826408, 0.19214942, -1.14619967, 0.84618466, 0.08171963, 0.24940178, -1.30511701, -0.94491206, 0.45434159, -0.85520250, -0.28689522, 0.89496163, 0.06730444, -0.16267634, -0.82731017, 1.87650562, 0.76644020, 0.97995670, 1.32178099, -1.11971083, 0.51459982, -1.30511701, -1.50909984, 1.53274148, 0.42914737, 0.12210341, -1.13801240, -0.55801513, 1.05253854, 0.67768364, 0.03849955, -0.35638119, 0.78284410, 0.80441162, -1.90006082, 0.93578429, -0.30905150, -1.50909984
sf_polygon( obj = df, x = "x", y = "y", z = "z", m = "m")
#> id
#> 1 1
#> geometry
#> 1 0.24940178, 1.07283825, 2.03936926, 0.44945378, 1.39181405, 0.42656655, 0.10758399, 0.02229473, 0.60361101, -0.26265057, -0.52826408, 0.19214942, -1.14619967, 0.84618466, 0.08171963, 0.24940178, -1.30511701, -0.94491206, 0.45434159, -0.85520250, -0.28689522, 0.89496163, 0.06730444, -0.16267634, -0.82731017, 1.87650562, 0.76644020, 0.97995670, 1.32178099, -1.11971083, 0.51459982, -1.30511701, -1.50909984, 1.53274148, 0.42914737, 0.12210341, -1.13801240, -0.55801513, 1.05253854, 0.67768364, 0.03849955, -0.35638119, 0.78284410, 0.80441162, -1.90006082, 0.93578429, -0.30905150, -1.50909984, 0.26306668, -1.79059186, -0.78825884, -1.13302167, 0.36365257, -0.28588791, 0.51766913, -0.10290867, -0.97406959, 1.27067230, 0.96086479, 0.76872137, 1.03593077, -0.47388707, -1.27533487, 0.26306668
sf_polygon( obj = df, x = 2, y = 3)
#> id
#> 1 1
#> geometry
#> 1 1.00000000, 1.00000000, 1.00000000, 2.00000000, 2.00000000, 2.00000000, 3.00000000, 3.00000000, 3.00000000, 1.00000000, 1.00000000, 1.00000000, 2.00000000, 2.00000000, 2.00000000, 1.00000000, 0.24940178, 1.07283825, 2.03936926, 0.44945378, 1.39181405, 0.42656655, 0.10758399, 0.02229473, 0.60361101, -0.26265057, -0.52826408, 0.19214942, -1.14619967, 0.84618466, 0.08171963, 0.24940178
sf_polygon( obj = df, x = 2, y = 3, z = 4)
#> id
#> 1 1
#> geometry
#> 1 1.00000000, 1.00000000, 1.00000000, 2.00000000, 2.00000000, 2.00000000, 3.00000000, 3.00000000, 3.00000000, 1.00000000, 1.00000000, 1.00000000, 2.00000000, 2.00000000, 2.00000000, 1.00000000, 0.24940178, 1.07283825, 2.03936926, 0.44945378, 1.39181405, 0.42656655, 0.10758399, 0.02229473, 0.60361101, -0.26265057, -0.52826408, 0.19214942, -1.14619967, 0.84618466, 0.08171963, 0.24940178, -1.30511701, -0.94491206, 0.45434159, -0.85520250, -0.28689522, 0.89496163, 0.06730444, -0.16267634, -0.82731017, 1.87650562, 0.76644020, 0.97995670, 1.32178099, -1.11971083, 0.51459982, -1.30511701
sf_polygon( obj = df, x = 2, y = 3, z = 4, m = 5)
#> id
#> 1 1
#> geometry
#> 1 1.00000000, 1.00000000, 1.00000000, 2.00000000, 2.00000000, 2.00000000, 3.00000000, 3.00000000, 3.00000000, 1.00000000, 1.00000000, 1.00000000, 2.00000000, 2.00000000, 2.00000000, 1.00000000, 0.24940178, 1.07283825, 2.03936926, 0.44945378, 1.39181405, 0.42656655, 0.10758399, 0.02229473, 0.60361101, -0.26265057, -0.52826408, 0.19214942, -1.14619967, 0.84618466, 0.08171963, 0.24940178, -1.30511701, -0.94491206, 0.45434159, -0.85520250, -0.28689522, 0.89496163, 0.06730444, -0.16267634, -0.82731017, 1.87650562, 0.76644020, 0.97995670, 1.32178099, -1.11971083, 0.51459982, -1.30511701, -1.50909984, 1.53274148, 0.42914737, 0.12210341, -1.13801240, -0.55801513, 1.05253854, 0.67768364, 0.03849955, -0.35638119, 0.78284410, 0.80441162, -1.90006082, 0.93578429, -0.30905150, -1.50909984
sf_polygon( obj = df, polygon_id = "ml_id", linestring_id = "l_id" )
#> ml_id
#> 1 1
#> 2 2
#> geometry
#> 1 0.24940178, 1.07283825, 2.03936926, 0.24940178, -1.30511701, -0.94491206, 0.45434159, -1.30511701, -1.50909984, 1.53274148, 0.42914737, -1.50909984, 0.26306668, -1.79059186, -0.78825884, 0.26306668, 0.44945378, 1.39181405, 0.42656655, 0.44945378, -0.85520250, -0.28689522, 0.89496163, -0.85520250, 0.12210341, -1.13801240, -0.55801513, 0.12210341, -1.13302167, 0.36365257, -0.28588791, -1.13302167, 0.10758399, 0.02229473, 0.60361101, 0.10758399, 0.06730444, -0.16267634, -0.82731017, 0.06730444, 1.05253854, 0.67768364, 0.03849955, 1.05253854, 0.51766913, -0.10290867, -0.97406959, 0.51766913
#> 2 -0.26265057, -0.52826408, 0.19214942, -0.26265057, 1.87650562, 0.76644020, 0.97995670, 1.87650562, -0.35638119, 0.78284410, 0.80441162, -0.35638119, 1.27067230, 0.96086479, 0.76872137, 1.27067230, -1.14619967, 0.84618466, 0.08171963, -1.14619967, 1.32178099, -1.11971083, 0.51459982, 1.32178099, -1.90006082, 0.93578429, -0.30905150, -1.90006082, 1.03593077, -0.47388707, -1.27533487, 1.03593077
sf_polygon( obj = df, polygon_id = 1, linestring_id = 2 )
#> id
#> 1 1
#> 2 2
#> geometry
#> 1 0.24940178, 1.07283825, 2.03936926, 0.24940178, -1.30511701, -0.94491206, 0.45434159, -1.30511701, -1.50909984, 1.53274148, 0.42914737, -1.50909984, 0.26306668, -1.79059186, -0.78825884, 0.26306668, 0.44945378, 1.39181405, 0.42656655, 0.44945378, -0.85520250, -0.28689522, 0.89496163, -0.85520250, 0.12210341, -1.13801240, -0.55801513, 0.12210341, -1.13302167, 0.36365257, -0.28588791, -1.13302167, 0.10758399, 0.02229473, 0.60361101, 0.10758399, 0.06730444, -0.16267634, -0.82731017, 0.06730444, 1.05253854, 0.67768364, 0.03849955, 1.05253854, 0.51766913, -0.10290867, -0.97406959, 0.51766913
#> 2 -0.26265057, -0.52826408, 0.19214942, -0.26265057, 1.87650562, 0.76644020, 0.97995670, 1.87650562, -0.35638119, 0.78284410, 0.80441162, -0.35638119, 1.27067230, 0.96086479, 0.76872137, 1.27067230, -1.14619967, 0.84618466, 0.08171963, -1.14619967, 1.32178099, -1.11971083, 0.51459982, 1.32178099, -1.90006082, 0.93578429, -0.30905150, -1.90006082, 1.03593077, -0.47388707, -1.27533487, 1.03593077
## keeping properties
df <- data.frame(
ml_id = c(1,1,1,1,1,1,1,1,1,2,2,2,2,2,2)
, l_id = c(1,1,1,2,2,2,3,3,3,1,1,1,2,2,2)
, x = rnorm(15)
, y = rnorm(15)
, z = rnorm(15)
, m = rnorm(15)
, val = letters[1:15]
, stringsAsFactors = FALSE
)
## using keep = TRUE means the first row of all non-geometries are kept
sf_polygon(
obj = df
, polygon_id = "ml_id"
, linestring_id = "l_id"
, x = "x"
, y = "y"
, keep = TRUE
)
#> ml_id z m val
#> 1 1 -2.0636545 0.1243011 a
#> 2 2 0.4154064 0.7470286 j
#> geometry
#> 1 -0.3056207, 2.2117695, -1.0416684, -0.3056207, -0.5788846, 1.7637894, 0.1329921, -0.5788846, -1.1465239, -1.6753273, 1.5259387, -1.1465239, 0.3764993, 1.1387077, 1.2412631, 0.3764993, 0.5541855, 1.9931103, -0.1541207, 0.5541855, 0.6120909, -0.4293801, 1.3604613, 0.6120909
#> 2 2.56440834, 1.06199914, 1.14269488, 2.56440834, -0.07085743, -0.27215368, -2.44668003, -0.07085743, 1.12383884, -0.39700149, -0.82326115, 1.12383884, 0.06548664, -1.09850890, -0.63317818, 0.06548664
## use 'list_column' to specify columns where you want to keep all the values
sf_polygon(
obj = df
, polygon_id = "ml_id"
, linestring_id = "l_id"
, x = "x"
, y = "y"
, keep = TRUE
, list_columns = "val"
)
#> ml_id z m val
#> 1 1 -2.0636545 0.1243011 a, b, c, a, d, e, f, d, g, h, i, g
#> 2 2 0.4154064 0.7470286 j, k, l, j, m, n, o, m
#> geometry
#> 1 -0.3056207, 2.2117695, -1.0416684, -0.3056207, -0.5788846, 1.7637894, 0.1329921, -0.5788846, -1.1465239, -1.6753273, 1.5259387, -1.1465239, 0.3764993, 1.1387077, 1.2412631, 0.3764993, 0.5541855, 1.9931103, -0.1541207, 0.5541855, 0.6120909, -0.4293801, 1.3604613, 0.6120909
#> 2 2.56440834, 1.06199914, 1.14269488, 2.56440834, -0.07085743, -0.27215368, -2.44668003, -0.07085743, 1.12383884, -0.39700149, -0.82326115, 1.12383884, 0.06548664, -1.09850890, -0.63317818, 0.06548664