Welcome! Zev Ross President, ZevRoss Spatial Analysis DataCamp - - PowerPoint PPT Presentation

welcome
SMART_READER_LITE
LIVE PREVIEW

Welcome! Zev Ross President, ZevRoss Spatial Analysis DataCamp - - PowerPoint PPT Presentation

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


slide-1
SLIDE 1

DataCamp Spatial Analysis with sf and raster in R

Welcome!

SPATIAL ANALYSIS WITH SF AND RASTER IN R

Zev Ross

President, ZevRoss Spatial Analysis

slide-2
SLIDE 2

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

slide-3
SLIDE 3

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.

slide-4
SLIDE 4

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

slide-5
SLIDE 5

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

slide-6
SLIDE 6

DataCamp Spatial Analysis with sf and raster in R

An st_read() example

> library(sf) > county <- st_read("folder1/county.shp")

slide-7
SLIDE 7

DataCamp Spatial Analysis with sf and raster in R

st_read() supports tons of vector formats

Shapefiles GeoJSON GPS netCDF Many others ...

slide-8
SLIDE 8

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

slide-9
SLIDE 9

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

slide-10
SLIDE 10

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

slide-11
SLIDE 11

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")

slide-12
SLIDE 12

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

slide-13
SLIDE 13

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")

slide-14
SLIDE 14

DataCamp Spatial Analysis with sf and raster in R

Many raster formats supported

GeoTIFF ESRI grids ENVI grids ERDAS grids Others...

slide-15
SLIDE 15

DataCamp Spatial Analysis with sf and raster in R

Write your data

Write vector data Write raster data

> st_write(my_poly, "data/my_poly.shp") > writeRaster(my_raster, "data/my_raster.tif")

slide-16
SLIDE 16

DataCamp Spatial Analysis with sf and raster in R

Let's practice!

SPATIAL ANALYSIS WITH SF AND RASTER IN R

slide-17
SLIDE 17

DataCamp Spatial Analysis with sf and raster in R

Getting to know your vector data

SPATIAL ANALYSIS WITH SF AND RASTER IN R

Zev Ross

President, ZevRoss Spatial Analysis

slide-18
SLIDE 18

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...

slide-19
SLIDE 19

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....

slide-20
SLIDE 20

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

slide-21
SLIDE 21

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]>

slide-22
SLIDE 22

DataCamp Spatial Analysis with sf and raster in R

Take it one step further, geometry as list column

The geometry column is a list column.

# Read in a vector file > library(sf) > 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....

slide-23
SLIDE 23

DataCamp Spatial Analysis with sf and raster in R

Geometry is a simple features list column -- sfc

The species column is not a list The geometry column is a list Geometry is a special type of list, a simple features list column

> is.list(trees$species) [1] FALSE > is.list(trees$geometry) [1] TRUE > class(trees$geometry) [1] "sfc_POINT" "sfc"

slide-24
SLIDE 24

DataCamp Spatial Analysis with sf and raster in R

Your first visual using plot()

# Plots each attribute (tree_id, species) > plot(trees)

slide-25
SLIDE 25

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))

slide-26
SLIDE 26

DataCamp Spatial Analysis with sf and raster in R

Let's practice!

SPATIAL ANALYSIS WITH SF AND RASTER IN R

slide-27
SLIDE 27

DataCamp Spatial Analysis with sf and raster in R

Getting to know your raster data

SPATIAL ANALYSIS WITH SF AND RASTER IN R

Zev Ross

President, ZevRoss Spatial Analysis

slide-28
SLIDE 28

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 Multi-band rasters read in with brick() will have a class of RasterBrick

> singleband <- raster("data/single.tif") > class(singleband) [1] "RasterLayer" attr(,"package") [1] "raster" > multiband <- brick("data/multi.tif") > class(multiband) [1] "RasterBrick" attr(,"package") [1] "raster"

slide-29
SLIDE 29

DataCamp Spatial Analysis with sf and raster in R

A quick look at the metadata of a RasterLayer object

Note that under "names" there is just one name, meaning just one band/layer.

> 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)

slide-30
SLIDE 30

DataCamp Spatial Analysis with sf and raster in R

The metadata in a RasterBrick object is very similar

Notice that instead of one name there are three, meaning that this is a multi-band raster.

> 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

slide-31
SLIDE 31

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

slide-32
SLIDE 32

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

slide-33
SLIDE 33

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 ...

slide-34
SLIDE 34

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

slide-35
SLIDE 35

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

slide-36
SLIDE 36

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

slide-37
SLIDE 37

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

slide-38
SLIDE 38

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

slide-39
SLIDE 39

DataCamp Spatial Analysis with sf and raster in R

Using plot() with a single-band raster

> plot(singleband)

slide-40
SLIDE 40

DataCamp Spatial Analysis with sf and raster in R

Using plot() with a multi-band raster

> plot(multiband)

slide-41
SLIDE 41

DataCamp Spatial Analysis with sf and raster in R

Using plotRGB() to create a true color image

> plotRGB(multiband)

slide-42
SLIDE 42

DataCamp Spatial Analysis with sf and raster in R

Let's practice!

SPATIAL ANALYSIS WITH SF AND RASTER IN R