---
title: "Manage trees and plot coordinates with BIOMASS"
author: "Arthur Pere"
date: "`r Sys.Date()`"
output: 
  prettydoc::html_pretty: 
    number_sections: yes
    toc: yes
    highlight: vignette
    self_contained: yes
    theme: cayman
vignette: >
  %\VignetteIndexEntry{Manage tree and plot coordinate with BIOMASS}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r setup, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE, echo = TRUE,
  comment = "#>", fig.align = "center"
)
require(BIOMASS)
require(knitr)
```


# Requirement

You need the following information to use the following function in your analysis :
  
  - Dimension of the plots
  - Coordinate GPS of minimum 3 points non-aligned and relatily far away with the corresponding relative coordinates
  - The origin corner
  - The X, Y directions

```{r}
trees <- read.csv(system.file("external", "NouraguesPlot.csv",
  package = "BIOMASS", mustWork = T
))
```

```{r echo=FALSE}
kable(head(trees), digits = 3, row.names = F, caption = "Head of the table trees")
```

We can see in the table that we have for each trees the name of the plot we have, the `xRel` and `yRel`, the relative coordinate of the trees inside the plot. The rest of the column is for calculate the AGB at the end.




```{r, fig.cap="Plot the coordinate long lat"}
coord <- read.csv(system.file("external", "Coord.csv",
  package = "BIOMASS", mustWork = T
))

plot(coord[, c("Long", "Lat")], asp = 1)
```

We can see on the plot that the corner coordinates are spread.


```{r echo=FALSE}
kable(head(coord), digits = 3, row.names = F, caption = "Head of the table coord")
```

In the table, we have the name of the plot, the coordinate `Lat`, `Long` (or another projected coordinates), and `xRel`, `yRel`, the relative coordinate for the points observed.



# To manage the plots
## Import the dataset and visualisation

The plot is referenced in the longitude latitude coordinate so you must have the package `proj4` if you are in this situation. If you have projected coordinate, you can continue with the `projCoord` argument instead of `longlat` argument. 

## Correct the GPS coordinate
```{r, cache=FALSE}
correct_plot <- correctCoordGPS(
  longlat = coord[, c("Long", "Lat")],
  coordRel = coord[, c("xRel", "yRel")],
  rangeX = c(0, 100), rangeY = c(0, 100), drawPlot = T,
  maxDist = 10, rmOutliers = T
)

str(correct_plot, max.level = 1)
```


The output of the function is a list with a data.frame `corner` it's the corner of the plot, `polygon` the spatial polygon, `outliers` the vector with the line number of the outliers and `codeUTM` the UTM code for the polygon.

The outliers are calculated by a measure of distance between the predicted points and the GPS points. If this distance is higher than the value of `maxDist`, the point is considered like outliers.


## Numbering the corner
We have to number the corner of the plot, it is working if we have exactly 4 points for each plot, so we have to do the correctCoordGPS before if we have not the correct number of points.

```{r}
coord_num <- numberCorner(
  projCoord = correct_plot$cornerCoords,
  plot = rep("NB1", 4),
  origin = c(F, F, F, T),
  clockWise = T
)


plot(coord_num[, c("X", "Y")], asp = 1)
text(coord_num[, c("X", "Y")], labels = coord_num[, "corner"], pos = 2, offset = 0.2)
```

On the graph, you can noted than the corner number 1 the origin of the plot.


## Cut the plot in multiple subplot

```{r}
subplot <- cutPlot(
  projCoord = coord_num[, c("X", "Y")],
  plot = coord_num[, c("plot")],
  corner = coord_num[, c("corner")],
  gridsize = 25, dimX = 100, dimY = 100
)
```

```{r echo=FALSE}
kable(head(subplot))
```


# Trees managements
## Attribute the trees to the subplot

```{r}
trees$subplot <- attributeTree(trees[, c("xRel", "yRel")], rep("NB1", nrow(trees)), subplot)
```



## Calculate the AGB and spatialisation
```{r}
trees$AGB <- computeAGB(trees$D, trees$WD, H = trees$H)

AGB <- summaryByPlot(trees$AGB, trees$subplot, drawPlot = T, subplot = subplot)

print(AGB)
```





## Attribute the trees to GPS coordinates

There is two maners to attribute the trees to GPS coordinates

```{r}
TreeCoord <- attributeTreeCoord(
  xy = trees[, c("xRel", "yRel")],
  plot = trees$plot,
  coordAbs = subplot,
  dim = c(100, 100)
)
```
```{r echo=FALSE}
kable(head(TreeCoord), digits = 3, row.names = F, caption = "Head of the table TreeCoord")
```

If you want to have in GPS (longitude/latitude) coordinates (need to install proj4 first) :
```{r}
#TreeCoord <- as.data.frame( proj4::project(TreeCoord, proj = correct_plot$codeUTM, inverse = T) )
```
```{r echo=FALSE}
kable(head(TreeCoord), digits = 3, row.names = F, caption = "Head of the table TreeCoord")
```

If you want to have the GPS (longitude/latitude) coordinates without passing through all this step however you must use the numberCorner function:
```{r}
coordAbs = data.frame(X = c(4.066923, 4.067865, 4.067842, 4.066905), Y = c(52.68883, 52.68877, 52.68793, 52.68783))

ncoordAbs = numberCorner(projCoord = coordAbs, 
                                plot = rep("NB1", 4), 
                                origin = c(T, F, F, F), 
                                clockWise = T)

TreeCoord <- attributeTreeCoord(
  xy = trees[, c("xRel", "yRel")],
  plot = trees$plot,
  coordAbs = ncoordAbs,
  dim = c(100, 100)
)
```
```{r echo=FALSE}
kable(head(TreeCoord), digits = 3, row.names = F, caption = "Head of the table TreeCoord")
```





