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.3676191, -0.5640758, 2.2137634, 1.0107082, 2.9263283, -1.2976272, -0.8911023, 0.6329409, -0.5569703, 0.6398364, -0.3090776, 1.1412638, -0.3209745, 0.6252955, -0.7352724, 1.3676191, -1.0529744, -2.0186193, -0.5162906, 0.9615230, -1.2323929, -1.9028400, -1.2544138, 0.3233006, 1.1991835, -0.5976572, 0.2424013, 0.9010702, 2.1705092, 1.0749764, 0.6061827, -1.0529744
sf_polygon( obj = df, x = "x", y = "y", z = "z")
#> id
#> 1 1
#> geometry
#> 1 1.36761907, -0.56407578, 2.21376343, 1.01070822, 2.92632835, -1.29762722, -0.89110225, 0.63294093, -0.55697030, 0.63983645, -0.30907758, 1.14126383, -0.32097453, 0.62529546, -0.73527237, 1.36761907, -1.05297437, -2.01861935, -0.51629061, 0.96152297, -1.23239289, -1.90283999, -1.25441379, 0.32330055, 1.19918346, -0.59765725, 0.24240126, 0.90107020, 2.17050918, 1.07497645, 0.60618267, -1.05297437, -1.30882377, 1.32424556, 0.19416736, 0.13645033, 0.43036483, 1.43585054, 0.92290832, -2.20421882, -1.46535551, -0.01642437, -1.02484215, 1.47695964, -1.20483117, -0.48320912, 1.54564296, -1.30882377
sf_polygon( obj = df, x = "x", y = "y", z = "z", m = "m")
#> id
#> 1 1
#> geometry
#> 1 1.36761907, -0.56407578, 2.21376343, 1.01070822, 2.92632835, -1.29762722, -0.89110225, 0.63294093, -0.55697030, 0.63983645, -0.30907758, 1.14126383, -0.32097453, 0.62529546, -0.73527237, 1.36761907, -1.05297437, -2.01861935, -0.51629061, 0.96152297, -1.23239289, -1.90283999, -1.25441379, 0.32330055, 1.19918346, -0.59765725, 0.24240126, 0.90107020, 2.17050918, 1.07497645, 0.60618267, -1.05297437, -1.30882377, 1.32424556, 0.19416736, 0.13645033, 0.43036483, 1.43585054, 0.92290832, -2.20421882, -1.46535551, -0.01642437, -1.02484215, 1.47695964, -1.20483117, -0.48320912, 1.54564296, -1.30882377, -0.26040403, 1.13278353, -1.95781596, -2.99392642, -1.24006695, -1.47989383, -0.69061880, -0.10604828, 1.18722924, 0.50106333, 1.03648008, 0.05242043, 0.71513638, -1.27788202, 1.06121717, -0.26040403
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.3676191, -0.5640758, 2.2137634, 1.0107082, 2.9263283, -1.2976272, -0.8911023, 0.6329409, -0.5569703, 0.6398364, -0.3090776, 1.1412638, -0.3209745, 0.6252955, -0.7352724, 1.3676191
sf_polygon( obj = df, x = 2, y = 3, z = 4)
#> 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.3676191, -0.5640758, 2.2137634, 1.0107082, 2.9263283, -1.2976272, -0.8911023, 0.6329409, -0.5569703, 0.6398364, -0.3090776, 1.1412638, -0.3209745, 0.6252955, -0.7352724, 1.3676191, -1.0529744, -2.0186193, -0.5162906, 0.9615230, -1.2323929, -1.9028400, -1.2544138, 0.3233006, 1.1991835, -0.5976572, 0.2424013, 0.9010702, 2.1705092, 1.0749764, 0.6061827, -1.0529744
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.36761907, -0.56407578, 2.21376343, 1.01070822, 2.92632835, -1.29762722, -0.89110225, 0.63294093, -0.55697030, 0.63983645, -0.30907758, 1.14126383, -0.32097453, 0.62529546, -0.73527237, 1.36761907, -1.05297437, -2.01861935, -0.51629061, 0.96152297, -1.23239289, -1.90283999, -1.25441379, 0.32330055, 1.19918346, -0.59765725, 0.24240126, 0.90107020, 2.17050918, 1.07497645, 0.60618267, -1.05297437, -1.30882377, 1.32424556, 0.19416736, 0.13645033, 0.43036483, 1.43585054, 0.92290832, -2.20421882, -1.46535551, -0.01642437, -1.02484215, 1.47695964, -1.20483117, -0.48320912, 1.54564296, -1.30882377
sf_polygon( obj = df, polygon_id = "ml_id", linestring_id = "l_id" )
#> ml_id
#> 1 1
#> 2 2
#> geometry
#> 1 1.3676191, -0.5640758, 2.2137634, 1.3676191, -1.0529744, -2.0186193, -0.5162906, -1.0529744, -1.3088238, 1.3242456, 0.1941674, -1.3088238, -0.2604040, 1.1327835, -1.9578160, -0.2604040, 1.0107082, 2.9263283, -1.2976272, 1.0107082, 0.9615230, -1.2323929, -1.9028400, 0.9615230, 0.1364503, 0.4303648, 1.4358505, 0.1364503, -2.9939264, -1.2400670, -1.4798938, -2.9939264, -0.8911023, 0.6329409, -0.5569703, -0.8911023, -1.2544138, 0.3233006, 1.1991835, -1.2544138, 0.9229083, -2.2042188, -1.4653555, 0.9229083, -0.6906188, -0.1060483, 1.1872292, -0.6906188
#> 2 0.63983645, -0.30907758, 1.14126383, 0.63983645, -0.59765725, 0.24240126, 0.90107020, -0.59765725, -0.01642437, -1.02484215, 1.47695964, -0.01642437, 0.50106333, 1.03648008, 0.05242043, 0.50106333, -0.32097453, 0.62529546, -0.73527237, -0.32097453, 2.17050918, 1.07497645, 0.60618267, 2.17050918, -1.20483117, -0.48320912, 1.54564296, -1.20483117, 0.71513638, -1.27788202, 1.06121717, 0.71513638
sf_polygon( obj = df, polygon_id = 1, linestring_id = 2 )
#> id
#> 1 1
#> 2 2
#> geometry
#> 1 1.3676191, -0.5640758, 2.2137634, 1.3676191, -1.0529744, -2.0186193, -0.5162906, -1.0529744, -1.3088238, 1.3242456, 0.1941674, -1.3088238, -0.2604040, 1.1327835, -1.9578160, -0.2604040, 1.0107082, 2.9263283, -1.2976272, 1.0107082, 0.9615230, -1.2323929, -1.9028400, 0.9615230, 0.1364503, 0.4303648, 1.4358505, 0.1364503, -2.9939264, -1.2400670, -1.4798938, -2.9939264, -0.8911023, 0.6329409, -0.5569703, -0.8911023, -1.2544138, 0.3233006, 1.1991835, -1.2544138, 0.9229083, -2.2042188, -1.4653555, 0.9229083, -0.6906188, -0.1060483, 1.1872292, -0.6906188
#> 2 0.63983645, -0.30907758, 1.14126383, 0.63983645, -0.59765725, 0.24240126, 0.90107020, -0.59765725, -0.01642437, -1.02484215, 1.47695964, -0.01642437, 0.50106333, 1.03648008, 0.05242043, 0.50106333, -0.32097453, 0.62529546, -0.73527237, -0.32097453, 2.17050918, 1.07497645, 0.60618267, 2.17050918, -1.20483117, -0.48320912, 1.54564296, -1.20483117, 0.71513638, -1.27788202, 1.06121717, 0.71513638
## 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 0.3860982 -0.5165517 a
#> 2 2 -1.1776539 -2.7826515 j
#> geometry
#> 1 -0.53509670, 0.46340262, 0.58266957, -0.53509670, 2.60939356, 0.87407716, 0.41381597, 2.60939356, -0.15554006, -0.70416405, -0.32212609, -0.15554006, 0.19013644, -1.25439695, 0.30994920, 0.19013644, 0.20666081, 0.71615802, -1.08397430, 0.20666081, -0.04159892, 1.02725643, -1.27759841, -0.04159892
#> 2 0.47495206, 1.84458453, -1.09799430, 0.47495206, -0.16356433, 1.61574689, 0.46102532, -0.16356433, -1.46586688, -2.42300965, 0.83047935, -1.46586688, 0.19481258, -0.08423601, 0.03962105, 0.19481258
## 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 0.3860982 -0.5165517 a, b, c, a, d, e, f, d, g, h, i, g
#> 2 2 -1.1776539 -2.7826515 j, k, l, j, m, n, o, m
#> geometry
#> 1 -0.53509670, 0.46340262, 0.58266957, -0.53509670, 2.60939356, 0.87407716, 0.41381597, 2.60939356, -0.15554006, -0.70416405, -0.32212609, -0.15554006, 0.19013644, -1.25439695, 0.30994920, 0.19013644, 0.20666081, 0.71615802, -1.08397430, 0.20666081, -0.04159892, 1.02725643, -1.27759841, -0.04159892
#> 2 0.47495206, 1.84458453, -1.09799430, 0.47495206, -0.16356433, 1.61574689, 0.46102532, -0.16356433, -1.46586688, -2.42300965, 0.83047935, -1.46586688, 0.19481258, -0.08423601, 0.03962105, 0.19481258