DataCamp Spatial Analysis with sf and raster in R
Welcome!
SPATIAL ANALYSIS WITH SF AND RASTER IN R
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
DataCamp Spatial Analysis with sf and raster in R
SPATIAL ANALYSIS WITH SF AND RASTER IN R
DataCamp Spatial Analysis with sf and raster in R
DataCamp Spatial Analysis with sf and raster in R
DataCamp Spatial Analysis with sf and raster in R
brick() functions
DataCamp Spatial Analysis with sf and raster in R
DataCamp Spatial Analysis with sf and raster in R
> library(sf) > county <- st_read("folder1/county.shp")
DataCamp Spatial Analysis with sf and raster in R
DataCamp Spatial Analysis with sf and raster in R
DataCamp Spatial Analysis with sf and raster in R
DataCamp Spatial Analysis with sf and raster in R
DataCamp Spatial Analysis with sf and raster in R
# elevation.tif is a single-band raster > elevation <- raster("elevation.tif")
DataCamp Spatial Analysis with sf and raster in R
DataCamp Spatial Analysis with sf and raster in R
# aerial.tif is a multi-band raster > aerial <- brick("aerial.tif")
DataCamp Spatial Analysis with sf and raster in R
DataCamp Spatial Analysis with sf and raster in R
> st_write(my_poly, "data/my_poly.shp") > writeRaster(my_raster, "data/my_raster.tif")
DataCamp Spatial Analysis with sf and raster in R
SPATIAL ANALYSIS WITH SF AND RASTER IN R
DataCamp Spatial Analysis with sf and raster in R
SPATIAL ANALYSIS WITH SF AND RASTER IN R
DataCamp Spatial Analysis with sf and raster in R
DataCamp Spatial Analysis with sf and raster in R
> 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
> 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
> 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
# 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....
DataCamp Spatial Analysis with sf and raster in R
> is.list(trees$species) [1] FALSE > is.list(trees$geometry) [1] TRUE > class(trees$geometry) [1] "sfc_POINT" "sfc"
DataCamp Spatial Analysis with sf and raster in R
# Plots each attribute (tree_id, species) > plot(trees)
DataCamp Spatial Analysis with sf and raster in R
# 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
DataCamp Spatial Analysis with sf and raster in R
SPATIAL ANALYSIS WITH SF AND RASTER IN R
DataCamp Spatial Analysis with sf and raster in R
> 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"
DataCamp Spatial Analysis with sf and raster in R
> 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)
DataCamp Spatial Analysis with sf and raster in R
> 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
DataCamp Spatial Analysis with sf and raster in R
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
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
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
raster() and brick() do not read in raster values by default
DataCamp Spatial Analysis with sf and raster in R
# 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
> inMemory(multiband) [1] FALSE
DataCamp Spatial Analysis with sf and raster in R
> 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
DataCamp Spatial Analysis with sf and raster in R
> plot(singleband)
DataCamp Spatial Analysis with sf and raster in R
> plot(multiband)
DataCamp Spatial Analysis with sf and raster in R
> plotRGB(multiband)
DataCamp Spatial Analysis with sf and raster in R
SPATIAL ANALYSIS WITH SF AND RASTER IN R