G Exercise 05

Last updated: 2020-09-28 17:50:20

G.1 Question 1

  • Recall that the get_neighbors and focal2 functions from Section 9.4, which we used to apply a focal filter. The focal2 function excludes the first and last rows and columns (Figure G.1).
Original mean filter applied on `dem.tif`Original mean filter applied on `dem.tif`

Figure G.1: Original mean filter applied on dem.tif

  • Modify the definitions of the get_neighbors and/or focal2 functions from Section 9.4, so that the first and last rows and columns are also processed, even though they have incomplete neighbornoods. This means that for focal cells in the first or last row/column, the function shoud be applied on the available values plus NA values for the “missing” neighbors. For example, for the pixel in row 8 column 1 of dem.tif, the \(3\times 3\) neighborhood consists of five non-NA values plus four NA values (Figure G.2).
Neighborhood around a corner pixel in a raster

Figure G.2: Neighborhood around a corner pixel in a raster

  • Apply the modified filter with the mean function and na.rm=TRUE on the dem.tif raster.
  • Plot the resulting raster, including rounded values, as shown in the right panel of (Figure G.3).
  • Hint: there are (at least) two ways to solve this exercise: (1) modify the get_neighbors function so that if the position is on matrix edges then the values are only extracted from intact cells, or (2) temporarily add a “buffer” of NA values on all sides of the matrix with the raster values, apply the filter on the expanded matrix, then discard the buffer.
Modified mean filter applied on `dem.tif`Modified mean filter applied on `dem.tif`

Figure G.3: Modified mean filter applied on dem.tif

(50 points)

G.2 Question 2

  • Read the MOD13A3_2000_2019.tif raster, with monthly NDVI values, into a stars object named r.
  • Reproject r to UTM with st_warp(r, crs = 32636).
  • Read the Shapefile named nafot.shp, which includes polygons of “Nafa” administrative regions in Israel, and reproject it to UTM as well.
  • Calculate the average NDVI for each of the polygons for each date (hint: use aggregate).
  • Plot the resulting time series of average NDVI for each of the “Nafot”, as shown in Figure G.4. (Hint: you can start from an empty plot using plot(..., col=NA, then add the NDVI times series using lines and a for loop; see Section 6.1.3 for an example)
  • The “Nafa” that has the highest and lowest NDVI (on any date) needs to be highlighted in blue and red, respectively. The other “Nafot” should be displayed in grey. Don’t specific use “Nafa” indices (such as 6) or names (such as "Golan"), instead calculate the required indices in your code.
NDVI time series in three "Nafa" polygons

Figure G.4: NDVI time series in three “Nafa” polygons

(50 points)