Skip to content

Commit a640df5

Browse files
author
Doug Leasure
committed
Merge branch 'dev'
2 parents b5e88fb + 5825a47 commit a640df5

26 files changed

Lines changed: 243 additions & 125 deletions

DESCRIPTION

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
Package: wopr
22
Title: An R Package to access the WorldPop Open Population Repository (WOPR)
33
Description: Provides access to the WorldPop Open Population Repository (WOPR) data catalogue and population estimates for specific locations via REST API.
4-
Version: 0.3.2
5-
Date: 2020-05-27
4+
Version: 0.3.3
5+
Date: 2020-06-11
66
Author: Douglas R Leasure, Maksym Bondarenko, Andrew J Tatem
77
Maintainer: Doug Leasure <D.R.Leasure@soton.ac.uk>
88
License: GNU GPLv3

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export(map)
1414
export(mapProxyFile)
1515
export(mapProxyMarker)
1616
export(mapProxyPoly)
17+
export(parseFilename)
1718
export(plotPanel)
1819
export(plotPop)
1920
export(plotPyramid)

R/checkLocal.R

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,32 @@
66

77
checkLocal <- function(dir, info=wopr:::woprVision_global$version_info){
88

9-
info$local_sql <- info$local_tiles <- FALSE
9+
info$local_sql <- info$local_tiles <- info$local_mastergrid <- FALSE
1010

1111
if(dir.exists(dir)){
1212
for(i in 1:nrow(info)){
1313

1414
path <- file.path(dir,info$country[i],'population',info$version[i])
1515

16-
prefix <- paste0(info$country[i],
17-
'_population_',
18-
gsub('.','_',as.character(info$version[i]), fixed=T),'_')
19-
20-
sql_path <- paste0(file.path(path, prefix),'sql.sql')
21-
mastergrid_path <- paste0(file.path(path,prefix),'mastergrid.tif')
22-
tile_path <- paste0(file.path(path, prefix), 'tiles')
23-
24-
if(file.exists(sql_path) & file.exists(mastergrid_path)) {
25-
info[i,'local_sql'] <- TRUE}
26-
if(dir.exists(tile_path)){
27-
info[i,'local_tiles'] <- TRUE}
16+
if(dir.exists(path)){
17+
18+
parsed <- parseFilename(list.files(path))
19+
20+
if(any(parsed$file_type=='sql') & any(parsed$file_type=='mastergrid')){
21+
info[i,'local_sql'] <- TRUE
22+
info[i,'local_sql_path'] <- file.path(path, parsed[which(parsed$file_type=='sql'),'filename'])
23+
info[i,'local_mastergrid_path'] <- file.path(path, parsed[which(parsed$file_type=='mastergrid'),'filename'])
24+
}
25+
26+
if(any(parsed$file_type=='tiles' & parsed$file_extension=='')){
27+
info[i,'local_tiles'] <- TRUE
28+
info[i,'local_tiles_path'] <- file.path(path, parsed[which(parsed$file_type=='tiles' & parsed$file_extension==''),'filename'])
29+
} else if(any(parsed$file_type=='tiles' & parsed$file_extension=='zip')){
30+
warning(paste('Tiles for',info$country[i],info$version[i],'still need to be unzipped.'), call.=F)
31+
}
32+
}
2833
}
2934
}
35+
3036
return(info)
31-
}
37+
}

R/map.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ map <- function(country, version, local_tiles=F, southern=F,
5050
addLegend(position='bottomright',
5151
pal=pal,
5252
values=bins,
53-
title='People per grid cell',
53+
title='People per grid cell<br>(all age-sex groups)',
5454
opacity=1,
5555
group='Population') %>%
5656

R/parseFilename.R

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#' Parse WOPR file names
2+
#' @description Parse WOPR file names to return the country, category, version, file type, optional info, and the file extension.
3+
#' @param filenames character vector. WOPR file name.
4+
#' @return data.frame. Table of results.
5+
#' @export
6+
7+
parseFilename <- function(filenames){
8+
9+
if(length(filenames)==0) {
10+
result <- NA
11+
} else {
12+
result <- data.frame(filename=filenames,
13+
country = NA,
14+
category = NA,
15+
version_major = NA,
16+
version_minor = NA,
17+
version_patch = NA,
18+
file_type = NA,
19+
file_optional = NA,
20+
file_extension = NA
21+
)
22+
23+
24+
testNumeric <- function(y){
25+
!is.na(suppressWarnings(as.numeric(y)))
26+
}
27+
28+
for(i in 1:nrow(result)){
29+
30+
x_split <- unlist(strsplit(filenames[i], '_'))
31+
32+
result[i,'country'] <- x_split[1]
33+
result[i,'category'] <- x_split[2]
34+
result[i,'version_major'] <- as.numeric(gsub('v','',unlist(strsplit(x_split[3], '-'))[1]))
35+
36+
# version format x-y-z
37+
if(grepl('-',x_split[3])){
38+
x3_split <- unlist(strsplit(x_split[3], '-'))
39+
result[i,'version_minor'] <- x3_split[2]
40+
if(length(x3_split) > 2){
41+
result[i,'version_patch'] <- x3_split[3]
42+
} else {
43+
result[i,'version_patch'] <- 0
44+
}
45+
46+
} else {
47+
result[i,'version_minor'] <- as.numeric(tools::file_path_sans_ext(x_split[4]))
48+
if(testNumeric(x_split[5])){
49+
result[i,'version_patch'] <- as.numeric(x_split[5])
50+
} else {
51+
result[i,'version_patch'] <- 0
52+
}
53+
}
54+
55+
n <- length(x_split)
56+
if(testNumeric(tools::file_path_sans_ext(x_split[n]))){
57+
result[i,'file_extension'] <- tools::file_ext(x_split[n])
58+
} else if(n==4 | testNumeric(x_split[n-1])){
59+
result[i,'file_type'] <- tools::file_path_sans_ext(x_split[n])
60+
result[i,'file_extension'] <- tools::file_ext(x_split[n])
61+
} else {
62+
result[i,'file_type'] <- x_split[n-1]
63+
result[i,'file_optional'] <- tools::file_path_sans_ext(x_split[n])
64+
result[i,'file_extension'] <- tools::file_ext(x_split[n])
65+
}
66+
}
67+
}
68+
69+
return(result)
70+
}

R/plotPop.R

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@
88

99
plotPop <- function(N, confidence=95, tails='Interval', popthresh=100){
1010

11-
s <- summaryPop(N=N,
12-
confidence=confidence/100,
13-
tails=ifelse(tails=='Interval',2,1),
14-
abovethresh=popthresh)
11+
s <- summaryPop(N = N,
12+
confidence = confidence/100,
13+
tails = ifelse(tails=='Interval',2,1),
14+
abovethresh = popthresh,
15+
round_result = FALSE)
1516

1617
# plot margins
1718
par(mar=c(4.5,1,4.5,0.5))
@@ -50,8 +51,19 @@ plotPop <- function(N, confidence=95, tails='Interval', popthresh=100){
5051
s$lower <- 0
5152
}
5253

53-
ilow <- min(which(d$x >= s$lower))
54-
iup <- max(which(d$x <= s$upper))
54+
# ilow <- min(which(d$x >= s$lower))
55+
# iup <- max(which(d$x <= s$upper))
56+
57+
if(any(d$x >= s$lower)){
58+
ilow <- min(which(d$x >= s$lower))
59+
} else{
60+
ilow <- 1
61+
}
62+
if(any(d$x <= s$upper)){
63+
iup <- max(which(d$x <= s$upper))
64+
} else{
65+
iup <- nrow(d)
66+
}
5567

5668
dsub <- d[ilow:iup,]
5769
dsub <- rbind(data.frame(x=rep(s$lower,2),
@@ -94,34 +106,40 @@ plotPop <- function(N, confidence=95, tails='Interval', popthresh=100){
94106

95107
if(is.numeric(N)){
96108

109+
# data
110+
pop_mean <- ifelse(s$mean > 5, round(s$mean), round(s$mean,1))
111+
pop_lower <- ifelse(s$mean > 5, round(s$lower), round(s$lower,1))
112+
pop_upper <- ifelse(s$mean > 5, round(s$upper), round(s$upper,1))
113+
pop_abovethresh <- round(s$abovethresh*100)
114+
97115
# Main title
98116
line <- 3
99-
mtext(paste0('Population Estimate: ',prettyNum(s$mean,big.mark=','),' people'),
117+
mtext(paste0('Population Estimate: ',prettyNum(pop_mean,big.mark=','),' people'),
100118
line=line, cex=1.5)
101119

102120
# Sub title A
103121
line <- 1.5
104122
cex <- 1.25
105123
if(tails=='Interval'){
106124

107-
mtext(paste0(round(confidence),'% probability: ', prettyNum(s$lower,big.mark=','),' - ', prettyNum(s$upper, big.mark=','),' people'),
125+
mtext(paste0(round(confidence),'% probability: ', prettyNum(pop_lower,big.mark=','),' - ', prettyNum(pop_upper, big.mark=','),' people'),
108126
line=line, cex=cex)
109127

110128
} else if(tails=='Lower Limit'){
111129

112-
mtext(paste0(round(confidence),'% probability: > ', prettyNum(s$lower,big.mark=','), ' people'),
130+
mtext(paste0(round(confidence),'% probability: > ', prettyNum(pop_lower,big.mark=','), ' people'),
113131
line=line, cex=cex)
114132

115133
} else if(tails=='Upper Limit'){
116134

117-
mtext(paste0(round(confidence),'% probability: < ', prettyNum(s$upper,big.mark=','),' people'),
135+
mtext(paste0(round(confidence),'% probability: < ', prettyNum(pop_upper,big.mark=','),' people'),
118136
line=line, cex=cex)
119137
}
120138

121139
# Sub title B
122140
if(is.numeric(popthresh)){
123141
line <- 0.25
124-
mtext(paste0(round(s$abovethresh*100),'% probability: > ', prettyNum(popthresh,big.mark=','), ' people (threshold)'),
142+
mtext(paste0(pop_abovethresh,'% probability: > ', prettyNum(popthresh,big.mark=','), ' people (threshold)'),
125143
line=line, cex=cex)
126144
}
127145
}

R/resultTable.R

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,11 @@
66

77
resultTable <- function(inp, rval){
88

9-
if(inp$pointpoly=='Upload File'){
10-
woprized <- T
11-
s <- sf::st_drop_geometry(rval$feature)
12-
} else {
13-
woprized <- F
14-
s <- summaryPop(N=rval$N,
15-
confidence=inp$ci_level/100,
16-
tails=ifelse(inp$ci_type=='Interval',2,1),
17-
abovethresh=inp$popthresh)
18-
}
9+
s <- summaryPop(N = rval$N,
10+
confidence = inp$ci_level/100,
11+
tails = ifelse(inp$ci_type=='Interval',2,1),
12+
abovethresh = inp$popthresh,
13+
round_result = FALSE)
1914

2015
names.result <- c('name','data','mode',
2116
'pop','pop_lower','pop_upper','abovethresh','popthresh',
@@ -24,12 +19,8 @@ resultTable <- function(inp, rval){
2419
result <- data.frame(matrix(NA, nrow=nrow(s), ncol=length(names.result)))
2520
names(result) <- names.result
2621

27-
if(woprized){
28-
result$name <- paste0('location_',1:nrow(result))
29-
} else {
30-
result$name <- inp$save_name
31-
}
32-
22+
result$name <- inp$save_name
23+
3324
result$data <- inp$data_select
3425
result$mode <- inp$pointpoly
3526
result$popthresh <- inp$popthresh
@@ -78,17 +69,6 @@ resultTable <- function(inp, rval){
7869
result$male_age <- ''
7970
}
8071

81-
if(inp$ci_type=='Interval'){
82-
result$pop_lower <- s$lower
83-
result$pop_upper <- s$upper
84-
} else if(inp$ci_type=='Upper Limit'){
85-
result$pop_lower <- NA
86-
result$pop_upper <- s$upper
87-
} else if(inp$ci_type=='Lower Limit'){
88-
result$pop_lower <- s$lower
89-
result$pop_upper <- NA
90-
}
91-
9272
for(i in 1:nrow(result)){
9373
result[i,'pop'] <- s[i,'mean']
9474
result[i,'pop_lower'] <- s[i,'lower']
@@ -97,9 +77,21 @@ resultTable <- function(inp, rval){
9777

9878
result[i,'geojson'] <- as.character(geojsonio::geojson_json(rval$feature[i,]))
9979
}
100-
result$pop <- as.integer(round(result$pop))
101-
result$pop_lower <- as.integer(round(result$pop_lower))
102-
result$pop_upper <- as.integer(round(result$pop_upper))
10380

81+
if(inp$ci_type=='Upper Limit'){
82+
result$pop_lower <- NA
83+
} else if(inp$ci_type=='Lower Limit'){
84+
result$pop_upper <- NA
85+
}
86+
87+
result$pop <- ifelse(s$mean > 5,
88+
as.integer(round(result$pop)),
89+
round(result$pop,3))
90+
result$pop_lower <- ifelse(s$mean > 5,
91+
as.integer(round(result$pop_lower)),
92+
round(result$pop_lower,3))
93+
result$pop_upper <- ifelse(s$mean > 5,
94+
as.integer(round(result$pop_upper)),
95+
round(result$pop_upper,3))
10496
return(result)
10597
}

R/summaryPop.R

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
#' Summary statistics
22
#' @description Summarize predicted posterior probability distribution for population estimates
3-
#' @param N Vector of posterior samples for the population total
4-
#' @param confidence The confidence level for the confidence intervals (e.g. 0.95 = 95 percent confidence intervals)
5-
#' @param tails The number of tails for the confidence intervals
6-
#' @param belowthresh The function will return the probability that the population size is less than _belowthresh_
7-
#' @param abovethresh The function will return the probability that the population size exceeds _abovethresh_
3+
#' @param N numeric vector. Vector of posterior samples for the population total
4+
#' @param confidence numeric. The confidence level for the confidence intervals (e.g. 0.95 = 95 percent confidence intervals)
5+
#' @param tails integer. The number of tails for the confidence intervals
6+
#' @param belowthresh numeric. The function will return the probability that the population size is less than _belowthresh_
7+
#' @param abovethresh numeric. The function will return the probability that the population size exceeds _abovethresh_
8+
#' @param round_result logical. If TRUE the results will be rounded to a pre-determined number of digits for each column.
89
#' @return A data.frame with columns containing the mean, median, lower and upper confidence intervals for the estimated population total.
910
#' The 'abovethresh' column reports the probability that the population is greater than _abovethresh_.
1011
#' The 'belowthresh' column reports the probability that the population is less than _abovethresh_.
1112
#' Note: One minus 'abovethresh' is the probability that the population is equal to or less than _abovethresh_.
1213
#' @export
1314

14-
summaryPop <- function(N, confidence=0.95, tails=2, belowthresh=NA, abovethresh=NA){
15+
summaryPop <- function(N, confidence=0.95, tails=2, belowthresh=NA, abovethresh=NA, round_result=F){
1516

1617
N <- as.numeric(N)
1718

@@ -29,8 +30,10 @@ summaryPop <- function(N, confidence=0.95, tails=2, belowthresh=NA, abovethresh=
2930
round0_cols <- c('mean','median','lower','upper')
3031
round3_cols <- c('abovethresh', 'belowthresh')
3132

32-
result[,round0_cols] <- as.integer(round(result[,round0_cols]))
33-
result[,round3_cols] <- round(result[,round3_cols], 3)
33+
if(round_result){
34+
result[,round0_cols] <- as.integer(round(result[,round0_cols]))
35+
result[,round3_cols] <- round(result[,round3_cols], 3)
36+
}
3437

3538
return(result)
3639
}

R/sysdata.rda

1.97 KB
Binary file not shown.

R/writeCatalogue.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#' Write WOPR catalogue
22
#' @description Creates a catalogue of the WOPR data currently in the output directory.
3-
#' @param outdir Output folder where data from the WorldPop GridFree catalogue are stored
3+
#' @param outdir Output folder where data from the WOPR catalogue are stored
44
#' @return Writes an updated data catalogue to disk as a .csv file.
55
#' @export
66

0 commit comments

Comments
 (0)