constructs an sf of POLYGON objects
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
)
sorted matrix or data.frame
x geometry column
y geometry column
z geometry column
m geometry column
column of ids for polygons
column of ids for lines (within polygons)
logical indicating whether polygons should be closed. If TRUE
,
all polygons will be checked and force closed if possible
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.
vector of column names to turn into a list.
sf
object of POLYGON geometries
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.
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.
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 -1.75627543, -0.69053790, -0.55854199, -0.53666333, 0.22712713, 0.97845492, -0.20888265, -1.39941046, 0.25853729, -0.44179945, 0.56859986, 2.12685046, 0.42485844, -1.68428153, 0.24940178, -1.75627543, 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, -1.30511701, 1.07283825
sf_polygon( obj = df, x = "x", y = "y", z = "z")
#> id
#> 1 1
#> geometry
#> 1 -1.75627543, -0.69053790, -0.55854199, -0.53666333, 0.22712713, 0.97845492, -0.20888265, -1.39941046, 0.25853729, -0.44179945, 0.56859986, 2.12685046, 0.42485844, -1.68428153, 0.24940178, -1.75627543, 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, -1.30511701, 1.07283825, -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.50909984, -0.94491206
sf_polygon( obj = df, x = "x", y = "y", z = "z", m = "m")
#> id
#> 1 1
#> geometry
#> 1 -1.75627543, -0.69053790, -0.55854199, -0.53666333, 0.22712713, 0.97845492, -0.20888265, -1.39941046, 0.25853729, -0.44179945, 0.56859986, 2.12685046, 0.42485844, -1.68428153, 0.24940178, -1.75627543, 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, -1.30511701, 1.07283825, -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.50909984, -0.94491206, 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, 0.26306668, 1.53274148
sf_polygon( obj = df, x = 2, y = 3)
#> id
#> 1 1
#> geometry
#> 1 1.0000000, 1.0000000, 1.0000000, 2.0000000, 2.0000000, 2.0000000, 3.0000000, 3.0000000, 3.0000000, 1.0000000, 1.0000000, 1.0000000, 2.0000000, 2.0000000, 2.0000000, 1.0000000, -1.7562754, -0.6905379, -0.5585420, -0.5366633, 0.2271271, 0.9784549, -0.2088827, -1.3994105, 0.2585373, -0.4417995, 0.5685999, 2.1268505, 0.4248584, -1.6842815, 0.2494018, -1.7562754
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, -1.75627543, -0.69053790, -0.55854199, -0.53666333, 0.22712713, 0.97845492, -0.20888265, -1.39941046, 0.25853729, -0.44179945, 0.56859986, 2.12685046, 0.42485844, -1.68428153, 0.24940178, -1.75627543, 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, -1.30511701, 1.07283825
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, -1.75627543, -0.69053790, -0.55854199, -0.53666333, 0.22712713, 0.97845492, -0.20888265, -1.39941046, 0.25853729, -0.44179945, 0.56859986, 2.12685046, 0.42485844, -1.68428153, 0.24940178, -1.75627543, 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, -1.30511701, 1.07283825, -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.50909984, -0.94491206
sf_polygon( obj = df, polygon_id = "ml_id", linestring_id = "l_id" )
#> ml_id
#> 1 1
#> 2 2
#> geometry
#> 1 -1.75627543, -0.69053790, -0.55854199, -1.75627543, 1.07283825, 2.03936926, 0.44945378, 1.07283825, -0.94491206, 0.45434159, -0.85520250, -0.94491206, 1.53274148, 0.42914737, 0.12210341, 1.53274148, -0.53666333, 0.22712713, 0.97845492, -0.53666333, 1.39181405, 0.42656655, 0.10758399, 1.39181405, -0.28689522, 0.89496163, 0.06730444, -0.28689522, -1.13801240, -0.55801513, 1.05253854, -1.13801240, -0.20888265, -1.39941046, 0.25853729, -0.20888265, 0.02229473, 0.60361101, -0.26265057, 0.02229473, -0.16267634, -0.82731017, 1.87650562, -0.16267634, 0.67768364, 0.03849955, -0.35638119, 0.67768364
#> 2 -0.44179945, 0.56859986, 2.12685046, -0.44179945, -0.52826408, 0.19214942, -1.14619967, -0.52826408, 0.76644020, 0.97995670, 1.32178099, 0.76644020, 0.78284410, 0.80441162, -1.90006082, 0.78284410, 0.42485844, -1.68428153, 0.24940178, 0.42485844, 0.84618466, 0.08171963, -1.30511701, 0.84618466, -1.11971083, 0.51459982, -1.50909984, -1.11971083, 0.93578429, -0.30905150, 0.26306668, 0.93578429
sf_polygon( obj = df, polygon_id = 1, linestring_id = 2 )
#> id
#> 1 1
#> 2 2
#> geometry
#> 1 -1.75627543, -0.69053790, -0.55854199, -1.75627543, 1.07283825, 2.03936926, 0.44945378, 1.07283825, -0.94491206, 0.45434159, -0.85520250, -0.94491206, 1.53274148, 0.42914737, 0.12210341, 1.53274148, -0.53666333, 0.22712713, 0.97845492, -0.53666333, 1.39181405, 0.42656655, 0.10758399, 1.39181405, -0.28689522, 0.89496163, 0.06730444, -0.28689522, -1.13801240, -0.55801513, 1.05253854, -1.13801240, -0.20888265, -1.39941046, 0.25853729, -0.20888265, 0.02229473, 0.60361101, -0.26265057, 0.02229473, -0.16267634, -0.82731017, 1.87650562, -0.16267634, 0.67768364, 0.03849955, -0.35638119, 0.67768364
#> 2 -0.44179945, 0.56859986, 2.12685046, -0.44179945, -0.52826408, 0.19214942, -1.14619967, -0.52826408, 0.76644020, 0.97995670, 1.32178099, 0.76644020, 0.78284410, 0.80441162, -1.90006082, 0.78284410, 0.42485844, -1.68428153, 0.24940178, 0.42485844, 0.84618466, 0.08171963, -1.30511701, 0.84618466, -1.11971083, 0.51459982, -1.50909984, -1.11971083, 0.93578429, -0.30905150, 0.26306668, 0.93578429
## 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 1.7637894 2.648932 a
#> 2 2 -0.2721537 -1.452300 j
#> geometry
#> 1 -1.7905919, -0.7882588, -1.1330217, -1.7905919, 2.2117695, -1.0416684, -1.1465239, 2.2117695, 0.3636526, -0.2858879, 0.5176691, 0.3636526, -1.6753273, 1.5259387, 0.5541855, -1.6753273, -0.1029087, -0.9740696, 1.2706723, -0.1029087, 1.9931103, -0.1541207, 2.5644083, 1.9931103
#> 2 0.9608648, 0.7687214, 1.0359308, 0.9608648, 1.0619991, 1.1426949, 1.1238388, 1.0619991, -0.4738871, -1.2753349, -0.3056207, -0.4738871, -0.3970015, -0.8232612, -0.5788846, -0.3970015
## 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 1.7637894 2.648932 a, b, c, a, d, e, f, d, g, h, i, g
#> 2 2 -0.2721537 -1.452300 j, k, l, j, m, n, o, m
#> geometry
#> 1 -1.7905919, -0.7882588, -1.1330217, -1.7905919, 2.2117695, -1.0416684, -1.1465239, 2.2117695, 0.3636526, -0.2858879, 0.5176691, 0.3636526, -1.6753273, 1.5259387, 0.5541855, -1.6753273, -0.1029087, -0.9740696, 1.2706723, -0.1029087, 1.9931103, -0.1541207, 2.5644083, 1.9931103
#> 2 0.9608648, 0.7687214, 1.0359308, 0.9608648, 1.0619991, 1.1426949, 1.1238388, 1.0619991, -0.4738871, -1.2753349, -0.3056207, -0.4738871, -0.3970015, -0.8232612, -0.5788846, -0.3970015