Skip to content

Commit

Permalink
Merge pull request #110 from AgrDataSci/devel
Browse files Browse the repository at this point in the history
fix-map-path
  • Loading branch information
kauedesousa authored May 31, 2023
2 parents 2843b65 + e0d0ed0 commit 65a9f95
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 66 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ dev
reportcm
run-local/local
tests
tempmap
8 changes: 6 additions & 2 deletions ClimMob.R
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ try_data = tryCatch({
tidynames = FALSE,
pivot.wider = TRUE)

rank_dat = organize_ranking_data(cmdatajson,
rank_dat = organize_ranking_data(cmdata = cmdatajson,
pars,
groups,
option_label = option,
Expand Down Expand Up @@ -97,7 +97,7 @@ if (any_error(try_data)) {
# 2. Organise quantitative data ####
try_quanti_data = tryCatch({

quanti_dat = organize_quantitative_data(cmdatajson,
quanti_dat = organize_quantitative_data(cmdata = cmdatajson,
pars,
groups = groups,
id = "id",
Expand Down Expand Up @@ -427,6 +427,10 @@ if (isTRUE(quanti_dat$quantitative)) {

}

if (isTRUE(trial_map$geoTRUE)) {
unlink(trial_map$mapDIR, recursive = TRUE)
}

# End of analysis


2 changes: 1 addition & 1 deletion modules/00_check_packages.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pkg <- c('caret', 'climatrends', 'ClimMobTools', 'ggparty', 'ggplot2', 'gosset',
'gridExtra', 'gtools', 'igraph', 'janitor', 'jsonlite', 'knitr', 'leaflet',
'mapview', 'multcompView', 'nasapower', 'partykit', 'patchwork',
'PlackettLuce', 'plotrix', 'pls', 'png', 'psychotools', 'qvcalc',
'remotes', 'rmarkdown')
'remotes', 'rmarkdown', 'lubridate')

install.packages(pkg)

Expand Down
1 change: 1 addition & 0 deletions modules/01_functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ library("plotrix")
library("gridExtra")
library("caret")
library("janitor")
library("lubridate")

#'Get colour pallet
#' @param x an integer
Expand Down
69 changes: 38 additions & 31 deletions modules/05_spatial_overview.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,43 +30,49 @@ get_testing_sites_map = function(cmdata, output_path, backward_path){

geoTRUE = all(any(lon), any(lat))

if (isTRUE(geoTRUE)) {

# find the vector with most completeness
less_nas = lapply(cmdata[lon], function(x){
sum(is.na(x))
})

lon = names(which.min(unlist(less_nas)))
lat = gsub("_longitude", "_latitude", lon)

lonlat = cmdata[, c(lon,lat)]

lonlat[1:2] = lapply(lonlat[1:2], as.numeric)
if (isFALSE(geoTRUE)) {
return(list(geoTRUE = FALSE))
}

# find the vector with most completeness
less_nas = lapply(cmdata[lon], function(x){
sum(is.na(x))
})

lon = names(which.min(unlist(less_nas)))
lat = gsub("_longitude", "_latitude", lon)

lonlat = cmdata[, c(lon,lat)]

lonlat[1:2] = lapply(lonlat[1:2], as.numeric)

lonlat[,1] = ifelse(lonlat[,1] > 180 | lonlat[,1] < -180, NA, lonlat[,1])

lonlat[,2] = ifelse(lonlat[,2] > 70 | lonlat[,2] < -70, NA, lonlat[,2])

nlonlat = dim(lonlat)[[1]]

if (nlonlat > 0) {

lonlat[,1] = ifelse(lonlat[,1] > 180 | lonlat[,1] < -180, NA, lonlat[,1])
trial_map = plot_map(data = lonlat,
make.clusters = FALSE,
xy = c(1, 2),
minimap = TRUE,
map_provider = "OpenStreetMap.Mapnik")

lonlat[,2] = ifelse(lonlat[,2] > 70 | lonlat[,2] < -70, NA, lonlat[,2])
tempmap = paste0(getwd(), "/tempmap/")

nlonlat = dim(lonlat)[[1]]
dir.create(tempmap, recursive = TRUE, showWarnings = FALSE)

if (nlonlat > 0) {

trial_map = plot_map(data = lonlat,
make.clusters = FALSE,
xy = c(1, 2),
minimap = TRUE,
map_provider = "OpenStreetMap.Mapnik")

mapview::mapshot(trial_map,
url = paste0(output_path, "/trial_map.html"),
file = paste0(output_path, "/trial_map.png"))

}
mapview::mapshot(trial_map,
url = paste0(tempmap, "/trial_map.html"),
file = paste0(tempmap, "/trial_map.png"))

}

result = list(map_path = paste0(output_path, "trial_map.png"),
result = list(geoTRUE = TRUE,
mapDIR = tempmap,
map_path = paste0(tempmap, "/trial_map.png"),
map = trial_map,
coords = lonlat)

Expand All @@ -75,7 +81,8 @@ get_testing_sites_map = function(cmdata, output_path, backward_path){
# .......................................
# Error in data
# this is a file that is generated to be used in case of errors
error_data_trial_map = list(map_path = "",
error_data_trial_map = list(geoTRUE = FALSE,
map_path = "",
map = 0L,
coords = data.frame())

4 changes: 3 additions & 1 deletion modules/09_agroclimatic_insights.R
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ get_agroclimatic_data = function(cmdata,

dates = cmdata[dates[1]]

dates = climatrends:::.coerce2Date(dates[,1])
dates = lubridate::as_date(dates[,1])

rain = rainfall(coords[noNAs, ],
day.one = dates[noNAs],
Expand Down Expand Up @@ -150,6 +150,8 @@ get_agroclimatic_data = function(cmdata,
rainfall = rain1,
temperature = temp1,
dates = dates,
window = c(min(dates, na.rm = TRUE),
max(dates, na.rm = TRUE) + ndays),
noNAs = noNAs,
date_used = date_used)

Expand Down
Loading

0 comments on commit 65a9f95

Please sign in to comment.