Skip to content

Commit 5c432d0

Browse files
Removed gdal_transform command line tool requirement
1 parent 574f92c commit 5c432d0

1 file changed

Lines changed: 24 additions & 77 deletions

File tree

vignettes/04-synthesis-data.Rmd

Lines changed: 24 additions & 77 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

@@ -18,12 +14,15 @@ The second is the cumulative growing degree days for all of 2017, which were cal
1814
They are combined by their common column, the date.
1915

2016
```{r synth_setup}
17+
library(raster)
2118
library(dplyr)
2219
library(ggplot2)
2320
library(jsonlite)
2421
library(lubridate)
2522
library(traits)
2623
library(inflection)
24+
library(sf)
25+
library(stringr)
2726
options(betydb_url = "https://terraref.ncsa.illinois.edu/bety/",
2827
betydb_api_version = 'v1')
2928
```
@@ -137,53 +136,22 @@ Below we retrieve all the available plots for a particular date, then find and c
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/",
172-
betydb_api_version = 'v1')
141+
betydb_api_version = 'v1',
142+
betydb_key = readLines('.betykey'))
173143
174-
# Makiong the query for our site
144+
# Making the query for our site
175145
sites <- betydb_query(table = "sites",
176146
sitename = "MAC Field Scanner Season 6 Range 19 Column 1")
177147
178148
# Assigning the geometry of the site (GeoJSON format)
179149
site.geom <- sites$geometry
180150
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)
151+
# Convert the polygon to something we can clip with. CRS value represents WGS84 Lat/Long
152+
site.shape = st_as_sfc(site.geom,crs = 4326)
153+
site.poly = st_cast(site.shape, "POINT")
154+
site.clip = as(site.poly,"Spatial")
187155
```
188156

189157
These are the names of the full field RGB data for the month of May.
@@ -217,65 +185,45 @@ image_files <-
217185
)
218186
```
219187

220-
```{r, echo = F}
221-
image_files <- file.path("vignettes/", image_files)
222-
```
223-
224-
225188
We will loop through these images, extract our plot data, and calculate the "greeness" of each extract.
226189
We are using the name of the file to extract the date for later.
227190

228191
```{r synth_get_greeness}
229-
library(raster)
230-
library(stringr)
231-
232192
# Extract the date from the file name
233193
getDate <- function(file_name){
234194
date <- str_match_all(file_name, '[0-9]{4}-[0-9]{2}-[0-9]{2}')[[1]][,1]
235195
return(date)
236196
}
237197
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
198+
# Returns the greeness value of the plot in the specified file
245199
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)
200+
band_image = raster(file_name, band = 1)
201+
red_crop = crop(band_image, clip_coords)
251202
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+
band_image = raster(file_name, band = 2)
204+
green_crop = crop(band_image, clip_coords)
255205
256-
green_image <- raster(out_file, band = 2)
257-
cellStats(green_image, stat = "mean")
258-
259-
add_rasters <- green_image + red_image
206+
add_rasters <- green_crop + red_crop
260207
numerator <- cellStats(add_rasters, stat = "sum")
261208
262-
subtract_rasters <- green_image - red_image
209+
subtract_rasters <- green_crop - red_crop
263210
denominator <- cellStats(subtract_rasters, stat = "sum")
264211
265212
greeness <- numerator / denominator
266-
267-
# Remove the temporary file
268-
if (file.exists(out_file))
269-
file.remove(out_file)
270-
213+
214+
# If we can't get a greeness value we return zero
215+
if (is.nan(greeness)) {
216+
greeness = 0;
217+
}
218+
271219
return(greeness)
272220
}
273221
274222
# Extract all the dates from the images
275223
day <- sapply(image_files, getDate, USE.NAMES = FALSE)
276224
277225
# Extract all the greeness for the plot
278-
greeness <- sapply(image_files, getGreeness, clip_coords=clip_coords, USE.NAMES = FALSE)
226+
greeness <- sapply(image_files, getGreeness, clip_coords=site.clip, USE.NAMES = FALSE)
279227
280228
# Build the final day and greeness
281229
greenness_df <- data.frame(day,greeness)
@@ -292,7 +240,6 @@ trait_canopy_cover <- betydb_query(table = "search",
292240
trait = "canopy_cover",
293241
date = "~2018 May",
294242
limit = "none")
295-
296243
trait_canopy_cover <- trait_canopy_cover %>%
297244
mutate(day = as.Date(raw_date))
298245
```

0 commit comments

Comments
 (0)