Convert data frame to SpatialPolygons object.
Arguments
- df
data frame containing polygon information, see details
- keys
vector of variable names used to group rows belonging to the same polygon
- coords
vector of variable names identifying the coordinate columns
- proj
the projection of the
SpatialPolygons
object. Needs to be of classCRS
Details
Each row in the data frame df
contains both coordinates and labels (or keys) that identify to which polygon the coordinates belong. This function groups the data frame according to keys
and forms a SpatialPolygons
object from the coordinates in each group. It is important that all rings are closed, that is, that the last row of each group is identical to the first row. Since keys
can be of length greater than one, we identify each polygon with a new key by forming an MD5 hash made out of the respective keys
variables that in themselves are unique (and therefore the hashed key is also unique). For lon-lat coordinates use proj = CRS("+proj=longlat +ellps=sphere")
.