Skip to contents

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

Usage

pts2poly_centroids(x, y, ...)

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

y

numeric; the perpendicular distance from the polygon centroid (center) to its edge (i.e. half the length of one side of a polygon)

...

passed to st_sf or to st_sfc, e.g. for passing named arguments crs and agr

Value

Object of class sfc (if x has exactly two columns) or class sf

(if x has exactly more than two columns). The object will have a geometry type of POLYGON. If the object is of class sf, the name of the geometry list-column will be "geometry"

Details

This function was designed for someone who reads in a .csv file with a grid of coordinates representing SDM prediction points and needs to create prediction polygons with the .csv file coordinates as the polygon centroids. However, the function can be used to create square polygons of any size around the provided points, regardless of if those polygons touch or overlap. The created polygons are oriented so that, in a 2D plane, their edges are parallel to either the x or the y axis.

If x contains more than two column, then additional columns will be treated as simple feature attributes, i.e. passed along as the first argument to st_sf

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

Examples

# Create an sfc object from a data frame of two columns
x <- data.frame(
  lon = c(5, 10, 15, 20, 5, 10, 15, 20),
  lat = c(5, 5, 5, 5, 10, 10, 10, 10)
)
pts2poly_centroids(x, 2.5, crs = 4326)
#> Geometry set for 8 features 
#> Geometry type: POLYGON
#> Dimension:     XY
#> Bounding box:  xmin: 2.5 ymin: 2.5 xmax: 22.5 ymax: 12.5
#> Geodetic CRS:  WGS 84
#> First 5 geometries:
#> POLYGON ((7.5 7.5, 2.5 7.5, 2.5 2.5, 7.5 2.5, 7...
#> POLYGON ((12.5 7.5, 7.5 7.5, 7.5 2.5, 12.5 2.5,...
#> POLYGON ((17.5 7.5, 12.5 7.5, 12.5 2.5, 17.5 2....
#> POLYGON ((22.5 7.5, 17.5 7.5, 17.5 2.5, 22.5 2....
#> POLYGON ((7.5 12.5, 2.5 12.5, 2.5 7.5, 7.5 7.5,...

# Create an sf object from a data frame of more than two columns
x <- data.frame(
  lon = c(5, 10, 15, 20, 5, 10, 15, 20),
  lat = c(5, 5, 5, 5, 10, 10, 10, 10),
  sdm.pred = runif(8),
  sdm.pred2 = runif(8)
)
pts2poly_centroids(x, 2.5, crs = 4326, agr = "constant")
#> Simple feature collection with 8 features and 2 fields
#> Attribute-geometry relationships: constant (2)
#> Geometry type: POLYGON
#> Dimension:     XY
#> Bounding box:  xmin: 2.5 ymin: 2.5 xmax: 22.5 ymax: 12.5
#> Geodetic CRS:  WGS 84
#>    sdm.pred  sdm.pred2                       geometry
#> 1 0.9697919 0.37896074 POLYGON ((7.5 7.5, 2.5 7.5,...
#> 2 0.6485013 0.35953067 POLYGON ((12.5 7.5, 7.5 7.5...
#> 3 0.9845655 0.84423142 POLYGON ((17.5 7.5, 12.5 7....
#> 4 0.5361333 0.45674918 POLYGON ((22.5 7.5, 17.5 7....
#> 5 0.7610594 0.17903429 POLYGON ((7.5 12.5, 2.5 12....
#> 6 0.5643857 0.96620920 POLYGON ((12.5 12.5, 7.5 12...
#> 7 0.7784543 0.71341195 POLYGON ((17.5 12.5, 12.5 1...
#> 8 0.6390610 0.02488603 POLYGON ((22.5 12.5, 17.5 1...