Skip to content

Commit

Permalink
Merge pull request #14 from AtlanticR/ocearch
Browse files Browse the repository at this point in the history
Ocearch
  • Loading branch information
stoyelq authored Sep 3, 2021
2 parents 67ee3c5 + c2a9968 commit e03f8dc
Show file tree
Hide file tree
Showing 10 changed files with 129 additions and 69 deletions.
19 changes: 17 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ R Shiny App to select search area(s) and generate reproducible reports (R Markdo
## Set up Revn:
Make sure that [RTools](https://cran.r-project.org/bin/windows/Rtools/) is installed.

```R
```r
install.packages("devtools")
install.packages("renv")
renv::activate()
Expand All @@ -20,13 +20,26 @@ renv::restore() # loads all needed packages (may take 15+ mins the first time)
```

Adding a package:
```R

``` r
install.packages("new_package")
renv::snapshot()
```

After adding a new package, commit the updated renv.lock file to source control.

## Download necessary data

After making sure that you have the necessary permissions to access the data, run:

``` r
source(here::here("dataprocessing/CopyRData.R"))
copy_rdata_files()
```

## Unit Tests:

Once Renv is setup and data has been loaded, running `testthat::test_local()` should pass all unit tests.

## Run the application

Expand All @@ -46,6 +59,8 @@ See [demo.webm](https://github.com/dfo-mar-odis/shinySpatialApp/raw/main/demo.we
- `app/R` includes the R files to be loaded (it loads packages and functions);
- `app/Rmd` includes all the `.Rmd` files needed to generate the report(s);
- `app/output` includes all outputs once generated: selected geometries as a `.geojson` and the report in all format (the different section are also added).
- `dataprocessing` contains all of the preprocessing scripts to prepare data for use in the application.
- `tests/testthat` contains the unit test scripts that are run when test_local() is called.

# **Collaborative Workflow**

Expand Down
21 changes: 21 additions & 0 deletions app/R/copyRData.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

copy_rdata_files <- function() {

rDataDir <- "\\\\ent.dfo-mpo.ca\\ATLShares\\Science\\BIODataSvc\\IN\\MSP\\Data\\Rdata\\data"

localDataDir <- here::here("app/data/")
remoteInfo <- file.info(list.files(rDataDir, full.names = TRUE))
remoteInfo["filenames"] = list.files(rDataDir)
localInfo <- file.info(list.files(localDataDir, full.names = TRUE))
localInfo["filenames"] = list.files(localDataDir)

allInfo <- merge(remoteInfo, localInfo, by="filenames")
updateList <- allInfo$mtime.x > allInfo$mtime.y
if (any(updateList)) {
updateFileList <- filter(allInfo, updateList)$filenames
updateFilePath <- rownames(filter(remoteInfo, remoteInfo$filename %in% updateFileList))
copyResults <- file.copy(updateFilePath, localDataDir, overwrite = TRUE)
}
return("Data files up to date :)")

}
3 changes: 2 additions & 1 deletion app/R/renderReport.R
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ renderReport <- function(input, geoms, outFileName = NULL, dirOut = "output",
)

} # end of language loop

msgInfo("Render complete")

if (all(ok)) {
# this is done to generate an html preview (see "report" tab)
htmlName <- switch_ext(basename(inFile), "html")
Expand Down
6 changes: 4 additions & 2 deletions app/Rmd/intro_EN.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@ knitr::opts_chunk$set(echo = FALSE, warning=FALSE, message=FALSE, fig.align="lef

```{r load-data-and-functions, echo=FALSE, include=FALSE, cache=FALSE}
minYear <- 2010
lapply(list.files(here::here("app/R"), pattern = ".[Rr]$", full.names = TRUE),source) # check helpers.R for a list of packages
copy_rdata_files() # make sure data files are up to date.
load(here::here("app/data/testData.RData"))
#load(here::here("app/data/OpenData.RData"))
#load(here::here("app/data/OpenData_sardist.RData"))
#load(here::here("app/data/SecureData.RData"))
#load("\\\\ent.dfo-mpo.ca\\ATLShares\\Science\\BIODataSvc\\SRC\\Reproducible_Reporting\\data\\SecureData.Rdata")
lapply(list.files(here::here("app/R"), pattern = ".[Rr]$", full.names = TRUE),source) # check helpers.R for a list of packages
```

```{r Load-Search-Area, echo=FALSE, include=FALSE, cache=FALSE}
Expand All @@ -57,7 +59,7 @@ list2env(mapDataList, envir = environment())

<!-- # ```{r child = c('../Rmd/report_cetaceans_EN.Rmd', 'report_fish_EN.Rmd', 'report_habitat_EN.Rmd', 'report_planning_EN.Rmd', 'report_SARA_EN.Rmd')} -->
<!-- # ``` -->
<!--```{r child = '../Rmd/report_fish_EN.Rmd'}
<!-- ```{r child = '../Rmd/report_fish_EN.Rmd'}
``` -->

<!-- END -->
Expand Down
3 changes: 2 additions & 1 deletion app/Rmd/intro_FR.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@ knitr::opts_chunk$set(echo = FALSE, warning=FALSE, message=FALSE, fig.align="lef

```{r load-data-and-functions, echo=FALSE, include=FALSE, cache=FALSE}
minYear <- 2010
lapply(list.files("../R", pattern = ".[Rr]$", full.names = TRUE),source) # check helpers.R for a list of packages
copy_rdata_files() # make sure data files are up do date.
load("../data/OpenData.RData")
load("../data/OpenData_sardist.RData")
load("../data/SecureData.RData")
#load("\\\\ent.dfo-mpo.ca\\ATLShares\\Science\\BIODataSvc\\SRC\\Reproducible_Reporting\\data\\SecureData.Rdata")
lapply(list.files("../R", pattern = ".[Rr]$", full.names = TRUE),source) # check helpers.R for a list of packages
```

```{r Load-Search-Area, echo=FALSE, include=FALSE, cache=FALSE}
Expand Down
39 changes: 39 additions & 0 deletions app/Rmd/report_fish_EN.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -209,3 +209,42 @@ if (!is.null(outputList)) {
knitr::kable(table_obis1, align="l", caption="Quality Tier: Medium. Security level: none. Ocean Biodiversity Information System (OBIS) observation records of species contained within the search area and listed by the <i>Species At Risk Act</i> (SARA), or assessed by the Committee on the Status of Endangered Wildlife in Canada (COSEWIC). <b>The absence of a species in this figure should be interpreted as an absence of reporting, not necessarily as an absence of the species in the area.</b>", booktabs = T, escape = F, linesep="") %>% kable_styling(bootstrap_options = "striped", full_width = F, position = "left")
}
```

### **OCEARCH Shark Tracker**
Contact: Bryan Franks ([email protected]) via Sean Butler ([email protected])
URL: <https://www.ocearch.org/tracker/>
Last retrieved on: July 22 2021 by Sean Butler
Quality Tier: High
Search year: 2013-2020
Security level: none
Data use constraints: DFO INTERNAL USE ONLY

OCEARCH is a data-centric organization built to help scientists collect previously unattainable data in the ocean while open sourcing our research and explorations. Open sourcing and inclusion at every level, and in real-time, allows the world to participate in their projects while being aware of the developing science. OCEARCH shares the data, including samples collected and tracking data, with collaborating institutions and students who request them for their studies. The tracking data is also open-sourced to the public via the OCEARCH Tracker. The OCEARCH Tracker is a free app that lets you observe the navigational pattern of animals that have been tagged with satellite tracking technology. White Shark locations from satellite trackers are summarized in this section from 1 Jan 2013 to 31 Oct 2020. Locations were clipped to only include locations within the Canadian EEZ. A speed filter was run with 3 m/s as the maximum speed and 2.5 km as the potential error of locations. This filter removed approximately 100 locations out of of 2,000. Resulting points ere then visualized in ArcGIS to identify potential errors and remove locations that seemed implausible. Each shark location was individually plotted to do a second visual examination and to remove or flag errors/outliers. There is additional QA/QC that could still be performed to this dataset to capture additional issues (e.g. there may be single shark locations in Canadian waters during a period of time that the speed filter would not remove or that were not captured in the visual inspection). The absence of White Sharks in this section map should be interpreted as an absence of tracking data, not necessarily as an absence of the species in the area.

#### ***Area-specific OCEARCH Shark Tracker search results***
*


```{r ocearch-results, include=FALSE, cache=FALSE}
ocearchOutputList <- master_intersect(ocearch_sf, mapDataList)
```


```{r ocearch-check, echo=FALSE, results='asis',}
Report_ocearch <- if (is.null(ocearchOutputList)) {
"There are no relevant White Shark records in the OCEARCH Shark Tracker for this search area."
} else {
numRecords <- length(ocearchOutputList$studyData$geometry)
distinctIndv <- dplyr::n_distinct(ocearchOutputList$studyData$ID)
paste("There are", numRecords, "relevant White Shark records for", distinctIndv, "individuals in OCEARCH Shark Tracker search results for this search area.")
}
Report_obis<-noquote(Report_ocearch)
writeLines(Report_ocearch)
```
```{r ocearch-plot, fig.height=8, fig.width=11, fig.cap='Quality Tier: Medium. Security level: none. Map showing the search area defined by the user (red outline) was used to query information from OCEARCH Shark Tracker observation records, for White Shark. Dataset set has not yet been completely error-checked. The absence of a species in this figure should be interpreted as an absence of tracking data, not necessarily as an absence of the species in the area.'}
if (!is.null(ocearchOutputList)) {
plot_rr_sf(areaMap, ocearchOutputList$mapPoints)
}
```

75 changes: 25 additions & 50 deletions app/Rmd/report_habitat_FR.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ Cartes satellitaires de la végétation intertidale et des polygones de présenc
Personne-ressource : <[email protected]>
Consulté le : 17 février 2021
Niveau de qualité : Moyennem
Niveau de sécurité : Aucun.
Niveau de sécurité : Aucun.

Le fucus (Ascophyllum nodosum) est une algue brune intertidale d’eau froide commune dont les crampons de longue durée s’agrippent à des substrats rocheux. Il domine la zone intertidale rocheuse du Canada atlantique et fournit un habitat pour les poissons, les invertébrés et les oiseaux. Il y a peu d’information disponible sur la répartition spatiale du fucus et de la végétation intertidale dans la région des Maritimes. Les données d’enquête existantes de l’industrie sont considérées comme exclusives et ne sont pas disponibles. Par conséquent, des cartes satellitaires à l’échelle régionale ont été élaborées pour indiquer l’emplacement, la densité relative de la végétation intertidale et les polygones qui délimitent les zones probablement occupées par le fucus (Lazin et al., en préparation).

Les cartes sont produites à l’aide de données satellitaires Sentinel-2 de ciel dégagé à une résolution au sol de 10 mètres, en appliquant un indice de végétation par différence normalisée (IVDN) bien établi pour détecter la végétation intertidale exposée à marée basse. Les parcelles de végétation sont délimitées à l’aide de seuils, et la probabilité de présence de fucus est attribuée en fonction de la classification du substrat riverain, des archives photographiques/vidéo aériennes et des données des bases de données sur la biodiversité en libre accès. Les produits finaux seront conservés sur le portail des données ouvertes du gouvernement du Canada et seront continuellement améliorés à mesure que de nouvelles données de terrain seront disponibles.
Les cartes sont produites à l’aide de données satellitaires Sentinel-2 de ciel dégagé à une résolution au sol de 10 mètres, en appliquant un indice de végétation par différence normalisée (IVDN) bien établi pour détecter la végétation intertidale exposée à marée basse. Les parcelles de végétation sont délimitées à l’aide de seuils, et la probabilité de présence de fucus est attribuée en fonction de la classification du substrat riverain, des archives photographiques/vidéo aériennes et des données des bases de données sur la biodiversité en libre accès. Les produits finaux seront conservés sur le portail des données ouvertes du gouvernement du Canada et seront continuellement améliorés à mesure que de nouvelles données de terrain seront disponibles.

Remarques sur les données :

Expand All @@ -21,62 +21,37 @@ Remarques sur les données :
#### ***Résultats de la recherche sur la végétation intertidale et le fucus, par zone***

```{r overlap-rockweed, echo=FALSE, results='asis'}
rockweed_intersect <- st_intersection(studyArea, st_make_valid(rockweed_sf))
rockweed_result <- as.numeric(nrow(rockweed_intersect))
# define polygon dataset and attribute to plot
poly_sf <- rockweed_sf
attribute <- "RWP"
# intersect polyData with 3 polygons (region, map area, and study box)
clipped <- master_intersect(poly_sf, mapDataList)
```
*
```{r overlap-rockweed-output, echo=FALSE, results='asis'}
Query_output_rockweed <- if(rockweed_result < 1){"There are no relevant records of predicted intertidal vegetation for this search area."
} else {"There are relevant records of predicted intertidal vegetation for this search area."}
Query_output_rockweed<-noquote(Query_output_rockweed)
writeLines(Query_output_rockweed)
```{r overlap rockweed output, echo=FALSE, results='asis'}
polyText<-if(is.null(clipped$studyData)){"Il n’existe aucun registre pertinent sur la végétation intertidale prévue pour cette zone de recherche."
} else {"Il existe des registres pertinentes sur la végétation intertidale prévue pour cette zone de recherche."}
polyText<-noquote(polyText)
writeLines(polyText)
```

```{r rockweed-plot, fig.height=8, fig.width=11, fig.cap='Quality Tier: Medium. Security level: none. Map showing the search area defined by the user: location of the proposed project/activity is highlighted in yellow (input polygon), surrounded by an exposure zone or user-defined buffer area in blue. Figure shows overlap between the search area and satellite-based maps of intertidal vegetation and rockweed presence polygons.'}
plot_rockweed <- function(rockweed_sf, areaMap, bboxMap) {
# crop rockweed layer to the map area to speed up plotting
rockweed <- st_crop(st_make_valid(rockweed_sf), bboxMap)
if (dim(rockweed)[1] == 0){
rockweedMap <- NULL
}else{
# define axis limit
axLim <-ggplot2::coord_sf(xlim = c(bboxMap["xmin"], bboxMap["xmax"]),
ylim = c(bboxMap["ymin"], bboxMap["ymax"]),
expand = FALSE)
# replace codes with words
rockweed$Rockweed <- ""
rockweed$Rockweed[which(rockweed$RWP == 1)] <- "1-Present"
rockweed$Rockweed[which(rockweed$RWP == 2)] <- "2-Likely Present"
rockweed$Rockweed[which(rockweed$RWP == 5)] <- "5-Unknown"
rockweed$Rockweed[which(rockweed$RWP == 0)] <- "Not Present"
rockweedMap <- areaMap +
geom_sf(data = rockweed, aes(fill = Rockweed), colour = NA) +
scale_fill_manual(values = c("#009E73", "#E69F00", "#0072B2","#999999")) +
studyBox_geom +
axLim
}
return(rockweedMap)
}
if(rockweed_result > 1){print(
plot_rockweed(rockweed_sf, areaMap, bboxMap))}
```{r rockweed-plot, fig.height=8, fig.width=11, fig.cap='Niveau de qualité : Moyenne. Niveau de sécurité : Aucun. Carte montrant la zone de recherche définie par l’utilisateur : l’emplacement du projet ou de l’activité proposé est surligné en jaune (polygone d’entrée), entouré d’une zone exposée ou d’une zone tampon définie par l’utilisateur en bleu. La figure illustre le chevauchement entre la zone de recherche et les cartes satellitaires de la végétation intertidale et des polygones de présence de fucus.'}
if(!is.null(clipped$studyData)){print(
plot_rr_sf(areaMap, clipped$mapData, attribute))}
```

```{r table-rockweed, echo=FALSE, cache=FALSE, include = FALSE}
if(rockweed_result > 1){
rwTable <- rockweedStats(rockweed_sf, studyArea)}
if(!is.null(clipped$studyData)){
rwTable <- rockweedStats(clipped$studyData, studyArea)}
```

```{r rockweed-table}
if(rockweed_result > 1){
knitr::kable(rwTable, align="l",
caption = "Number of vegetation polygons and surface area occupied by each category for the study area. Total intertidal vegetations is a sum of all categories.", booktabs = T, escape = F, linesep = "") %>%
if(!is.null(clipped$studyData)){
knitr::kable(rwTable,align="l",
caption = "Nombre de polygones de végétation et la superficie occupée par chaque catégorie pour la zone d’étude. La végétation intertidale totale est la somme de tous les catégories.", booktabs = T, escape = F, linesep="") %>%
kable_styling(bootstrap_options = "striped", full_width = T, position = "left")
}
}
```

Loading

0 comments on commit e03f8dc

Please sign in to comment.