Convert matrix or dataframe with coordinates to a spatial object

locs2sp(locs, proj = "+init=epsg:4326", lon.col = NULL, lat.col = NULL)

Arguments

locs

A matrix or dataframe containing coordinates data. If a matrix, the first two columns will be assumed to contain longitude and latitude coordinates, respectively. If a dataframe, the function will try to guess the columns containing the coordinates based on column names.

proj

Character string specifying the projection (see proj4string or http://spatialreference.org). Default is geographic (unprojected) coordinates, datum WGS84.

lon.col

Character (optional). Name of the column containing longitude data.

lat.col

Character (optional). Name of the column containing latitude data.

Value

A SpatialPoints or SpatialPointsDataFrame.

Examples

locs <- matrix(runif(20), ncol = 2) locs.sp <- locs2sp(locs)
#> As locs is a matrix, assuming first two columns are longitude and latitude, respectively.
locs <- data.frame(species = rep("Laurus nobilis", 10), x = runif(10), y = runif(10)) locs.sp <- locs2sp(locs)
#> Assuming 'y' is latitude
#> Assuming 'x' is longitude