Skip to content

Commit e4f3134

Browse files
Changed from pull request
1 parent 10ef1ef commit e4f3134

File tree

1 file changed

+12
-16
lines changed

1 file changed

+12
-16
lines changed

vignettes/04-synthesis-data.Rmd

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ The second is the cumulative growing degree days for all of 2017, which were cal
1414
They are combined by their common column, the date.
1515

1616
```{r synth_setup}
17-
library(raster)
1817
library(dplyr)
1918
library(ggplot2)
2019
library(jsonlite)
@@ -130,6 +129,7 @@ ggplot(data.frame(inf_points = unique(all_cultivars$inf_point))) +
130129
## Get image data
131130

132131
In this examnple we will extract our plot data from a series of images taken in May of Season 6, measure its "greeness" annd plot that against the plant heights from above in this vignette.
132+
133133
The chosen statistic here is the normalised green-red difference index, NGRDI=(R-G)/(R+G) (Rasmussen et al., 2016), which uses the red and green bands from the image raster.
134134

135135
Below we retrieve all the available plots for a particular date, then find and convert the plot boundary JSON into tuples.
@@ -148,9 +148,9 @@ sites <- betydb_query(table = "sites",
148148
site.geom <- sites$geometry
149149
150150
# Convert the polygon to something we can clip with. CRS value represents WGS84 Lat/Long
151-
site.shape = st_as_sfc(site.geom,crs = 4326)
152-
site.poly = st_cast(site.shape, "POINT")
153-
site.clip = as(site.poly,"Spatial")
151+
site.shape <- st_as_sfc(site.geom,crs = 4326)
152+
site.poly <- st_cast(site.shape, "POINT")
153+
site.clip <- as(site.poly,"Spatial")
154154
```
155155

156156
These are the names of the full field RGB data for the month of May.
@@ -163,7 +163,6 @@ image_files <-
163163
c('fullfield_L1_ua-mac_2018-05-01_rgb_stereovis_ir_sensors_fullfield_sorghum6_shade_may2018_thumb.tif',
164164
'fullfield_L1_ua-mac_2018-05-02_rgb_stereovis_ir_sensors_fullfield_sorghum6_shade_may2018_thumb.tif',
165165
'fullfield_L1_ua-mac_2018-05-03_rgb_stereovis_ir_sensors_fullfield_sorghum6_shade_may2018_thumb.tif',
166-
'fullfield_L1_ua-mac_2018-05-04_rgb_stereovis_ir_sensors_fullfield_sorghum6_settingstest_may2018_thumb.tif',
167166
'fullfield_L1_ua-mac_2018-05-05_rgb_stereovis_ir_sensors_fullfield_sorghum6_shade_may2018_thumb.tif',
168167
'fullfield_L1_ua-mac_2018-05-06_rgb_stereovis_ir_sensors_fullfield_sorghum6_shade_may2018_thumb.tif',
169168
'fullfield_L1_ua-mac_2018-05-08_rgb_stereovis_ir_sensors_fullfield_sorghum6_shade_may2018_thumb.tif',
@@ -175,7 +174,6 @@ image_files <-
175174
'fullfield_L1_ua-mac_2018-05-15_rgb_stereovis_ir_sensors_fullfield_sorghum6_sun_may2018_thumb.tif',
176175
'fullfield_L1_ua-mac_2018-05-17_rgb_stereovis_ir_sensors_fullfield_sorghum6_shade_may2018_thumb.tif',
177176
'fullfield_L1_ua-mac_2018-05-18_rgb_stereovis_ir_sensors_fullfield_sorghum6_sun_may2018_thumb.tif',
178-
'fullfield_L1_ua-mac_2018-05-19_rgb_stereovis_ir_sensors_plots_sorghum6_sun_thumb.tif',
179177
'fullfield_L1_ua-mac_2018-05-20_rgb_stereovis_ir_sensors_plots_sorghum6_shade_thumb.tif',
180178
'fullfield_L1_ua-mac_2018-05-21_rgb_stereovis_ir_sensors_fullfield_sorghum6_shade_may2018_thumb.tif',
181179
'fullfield_L1_ua-mac_2018-05-22_rgb_stereovis_ir_sensors_plots_sorghum6_sun_thumb.tif',
@@ -192,6 +190,8 @@ We will loop through these images, extract our plot data, and calculate the "gre
192190
We are using the name of the file to extract the date for later.
193191

194192
```{r synth_get_greeness}
193+
library(raster)
194+
195195
# Extract the date from the file name
196196
getDate <- function(file_name){
197197
date <- str_match_all(file_name, '[0-9]{4}-[0-9]{2}-[0-9]{2}')[[1]][,1]
@@ -200,11 +200,12 @@ getDate <- function(file_name){
200200
201201
# Returns the greeness value of the plot in the specified file
202202
getGreeness <- function(file_name, clip_coords){
203-
band_image = raster(file_name, band = 1)
204-
red_crop = crop(band_image, clip_coords)
203+
204+
band_image <- raster(file_name, band = 1)
205+
red_crop <- crop(band_image, clip_coords)
205206
206-
band_image = raster(file_name, band = 2)
207-
green_crop = crop(band_image, clip_coords)
207+
band_image <- raster(file_name, band = 2)
208+
green_crop <- crop(band_image, clip_coords)
208209
209210
add_rasters <- green_crop + red_crop
210211
numerator <- cellStats(add_rasters, stat = "sum")
@@ -213,12 +214,7 @@ getGreeness <- function(file_name, clip_coords){
213214
denominator <- cellStats(subtract_rasters, stat = "sum")
214215
215216
greeness <- numerator / denominator
216-
217-
# If we can't get a greeness value we return zero
218-
if (is.nan(greeness)) {
219-
greeness = 0;
220-
}
221-
217+
222218
return(greeness)
223219
}
224220

0 commit comments

Comments
 (0)