inst/deploy_scripts/ASF_timeslider.R

library(svamap)
library(sp)

##
##Read in the point data
########################
pts <- read.csv2(file.path(Sys.getenv("path_to_t_disk"), "Falkenrapporter/E18-024 Grundrapport.csv"), encoding="UTF-8", stringsAsFactors = FALSE)

## Filter only those that are wild:
selection <-
  as.logical(pts[, "Djurh\u00E5llning"] == "Vilt (Jakt - fiske - natur)" &
               !is.na(pts[, "Djurh\u00E5llning"]))
pts <- pts[selection,]

## Mark points without coordinates
pts$xy <- 1
pts$xy[pts$Gisx==""] <- 0

## Assign random coordinates to missing coord points
loc <- readRDS(file.path(Sys.getenv("path_to_i_disk"), "ESS/ESS gemensamt/Projekt/Salmonella_katt/data/Locality_NUTS.RDS"))

## retreive coordinates and put in original data
pts$Gisx[pts$Gisx==""] <- loc$Y[match(pts$Kundort[pts$Gisx==""], loc$LOCALITY)]+sample(100:500, 1)
pts$Gisy[pts$Gisy==""] <- loc$X[match(pts$Kundort[pts$Gisy==""], loc$LOCALITY)]+sample(100:500, 1)
pts_temp <- pts
## Convert pts to S4
coordinates(pts_temp) <- cbind(as.numeric(pts_temp$Gisy), as.numeric(pts_temp$Gisx))

proj_3021 = "+proj=tmerc +lat_0=0 +lon_0=15.80827777777778 +k=1 +x_0=1500000 +y_0=0 +ellps=bessel +units=m +no_defs"
proj_4326 = "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"

proj4string(pts_temp)<- CRS(proj_3021)
pts_temp <- spTransform(pts_temp, CRSobj = proj_4326)
coordinates(pts) <- round(coordinates(pts_temp), 4)

## Define the text to go in the popup for the point data
########################
## pts$popup_text <- paste0("Ankomstdatum: ", as.Date(pts$Ankomstdatum),
##                          "<br>Djuridentitet: ", pts@data$Djuridentitet,
##                          "<br>Provtagningsorsak Namn: ", pts@data$Provtagningsorsak.Namn)

## Drop data that you don't want to display from points
########################
pts@data <- data.frame(result = pts@data$Status..numerisk.,
                       Date = as.Date(pts@data$Ankomstdatum),
                       Djuridentitet = pts@data$Djuridentitet,
                       Namn = pts@data$Provtagningsorsak.Namn,
                       Coordinates = pts@data$xy, stringsAsFactors = FALSE)

## Read wildboar density
########################
load(file.path(Sys.getenv("path_to_i_disk"), "ESS/EPIZ/Sjukdomar/CSF ASF/WildboarDensity/2017WildboarDensity_simple.RData"))

## Read ADSN data
########################
adns <- read.csv2(file.path(Sys.getenv("path_to_i_disk"), "ESS/EPIZ/Sjukdomar/CSF ASF/WildboarDensity/Wildboar_ADNS.csv"), header=TRUE, encoding = "latin1")

adns <- adns[adns$Country != "03 ITALY",]

adns$Date <- as.Date(adns$Confirmation.date, format = "%d-%m-%Y")

# filter disease
adns <- adns[adns$Disease == "12 African swine fever in wild boar",]

adns <- adns[order(as.Date(adns$Date)),]

# concatenate coordinates
coordinates(adns) <- round(cbind(adns$Longitude.x.+ adns$Longitude.y./60 +adns$Longitude.z../3600,
                                 adns$Latitude.x.+ adns$Latitude.y./60 +adns$Latitude.z../3600), 4)

proj4string(adns)<- CRS(proj_4326)
adns@data <- data.frame(Country = adns@data$Country,
                        Date = adns@data$Date)

## Table for barchart
#######################
pivot_adns <- adns@data
st <- as.Date("2014-01-01")
en <- as.Date(Sys.time())
pivot_adns$month <- factor(format(pivot_adns$Date, "%Y-%m"),
                           levels = unique(format(seq(en, st, by = "-1 day"), "%Y-%m")))
pivot_adns$Country <- substr(as.character(pivot_adns$Country), 4, nchar(as.character(pivot_adns$Country)))
pivot_adns$Country <- tools::toTitleCase(tolower(pivot_adns$Country))
pivot_adns <- as.data.frame.matrix(table(pivot_adns$month, pivot_adns$Country))
pivot_adns$date <- rownames(pivot_adns)

## Can be removed when we add sorting in js
pivot_adns <- pivot_adns[order(pivot_adns$date),]

coordinates(pivot_adns) <- cbind(rep(0, nrow(pivot_adns)), rep(0, nrow(pivot_adns)))

##Drop columns that we don't use
adns@data <- data.frame(Date = adns@data$Date)
##Write data to geojson
########################
path_to_data <- write_data(list(pts, Wildboar_density, adns, pivot_adns))
##
## Deploy map
path <- "deploy_pages/asf_timeslider"
dir.create(path, showWarnings = FALSE, recursive = TRUE)

write_page(data = path_to_data,
                   path = path,
                   template = "ASF_timeslider/map.html",
                   overwrite = TRUE,
                   browse = FALSE)
file.copy(system.file("assets/images", package = "svamap"),
          file.path(path, "map"),
          recursive = TRUE)
SVA-SE/svamap documentation built on Sept. 25, 2020, 3:53 p.m.