Similarly to the example, generate a Point object in R. Use both, the sp and the sf “approach”.
Create a matrix pts of random numbers with two columns and as many rows as you like. These are your points.
Create a dataframe attrib_df with the same number of rows as your pts matrix and a column that holds an attribute. You can make up any attribute.
Use the appropriate commands and pts to create
a SpatialPointsDataFrame and
an sf object with a geometry column of class sfc_POINT.
Try to subset your spatial object using the attribute you have added and the way you are used to from regular data frames.
How do you determine the bounding box of your spatial object?
Loaded essential packages for this exercise.
library(sp)
library(sf)
## Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1
pts <- matrix(runif(20), ncol=2)
str(pts)
## num [1:10, 1:2] 0.791 0.84 0.566 0.718 0.452 ...
# dimnames(pts)[[1]] <- letters[1:10]
sp_pts <- SpatialPoints(pts)
str(sp_pts)
## Formal class 'SpatialPoints' [package "sp"] with 3 slots
## ..@ coords : num [1:10, 1:2] 0.791 0.84 0.566 0.718 0.452 ...
## .. ..- attr(*, "dimnames")=List of 2
## .. .. ..$ : NULL
## .. .. ..$ : chr [1:2] "coords.x1" "coords.x2"
## ..@ bbox : num [1:2, 1:2] 0.1653 0.0214 0.84 0.7651
## .. ..- attr(*, "dimnames")=List of 2
## .. .. ..$ : chr [1:2] "coords.x1" "coords.x2"
## .. .. ..$ : chr [1:2] "min" "max"
## ..@ proj4string:Formal class 'CRS' [package "sp"] with 1 slot
## .. .. ..@ projargs: chr NA
attrib_df <- data.frame(a = 1:10)
sp_pts_df<- SpatialPointsDataFrame(sp_pts, attrib_df, match.ID = FALSE)
str(sp_pts_df)
## Formal class 'SpatialPointsDataFrame' [package "sp"] with 5 slots
## ..@ data :'data.frame': 10 obs. of 1 variable:
## .. ..$ a: int [1:10] 1 2 3 4 5 6 7 8 9 10
## ..@ coords.nrs : num(0)
## ..@ coords : num [1:10, 1:2] 0.791 0.84 0.566 0.718 0.452 ...
## .. ..- attr(*, "dimnames")=List of 2
## .. .. ..$ : NULL
## .. .. ..$ : chr [1:2] "coords.x1" "coords.x2"
## ..@ bbox : num [1:2, 1:2] 0.1653 0.0214 0.84 0.7651
## .. ..- attr(*, "dimnames")=List of 2
## .. .. ..$ : chr [1:2] "coords.x1" "coords.x2"
## .. .. ..$ : chr [1:2] "min" "max"
## ..@ proj4string:Formal class 'CRS' [package "sp"] with 1 slot
## .. .. ..@ projargs: chr NA
pts_sfg <- st_multipoint(pts)
class(pts_sfg)
## [1] "XY" "MULTIPOINT" "sfg"
(sfc_POINT <- st_sfc(pts_sfg))
## Geometry set for 1 feature
## geometry type: MULTIPOINT
## dimension: XY
## bbox: xmin: 0.1653491 ymin: 0.0214201 xmax: 0.8400387 ymax: 0.7650506
## CRS: NA
## MULTIPOINT ((0.7905091 0.6822468), (0.8400387 0...
(sf_POINT_sf <- st_sf(attrib_df, sfc_POINT))
## Warning in data.frame(..., check.names = FALSE): row names were found from a
## short variable and have been discarded
## Simple feature collection with 10 features and 1 field
## geometry type: MULTIPOINT
## dimension: XY
## bbox: xmin: 0.1653491 ymin: 0.0214201 xmax: 0.8400387 ymax: 0.7650506
## CRS: NA
## a sfc_POINT
## 1 1 MULTIPOINT ((0.7905091 0.68...
## 2 2 MULTIPOINT ((0.7905091 0.68...
## 3 3 MULTIPOINT ((0.7905091 0.68...
## 4 4 MULTIPOINT ((0.7905091 0.68...
## 5 5 MULTIPOINT ((0.7905091 0.68...
## 6 6 MULTIPOINT ((0.7905091 0.68...
## 7 7 MULTIPOINT ((0.7905091 0.68...
## 8 8 MULTIPOINT ((0.7905091 0.68...
## 9 9 MULTIPOINT ((0.7905091 0.68...
## 10 10 MULTIPOINT ((0.7905091 0.68...
The subset contains the spatial objects of attribute values between 2 and 8 (exclusive).
subset(sf_POINT_sf, a>2 & a<8)
## Simple feature collection with 5 features and 1 field
## geometry type: MULTIPOINT
## dimension: XY
## bbox: xmin: 0.1653491 ymin: 0.0214201 xmax: 0.8400387 ymax: 0.7650506
## CRS: NA
## a sfc_POINT
## 3 3 MULTIPOINT ((0.7905091 0.68...
## 4 4 MULTIPOINT ((0.7905091 0.68...
## 5 5 MULTIPOINT ((0.7905091 0.68...
## 6 6 MULTIPOINT ((0.7905091 0.68...
## 7 7 MULTIPOINT ((0.7905091 0.68...
For sp package, the bounding box can be determined by using str() function. The output will show the bounding box as @ bbox class.
For For sf package, the bounding box can be determined by using class() function. The output will show the bounding box as “bbox: xmin: ymin: xmax: ymax:”.