Skip to contents

Create polygon(s) from a data frame with the coordinates of the polygon vertices

Usage

pts2poly_vertices(x, ...)

Arguments

x

data frame with at least two columns; the first two columns must contain longitude and latitude coordinates, respectively. See 'Details' section for how additional columns are handled

...

passed to st_sfc, e.g. for passing named argument crs

Value

Object of class sfc with the geometry type POLYGON

Details

Vertices of different polygons must be demarcated by rows with values of NA in both the first and second columns (i.e. the longitude and latitude columns).

All columns in x besides the first two columns are ignored.

If a crs is not specified in ..., then the crs attribute of the polygon(s) will be NULL.

Examples

x <- data.frame(
  lon = c(40, 40, 50, 50, 40),
  lat = c(0, 10, 10, 0, 0)
)
pts2poly_vertices(x, crs = 4326)
#> Geometry set for 1 feature 
#> Geometry type: POLYGON
#> Dimension:     XY
#> Bounding box:  xmin: 40 ymin: 0 xmax: 50 ymax: 10
#> Geodetic CRS:  WGS 84
#> POLYGON ((40 0, 40 10, 50 10, 50 0, 40 0))

# Create an sf object
x <- data.frame(
  lon = c(40, 40, 50, 50, 40, NA, 20, 20, 30, 30, 20),
  lat = c(0, 10, 10, 0, 0, NA, 0, 10, 10, 0, 0)
)
sf::st_sf(Pred = 1:2, geometry = pts2poly_vertices(x, crs = 4326))
#> Simple feature collection with 2 features and 1 field
#> Geometry type: POLYGON
#> Dimension:     XY
#> Bounding box:  xmin: 20 ymin: 0 xmax: 50 ymax: 10
#> Geodetic CRS:  WGS 84
#>   Pred                       geometry
#> 1    1 POLYGON ((40 0, 40 10, 50 1...
#> 2    2 POLYGON ((20 0, 20 10, 30 1...