diff --git a/R/spatial_qaqc.R b/R/spatial_qaqc.R index 1f6c1374..40389bbf 100644 --- a/R/spatial_qaqc.R +++ b/R/spatial_qaqc.R @@ -231,7 +231,7 @@ spatial_qaqc <- function(dat, project, spat, lon.dat, lat.dat, lon.spat = NULL, } # points on land ---- - + sf_use_s2(FALSE) # by default 'sf' package enables 's2' but this sometimes generates errors with st_intersects land_pts <- sf::st_intersects(dat_sf, base_map) obs_on_land <- lengths(land_pts) > 0 @@ -263,7 +263,6 @@ spatial_qaqc <- function(dat, project, spat, lon.dat, lat.dat, lon.spat = NULL, } # points outside zone ---- - pts_int <- sf::st_intersects(dat_sf, spatdat) obs_outside <- lengths(pts_int) == 0 @@ -355,24 +354,25 @@ spatial_qaqc <- function(dat, project, spat, lon.dat, lat.dat, lon.spat = NULL, fishset_theme() # Spatial summary table ---- - - if (any(!is.null(out_col), !is.null(land_col), !is.null(bound_col))) { - - spat_tab <- agg_helper(dataset, value = c(expected_col, out_col, land_col, bound_col), - group = c("YEAR", group), fun = sum) - - year_tab <- agg_helper(dataset, value = "YEAR", group = group, count = TRUE, fun = "percent") - - spat_tab <- dplyr::left_join(spat_tab, year_tab, by = "YEAR") - - last_cols <- names(spat_tab)[!names(spat_tab) %in% c("YEAR", "n", group)] - - spat_tab <- spat_tab[order(spat_tab$YEAR), - c("YEAR", "n", group, last_cols)] - } + spat_tab <- agg_helper(dataset, value = c(expected_col, out_col, land_col, bound_col), + group = c("YEAR", group), fun = sum) + + year_tab <- agg_helper(dataset, value = "YEAR", group = group, count = TRUE, fun = "percent") + + spat_tab <- dplyr::left_join(spat_tab, year_tab, by = "YEAR") + + last_cols <- names(spat_tab)[!names(spat_tab) %in% c("YEAR", "n", group)] + + spat_tab <- spat_tab[order(spat_tab$YEAR), + c("YEAR", "n", group, last_cols)] # distance from nearest zone (meters) ---- - if (sum(obs_outside) > 0) { + if(p_expected < 50) { + showNotification("Over 50% of the observations are outside of the study location and/or on land. Check for erros in latitude and longitude values in the primary data table.", + type = "error", + duration = 60) + + } else if(sum(obs_outside) > 0) { nearest <- sf::st_nearest_feature(dat_sf[obs_outside, ], spatdat) dist.rec <- sf::st_distance(dat_sf[obs_outside, ], spatdat[nearest, ], @@ -427,7 +427,7 @@ spatial_qaqc <- function(dat, project, spat, lon.dat, lat.dat, lon.spat = NULL, if (sum(obs_on_bound, obs_outside) == 0) { - cat(c("All observations occur on land and within regulatory zones.", + cat(c("All observations occur within regulatory zones.", "No observations fall on zone boundaries."), file = tmp) } diff --git a/inst/ShinyFiles/MainApp/server.R b/inst/ShinyFiles/MainApp/server.R index 2e211ab1..b63a7f1e 100644 --- a/inst/ShinyFiles/MainApp/server.R +++ b/inst/ShinyFiles/MainApp/server.R @@ -3343,8 +3343,8 @@ server = function(input, output, session) { if (!is_value_empty(out)) { - flag_nms <- c("ON_LAND", "OUTSIDE_ZONE", "ON_ZONE_BOUNDARY") - spat_qaqc_r$flag <- vapply(flag_nms, function(x) x %in% names(out$dataset), logical(1)) + flag_nms <- c("ON_LAND", "OUTSIDE_ZONE", "ON_ZONE_BOUNDARY", "EXPECTED_LOC") + spat_qaqc_r$flag <- flag_nms %in% names(out$dataset) spat_qaqc$out_df <- subset(out$dataset, select=-c(YEAR)) # remove 'YEAR' from table out$dataset <- NULL @@ -3522,20 +3522,21 @@ server = function(input, output, session) { if (input$select_spat_tab == "out_zone") { - if (sum(dist_filter()) > 0) spat_qaqc$out_df[dist_filter(), c(input$spat_qaqc_ID, input$spat_qaqc_date, input$spat_qaqc_lat, - input$spat_qaqc_lon, "ON_LAND", "ON_ZONE_BOUNDARY", "EXPECTED_LOC")] - } else { # "all" + if (sum(dist_filter()) > 0){ + spat_qaqc$out_df[dist_filter(), c(input$spat_qaqc_ID, input$spat_qaqc_date, input$spat_qaqc_lat, + input$spat_qaqc_lon, "ON_LAND", "OUTSIDE_ZONE", "ON_ZONE_BOUNDARY", "EXPECTED_LOC")] + } - new_cols <- c("ON_LAND", "ON_ZONE_BOUNDARY", "EXPECTED_LOC") + } else { # "all" + new_cols <- c("ON_LAND", "OUTSIDE_ZONE", "ON_ZONE_BOUNDARY", "EXPECTED_LOC") new_cols <- new_cols[which(spat_qaqc_r$flag)] spat_qaqc$out_df[,c(input$spat_qaqc_ID, input$spat_qaqc_lat, - input$spat_qaqc_lon, new_cols)] + input$spat_qaqc_lon, new_cols)] } } }) - observe({ spat_qaqc_r$c_tab <- c_tab() }) @@ -3586,7 +3587,6 @@ server = function(input, output, session) { observeEvent(input$dist_remove_bttn, { # add pop-up confirming removal - showModal( modalDialog(title = paste("Remove", sum(dist_filter()), "rows?"), @@ -3662,19 +3662,13 @@ server = function(input, output, session) { q_test <- quietly_test(degree) - if (input$select_spat_tab == "out_zone") { - - values$dataset[dist_filter(), ] <- - q_test(c_tab(), project = project$name, lat = input$spat_qaqc_lat, - lon = input$spat_qaqc_lon, latsign = input$spat_filter_lat, - lonsign = input$spat_filter_lon, replace = TRUE) - } else { + values$dataset <- + q_test(values$dataset, project = project$name, lat = input$spat_qaqc_lat, + lon = input$spat_qaqc_lon, latsign = input$spat_filter_lat, + lonsign = input$spat_filter_lon, replace = TRUE) - values$dataset <- - q_test(c_tab(), project = project$name, lat = input$spat_qaqc_lat, - lon = input$spat_qaqc_lon, latsign = input$spat_filter_lat, - lonsign = input$spat_filter_lon, replace = TRUE) - } + # Reassign reactive to update table + spat_qaqc$out_df[,c(input$spat_qaqc_lat,lon = input$spat_qaqc_lon)] <- values$dataset[,c(input$spat_qaqc_lat,lon = input$spat_qaqc_lon)] showNotification("Spatial corrections completed", type = "message", duration = 60) })