-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathanalysePostThin_service.R
117 lines (95 loc) · 5.04 KB
/
analysePostThin_service.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
################################################################################
## Author: Gérald Fenoy, [email protected]
## Daniel McInerney, [email protected]
##
## Copyright (c) 2019 Coillte Teoranta. All rights reserved.
##
## This work has been supported by Coillte Teoranta, Forest Resource Planning
################################################################################
## Permission is hereby granted, free of charge, to any person obtaining a
## copy of this software and associated documentation files (the "Software"),
## to deal in the Software without restriction, including without limitation
## the rights to use, copy, modify, merge, publish, distribute, sublicense,
## and/or sell copies of the Software, and to permit persons to whom the
## Software is furnished to do so, subject to the following conditions:
##
## The above copyright notice and this permission notice shall be included
## in all copies or substantial portions of the Software.
##
## THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
## OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
## FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
## THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
## LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
## FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
## DEALINGS IN THE SOFTWARE.
################################################################################
source("minimal.r")
suppressPackageStartupMessages(library(readxl))
suppressPackageStartupMessages(library(dplyr))
suppressPackageStartupMessages(library(ggplot2))
suppressPackageStartupMessages(library(RSQLite))
analyze <- function(conf,inputs,outputs) {
sink(stderr())
# args <- commandArgs(TRUE)
posthinDB <- inputs[["db"]][["value"]]
inputData <- inputs[["data"]][["cache_file"]]
## set up database connection
drv <- dbDriver('SQLite')
con <- dbConnect(drv, posthinDB)
data <- read_excel(inputData)
zoo[["conf"]][["lenv"]][["message"]] <<- 'Post Thin data read in...'
##write field measurements to the SQLite table
dbWriteTable(con, "fieldmeasurements", data, append=TRUE)
##handle 0 in the com_sub if it has been incorrectly entered
data %>% mutate(Comp_sub=ifelse(substr(Comp_sub,6,6)==0, paste(substr(Comp_sub,1,5),'O',substr(Comp_sub,7,7),sep=''), paste(substr(Comp_sub,1,7)))) -> data
kml_origin <- dbGetQuery(con, "select name, com_sub_real FROM kml_origin")
##generate the data for the plot location table
data <- left_join(data, kml_origin, by=c('Plotname'='name'))
data %>% select(HU_ID, Comp_sub, com_sub_real, Plotname, Plotdate, 'Plotlocation - Latitude', 'Plotlocation - Longitude') %>%
rename('Lat' = 'Plotlocation - Latitude') %>%
rename('Long' = 'Plotlocation - Longitude') %>%
unique() -> plotLocation
##write field measurements to the SQLite table
dbWriteTable(con, "plotLocation", plotLocation, append=TRUE)
zoo[["conf"]][["lenv"]][["message"]] <<- 'Plot location created and uploaded...'
##calculate the Plot Level Estimates
data %>% mutate_at(c('Height'), ~na_if(.,0)) -> data
data %>% mutate(ba = pi * (Diameter/10)^2 / 40000) %>%
mutate_at(vars(Height), ~replace(., . == 0, NA)) %>%
mutate(n_plots = length(unique(Plotname))) %>%
group_by(HU_ID, com_sub_real, Plotname, Speciesname) %>%
summarize(m_dbh = round(mean(Diameter)/10,0),
max_dbh = round(max(Diameter)/10,0),
min_dbh = round(min(Diameter)/10,0),
n_stems = n(),
n_plots = mean(n_plots),
mean_height = round(mean(na.omit(Height)),1),
stemsha = as.integer(n()/mean(Plotsize)),
basalha = round(sum(ba)/mean(Plotsize),0)) -> plotEstimates
##write field measurements to the SQLite table
dbWriteTable(con, "plotEstimates", plotEstimates, append=TRUE)
##calculate the Sub-compartment Estimates
plotEstimates %>% group_by(HU_ID, com_sub_real, Speciesname) %>%
summarize(m_dbh = round(mean(m_dbh),0),
###dmci: here we handle the calculation of the second spp
## specifically if spp2:n are not present in all plots
m_stemsha = as.integer(sum(stemsha)/n_plots),
m_height = round(mean(mean_height)),
m_basalha = round(mean(basalha),0)) -> comsubEstimates
zoo[["conf"]][["lenv"]][["message"]] <<- 'Sub-compartment estimates generated and uploaded...'
##write field measurements to the SQLite table
dbWriteTable(con, "comsubEstimates", comsubEstimates, append=TRUE)
# Set the result
#zoo[["outputs"]][["Result"]][["generated_file"]] <<- posthinDB
zoo[["outputs"]][["Result"]][["value"]] <<- posthinDB
#zoo[["outputs"]][["Result"]][["storage"]] <<- posthinDB
#cat('\n')
#cat('\n')
#cat('\n')
# Return SERVICE_SUCCEEDEED
return(zoo[["SERVICE_SUCCEEDEED"]])
##SELECT InitSpatialMetaData();
##SELECT AddGeometryColumn('plotLocation', 'Geometry', 4326, 'POINT', 2);
##UPDATE plotLocations SET Geometry=MakePoint(Long, Lat, 4326);
}