-
Notifications
You must be signed in to change notification settings - Fork 0
/
Transport Shiny.R
266 lines (221 loc) · 9.28 KB
/
Transport Shiny.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
library(shiny)
library(shinythemes)
library(shinycssloaders)
library(data.table)
library(tools)
library(openxlsx)
library(sf)
library(lubridate)
library(ggplot2)
library(plotly)
library(viridis)
library(parsedate)
library(sfnetworks)
ui <- fluidPage(
theme=shinytheme('cosmo'),
titlePanel("Fluvial Transport App"),
sidebarLayout(
sidebarPanel(
# Explanatory text using HTML
HTML("<p>This app calculates the distance moved for each subsequent survey date and the total distance moved in meters.<p>
<p>Store all data in one folder and enter the path to this folder. Do not include quotation marks in the path name.<p>
<p>Files may be a combination of .csv or .xlsx. A .shp centerline must be included as well.<p>
<p>Specify the coordinate reference system. The default is WGS84 UTM Zone 18N. Dates must be real.</p>
<p>After processing, results in the table can be downloaded. Processing times are long for many files.</p>"),
textInput("folder_path", "Enter Folder Path:", value = ""),
textInput("crs", "Enter CRS:", value = "32618"),
textInput("ID", "Enter ID Column", value= "Comment"),
textInput("X", "Enter X Coordinate Column", value="Easting"),
textInput("Y", "Enter Y Coordinate Column", value="Northing"),
textInput("Date", "Enter Date Column", value="GPSTime"),
actionButton("process_data", "Process Data"),
downloadButton("download_transport", "Download Transport Table") # Add download button
),
mainPanel(
tabsetPanel(
# Tab for displaying the output table
tabPanel("Output Table", withSpinner(tableOutput("table"))),
# Tab for displaying the plot
tabPanel("Plot", withSpinner(plotlyOutput("plot")))
)
)
)
)
server <- function(input, output) {
transport_data <- reactiveVal(NULL)
plotly_obj <- reactiveVal(NULL)
observeEvent(input$process_data, {
# Get user inputs
folder_path <- input$folder_path
crs_string <- input$crs
id_column <- input$ID
x_column <- input$X
y_column <- input$Y
date_column <- input$Date
# Ensure CRS is numeric value
crs_string = as.numeric(crs_string)
# Function to list files in folder
list_full = function(folder_path){
csv_files <<- list.files(folder_path, pattern='csv', full.names=T)
xlsx_files <<- list.files(folder_path, pattern='xlsx', full.names=T)
shapefiles <<- list.files(folder_path, pattern='shp', full.names=T)
}
#Call function to list files
list_full(folder_path)
# Function to read files in a folder
files = lapply(csv_files, fread)
Data = rbindlist(files,fill=T)
# Include Excel files
if (length(xlsx_files)>0) {
excel = lapply(xlsx_files, read.xlsx)
if(length(xlsx_files==1))
excel = do.call(rbind,excel)
excel = setDT(excel)
if (length(xlsx_files)>1) {
excel = rbindlist(excel)
Data = rbind(Data,excel)
}
}
# Test formatting
date_check = function(data) {
dates = as.character(substr(parse_date(data[,(strsplit(GPSTime,' '))]),start=1,stop=10))
pattern = "^\\d{4}-\\d{2}-\\d{2}$"
results = ifelse(sapply(dates, grepl, pattern=pattern), 0, 1)
if(c(1)%in%results){
stop("Error: Unable to interpret date format of at least one file.")}
data = data[,Date:=as.character(substr(parse_date(data[,(strsplit(GPSTime,' '))]),start=1,stop=10))]
}
date_check(Data)
# Read shapefile
centerline = read_sf(shapefiles[endsWith(shapefiles,'shp')])
if(nrow(centerline)>1){
if(st_geometry_type(centerline)!='LINESTRING'){print('A continuos centerline mustbe supplied. The current centerline is conposed of discontinous segments.')
stop()}
else{centerline = st_line_merge(line_sf$geometry)}
}
if(is.na(st_crs(centerline))){centerline = st_set_crs(centerline, crs_string)}
# Avoid variable call issues
Data = Data[,ID:=get(id_column)]
# Clean Data
Data = Data[,c('Date',..y_column,..x_column,'ID')]
# Determine repeat observations
Data = Data[,count:=.N,by='ID']
# Function to get Survey Dates from column with date and time
survey_dates = function (table) {
surveys <<- unique(table$Date)
}
# Call survey date function
survey_dates(Data)
# Create sf object of Data
Data = na.omit(Data)
sf_Data <<- st_as_sf(Data,coords = c(x_column,y_column),crs=crs_string)
if(st_crs(centerline)!=st_crs(sf_Data)){centerline = st_transform(centerline,crs_string)}
# Snap tracers to centerline
snap_point_to_line <- function(point, line) {
nearest_points <- st_nearest_points(point, line)
if (length(nearest_points) > 1) {
length_test = function(points) {
line_lengths = st_length(points)
}
shortest = which.min((lapply(nearest_points, length_test)))
nearest_points = nearest_points[shortest]
}
nearest_points = st_cast(nearest_points,'POINT')[seq(2, 2, 2)]
nearest_points_coordinates = st_coordinates(nearest_points)
return(nearest_points_coordinates)
}
snapped_points = data.table()
for (i in seq_along(sf_Data$geometry)) {
point = try({
snap_point_to_line(sf_Data[i,'geometry'], line = st_zm(centerline))
}, silent = TRUE)
snapped_points = rbind(snapped_points,point)
}
# lapply no longer works with sf object
#snapped_points <- lapply(sf_Data$geometry, snap_point_to_line, line = st_zm(centerline))
table = snapped_points
#table = do.call(rbind,snapped_points)
#table = as.data.table(table)
table = st_as_sf(table,coords=c('X','Y'),crs=crs_string)
# Add snapped_sf as a new column in the data.table
Data = Data[, Location:=table]
# Adjust table to output format
adjust_table = function(data) {
Movement = copy(data)
Movement = Movement[,c('ID','Date','Location','count')]
Movement = unique(Movement,by=c('ID','Date'))
Movement = dcast(Movement,formula = ID ~ Date, value.var = 'Location', fun.aggregate = NULL)
date_order = order(as.Date(surveys,format="%Y-%m-%d"))
surveys = surveys[date_order]
setcolorder(Movement,c('ID',surveys))
Movement <<- Movement
}
adjust_table(Data)
# Define a function to calculate distances
distance <- function(data) {
Transport <- data.table()
ID <- data[,"ID"]
data <- data[, !'ID', with = FALSE]
num_cols <- ncol(data)
for (i in 1:(num_cols - 1)) {
for (j in (i + 1):num_cols) {
col1 <- st_as_sf(data[,..i])
col2 <- st_as_sf(data[,..j])
new_col_name <- paste0(names(data)[i], "_", names(data)[j], "_Distance")
# Initialize an empty vector to store distances
distances = data.table()
# Calculate distances for all pairs of values in col1 and col2
for (k in 1:nrow(col1)) {
#Networking function
create_network = function(index){
network = as_sfnetwork(centerline, directed = FALSE)
point1 = st_as_sfc(col1[index,1])
point2 = st_as_sfc(col2[index,1])
suppressWarnings({network = st_network_blend(network,point1)
network = st_network_blend(network,point2)})
sublines = st_as_sf(activate(network,'edges'))
length = st_length(sublines[2,'geometry'])
return(length)
}
row = ifelse(st_is_empty(col1[k,1]) | st_is_empty(col2[k,1]), NA_real_,create_network(k))
distances = rbind(distances,row)
}
# Add the distances to the Transport table
Transport[, (new_col_name) := distances]
}
}
# Combine comment and Transport into a single table
Transport <<- cbind(ID, Transport)
}
distance(Movement)
Transport = Transport[,TotalDistance:=(do.call(pmax,c(Transport[,-1],na.rm=T)))]
transport_data(Transport)
# Create the ggplot object
river_map <- ggplot() +
geom_sf(data = centerline) +
geom_sf(data = sf_Data, aes(color = as.character(year(as.Date(Date))))) +
scale_color_viridis(discrete = TRUE, option = "D") +
labs(x = 'Easting (m)', y = 'Northing (m)', color = 'Year',title = 'Tracer Locations') +
theme_bw()+theme(axis.text.x = element_text(angle=45, vjust=1, hjust=1))
# Convert ggplot to a Plotly plot
plotly_obj(ggplotly(river_map, width = 800, height = 600))
})
# For rendering a table, you can use:
output$table <- renderTable({
transport_data()
})
# Render the Plotly plot
output$plot <- renderPlotly({
plotly_obj()
})
# Define a download handler for the download button
output$download_transport <- downloadHandler(
filename = function() {
paste("Transport_Table_", Sys.Date(), ".csv", sep = "")
},
content = function(file) {
write.csv(transport_data(), file)
}
)
}
shinyApp(ui, server)