0% found this document useful (0 votes)
30 views42 pages

Welcome!: Zev Ross

This document provides an overview of spatial analysis in R using the sf and raster packages. It discusses reading vector data with st_read(), including shapefiles and GeoJSON, and raster data with raster() and brick() for single-band and multi-band rasters. Vector data is stored as sf data frames with special properties like geometry stored as list columns. Rasters are RasterLayer or RasterBrick objects containing metadata about dimensions, extent and bands.

Uploaded by

Bishop Walter
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views42 pages

Welcome!: Zev Ross

This document provides an overview of spatial analysis in R using the sf and raster packages. It discusses reading vector data with st_read(), including shapefiles and GeoJSON, and raster data with raster() and brick() for single-band and multi-band rasters. Vector data is stored as sf data frames with special properties like geometry stored as list columns. Rasters are RasterLayer or RasterBrick objects containing metadata about dimensions, extent and bands.

Uploaded by

Bishop Walter
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 42

DataCamp Spatial Analysis with sf and raster in R

SPATIAL ANALYSIS WITH SF AND RASTER IN R

Welcome!

Zev Ross
President,
ZevRoss Spatial Analysis
DataCamp Spatial Analysis with sf and raster in R

Packages we will use in this course

Two key packages

sf for vectors
raster for grids

Additional packages discussed

ggplot2
tmap
dplyr
DataCamp Spatial Analysis with sf and raster in R

Analyze New York City data

You will compute tree density and greenspace by neighborhood in New York City.
DataCamp Spatial Analysis with sf and raster in R

Reading spatial data


Read in vector data with the sf package and st_read() function

Read in raster data using the raster package and either the raster() or

brick() functions
DataCamp Spatial Analysis with sf and raster in R

Reading vector data with st_read()


The st_read() function can be used to read a wide range of formats

You feed the function your file path


The function guesses the format of the file based on the input file suffix
DataCamp Spatial Analysis with sf and raster in R

An st_read() example
> library(sf)
> county <- st_read("folder1/county.shp")
DataCamp Spatial Analysis with sf and raster in R

st_read() supports tons of vector formats


Shapefiles
GeoJSON
GPS
netCDF
Many others ...
DataCamp Spatial Analysis with sf and raster in R

In this course you will be reading shapefiles


New York City tree data from the Street Tree Census
New York City neighborhoods
New York City parks
DataCamp Spatial Analysis with sf and raster in R

Read in raster data using raster() and brick()


Use raster() to read single-band rasters

Use brick() to read multi-band rasters


DataCamp Spatial Analysis with sf and raster in R

Single-band rasters
These have a single band/layer with a set of values, so one value per grid cell
Examples might include rasters of elevation or land use
DataCamp Spatial Analysis with sf and raster in R

Use raster() for single-band rasters


# elevation.tif is a single-band raster
> elevation <- raster("elevation.tif")
DataCamp Spatial Analysis with sf and raster in R

Multi-band rasters
Each grid cell has multiple values, one for each layer
Examples include satellite images or aerial photos
DataCamp Spatial Analysis with sf and raster in R

Use brick() for multi-band rasters


# aerial.tif is a multi-band raster
> aerial <- brick("aerial.tif")
DataCamp Spatial Analysis with sf and raster in R

Many raster formats supported


GeoTIFF
ESRI grids
ENVI grids
ERDAS grids
Others...
DataCamp Spatial Analysis with sf and raster in R

Write your data

Write vector data

> st_write(my_poly, "data/my_poly.shp")

Write raster data

> writeRaster(my_raster, "data/my_raster.tif")


DataCamp Spatial Analysis with sf and raster in R

SPATIAL ANALYSIS WITH SF AND RASTER IN R

Let's practice!
DataCamp Spatial Analysis with sf and raster in R

SPATIAL ANALYSIS WITH SF AND RASTER IN R

Getting to know your vector


data

Zev Ross
President,
ZevRoss Spatial Analysis
DataCamp Spatial Analysis with sf and raster in R

Vector spatial objects are data frames!


Spatial objects are stored as data frames
One row per feature
You can use standard tools like dplyr to slice and dice

These data frames have some special properties...


DataCamp Spatial Analysis with sf and raster in R

sf data frames have special properties


They include spatial metadata like the coordinate reference system
The geometry is stored in a list column

> trees <- st_read("trees.shp")


> head(trees, 3)
# Simple feature collection with 6 features and 2 fields
# geometry type: POINT
# dimension: XY
# bbox: xmin: -74.13116 ymin: 40.62351 xmax: -73.80057 ...
# epsg (SRID): 4326
# proj4string: +proj=longlat +datum=WGS84 +no_defs
# tree_id species geometry
# 1 558423 honeylocust POINT(-73.8005714931568 40....
# 2 286191 Callery pear POINT(-73.9542172518889 40....
# 3 257044 Chinese elm POINT(-73.9230947243388 40....
DataCamp Spatial Analysis with sf and raster in R

The magic of the list column

A non-spatial example

> d <- data.frame(a = 1:3, b = 1:3)

> new_list <- list(1:2, 1:5, 1:3)

> new_list
[[1]]
[1] 1 2

[[2]]
[1] 1 2 3 4 5

[[3]]
[1] 1 2 3
DataCamp Spatial Analysis with sf and raster in R

A list column is another variable in your data frame

Each list element can contain as much (or as little) detail as you need.

> d$c <- new_list

library(dplyr)
> d <- tbl_df(d)

> d
# A tibble: 3 x 3
a b c
<int> <int> <list>
1 1 1 <int [2]>
2 2 2 <int [5]>
3 3 3 <int [3]>
DataCamp Spatial Analysis with sf and raster in R

Take it one step further, geometry as list column


# Read in a vector file
> library(sf)
> trees <- st_read("trees.shp")

The geometry column is a list column.

> head(trees, 3)
# Simple feature collection with 6 features and 2 fields
# geometry type: POINT
# dimension: XY
# bbox: xmin: -74.13116 ymin: 40.62351 xmax: -73.80057 ...
# epsg (SRID): 4326
# proj4string: +proj=longlat +datum=WGS84 +no_defs
# tree_id species geometry
# 1 558423 honeylocust POINT(-73.8005714931568 40....
# 2 286191 Callery pear POINT(-73.9542172518889 40....
# 3 257044 Chinese elm POINT(-73.9230947243388 40....
DataCamp Spatial Analysis with sf and raster in R

Geometry is a simple features list column -- sfc

The species column is not a list

> is.list(trees$species)
[1] FALSE

The geometry column is a list

> is.list(trees$geometry)
[1] TRUE

Geometry is a special type of list, a simple features list column

> class(trees$geometry)
[1] "sfc_POINT" "sfc"
DataCamp Spatial Analysis with sf and raster in R

Your first visual using plot()


# Plots each attribute (tree_id, species)
> plot(trees)
DataCamp Spatial Analysis with sf and raster in R

Extract and plot only the geometry with st_geometry()


# Extract geometry
> geo <- st_geometry(trees)

# Plot only the geometry


> plot(st_geometry(trees))
DataCamp Spatial Analysis with sf and raster in R

SPATIAL ANALYSIS WITH SF AND RASTER IN R

Let's practice!
DataCamp Spatial Analysis with sf and raster in R

SPATIAL ANALYSIS WITH SF AND RASTER IN R

Getting to know your raster


data

Zev Ross
President,
ZevRoss Spatial Analysis
DataCamp Spatial Analysis with sf and raster in R

Rasters will be stored as a RasterLayer or RasterBrick

Single-band rasters read in with raster() will have a class of RasterLayer

> singleband <- raster("data/single.tif")


> class(singleband)
[1] "RasterLayer"
attr(,"package")
[1] "raster"

Multi-band rasters read in with brick() will have a class of RasterBrick

> multiband <- brick("data/multi.tif")


> class(multiband)
[1] "RasterBrick"
attr(,"package")
[1] "raster"
DataCamp Spatial Analysis with sf and raster in R

A quick look at the metadata of a RasterLayer object


> singleband
# class : RasterLayer
# dimensions : 230, 253, 58190 (nrow, ncol, ncell)
# resolution : 300, 300 (x, y)
# extent : 1793685, 1869585, 2141805, 2210805 (xmin, xmax, ymin, ymax)
# coord. ref. : +proj=aea +lat_1=29.5 +lat_2=45.5 +lat_0=23 +lon_0=-96 ...
# data source : /Users/johndoe/git-repos/courses-geographic-information ...
# names : canopy
# values : 0, 255 (min, max)

Note that under "names" there is just one name, meaning just one band/layer.
DataCamp Spatial Analysis with sf and raster in R

The metadata in a RasterBrick object is very similar


> multiband
# class : RasterBrick
# dimensions : 773, 801, 619173, 3 (nrow, ncol, ncell, nlayers)
# resolution : 29.98979, 30.00062 (x, y)
# extent : 575667.9, 599689.7, 4503277, 4526468 (xmin, xmax, ymin, ...
# coord. ref. : +proj=utm +zone=18 ...
# data source : /Users/johndoe/git-repos/courses-geographic-information ...
# names : manhattan.1, manhattan.2, manhattan.3
# min values : 0, 0, 0
# max values : 255, 255, 255

Notice that instead of one name there are three, meaning that this is a multi-band
raster.
DataCamp Spatial Analysis with sf and raster in R

Functions to extract pieces of the metadata (I)

extent() gives the minimum and maximum X and Y coordinates of the raster

> extent(multiband)
# class : Extent
# xmin : 575667.9
# xmax : 599689.7
# ymin : 4503277
# ymax : 4526468
DataCamp Spatial Analysis with sf and raster in R

Functions to extract pieces of the metadata (II)

ncell() and nlayers() give the total number of grid cells or layers, respectively

> ncell(multiband)
[1] 619173
> nlayers(multiband)
[1] 3
DataCamp Spatial Analysis with sf and raster in R

Functions to extract pieces of the metadata (III)

crs() gives the coordinate reference system

> crs(multiband)
# CRS arguments:
# +proj=utm +zone=18 +datum=WGS84 +units=m +no_defs +ellps=WGS84 ...
DataCamp Spatial Analysis with sf and raster in R

Note when importing rasters..


raster() and brick() do not read in raster values by default

Why? Rasters can be very big


To conserve memory raster values are imported only when required
DataCamp Spatial Analysis with sf and raster in R

Importing rasters - example

As an example, the multiband raster we're reading in is approximately 1.5


megabytes on disk but the object.size() function shows that in memory it's just
0.013 megabytes.

# File size on disk (nearly 2 million bytes)


> file.size("data/multi.tif")
[1] 1859955

# File size in memory (only 12,000 bytes)


> object.size(multiband)
12624 bytes
DataCamp Spatial Analysis with sf and raster in R

The inMemory() function tells you if the raster values have been
read into R

> inMemory(multiband)
[1] FALSE
DataCamp Spatial Analysis with sf and raster in R

You can read the values with the getValues() function


> vals <- getValues(multiband)
> head(vals)
manhattan.1 manhattan.2 manhattan.3
[1,] 92 105 79
[2,] 95 108 80
[3,] 99 112 84
[4,] 102 115 85
[5,] 102 116 83
[6,] 101 115 82
DataCamp Spatial Analysis with sf and raster in R

Creating quick maps of your rasters with plot() and plotRGB()


Use plot() for single-band rasters or to look at the individual layers in a multi-

band raster
Use plotRGB() to create a true color map of a multi-layered object
DataCamp Spatial Analysis with sf and raster in R

Using plot() with a single-band raster


> plot(singleband)
DataCamp Spatial Analysis with sf and raster in R

Using plot() with a multi-band raster


> plot(multiband)
DataCamp Spatial Analysis with sf and raster in R

Using plotRGB() to create a true color image


> plotRGB(multiband)
DataCamp Spatial Analysis with sf and raster in R

SPATIAL ANALYSIS WITH SF AND RASTER IN R

Let's practice!

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy