create objects of class SpatialPoints or SpatialPointsDataFrame
SpatialPoints.Rdcreate objects of class SpatialPoints-class or
SpatialPointsDataFrame-class from
coordinates, and from coordinates and data.frames
Usage
SpatialPoints(coords, proj4string=CRS(as.character(NA)), bbox = NULL)
SpatialPointsDataFrame(coords, data, coords.nrs = numeric(0),
proj4string = CRS(as.character(NA)), match.ID, bbox = NULL)Arguments
- coords
numeric matrix or data.frame with coordinates (each row is a point); in case of SpatialPointsDataFrame an object of class SpatialPoints-class is also allowed
- proj4string
projection string of class CRS-class
- bbox
bounding box matrix, usually NULL and constructed from the data, but may be passed through for coercion purposes if clearly needed
- data
object of class
data.frame; the number of rows indatashould equal the number of points in thecoordsobject- coords.nrs
numeric; if present, records the column positions where in
datathe coordinates were taken from (used by coordinates<-)- match.ID
logical or character; if missing, and
coordsanddataboth have row names, and their order does not correspond, matching is done by these row names and a warning is issued; this warning can be suppressed by settingmatch.IDto TRUE. If TRUE AND coords has non-automatic rownames (i.e., coerced to a matrix byas.matrix,dimnames(coords)[[1]]is notNULL), ANDdatahas row.names (i.e. is a data.frame), then theSpatialPointsDataFrameobject is formed by matching the row names of both components, leaving the order of the coordinates in tact. Checks are done to see whether both row names are sufficiently unique, and all data are matched. If FALSE, coordinates and data are simply "glued" together, ignoring row names. If character: indicates the column indatawith coordinates IDs to use for matching records. See examples below.
Value
SpatialPoints returns an object of class SpatialPoints;
SpatialPointsDataFrame returns an object of class SpatialPointsDataFrame;
Examples
set.seed(1331)
pts = cbind(1:5, 1:5)
dimnames(pts)[[1]] = letters[1:5]
df = data.frame(a = 1:5)
row.names(df) = letters[5:1]
library(sp)
options(warn=1) # show warnings where they occur
SpatialPointsDataFrame(pts, df) # warn
#> Warning: forming a SpatialPointsDataFrame based on maching IDs, not on record order. Use match.ID = FALSE to match on record order
#> coordinates a
#> a (1, 1) 5
#> b (2, 2) 4
#> c (3, 3) 3
#> d (4, 4) 2
#> e (5, 5) 1
SpatialPointsDataFrame(pts, df, match.ID = TRUE) # don't warn
#> coordinates a
#> a (1, 1) 5
#> b (2, 2) 4
#> c (3, 3) 3
#> d (4, 4) 2
#> e (5, 5) 1
SpatialPointsDataFrame(pts, df, match.ID = FALSE) # don't warn
#> coordinates a
#> e (1, 1) 1
#> d (2, 2) 2
#> c (3, 3) 3
#> b (4, 4) 4
#> a (5, 5) 5
df$m = letters[5:1]
SpatialPointsDataFrame(pts, df, match.ID = "m") # don't warn
#> coordinates a m
#> a (1, 1) 5 a
#> b (2, 2) 4 b
#> c (3, 3) 3 c
#> d (4, 4) 2 d
#> e (5, 5) 1 e
dimnames(pts)[[1]] = letters[5:1]
SpatialPointsDataFrame(pts, df) # don't warn: ID matching doesn't reorder
#> coordinates a m
#> e (1, 1) 1 e
#> d (2, 2) 2 d
#> c (3, 3) 3 c
#> b (4, 4) 4 b
#> a (5, 5) 5 a