Я пытаюсь создать веб-приложение, используя блестящий R. Веб-приложение должно принимать два изображения, введенных пользователем, выполнять некоторые функции и отображать выходные данные для загрузки.
Я хотел бы реализовать функцию vwf
и функцию mcws
из пакета ForestTools
для получения пространственных файлов, которые могут быть загружены пользователем. Это приложение будет работать на локальном компьютере.
Упомянутые здесь DSM и DEM - это модель Digital Surface и Digital Elevation.
Вот код, который я реализовал в R.
library(raster)
library(ForestTools)
#data input
dsm<- raster("dsm.img")
dem<- raster("dem.img")
#canopy height model generation
chm<- dsm - dem
plot(chm)
lin <- function(x){x * 0.05 + 0.6} #linear function to define the window size
#variable window filter function to define treetops (predefined)
ttops <- vwf(CHM = chm, winFun = lin, minHeight = 3)
summary(ttops) #Chech the minimum, mean and maximum tree height
plot(ttops)
#raster format for delineating crown area.
crwn_poly<- mcws(treetops = ttops, CHM = chm, format = "polygons", minHeight = 1.5, verbose = FALSE) #vector polygons for crown boundaries.
#Specify the path for the file location (could be any local folder)
writeOGR(crownsPoly, dsn = "specify path", layer = 'poly', driver = "ESRI Shapefile")
writeOGR(ttops, dsn = "ttops", layer = 'point', driver = "ESRI Shapefile")
Shiny R код:
library(shiny)
library(ForestTools)
ui<- fluidPage(
sidebarPanel(
#Adding two input image files for DSM and DEM
fileInput(inputId = "file1",
label = "Upload DSM",
accept = c('image/png', 'image/jpeg','image/jpg', 'image/tif') #Input image 1 DSM
),
fileInput(inputId = "file2",
label = "Upload DEM",
accept = c('image/png', 'image/jpeg','image/jpg') #input image 2 DEM
),
#Input value for minumum height to delineate the trees
numericInput("Tree_Ht", "Minimum Treetop height:",3, min = 1, max = 20 ), #minimum tree height set by the user
actionButton(inputId = "Input_action", label = "Submit") #submit button to start processing
#a download function is also needed to download these shapefiles. Need to work on it as well.
#I am looking through it. But you can add in your input as well.
),
#Two output files preferably a plot of Tree height and Crown width
#Third output CHM generation
mainPanel(
plotOutput("Plot1"), #CHM
plotOutput("Plot2"), #Tree height
plotOutput("Plot3") #Crown polygons
)
)
subs<- function(x1,x2){x1-x2} #Functoin for subtracting DSM and DEM
lin <- function(x){x * 0.05 + 0.6} #function for treetops
server <- function(input, output,session){
observeEvent(input$file1, {
DSM<- input$file1
if (is.null(DSM))
return()
file.copy(DSM$datapath, file.path("c:/temp", DSM$name) )
})
observeEvent(input$file2, {
DEM<- input$file2
if (is.null(DEM))
return()
file.copy(DEM$datapath, file.path("c:/temp", DEM$name) )
})
library(raster)
library(ForestTools)
#Generate CHM
chm<- eventReactive(input$input_action{
subs(DSM,DEM)
})
output$Plot1<-renderPlot({ #Plot 1 CHM
plot(chm)
})
#Treetops marking function using variable window filter (VWF)
trtops<- eventReactive({
ttops <- vwf(CHM = chm, winFun = lin, minHeight = 2)
})
output$Plot2<-renderPlot({ #plot 2 Treetops
plot(chm, xlab = "", ylab = "", xaxt='n', yaxt = 'n')
plot(ttops, col = "blue", pch = 20, cex = 0.5, add = TRUE)
})
crown<- eventReactive({
cwns <- mcws(treetops = ttops, CHM = chm, minHeight = input$Tree_Ht , verbose = FALSE)
})
output$Plot3<-renderPlot({ #plot 3 Crown map
plot(chm, xlab = "", ylab = "", xaxt='n', yaxt = 'n')
plot(cwns, col = "blue", pch = 20, cex = 0.5, add = TRUE)
})
}
shinyApp(server = server, ui = ui)