First we need to set up R to have all the functionality that we need
We will install the following packages in order to: - Read and write raster data (raster) - Read and write shapefiles (sf) - Visualize data (ggplot2)
In this tutorial we are going to lear how to use the remote sensing data that we created in Google Earth Engine to train a model to make spatial predictions.
Objectives: - Import and visualize raster - Import and visualize shp files - Resample rasters to same extent and resolution - Create a random spatial sample and fit a model - Make a prediction back to raster layers
In the following section we are going to use the raster function to import our images. We just need to tell the computer where to look. ../ backs us out of the current working directory (which is the Tutorials folder) and then looks in the Example_Data folder.
First we will import our population and nighttime lights DNB data. We will read it in, print out a description and then plot them.
pop = raster('../Example_Data/Belize_median_POP_2010.tif')
print(pop)
## class : RasterLayer
## dimensions : 637, 366, 233142 (nrow, ncol, ncell)
## resolution : 0.004491576, 0.004491576 (x, y)
## extent : -89.26559, -87.62167, 15.74298, 18.60411 (xmin, xmax, ymin, ymax)
## crs : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
## source : /home/mmann/Documents/Github/Belize_GEE_R_Tutorial/Example_Data/Belize_median_POP_2010.tif
## names : Belize_median_POP_2010
## values : 0.03336188, 36.27045 (min, max)
plot(pop)
dnb = raster('../Example_Data/Belize_median_DNB_2014_2018.tif')
print(dnb)
## class : RasterLayer
## dimensions : 637, 366, 233142 (nrow, ncol, ncell)
## resolution : 0.004491576, 0.004491576 (x, y)
## extent : -89.26559, -87.62167, 15.74298, 18.60411 (xmin, xmax, ymin, ymax)
## crs : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
## source : /home/mmann/Documents/Github/Belize_GEE_R_Tutorial/Example_Data/Belize_median_DNB_2014_2018.tif
## names : Belize_median_DNB_2014_2018
plot(dnb)
One important feature to note here is that the images have the same pixel resolution, same number of rows and columns and the same projection.
print(dim(dnb))
## [1] 637 366 1
print(dim(dnb) == dim(pop))
## [1] TRUE TRUE TRUE
print(res(pop))
## [1] 0.004491576 0.004491576
print(res(pop)==res(dnb))
## [1] TRUE TRUE
print(proj4string(dnb))
## [1] "+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"
print(proj4string(dnb) ==proj4string(pop))
## [1] TRUE
dnb_pop_stack = stack(dnb, pop)
print(dnb_pop_stack)
## class : RasterStack
## dimensions : 637, 366, 233142, 2 (nrow, ncol, ncell, nlayers)
## resolution : 0.004491576, 0.004491576 (x, y)
## extent : -89.26559, -87.62167, 15.74298, 18.60411 (xmin, xmax, ymin, ymax)
## crs : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
## names : Belize_median_DNB_2014_2018, Belize_median_POP_2010
## min values : ?, 0.03336188
## max values : ?, 36.27045
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
summary(cars)
## speed dist
## Min. : 4.0 Min. : 2.00
## 1st Qu.:12.0 1st Qu.: 26.00
## Median :15.0 Median : 36.00
## Mean :15.4 Mean : 42.98
## 3rd Qu.:19.0 3rd Qu.: 56.00
## Max. :25.0 Max. :120.00
You can also embed plots, for example:
Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.