welcome
play

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


  1. DataCamp Spatial Analysis with sf and raster in R SPATIAL ANALYSIS WITH SF AND RASTER IN R Welcome! Zev Ross President, ZevRoss Spatial Analysis

  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

  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.

  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

  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

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

  7. DataCamp Spatial Analysis with sf and raster in R st_read() supports tons of vector formats Shapefiles GeoJSON GPS netCDF Many others ...

  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

  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

  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

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

  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

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

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

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

  16. DataCamp Spatial Analysis with sf and raster in R SPATIAL ANALYSIS WITH SF AND RASTER IN R Let's practice!

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

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

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

  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

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

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

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

  24. DataCamp Spatial Analysis with sf and raster in R Your first visual using plot() # Plots each attribute (tree_id, species) > plot(trees)

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

  26. DataCamp Spatial Analysis with sf and raster in R SPATIAL ANALYSIS WITH SF AND RASTER IN R Let's practice!

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

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

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

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

  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

  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

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

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend