Skip to content

Commit

Permalink
Merge branch 'gh-pages' of https://github.com/OHI-Science/ohiprep_v2024
Browse files Browse the repository at this point in the history
… into gh-pages
  • Loading branch information
dustin-duncan committed Sep 10, 2024
2 parents 79aac26 + 0dedd40 commit 079b008
Show file tree
Hide file tree
Showing 29 changed files with 1,898 additions and 260 deletions.
52 changes: 37 additions & 15 deletions Reference/CRS/crs_exercise_anna.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,18 @@ library(tidyverse)
Data describing commercial landings (tonnes) in 2017.

```{r}
# Read in commercial landings data
# ---- Read in commercial landings data ----
fish <- terra::rast(here("Reference", "CRS", "commercial_landings_2017.tif"))
# ---- preliminary data exploration -----
# print out geospatial data info (class, dimensions, resolution, extent, CRS, etc.)
fish # equivalent to (fish)
# print out summary of value layer
summary(fish)
## Take a look
# ---- raw plot ----
## take a look!
terra::plot(fish)
# adjust margins: par(mar = c(bottom, left, top, right))
Expand All @@ -57,8 +59,19 @@ par(oma = c(0,0,0,0))
terra::plot(fish, mar = c(0, 1.5, 0, 5) )#,
#oma = c(0,0,0,0)) # outer margin area
# ---- log-transformed plot ----
# lets log so we can visualize this a little better
terra::plot(log(fish + 1), mar = c(0,1.5,0,3.5))
terra::plot(log(fish + 1) , mar = c(0,1.5,0,3.5))
#par(mar = c(5, 4, 4, 0))
terra::plot(log(fish + 1), main = "Log-transformed Commercial Landings (EPSG:4326)", mar = c(1.5, 0, 2.1, 1))
# library(tidyterra)
# ggplot() +
# geom_spatraster(data = fish, aes(fill = commercial_landings_2017)) +
# coord_sf(crs = 4326) +
# scale_fill_grass_c(palette = "plasma")
```


Expand All @@ -78,8 +91,11 @@ Check against Sea Around Us to make sure this seems generally reasonable.

```{r}
cell_area <- terra::cellSize(fish)
terra::plot(cell_area, box = TRUE,
mar = c(2, 1.5, 1, 8)) # adjusted margins for exporting plot
# terra::plot(cell_area, box = TRUE,
# mar = c(2, 1.5, 1, 8)) # adjusted margins for exporting plot
terra::plot(cell_area, main = "Cell Size: Original CRS (EPSG:4326)", mar = c(0, 1.5, 0, 6))
print(cell_area)
```
Expand Down Expand Up @@ -136,7 +152,8 @@ terra::global(fish_moll, "sum", na.rm = TRUE)
# 389.3 million
```

- 389 million tonnes is a much higher value than the raw data's ~87 million value. Reprojecting the data impacted the values to a significant degree. Cell sizes have been stretched and increased to be uniform across the globe. When perform our global summation after transforming, the
- 389 million tonnes is a much higher value than the raw data's ~87 million value. Reprojecting the data impacted the values to a significant degree. Cell sizes have been stretched and increased to be uniform across the globe. When we performed our global summation after transforming, the commercial fish landings values were counts per cell, not density (counts per km^2, for example). This means that when we projected the data from EPSG:4326 to Mollweide, the count associated with one cell in the original raster was copied over to potentially multiple cells assigned to the same area in the Mollweide template.


```{r}
# calculate cell area
Expand All @@ -145,15 +162,15 @@ cell_area_moll <- terra::cellSize(fish_moll)
#par(mai = c(1, 1, 3, 5)) # set margins: bottom, left, top, right
#par(oma = c(1, 1, 3, 4))
#par(mar = c(1,1,1,1))
terra::plot(cell_area_moll, main = "Mollweide projection cell size", mar = c(0, 2, 0, 6))
terra::plot(cell_area_moll, main = "Cell Size: Mollweide Projection", mar = c(0, 2, 0, 6))
terra::plot(cell_area, main = "Original projection cell size")
terra::plot(cell_area, main = "Cell Size: Original CRS (EPSG:4326)", mar = c(0, 1.5, 0, 6))
```



```{r}
(cell_area_moll$area)
cell_area_moll$area
# min 0
# max is 547 million
cell_area$area
Expand All @@ -165,7 +182,7 @@ cell_area$area
Compare to the original tonnes? What is happening? How can you improve this?
- original tons: 86331119
- Mollweide tons: 389271065
- the cell size area is now relatively consistent across the globe -- old cell sizes have stretched etc., to fit a standardized cell size (I tried to extend the margins to see the full scalebar label values but struggled)
- the cell size area is now relatively consistent across the globe -- old cell sizes have stretched etc., to fit a standardized cell size
- perform global calculations before reprojecting


Expand All @@ -182,24 +199,26 @@ Compare to the original tonnes? What is happening? How can you improve this?
# get density (tonnes/area)
fish_density <- fish / cell_area
plot(log(fish_density))
par(mar = c(4, 4, 4, 0)) # set margins: bottom, left, top, right
terra::plot(log(fish_density), main = "Commercial Landings Density (original CRS: EPSG 4326)")
# reproject to Mollweide
fish_density_moll <- terra::project(fish_density, crs(moll))
fish_density_moll
plot(log(fish_density_moll))
terra::plot(log(fish_density_moll), main = "Commercial Landings Density (projected CRS: Mollweide)")
# get area of cells
new_cell_size <- cellSize(fish_density_moll)
plot(new_cell_size)
terra::plot(new_cell_size)
# multiply density by area to get count (tonnes)
new_tonnes <- fish_density_moll * new_cell_size
plot(log(new_tonnes + 1))
terra::plot(log(new_tonnes + 1), main = "Density to Counts: Mollweide")
# sum to check
global(new_tonnes, "sum", na.rm = TRUE)
terra::global(new_tonnes, "sum", na.rm = TRUE)
# 85 million!
```

Expand Down Expand Up @@ -249,6 +268,9 @@ rob <- "+proj=robin +datum=WGS84 +units=m +no_defs"
fish_moll_rob <- terra::project(fish_moll, crs(rob))
plot(log(fish_moll_rob + 1))
terra::global(fish_moll_rob, "sum", na.rm = TRUE)
# now back to lat long:
fish_moll_rob_latlon <- project(fish_moll_rob, fish)
Expand Down
Binary file added Reference/CRS/figs/cell_size_mollweide.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Reference/CRS/figs/cell_size_og_crs.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Reference/CRS/figs/density_mollweide_plot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Reference/CRS/figs/density_original_crs_plot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Reference/CRS/figs/density_to_counts_moll.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Reference/CRS/figs/fish_log_plot_mollweide.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Reference/CRS/figs/fish_log_plot_og_crs.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed Reference/CRS/figs/fish_moll_cell_area.png
Binary file not shown.
Binary file removed Reference/CRS/figs/fish_moll_log_plot.png
Binary file not shown.
Binary file added Reference/CRS/figs/original_crs_log_plot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
461 changes: 461 additions & 0 deletions Reference/CRS/testing-tabs.html

Large diffs are not rendered by default.

72 changes: 72 additions & 0 deletions Reference/CRS/testing-tabs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@


CSS


<style>
.tab {
overflow: hidden;
border: 1px solid #ccc;
background-color: #f1f1f1;
}

.tab button {
background-color: inherit;
float: left;
border: none;
outline: none;
cursor: pointer;
padding: 14px 16px;
transition: 0.3s;
}

.tab button:hover {
background-color: #ddd;
}

.tab button.active {
background-color: #ccc;
}

.tabcontent {
display: none;
padding: 6px 12px;
border: 1px solid #ccc;
border-top: none;
}
</style>



Text introducing plots

<div class="tab">
<button class="tablinks" onclick="openTab(event, 'Figure1')" id="defaultOpen">Mollweide Projection Cell Size</button>
<button class="tablinks" onclick="openTab(event, 'Figure2')">OG CRS Cell Size</button>
</div>

<div id="Figure1" class="tabcontent">
<img src="figs/cell_size_mollweide.png" alt="Mollweide Projection">
</div>

<div id="Figure2" class="tabcontent">
<img src="figs/cell_size_og_crs.png" alt="Original CRS EPSG:4326">
</div>

<script>
function openTab(evt, tabName) {
var i, tabcontent, tablinks;
tabcontent = document.getElementsByClassName("tabcontent");
for (i = 0; i < tabcontent.length; i++) {
tabcontent[i].style.display = "none";
}
tablinks = document.getElementsByClassName("tablinks");
for (i = 0; i < tablinks.length; i++) {
tablinks[i].className = tablinks[i].className.replace(" active", "");
}
document.getElementById(tabName).style.display = "block";
evt.currentTarget.className += " active";
}

document.getElementById("defaultOpen").click();
</script>
4 changes: 2 additions & 2 deletions globalprep/ao/v2024/ao_need_data_prep.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ v2024
One more year of data
# Data Source

**Downloaded**: 2023-07-12
**Downloaded**: 2024-07-30

**Description**:
GDP adjusted per capita by PPP (ppppcgdp)
Expand All @@ -41,7 +41,7 @@ GDP per capita based on purchasing power parity (PPP). PPP GDP is gross domestic

Data is available directly to R through the WDI package.

**Time range**: 1990-2022
**Time range**: 1990-2023

***

Expand Down
10 changes: 5 additions & 5 deletions globalprep/ao/v2024/ao_need_data_prep.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<meta http-equiv="X-UA-Compatible" content="IE=EDGE" />


<meta name="author" content="Compiled on Fri Aug 23 17:40:31 2024 by sujeet" />
<meta name="author" content="Compiled on Thu Sep 5 17:42:48 2024 by lecuona" />


<title>OHI 2024 - Artisanal Opportunities: Preparing need data</title>
Expand Down Expand Up @@ -1647,8 +1647,8 @@

<h1 class="title toc-ignore">OHI 2024 - Artisanal Opportunities:
Preparing need data</h1>
<h4 class="author"><em>Compiled on Fri Aug 23 17:40:31 2024 by
sujeet</em></h4>
<h4 class="author"><em>Compiled on Thu Sep 5 17:42:48 2024 by
lecuona</em></h4>

</div>

Expand All @@ -1664,7 +1664,7 @@ <h1><span class="header-section-number">2</span> Updates from previous
<p>v2023 One more year of data. Updated so that pop_weights data is also
read in through the WDI package, and saved as annual data instead of
only the most recent year. v2024 One more year of data # Data Source</p>
<p><strong>Downloaded</strong>: 2023-07-12</p>
<p><strong>Downloaded</strong>: 2024-07-30</p>
<p><strong>Description</strong>:<br />
GDP adjusted per capita by PPP (ppppcgdp) <a href="http://data.worldbank.org/indicator/NY.GDP.PCAP.PP.KD" class="uri">http://data.worldbank.org/indicator/NY.GDP.PCAP.PP.KD</a>
Reported at country scale.</p>
Expand All @@ -1679,7 +1679,7 @@ <h1><span class="header-section-number">2</span> Updates from previous
depletion and degradation of natural resources. Data are in constant
international dollars based on the 2011 ICP round.</p>
<p>Data is available directly to R through the WDI package.</p>
<p><strong>Time range</strong>: 1990-2022</p>
<p><strong>Time range</strong>: 1990-2023</p>
<hr />
</div>
<div id="methods" class="section level1" number="3">
Expand Down
31 changes: 16 additions & 15 deletions globalprep/lsp/v2024/lsp_data_prep.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ output:
number_sections: yes
theme: cerulean
toc: yes
toc_float: true
pdf_document:
toc: yes
word_document:
Expand Down Expand Up @@ -48,7 +49,7 @@ v2021 Using updated February 2021 data

# Data Source

**Reference**: IUCN and UNEP-WCMC (2022), The World Database on Protected Areas (WDPA) [On-line], May 2024. Cambridge, UK: UNEP-WCMC. Available at: www.protectedplanet.net.
**Reference**: IUCN and UNEP-WCMC (2023), The World Database on Protected Areas (WDPA) [On-line], May 2024. Cambridge, UK: UNEP-WCMC. Available at: www.protectedplanet.net.

**Downloaded**: May 24, 2024

Expand Down Expand Up @@ -118,9 +119,9 @@ Comparing the global WDPA raster to the 3 nautical miles offshore and 1 km inlan
```{r lsp_zonal_stats, eval = FALSE}
# list intermedite file paths
zonal_files <- c('zonal_3nm' = file.path(dir_goal, 'intermediate', 'zonal_stats_3nm.csv'),
'zonal_1km' = file.path(dir_goal, 'intermediate', 'zonal_stats_1km.csv'),
'zonal_eez' = file.path(dir_goal, 'intermediate', 'zonal_stats_eez.csv'))
zonal_files <- c('zonal_3nm' = file.path(dir_goal, 'int', 'zonal_stats_3nm.csv'),
'zonal_1km' = file.path(dir_goal, 'int', 'zonal_stats_1km.csv'),
'zonal_eez' = file.path(dir_goal, 'int', 'zonal_stats_eez.csv'))
# load raster created in 1_prep_wdpa_rast.rmd
rast_wdpa <- terra::rast(file.path(dir_goal_anx, 'rast', 'wdpa_2024_moll_500m.tif'))
Expand All @@ -134,7 +135,7 @@ rgn_rast_list <- c(
'zonal_1km' = file.path(dir_zones, 'rgn_inland1km_mol_500mcell.tif'),
'zonal_eez' = file.path(dir_zones, 'rgn_eez_mol_500mcell.tif'))
### Remove all files in `intermediate` if it's the first time working through this data prep for this assessment
### Remove all files in `int` if it's the first time working through this data prep for this assessment
### Filters out finished zonal files: if zonal files don't exist yet, they will be created (comment out to recalculate)
zonal_files_to_run <- zonal_files[!file.exists(zonal_files)]
rgn_rast_list <- rgn_rast_list[!file.exists(zonal_files)]
Expand Down Expand Up @@ -201,7 +202,7 @@ cat('writeRaster elapsed: ', (proc.time() - ptm)[3])
z <- lsp_crosstab(rgn_rast_list[3], rast_values = rast_wdpa) #~51 min minutes to run #eez
cat('writeRaster elapsed: ', (proc.time() - ptm)[3])
## Save these files to the intermediate folder
## Save these files to the int folder
write_csv(x, zonal_files_to_run[1])
write_csv(y, zonal_files_to_run[2])
write_csv(z, zonal_files_to_run[3])
Expand Down Expand Up @@ -306,10 +307,10 @@ prot_1km <- stats_1km %>% calc_areas()
prot_3nm <- stats_3nm %>% calc_areas()
prot_eez <- stats_eez %>% calc_areas()
# write results to intermediate folder as csv
write_csv(prot_3nm, file.path(dir_goal, 'intermediate', 'area_protected_3nm.csv'))
write_csv(prot_1km, file.path(dir_goal, 'intermediate', 'area_protected_1km.csv'))
write_csv(prot_eez, file.path(dir_goal, 'intermediate', 'area_protected_eez.csv'))
# write results to int folder as csv
write_csv(prot_3nm, file.path(dir_goal, 'int', 'area_protected_3nm.csv'))
write_csv(prot_1km, file.path(dir_goal, 'int', 'area_protected_1km.csv'))
write_csv(prot_eez, file.path(dir_goal, 'int', 'area_protected_eez.csv'))
```

------------------------------------------------------------------------
Expand All @@ -323,10 +324,10 @@ From the protected area files, write out the individual layers ready for the Too

```{r write_layers, eval = FALSE}
# read in files and rename
prot_3nm <- read_csv(file.path(dir_goal, 'intermediate', 'area_protected_3nm.csv')) %>%
prot_3nm <- read_csv(file.path(dir_goal, 'int', 'area_protected_3nm.csv')) %>%
rename(area = a_tot_km2,
a_prot_3nm = a_prot_km2)
prot_1km <- read_csv(file.path(dir_goal, 'intermediate', 'area_protected_1km.csv')) %>%
prot_1km <- read_csv(file.path(dir_goal, 'int', 'area_protected_1km.csv')) %>%
rename(area = a_tot_km2,
a_prot_1km = a_prot_km2)
Expand All @@ -353,12 +354,12 @@ Some goals require calculation of resilience nearshore (3nm) or entire EEZ.
area_ref = .30 ### 30% of area protected = reference point
# read in regional data csvs from intermediate
resil_3nm <- read_csv(file.path(dir_goal, 'intermediate', 'area_protected_3nm.csv')) %>%
# read in regional data csvs from int
resil_3nm <- read_csv(file.path(dir_goal, 'int', 'area_protected_3nm.csv')) %>%
mutate(resilience.score = (a_prot_km2 / a_tot_km2) / area_ref,
resilience.score = ifelse(resilience.score > 1, 1, resilience.score))
resil_eez <- read_csv(file.path(dir_goal, 'intermediate', 'area_protected_eez.csv')) %>%
resil_eez <- read_csv(file.path(dir_goal, 'int', 'area_protected_eez.csv')) %>%
mutate(resilience.score = (a_prot_km2 / a_tot_km2) / area_ref,
resilience.score = ifelse(resilience.score > 1, 1, resilience.score))
# ask Mel about using file.path versus here function
Expand Down
1,227 changes: 1,178 additions & 49 deletions globalprep/lsp/v2024/lsp_data_prep.html

Large diffs are not rendered by default.

Loading

0 comments on commit 079b008

Please sign in to comment.