-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Vincyane Badouard
committed
Aug 16, 2021
1 parent
fac865b
commit 8bcb7b8
Showing
10 changed files
with
352 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
--- | ||
title: "Crowns_draft" | ||
author: "Vincyane Badouard" | ||
date: "16/08/2021" | ||
output: html_document | ||
--- | ||
|
||
```{r} | ||
inventory <- addtreedim(inventorycheckformat(Paracou6_2016)) | ||
dat <- inventory[679,] | ||
``` | ||
|
||
|
||
```{r} | ||
# The crown | ||
treefromthesky <- function(dat){ | ||
Crown <- dat %>% | ||
mutate(xCrown = Xutm, # X centroid | ||
yCrown = Yutm, # Y ventroid | ||
exCrown = CrownDiameter/2, | ||
eyCrown = CrownHeight/2) %>% | ||
st_as_sf(coords = c("xCrown", "yCrown")) # ellipse centroid coordinates | ||
Crown <- st_ellipse(Crown, Crown$exCrown, Crown$eyCrown) # create the ellipse | ||
} | ||
``` | ||
|
||
```{r} | ||
library(ggplot2) | ||
g <- ggplot() + | ||
geom_sf(data = st_as_sf(inventory, coords = c("Xutm", "Yutm")), aes(label = ScientificName)) + | ||
geom_sf(data = Crown, fill = "forestgreen") # trees polygons | ||
plotly::ggplotly(g) | ||
``` | ||
|
||
```{r} | ||
Canopy <- inventory %>% | ||
group_by(idTree) %>% # for each tree | ||
do(Crowns = # inform geometry. # Filling a column from a function whose input is a table | ||
treefromthesky(.) %>% | ||
st_as_text()) %>% # as text to easy join with a non spacial table | ||
tidyr::unnest(Crowns) # here to pass from list to character | ||
inventory <- left_join(inventory, Canopy, by = "idTree") # join spatial filtered inventory and non spatial complete inventory | ||
``` | ||
|
||
```{r} | ||
#The small ones first so that they are behind the big ones on the plot | ||
inventory <- arrange(inventory, TreeHeight) | ||
ggplot() + | ||
geom_sf(data = getgeometry(inventory, Crowns), | ||
aes(alpha = TreeHeight), # label = paste(idTree, Species), | ||
fill = "forestgreen") | ||
``` | ||
|
||
# Plotly version | ||
```{r} | ||
#The small ones first so that they are behind the big ones on the plot | ||
inventory <- arrange(inventory, TreeHeight) %>% | ||
filter(TreeHeight >30) | ||
g <- ggplot() + | ||
geom_sf(data = getgeometry(inventory, Crowns), | ||
aes(label = paste(idTree, Species), alpha = 1/2), | ||
fill = "forestgreen") # trees polygons | ||
plotly::ggplotly(g) | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
#' createcanopy | ||
#' | ||
#' @param inventory (data.frame) | ||
#' | ||
#' @return a dataframe with a column 'Crowns' containing the ellipses | ||
#' (sfc_POLYGON) as trees crown, with their diameter and height filled in, | ||
#' representing trees from the sky. | ||
#' @export | ||
#' | ||
#' @importFrom dplyr group_by do left_join | ||
#' @importFrom sf st_as_text | ||
#' @importFrom tidyr unnest | ||
#' | ||
#' @examples | ||
#' data(Paracou6_2016) | ||
#' Paracou6_2016 <- dplyr::slice(Paracou6_2016, 1:10) | ||
#' | ||
#' inventory <- addtreedim(inventorycheckformat(Paracou6_2016)) | ||
#' | ||
#' inventory <- createcanopy(inventory) | ||
#' | ||
#' # The small ones first so that they are behind the big ones on the plot | ||
#' inventory <- dplyr::arrange(inventory, TreeHeight) | ||
#' library(ggplot2) | ||
#' ggplot() + | ||
#' geom_sf(data = getgeometry(inventory, Crowns), | ||
#' aes(alpha = TreeHeight), | ||
#' fill = "forestgreen") | ||
#' | ||
createcanopy <- function(inventory){ | ||
|
||
# Arguments check | ||
|
||
if(!inherits(inventory, "data.frame")) | ||
stop("The 'inventory' argument of the 'createcanopy' function must be a data.frame") | ||
|
||
# Global variables | ||
idTree <- Crowns <- . <- NULL | ||
|
||
# Function | ||
|
||
Canopy <- inventory %>% | ||
|
||
group_by(idTree) %>% # for each tree | ||
|
||
do(Crowns = # inform geometry. # do: filling a column from a function whose input is a table | ||
treefromthesky(.) %>% | ||
|
||
st_as_text()) %>% # as text to easy join with a non spacial table | ||
tidyr::unnest(Crowns) # here to pass from list to character | ||
|
||
inventory <- left_join(inventory, Canopy, by = "idTree") # join the column 'Crowns' to the inventory | ||
|
||
} | ||
|
||
#' treefromthesky | ||
#' | ||
#' @param dat 1 row data.frame with columns: | ||
#' Xutm, Yutm (Tree coordinates), CrownDiameter, CrownHeight | ||
#' | ||
#' @return an ellipse (sfc_POLYGON) as a crown, with its diameter and height | ||
#' filled in, representing the tree from the sky. | ||
#' @export | ||
#' | ||
#' @importFrom dplyr mutate | ||
#' @importFrom sf st_as_sf | ||
#' @importFrom nngeo st_ellipse | ||
#' | ||
#' @examples | ||
#' inventory <- addtreedim(inventorycheckformat(Paracou6_2016)) | ||
#' dat <- inventory[679,] | ||
#' | ||
#' Crown <- treefromthesky(dat) | ||
#' | ||
#' library(ggplot2) | ||
#' ggplot() + | ||
#' geom_sf(data = st_as_sf(inventory, coords = c("Xutm", "Yutm"))) + | ||
#' geom_sf(data = Crown, fill = "forestgreen") # trees polygons | ||
#' | ||
treefromthesky <- function(dat){ | ||
|
||
# Arguments check | ||
|
||
if(!inherits(dat, "data.frame")) | ||
stop("The 'dat' argument of the 'treefromthesky' function must be a data.frame") | ||
|
||
if(nrow(dat)!=1) | ||
stop("the data.frame given in the 'dat' argument | ||
of the 'treefromthesky' function must contain only 1 row") | ||
|
||
# Global variables | ||
Xutm <- Yutm <- CrownDiameter <- CrownHeight <- NULL | ||
|
||
# Function | ||
|
||
Crown <- dat %>% | ||
mutate(xCrown = Xutm, # X centroid | ||
yCrown = Yutm, # Y ventroid | ||
exCrown = CrownDiameter/2, # crown radius | ||
eyCrown = CrownHeight/2) %>% # half crown height | ||
st_as_sf(coords = c("xCrown", "yCrown")) # ellipse centroid coordinates | ||
|
||
Crown <- st_ellipse(Crown, Crown$exCrown, Crown$eyCrown) # create the ellipse | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.