library(sp)
## Warning: package 'sp' was built under R version 4.0.2
library(sf)
## Warning: package 'sf' was built under R version 4.0.2
## Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1
#Make matrix of pts
pts <- matrix(runif(8), ncol=2)
pts
## [,1] [,2]
## [1,] 0.4980298 0.6095592
## [2,] 0.5268468 0.0988632
## [3,] 0.9318653 0.5188862
## [4,] 0.2070848 0.7614984
#Make Data Frame
attrib_df <- data.frame(eyes = c(1:4), ears = c(2:5))
attrib_df
## eyes ears
## 1 1 2
## 2 2 3
## 3 3 4
## 4 4 5
#Make spatial points
pts_spatpoints <- SpatialPoints(pts)
pts_spatpoints
## SpatialPoints:
## coords.x1 coords.x2
## [1,] 0.4980298 0.6095592
## [2,] 0.5268468 0.0988632
## [3,] 0.9318653 0.5188862
## [4,] 0.2070848 0.7614984
## Coordinate Reference System (CRS) arguments: NA
#Make spatial points data frame
pts_spatpointsdf <- SpatialPointsDataFrame(pts_spatpoints, attrib_df, match.ID = FALSE)
pts_spatpointsdf
## coordinates eyes ears
## 1 (0.4980298, 0.6095592) 1 2
## 2 (0.5268468, 0.0988632) 2 3
## 3 (0.9318653, 0.5188862) 3 4
## 4 (0.2070848, 0.7614984) 4 5
#To multipoints
pts_sfpack <- st_multipoint(pts)
pts_sfpack
## MULTIPOINT ((0.4980298 0.6095592), (0.5268468 0.0988632), (0.9318653 0.5188862), (0.2070848 0.7614984))
sf_point <- st_sfc(pts_sfpack)
sf_point
## Geometry set for 1 feature
## geometry type: MULTIPOINT
## dimension: XY
## bbox: xmin: 0.2070848 ymin: 0.0988632 xmax: 0.9318653 ymax: 0.7614984
## CRS: NA
## MULTIPOINT ((0.4980298 0.6095592), (0.5268468 0...
#Subset
subset <- st_sf(attrib_df, sf_point)
## Warning in data.frame(..., check.names = FALSE): row names were found from a
## short variable and have been discarded
subset
## Simple feature collection with 4 features and 2 fields
## geometry type: MULTIPOINT
## dimension: XY
## bbox: xmin: 0.2070848 ymin: 0.0988632 xmax: 0.9318653 ymax: 0.7614984
## CRS: NA
## eyes ears sf_point
## 1 1 2 MULTIPOINT ((0.4980298 0.60...
## 2 2 3 MULTIPOINT ((0.4980298 0.60...
## 3 3 4 MULTIPOINT ((0.4980298 0.60...
## 4 4 5 MULTIPOINT ((0.4980298 0.60...
#how do you determine bbox coordinates?
##the bounding box is defined by the highest and lowest x and y coordinates of the dataset, xmin, xmax, ymin, ymax