Skip to content

Commit 0232fe6

Browse files
Merge pull request #113 from terraref/develop
Remove gdal install requirement
2 parents b220b48 + e4f3134 commit 0232fe6

File tree

1 file changed

+18
-71
lines changed

1 file changed

+18
-71
lines changed

vignettes/04-synthesis-data.Rmd

Lines changed: 18 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@ The objective of this vignette is to walk through how to combine our several typ
55
For the first analysis, we want to figure out how the number of sufficiently warm days affects the amount of canopy cover at our site. We do this by combining the canopy cover data with the meteorological data on growing degree days, then modeling and plotting their relationship. We are specifically interested in figuring out when the increase in canopy cover starts to slow down in response to warm temperature days.
66

77
The second analysis compares greenness from image data with canopy cover.
8-
The second analysis uses the gdal_translate tool from the [GDAL](https://www.gdal.org/) package.
9-
You can use one of the prepared downloads availble on the GDAL web site to install the needed software tool.
10-
Alternatively, it's possible to download and build the tools on your system.
11-
For example, on the Mac, the command is `brew install gdal`.
128

139
## Get and join data
1410

@@ -24,6 +20,8 @@ library(jsonlite)
2420
library(lubridate)
2521
library(traits)
2622
library(inflection)
23+
library(sf)
24+
library(stringr)
2725
options(betydb_url = "https://terraref.ncsa.illinois.edu/bety/",
2826
betydb_api_version = 'v1')
2927
```
@@ -131,59 +129,28 @@ ggplot(data.frame(inf_points = unique(all_cultivars$inf_point))) +
131129
## Get image data
132130

133131
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+
134133
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.
135134

136135
Below we retrieve all the available plots for a particular date, then find and convert the plot boundary JSON into tuples.
137136
We will use these tuples to extract the data for our plot.
138137

139138
```{r get_plot_boundary}
140-
library(traits)
141-
library(stringr)
142-
143-
# Function for breaking apart a corner into its Lat, Lon components
144-
getLatLon <- function(corner){
145-
p <- strsplit(corner, ' ')
146-
return (c(p[[1]][1], p[[1]][2]))
147-
}
148-
149-
# Gets the bounding box of the array of points
150-
getBounds <-function(bounds){
151-
minX <- NA
152-
minY <- NA
153-
maxX <- NA
154-
maxY <- NA
155-
for (c in unique(bounds)){
156-
p = getLatLon(c)
157-
if (is.na(minX) || (minX > p[2]))
158-
minX = p[2]
159-
if (is.na(minY) || (minY > p[1]))
160-
minY = p[1]
161-
if (is.na(maxX) || (maxX < p[2]))
162-
maxX = p[2]
163-
if (is.na(maxY) || (maxY < p[1]))
164-
maxY = p[1]
165-
}
166-
167-
return (c(minX, minY, maxX, maxY))
168-
}
169-
170139
# Setting up our options
171140
options(betydb_url = "https://terraref.ncsa.illinois.edu/bety/",
172141
betydb_api_version = 'v1')
173142
174-
# Makiong the query for our site
143+
# Making the query for our site
175144
sites <- betydb_query(table = "sites",
176145
sitename = "MAC Field Scanner Season 6 Range 19 Column 1")
177146
178147
# Assigning the geometry of the site (GeoJSON format)
179148
site.geom <- sites$geometry
180149
181-
# Stripping out the extra information to get to the points
182-
complete_str <- str_match_all(site.geom, '(\\(\\(\\((.*)\\)\\)\\))')[[1]][, 3]
183-
bounds <- strsplit(complete_str, ', ')[[1]]
184-
185-
# Getting the bounding box of the polygon
186-
bounding_box = getBounds(bounds)
150+
# 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")
187154
```
188155

189156
These are the names of the full field RGB data for the month of May.
@@ -196,7 +163,6 @@ image_files <-
196163
c('fullfield_L1_ua-mac_2018-05-01_rgb_stereovis_ir_sensors_fullfield_sorghum6_shade_may2018_thumb.tif',
197164
'fullfield_L1_ua-mac_2018-05-02_rgb_stereovis_ir_sensors_fullfield_sorghum6_shade_may2018_thumb.tif',
198165
'fullfield_L1_ua-mac_2018-05-03_rgb_stereovis_ir_sensors_fullfield_sorghum6_shade_may2018_thumb.tif',
199-
'fullfield_L1_ua-mac_2018-05-04_rgb_stereovis_ir_sensors_fullfield_sorghum6_settingstest_may2018_thumb.tif',
200166
'fullfield_L1_ua-mac_2018-05-05_rgb_stereovis_ir_sensors_fullfield_sorghum6_shade_may2018_thumb.tif',
201167
'fullfield_L1_ua-mac_2018-05-06_rgb_stereovis_ir_sensors_fullfield_sorghum6_shade_may2018_thumb.tif',
202168
'fullfield_L1_ua-mac_2018-05-08_rgb_stereovis_ir_sensors_fullfield_sorghum6_shade_may2018_thumb.tif',
@@ -208,7 +174,6 @@ image_files <-
208174
'fullfield_L1_ua-mac_2018-05-15_rgb_stereovis_ir_sensors_fullfield_sorghum6_sun_may2018_thumb.tif',
209175
'fullfield_L1_ua-mac_2018-05-17_rgb_stereovis_ir_sensors_fullfield_sorghum6_shade_may2018_thumb.tif',
210176
'fullfield_L1_ua-mac_2018-05-18_rgb_stereovis_ir_sensors_fullfield_sorghum6_sun_may2018_thumb.tif',
211-
'fullfield_L1_ua-mac_2018-05-19_rgb_stereovis_ir_sensors_plots_sorghum6_sun_thumb.tif',
212177
'fullfield_L1_ua-mac_2018-05-20_rgb_stereovis_ir_sensors_plots_sorghum6_shade_thumb.tif',
213178
'fullfield_L1_ua-mac_2018-05-21_rgb_stereovis_ir_sensors_fullfield_sorghum6_shade_may2018_thumb.tif',
214179
'fullfield_L1_ua-mac_2018-05-22_rgb_stereovis_ir_sensors_plots_sorghum6_sun_thumb.tif',
@@ -221,61 +186,43 @@ image_files <-
221186
image_files <- file.path("vignettes/", image_files)
222187
```
223188

224-
225189
We will loop through these images, extract our plot data, and calculate the "greeness" of each extract.
226190
We are using the name of the file to extract the date for later.
227191

228192
```{r synth_get_greeness}
229193
library(raster)
230-
library(stringr)
231194
232195
# Extract the date from the file name
233196
getDate <- function(file_name){
234197
date <- str_match_all(file_name, '[0-9]{4}-[0-9]{2}-[0-9]{2}')[[1]][,1]
235198
return(date)
236199
}
237200
238-
239-
# Get the clip coordinates into the correct order
240-
clip_coords <- paste(toString(bounding_box[4]),' ',toString(bounding_box[3]),
241-
' ',toString(bounding_box[2]),' ',toString(bounding_box[1]))
242-
243-
244-
# Returns the greeness value of the clipped image
201+
# Returns the greeness value of the plot in the specified file
245202
getGreeness <- function(file_name, clip_coords){
246-
out_file <- "extract.tif"
247-
248-
# Execute the GDAL command to extract the plot
249-
command = paste("gdal_translate -projwin ",clip_coords," ",file_name," ",out_file)
250-
system(command)
251-
252-
# Load the red & green bands of the image and calculate the greeness value
253-
red_image <- raster(out_file, band = 1)
254-
cellStats(red_image, stat = "mean")
203+
204+
band_image <- raster(file_name, band = 1)
205+
red_crop <- crop(band_image, clip_coords)
255206
256-
green_image <- raster(out_file, band = 2)
257-
cellStats(green_image, stat = "mean")
207+
band_image <- raster(file_name, band = 2)
208+
green_crop <- crop(band_image, clip_coords)
258209
259-
add_rasters <- green_image + red_image
210+
add_rasters <- green_crop + red_crop
260211
numerator <- cellStats(add_rasters, stat = "sum")
261212
262-
subtract_rasters <- green_image - red_image
213+
subtract_rasters <- green_crop - red_crop
263214
denominator <- cellStats(subtract_rasters, stat = "sum")
264215
265216
greeness <- numerator / denominator
266217
267-
# Remove the temporary file
268-
if (file.exists(out_file))
269-
file.remove(out_file)
270-
271218
return(greeness)
272219
}
273220
274221
# Extract all the dates from the images
275222
day <- sapply(image_files, getDate, USE.NAMES = FALSE)
276223
277224
# Extract all the greeness for the plot
278-
greeness <- sapply(image_files, getGreeness, clip_coords=clip_coords, USE.NAMES = FALSE)
225+
greeness <- sapply(image_files, getGreeness, clip_coords=site.clip, USE.NAMES = FALSE)
279226
280227
# Build the final day and greeness
281228
greenness_df <- data.frame(day,greeness)
@@ -292,7 +239,7 @@ trait_canopy_cover <- betydb_query(table = "search",
292239
trait = "canopy_cover",
293240
date = "~2018 May",
294241
limit = "none")
295-
242+
296243
trait_canopy_cover <- trait_canopy_cover %>%
297244
mutate(day = as.Date(raw_date))
298245
```

0 commit comments

Comments
 (0)