@@ -92,13 +92,13 @@ describe("data/NEON-DS-Airborne-Remote-Sensing/HARV/DSM/HARV_dsmCrop.tif")
9292If you wish to store this information in R, you can do the following:
9393
9494``` {r}
95- HARV_dsmCrop_info <- capture.output(
95+ harv_metadata <- capture.output(
9696 describe("data/NEON-DS-Airborne-Remote-Sensing/HARV/DSM/HARV_dsmCrop.tif")
9797)
9898```
9999
100100Each line of text that was printed to the console is now stored as an element of
101- the character vector ` HARV_dsmCrop_info ` . We will be exploring this data throughout this
101+ the character vector ` harv_metadata ` . We will be exploring this data throughout this
102102episode. By the end of this episode, you will be able to explain and understand the output above.
103103
104104## Open a Raster in R
@@ -122,18 +122,18 @@ we'll use a naming convention of `datatype_HARV`.
122122First we will load our raster file into R and view the data structure.
123123
124124``` {r}
125- DSM_HARV <-
125+ dsm_harv <-
126126 rast("data/NEON-DS-Airborne-Remote-Sensing/HARV/DSM/HARV_dsmCrop.tif")
127127
128- DSM_HARV
128+ dsm_harv
129129```
130130
131131The information above includes a report of min and max values, but no other data
132132range statistics. Similar to other R data structures like vectors and data frame
133133columns, descriptive statistics for raster data can be retrieved like
134134
135135``` {r}
136- summary(DSM_HARV )
136+ summary(dsm_harv )
137137```
138138
139139but note the warning - unless you force R to calculate these statistics using
@@ -142,7 +142,7 @@ calculate from that instead. To force calculation all the values, you can use
142142the function ` values ` :
143143
144144``` {r}
145- summary(values(DSM_HARV ))
145+ summary(values(dsm_harv ))
146146```
147147
148148To visualise this data in R using ` ggplot2 ` , we need to convert it to a
@@ -151,14 +151,14 @@ lesson](https://datacarpentry.org/r-intro-geospatial/04-data-structures-part2).
151151The ` terra ` package has an built-in function for conversion to a plotable dataframe.
152152
153153``` {r}
154- DSM_HARV_df <- as.data.frame(DSM_HARV , xy = TRUE)
154+ dsm_harv_df <- as.data.frame(dsm_harv , xy = TRUE)
155155```
156156
157157Now when we view the structure of our data, we will see a standard
158158dataframe format.
159159
160160``` {r}
161- str(DSM_HARV_df )
161+ str(dsm_harv_df )
162162```
163163
164164We can use ` ggplot() ` to plot this data. We will set the color scale to
@@ -171,7 +171,7 @@ ggplot2 if needed, you can learn about them at their help page `?coord_map`.
171171``` {r ggplot-raster, fig.cap="Raster plot with ggplot2 using the viridis color scale"}
172172
173173ggplot() +
174- geom_raster(data = DSM_HARV_df , aes(x = x, y = y, fill = HARV_dsmCrop)) +
174+ geom_raster(data = dsm_harv_df , aes(x = x, y = y, fill = HARV_dsmCrop)) +
175175 scale_fill_viridis_c() +
176176 coord_quickmap()
177177```
@@ -200,7 +200,7 @@ For faster, simpler plots, you can use the `plot` function from the `terra` pack
200200See ` ?plot ` for more arguments to customize the plot
201201
202202``` {r, eval=TRUE}
203- plot(DSM_HARV )
203+ plot(dsm_harv )
204204```
205205
206206:::::::::::::::::
@@ -231,7 +231,7 @@ We can view the CRS string associated with our R object using the`crs()`
231231function.
232232
233233``` {r view-resolution-units}
234- crs(DSM_HARV , proj = TRUE)
234+ crs(dsm_harv , proj = TRUE)
235235```
236236
237237::::::::::::::::::::::::::::::::::::::: challenge
@@ -261,7 +261,7 @@ and datum (`datum=`).
261261
262262### UTM Proj4 String
263263
264- A projection string (like the one of ` DSM_HARV ` ) specifies the UTM projection
264+ A projection string (like the one of ` dsm_harv ` ) specifies the UTM projection
265265as follows:
266266
267267` +proj=utm +zone=18 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0 `
@@ -290,11 +290,11 @@ Raster statistics are often calculated and embedded in a GeoTIFF for us. We
290290can view these values:
291291
292292``` {r view-min-max}
293- minmax(DSM_HARV )
293+ minmax(dsm_harv )
294294
295- min(values(DSM_HARV ))
295+ min(values(dsm_harv ))
296296
297- max(values(DSM_HARV ))
297+ max(values(dsm_harv ))
298298```
299299
300300::::::::::::::::::::::::::::::::::::::::: callout
@@ -306,17 +306,17 @@ calculated, we can calculate them using the
306306` setMinMax() ` function.
307307
308308``` {r, eval=FALSE}
309- DSM_HARV <- setMinMax(DSM_HARV )
309+ dsm_harv <- setMinMax(dsm_harv )
310310```
311311
312312::::::::::::::::::::::::::::::::::::::::::::::::::
313313
314- We can see that the elevation at our site ranges from ` r min(terra::values(DSM_HARV )) ` m to
315- ` r max(terra::values(DSM_HARV )) ` m.
314+ We can see that the elevation at our site ranges from ` r min(terra::values(dsm_harv )) ` m to
315+ ` r max(terra::values(dsm_harv )) ` m.
316316
317317## Raster Bands
318318
319- The Digital Surface Model object (` DSM_HARV ` ) that we've been working with is a
319+ The Digital Surface Model object (` dsm_harv ` ) that we've been working with is a
320320single band raster. This means that there is only one dataset stored in the
321321raster: surface elevation in meters for one time period.
322322
@@ -327,7 +327,7 @@ function to import one single band from a single or multi-band raster. We can
327327view the number of bands in a raster using the ` nlyr() ` function.
328328
329329``` {r view-raster-bands}
330- nlyr(DSM_HARV )
330+ nlyr(dsm_harv )
331331```
332332
333333However, raster data can also be multi-band, meaning that one raster file contains data for more than one variable or time period for each cell.
@@ -347,15 +347,15 @@ airplane which only flew over some part of a defined region.
347347In the image below, the pixels that are black have ` NoDataValue ` s. The camera
348348did not collect data in these areas.
349349
350- ``` {r demonstrate-no-data-black-ggplot, echo=FALSE}
350+ ``` {r demonstrate-no-data-black-ggplot, echo=FALSE, message=FALSE, warning=FALSE }
351351# no data demonstration code - not being taught
352352# Use stack function to read in all bands
353- RGB_stack <-
353+ rgb_stack <-
354354 rast("data/NEON-DS-Airborne-Remote-Sensing/HARV/RGB_Imagery/HARV_RGB_Ortho.tif")
355355
356356# aggregate cells from 0.25m to 2m for plotting to speed up the lesson and
357357# save memory
358- RGB_2m <- raster::aggregate(RGB_stack , fact = 8, fun = median)
358+ RGB_2m <- raster::aggregate(rgb_stack , fact = 8, fun = median)
359359# fix data values back to integer datatype
360360values(RGB_2m) <- as.integer(round(values(RGB_2m)))
361361
@@ -424,7 +424,7 @@ ggplot() +
424424 coord_quickmap()
425425
426426# memory saving
427- rm(RGB_2m, RGB_stack , RGB_2m_df_nd, RGB_2m_df, RGB_2m_nas)
427+ rm(RGB_2m, rgb_stack , RGB_2m_df_nd, RGB_2m_df, RGB_2m_nas)
428428```
429429
430430The value that is conventionally used to take note of missing data (the
@@ -451,14 +451,14 @@ of `NA` will be ignored by R as demonstrated above.
451451## Challenge
452452
453453Use the output from the ` describe() ` and ` sources() ` functions to find out what
454- ` NoDataValue ` is used for our ` DSM_HARV ` dataset.
454+ ` NoDataValue ` is used for our ` dsm_harv ` dataset.
455455
456456::::::::::::::: solution
457457
458458## Answers
459459
460460``` {r}
461- describe(sources(DSM_HARV ))
461+ describe(sources(dsm_harv ))
462462```
463463
464464` NoDataValue ` are encoded as -9999.
@@ -495,25 +495,25 @@ elevation values over 400m with a contrasting colour.
495495
496496``` {r demo-bad-data-highlighting, echo=FALSE, message=FALSE, warning=FALSE}
497497# reclassify raster to ok/not ok
498- DSM_highvals <- classify(DSM_HARV ,
498+ dsm_high <- classify(dsm_harv ,
499499 rcl = matrix(c(0, 400, NA_integer_, 400, 420, 1L),
500500 ncol = 3, byrow = TRUE),
501501 include.lowest = TRUE)
502- DSM_highvals <- as.data.frame(DSM_highvals , xy = TRUE)
502+ dsm_high <- as.data.frame(dsm_high , xy = TRUE)
503503
504- DSM_highvals <- DSM_highvals [!is.na(DSM_highvals $HARV_dsmCrop), ]
504+ dsm_high <- dsm_high [!is.na(dsm_high $HARV_dsmCrop), ]
505505
506506ggplot() +
507- geom_raster(data = DSM_HARV_df , aes(x = x, y = y, fill = HARV_dsmCrop)) +
507+ geom_raster(data = dsm_harv_df , aes(x = x, y = y, fill = HARV_dsmCrop)) +
508508 scale_fill_viridis_c() +
509509 # use reclassified raster data as an annotation
510- annotate(geom = 'raster', x = DSM_highvals $x, y = DSM_highvals $y,
511- fill = scales::colour_ramp('deeppink')(DSM_highvals $HARV_dsmCrop)) +
510+ annotate(geom = 'raster', x = dsm_high $x, y = dsm_high $y,
511+ fill = scales::colour_ramp('deeppink')(dsm_high $HARV_dsmCrop)) +
512512 ggtitle("Elevation Data", subtitle = "Highlighting values > 400m") +
513513 coord_quickmap()
514514
515515# memory saving
516- rm(DSM_highvals )
516+ rm(dsm_high )
517517```
518518
519519## Create A Histogram of Raster Values
@@ -525,7 +525,7 @@ useful in identifying outliers and bad data values in our raster data.
525525``` {r view-raster-histogram}
526526
527527ggplot() +
528- geom_histogram(data = DSM_HARV_df , aes(HARV_dsmCrop))
528+ geom_histogram(data = dsm_harv_df , aes(HARV_dsmCrop))
529529
530530```
531531
@@ -539,7 +539,7 @@ by using the `bins` value in the `geom_histogram()` function.
539539
540540``` {r view-raster-histogram2}
541541ggplot() +
542- geom_histogram(data = DSM_HARV_df , aes(HARV_dsmCrop), bins = 40)
542+ geom_histogram(data = dsm_harv_df , aes(HARV_dsmCrop), bins = 40)
543543
544544```
545545
@@ -554,7 +554,7 @@ no bad data values in this particular raster.
554554
555555Use ` describe() ` to determine the following about the ` NEON-DS-Airborne-Remote-Sensing/HARV/DSM/HARV_DSMhill.tif ` file:
556556
557- 1 . Does this file have the same CRS as ` DSM_HARV ` ?
557+ 1 . Does this file have the same CRS as ` dsm_harv ` ?
5585582 . What is the ` NoDataValue ` ?
5595593 . What is resolution of the raster data?
5605604 . How large would a 5x5 pixel area be on the Earth's surface?
@@ -571,7 +571,7 @@ describe("data/NEON-DS-Airborne-Remote-Sensing/HARV/DSM/HARV_DSMhill.tif")
571571```
572572
573573
574- 1 . If this file has the same CRS as DSM_HARV ? Yes: UTM Zone 18, WGS84, meters.
574+ 1 . If this file has the same CRS as dsm_harv ? Yes: UTM Zone 18, WGS84, meters.
5755752 . What format ` NoDataValues ` take? -9999
5765763 . The resolution of the raster data? 1x1
5775774 . How large a 5x5 pixel area would be? 5mx5m How? We are given resolution of 1x1 and units in meters, therefore resolution of 5x5 means 5x5m.
0 commit comments