Reading data

Vector data - use sf package and st_read() function

Raster data - use the raster package and raster() or brick() function

raster() function is for reading single band rasters brick() function is for reading multi-band rasters

Writing data

Vector data - use st_write(m_poly, “path/to/file.ext”)

Raster data - writeRaster(my_raster, “path/to/raster.ext”)

# load sf package
library(sf)
## Linking to GEOS 3.11.0, GDAL 3.5.1, PROJ 9.0.1; sf_use_s2() is TRUE
# read in the trees shapefile
trees <- st_read("data/trees/trees.shp")
## Reading layer `trees' from data source 
##   `/Users/duke/learning/RSpatial/spatialanalysis_sf_raster/data/trees/trees.shp' 
##   using driver `ESRI Shapefile'
## Simple feature collection with 65217 features and 7 fields
## Geometry type: POINT
## Dimension:     XY
## Bounding box:  xmin: -74.2546 ymin: 40.49894 xmax: -73.70078 ymax: 40.91165
## Geodetic CRS:  WGS 84
# read in neighborhoods
neighborhoods <- st_read("data/neighborhoods/neighborhoods.shp")
## Reading layer `neighborhoods' from data source 
##   `/Users/duke/learning/RSpatial/spatialanalysis_sf_raster/data/neighborhoods/neighborhoods.shp' 
##   using driver `ESRI Shapefile'
## Simple feature collection with 195 features and 5 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -74.25559 ymin: 40.49612 xmax: -73.70001 ymax: 40.91553
## Geodetic CRS:  WGS 84
# read in parks
parks <- st_read("data/parks/parks.shp")
## Reading layer `parks' from data source 
##   `/Users/duke/learning/RSpatial/spatialanalysis_sf_raster/data/parks/parks.shp' 
##   using driver `ESRI Shapefile'
## Simple feature collection with 2008 features and 14 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -74.25609 ymin: 40.49449 xmax: -73.70905 ymax: 40.91133
## Geodetic CRS:  WGS 84
# view the first few trees
head(trees)
## Simple feature collection with 6 features and 7 fields
## Geometry type: POINT
## Dimension:     XY
## Bounding box:  xmin: -74.13116 ymin: 40.62351 xmax: -73.80057 ymax: 40.77393
## Geodetic CRS:  WGS 84
##   tree_id  nta longitude latitude stump_diam      species      boroname
## 1  558423 QN76 -73.80057 40.67035          0  honeylocust        Queens
## 2  286191 MN32 -73.95422 40.77393          0 Callery pear     Manhattan
## 3  257044 QN70 -73.92309 40.76196          0  Chinese elm        Queens
## 4  603262 BK09 -73.99866 40.69312          0       cherry      Brooklyn
## 5   41769 SI22 -74.11773 40.63166          0       cherry Staten Island
## 6   24024 SI07 -74.13116 40.62351          0    red maple Staten Island
##                     geometry
## 1 POINT (-73.80057 40.67035)
## 2 POINT (-73.95422 40.77393)
## 3 POINT (-73.92309 40.76196)
## 4 POINT (-73.99866 40.69312)
## 5 POINT (-74.11773 40.63166)
## 6 POINT (-74.13116 40.62351)

Reading Raster data

# load library raster
library(raster)
## Loading required package: sp
# read in tree camopy single-band raster
canopy <- raster("data/canopy.tif")

# Read in manhattan Landsat image multi-band raster
manhattan <- brick("data/manhattan.tif")

# ge the class for the new objects
class(canopy)
## [1] "RasterLayer"
## attr(,"package")
## [1] "raster"
class(manhattan)
## [1] "RasterBrick"
## attr(,"package")
## [1] "raster"

sf objects and data frames

sf are just data frames with some special properties. this means that packages like `dplyr` can be used to manipulate sf objects

library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:raster':
## 
##     intersect, select, union
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
# use filter to limit honey locust trees
honeylocust <- trees %>% filter(species == "honeylocust")

# count the number of rows
nrow(honeylocust)
## [1] 6418
# limit to tree_id and boroname variables

honeylocust_lim <- honeylocust %>% select(tree_id, boroname)
head(honeylocust_lim)
## Simple feature collection with 6 features and 2 fields
## Geometry type: POINT
## Dimension:     XY
## Bounding box:  xmin: -73.97524 ymin: 40.67035 xmax: -73.80057 ymax: 40.83136
## Geodetic CRS:  WGS 84
##   tree_id boroname                   geometry
## 1  558423   Queens POINT (-73.80057 40.67035)
## 2  548625 Brooklyn POINT (-73.97524 40.68575)
## 3  549439 Brooklyn POINT (-73.94137 40.67557)
## 4  181757 Brooklyn POINT (-73.89377 40.67169)
## 5  379387   Queens  POINT (-73.8221 40.69365)
## 6  383562    Bronx POINT (-73.90041 40.83136)

Geometry as list-columns in sf

A list-column behaves to a certain extent like any other R column. the main difference is that instead of a standard value such as a single number, character or boolean value, each observation value in that column is a piece of an R list and that list can be as complex as needed. The list-column allows you to store farmore information in a single variable. so sf takes this advantage to store all geographic information for each feature in the list.