Compute tree density and average tree canopy by neighborhood Zev - - PowerPoint PPT Presentation

compute tree density and average tree canopy by
SMART_READER_LITE
LIVE PREVIEW

Compute tree density and average tree canopy by neighborhood Zev - - PowerPoint PPT Presentation

DataCamp Spatial Analysis with sf and raster in R SPATIAL ANALYSIS WITH SF AND RASTER IN R Compute tree density and average tree canopy by neighborhood Zev Ross President, ZevRoss Spatial Analysis DataCamp Spatial Analysis with sf and


slide-1
SLIDE 1

DataCamp Spatial Analysis with sf and raster in R

Compute tree density and average tree canopy by neighborhood

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

Tree census data and tree canopy data: are they correlated?

Tree census vector (points) Tree canopy raster

slide-3
SLIDE 3

DataCamp Spatial Analysis with sf and raster in R

Create two measures of greenery by neighborhood

Tree density by neighborhood Average tree canopy by neighborhood

slide-4
SLIDE 4

DataCamp Spatial Analysis with sf and raster in R

Compute tree density part 1: counts of trees by neighborhood

Count trees by hood: count() from dplyr Drop geometry: set_geometry(geo, NULL) Rename column: rename() from dplyr Use hist() to look at the data

slide-5
SLIDE 5

DataCamp Spatial Analysis with sf and raster in R

Compute tree density part 2: compute areas and join in counts

Use st_area() and mutate() to add areas to the neighborhoods data frame Join in the count data from the previous step with left_join() Finally compute density with counts/area

slide-6
SLIDE 6

DataCamp Spatial Analysis with sf and raster in R

Tree density final result

> head(neighborhoods) # Simple feature collection with 6 features and 8 fields # geometry type: MULTIPOLYGON # dimension: XY # bbox: xmin: -74.00736 ymin: 40.61264 xmax: -73.77574 ... # epsg (SRID): 4326 # proj4string: +proj=longlat +ellps=WGS84 +no_defs # county_fip boro_name hood ntaname boro_code area tree_cnt # 1 047 Brooklyn BK88 Borough Park 3 5017229 565 # 2 081 Queens QN52 East Flushing 4 2736433 295 # 3 081 Queens QN48 Auburndale 4 3173995 507 # 4 081 Queens QN51 Murray Hill 4 4876380 732 # 5 081 Queens QN27 East Elmhurst 4 1832715 211 # 6 005 Bronx BX35 Morrisania-Melrose 2 1569317 214 # tree_density geometry # 1 0.0001126120 MULTIPOLYGON(((-73.97604935... # 2 0.0001078046 MULTIPOLYGON(((-73.79493246... # 3 0.0001597356 MULTIPOLYGON(((-73.77573836... # 4 0.0001501114 MULTIPOLYGON(((-73.80379022... # 5 0.0001151297 MULTIPOLYGON(((-73.86109724... # 6 0.0001363650 MULTIPOLYGON(((-73.89696589...

slide-7
SLIDE 7

DataCamp Spatial Analysis with sf and raster in R

Steps to compute average tree canopy

Make sure that the raster and vector layers have the same CRS Compute mean tree canopy by neighborhood

slide-8
SLIDE 8

DataCamp Spatial Analysis with sf and raster in R

Tree canopy final result

> head(neighborhoods) # Simple feature collection with 6 features and 9 fields # geometry type: MULTIPOLYGON # dimension: XY # bbox: xmin: 1828213 ymin: 2168794 xmax: 1844284 ymax: 2194502 # epsg (SRID): NA # proj4string: +proj=aea +lat_1=29.5 +lat_2=45.5 +lat_0=23 ... # county_fip boro_name hood ntaname boro_code area total # 1 047 Brooklyn BK88 Borough Park 3 5017229 565 # 2 081 Queens QN52 East Flushing 4 2736433 295 # 3 081 Queens QN48 Auburndale 4 3173995 507 # 4 081 Queens QN51 Murray Hill 4 4876380 732 # 5 081 Queens QN27 East Elmhurst 4 1832715 211 # 6 005 Bronx BX35 Morrisania-Melrose 2 1569317 214 # tree_density avg_canopy geometry # 1 0.0001126120 0.7253846 MULTIPOLYGON(((1830843.4956... # 2 0.0001078046 8.4133333 MULTIPOLYGON(((1842344.8587... # 3 0.0001597356 4.1568750 MULTIPOLYGON(((1844283.8606... # 4 0.0001501114 8.5205000 MULTIPOLYGON(((1841161.6511... # 5 0.0001151297 2.5930000 MULTIPOLYGON(((1836803.3413... # 6 0.0001363650 3.1740000 MULTIPOLYGON(((1832056.9137...

slide-9
SLIDE 9

DataCamp Spatial Analysis with sf and raster in R

Let's practice!

SPATIAL ANALYSIS WITH SF AND RASTER IN R

slide-10
SLIDE 10

DataCamp Spatial Analysis with sf and raster in R

Exploratory data analysis with ggplot2

SPATIAL ANALYSIS WITH SF AND RASTER IN R

Zev Ross

President, ZevRoss Spatial Analysis

slide-11
SLIDE 11

DataCamp Spatial Analysis with sf and raster in R

Making nice(R) maps

ggplot2 for plots and maps tmap for polished maps

slide-12
SLIDE 12

DataCamp Spatial Analysis with sf and raster in R

Our example data, county area and population

# New York State > county <- st_read("county.shp") > head(county) # geoid total_pop area geometry # 1 36101 98665 3601.568 MULTIPOLYGON (((-77.423093 ... # 2 36091 223774 2097.880 MULTIPOLYGON (((-74.124335 ... # 3 36003 48070 2665.875 MULTIPOLYGON (((-78.309193 ... # 4 36075 121183 2464.746 MULTIPOLYGON (((-76.617586 ... # 5 36111 181300 2911.758 MULTIPOLYGON (((-74.67402 4... # 6 36089 112011 6939.259 MULTIPOLYGON (((-74.939447 ...

slide-13
SLIDE 13

DataCamp Spatial Analysis with sf and raster in R

Set up an empty plot with ggplot()

> library(ggplot2) > ggplot(data = county, aes(x = total_pop, y = area))

slide-14
SLIDE 14

DataCamp Spatial Analysis with sf and raster in R

Examples of the two basic layer types

Geometric layers points: geom_point() lines: geom_line() histogram: geom_histogram() map: geom_sf() Statistical layers smooths: stat_smooth() density: stat_density()

slide-15
SLIDE 15

DataCamp Spatial Analysis with sf and raster in R

Adding layers is easy with the + symbol

> ggplot(data = county, aes(x = total_pop, y = area)) + + geom_point()

slide-16
SLIDE 16

DataCamp Spatial Analysis with sf and raster in R

Adding multiple layers is also easy

> ggplot(data = county, aes(x = total_pop, y = area)) + + geom_point() + + stat_smooth()

slide-17
SLIDE 17

DataCamp Spatial Analysis with sf and raster in R

Create a map with geom_sf()

> ggplot(data = county) + + geom_sf()

slide-18
SLIDE 18

DataCamp Spatial Analysis with sf and raster in R

Use aes() to link the fill color to a variable

> ggplot(data = county, aes(fill = area)) + + geom_sf()

slide-19
SLIDE 19

DataCamp Spatial Analysis with sf and raster in R

Change fill colors with scale_fill_gradient()

> ggplot(data = county, aes(fill = area)) + + geom_sf() + + scale_fill_gradient(low = "wheat1", high = "red")

slide-20
SLIDE 20

DataCamp Spatial Analysis with sf and raster in R

Maps are better with a projected CRS

> county <- st_transform(county, crs = 32618) > ggplot(data = county, aes(fill = area)) + + geom_sf() + + scale_fill_gradient(low = "wheat1", high = "red")

slide-21
SLIDE 21

DataCamp Spatial Analysis with sf and raster in R

Use cor() from base R to compute correlation

> cor(county$area, county$total_pop) [1] -0.378985

slide-22
SLIDE 22

DataCamp Spatial Analysis with sf and raster in R

Let's practice!

SPATIAL ANALYSIS WITH SF AND RASTER IN R

slide-23
SLIDE 23

DataCamp Spatial Analysis with sf and raster in R

Create final, polished maps with tmap

SPATIAL ANALYSIS WITH SF AND RASTER IN R

Zev Ross

President, ZevRoss Spatial Analysis

slide-24
SLIDE 24

DataCamp Spatial Analysis with sf and raster in R

Initial plot setup function tm_shape()

# Running this with no layers will produce an error > tm_shape(county)

slide-25
SLIDE 25

DataCamp Spatial Analysis with sf and raster in R

tmap layers

Polygons: tm_polygons() Image raster: tm_rgb() Lines: tm_lines() Points: either tm_bubbles() or tm_dots() Many others ...

slide-26
SLIDE 26

DataCamp Spatial Analysis with sf and raster in R

Add layers with a + sign

> tm_shape(county) + + tm_polygons()

slide-27
SLIDE 27

DataCamp Spatial Analysis with sf and raster in R

Add multiple layers (same data)

> tm_shape(county) + + tm_polygons() + + tm_bubbles(col = "forestgreen")

slide-28
SLIDE 28

DataCamp Spatial Analysis with sf and raster in R

Add multiple layers (different data)

> tm_shape(elevation) + + tm_raster() + + tm_shape(county) + + tm_borders()

slide-29
SLIDE 29

DataCamp Spatial Analysis with sf and raster in R

Color based on a variable

> tm_shape(county) + + tm_polygons(col = "total_pop")

slide-30
SLIDE 30

DataCamp Spatial Analysis with sf and raster in R

Multiple maps in one with tmap_arrange()

> map1 <- tm_shape(county) + + tm_polygons(col = "total_pop") > map2 <- tm_shape(elevation) + + tm_raster() > tmap_arrange(map1, map2, nrow = 1)

slide-31
SLIDE 31

DataCamp Spatial Analysis with sf and raster in R

slide-32
SLIDE 32

DataCamp Spatial Analysis with sf and raster in R

Let's practice!

SPATIAL ANALYSIS WITH SF AND RASTER IN R

slide-33
SLIDE 33

DataCamp Spatial Analysis with sf and raster in R

GIS in R - You're ready!

SPATIAL ANALYSIS WITH SF AND RASTER IN R

Zev Ross

President, ZevRoss Spatial Analysis

slide-34
SLIDE 34

DataCamp Spatial Analysis with sf and raster in R

GIS with the sf and raster packages

sf is a dramatic improvement over sp for handling vectors raster is a smart and intuitive package that can perform spatial analysis with

rasters

slide-35
SLIDE 35

DataCamp Spatial Analysis with sf and raster in R

No problem to read spatial data into R

st_read("myvector.shp") raster("myraster.tif") or brick("myraster.tif")

slide-36
SLIDE 36

DataCamp Spatial Analysis with sf and raster in R

Navigating spatial objects in R

Vectors

sf objects are data frames

Geometry is in list-columns

st_geometry() returns just the geometry

Rasters Can be single or multi-band Informational functions include extent(), crs(), ncell(), nlayers() Values are left on disk but can be retrieved with getValues()

slide-37
SLIDE 37

DataCamp Spatial Analysis with sf and raster in R

Checking and changing the coordinate reference systems

st_crs() and crs() to get or set the CRS st_transform() and projectRaster() to change the CRS

slide-38
SLIDE 38

DataCamp Spatial Analysis with sf and raster in R

Preparing vector and raster layers

Prepare layers with dplyr Simplify with st_simplify() and aggregate()

slide-39
SLIDE 39

DataCamp Spatial Analysis with sf and raster in R

Spatial analysis with vectors

Buffer with st_buffer() Create centroids with st_centroid() Create a bounding box with st_makegrid() or st_convex_hull() Dissolve with st_union() Relationships with st_contains(), st_intersects() and others..

slide-40
SLIDE 40

DataCamp Spatial Analysis with sf and raster in R

Spatial analysis with rasters

Mask a raster with mask() Crop a raster with crop() Extract values from rasters with extract() Perform raster math with overlay()

slide-41
SLIDE 41

DataCamp Spatial Analysis with sf and raster in R

Plot quickly with plot() or plotRGB()

Use plot(polys) to quickly plot vector attributes Use plot(st_geometry(polys)) to plot geometry only Use plot(raster1) to plot a one-band raster Use plotRGB(raster2) to plot a multi-band raster

slide-42
SLIDE 42

DataCamp Spatial Analysis with sf and raster in R

Visualize your data with ggplot2 and tmap

> library(ggplot2) > ggplot(polys) + + geom_sf() > library(tmap) > tm_shape(polys) + + tm_polygons()

slide-43
SLIDE 43

DataCamp Spatial Analysis with sf and raster in R

Have fun!

SPATIAL ANALYSIS WITH SF AND RASTER IN R