For the cities we do not implement the pair-wise approach, because we assume travel costs will be relatively low anyways.
The function oversample_wrapper_non_pair
returns a SpatialPolygonsDataFrame
with the sampled units and replacements units. The sampled units are controlled by zooming into satelitte images to determine if there are actually people living in those units. For example, Figure 6.1 shows a clearly populated site.
make_pic("Nairobi@1@1",nairobi_sample_150,image_name = "example")
sapply(nairobi_replaced_id$Type %>%
unique,make_pic_loop,
data = nairobi_replaced_id,
sp_obj = nairobi_sample_150,
addition="Nairobi")
Table 6.1: Number of units that need to be resampled from each bin.
Bin | Frequency |
---|---|
2 | 4 |
3 | 2 |
4 | 1 |
5 | 1 |
6 | 3 |
7 | 3 |
8 | 1 |
If the resampled unit is empty, it is left out.
make_pic_loop(type = "replaced",
data = nairobi_replacement_units,
sp_obj = nairobi_1k_sample_complete,
addition = "Nairobi")
coord_string <- c("S03°23.602’;E037°40.783’
S03°23.604’;E037°40.756’
S03°16.746’;E038°18.349’
S03°16.724’;E038°18.337’
S-1.842537;E36.785076
S-1.839773;E36.782486
S-1.389256;E36.866333
S-1.389330;E36.683559
S-1.994650;E36.867916
S-1.994853;E36.866333
S01°06.434’;E036°38.363’
S01°06.461’;E036°38.355’
S01°00.679’;E036°54.028’
S01°00.673’;E036°54.022’
S01°14.124’;E036°56.020’
S01°14.152’;E036°56.081’")
coord_matrix <- coord_string %>%
strsplit("\n") %>%
unlist() %>%
strsplit(";") %>%
do.call(rbind,.)
coord_degree <- coord_matrix[grep("°",coord_matrix[,1]),] %>%
apply(2,function(y) {
y %>%
gsub("S","-",.,fixed=TRUE) %>%
gsub("[A-Z]","",.) %>%
strsplit("[°\\.]") %>%
lapply(function(x) {
x <- x %>%
gsub("[^0-9\\-]","",.) %>%
as.numeric()
x[1] + x[2]/60 + x[3]/3600
}) %>%
unlist()
})
coord_decimal <- coord_matrix[!grepl("°",coord_matrix[,1]),] %>%
gsub("[A-Z]","",.) %>%
apply(2,as.numeric)
# nairobi_fdgs <- rbind(coord_degree,coord_decimal) %>%
# as.data.frame() %>%
# SpatialPoints(CRS("+init=epsg:4326")) %>%
# spTransform(CRS(proj4string(nairobi_1k_sample_complete_100m)))
#
#
# if (over(nairobi_1k_sample_complete_100m,nairobi_fdgs,returnList = FALSE) %>%
# is.na() %>%
# `!` %>%
# which() %>%
# length() == 0) cat("No overlap between sample and FDGs.")
Table 6.2: Lusaka: Number of units that need to be resampled from each bin.
Bin | Frequency |
---|---|
1 | 4 |
2 | 8 |
3 | 8 |
4 | 9 |
5 | 4 |
6 | 4 |
7 | 6 |
8 | 4 |
sapply(lusaka_replaced_id$Type %>%
unique,make_pic_loop,
data = lusaka_replaced_id,
sp_obj = lusaka_sample_150,
addition = "Lusaka")
make_pic_loop(type = "replaced",
data = lusaka_replacement_units,
sp_obj = lusaka_1k_sample_complete,
addition = "Lusaka")
zambia_fdgs <- readKML("data/fdgs/Zambia Northern Eastern FGD Locations.kml") %>%
as("SpatialPoints")
## OGR data source with driver: KML
## Source: "D:\Users\senic\Dropbox\Projects\GLD sampling methods\master_1\sampling_documentation\data\fdgs\Zambia Northern Eastern FGD Locations.kml", layer: "Northern___Eastern_FGD_Locations"
## with 9 features
## It has 2 fields
if (over(lusaka_1k_sample_complete,zambia_fdgs) %>%
is.na %>%
`!` %>%
which() %>%
length() == 0) cat("No overlap between sample and FDGs.")
## No overlap between sample and FDGs.
Table 6.3: Lilongwe: Number of units that need to be resampled from each bin.
Bin | Frequency |
---|---|
1 | 2 |
2 | 6 |
3 | 1 |
4 | 2 |
5 | 6 |
6 | 8 |
7 | 7 |
8 | 7 |
sapply(lilongwe_replaced_id$Type %>%
unique,make_pic_loop,
data = lilongwe_replaced_id,
sp_obj = lilongwe_sample_150,
addition = "Lilongwe")
make_pic_loop(type = "replaced",
data = lilongwe_replacement_units,
sp_obj = lilongwe_1k_sample_complete,
addition = "Lilongwe")
#
malawi_fdgs <- read.csv("data/fdgs/GLD_FDGs_GPS Recordings.csv") %>% head %>%
dplyr::select(gps.Longitude,gps.Latitude) %>%
SpatialPoints(proj4string = CRS(proj4string(lilongwe_1k_sample_complete)))
#
#
if (over(lilongwe_1k_sample_complete[lilongwe_1k_sample_complete$type=="Unit_2",],malawi_fdgs) %>%
is.na %>%
`!` %>%
which() %>%
length() == 0) cat("No overlap between sample and FDGs.")
## No overlap between sample and FDGs.
zam_check <- readLines("output/zambia_check.txt",warn=FALSE)
zam_check <- zam_check[zam_check!=""]
zambia_checking <- zam_check %>%
strsplit(" ") %>%
lapply(function(x) {
if (length(x)==1) return(data.frame(ID=x[1],Full="Full"))
if (x[2]=="a") return(data.frame(ID=x[1],Full="Almost Full"))
return(NULL)
}) %>%
do.call(rbind,.)
zambia_checking$unit3 <- zambia_checking[,1] %>%
strsplit("@") %>%
lapply(function(x) paste(x[1],x[2],x[3],sep="@")) %>%
unlist()
to_be_replaced <- zambia_checking %>%
filter(Full=="Full") %>%
group_by(unit3) %>%
summarise(Full=n()) %>%
filter(Full<2)
empty_units <- zam_check[grepl("repl",zam_check)] %>% gsub(" repl","",.)
to_be_replaced <- data.frame(unit3=empty_units[!empty_units%in%to_be_replaced[,1]],
Full=0)
to_be_replaced %>%
mutate(bin=strsplit(unit3,"@") %>%
lapply(function(x) paste(x[1],x[2],sep="@")) %>%
unlist()) %>%
group_by(bin) %>%
summarise(n())
## # A tibble: 10 x 2
## bin `n()`
## <chr> <int>
## 1 Zambia@1 4
## 2 Zambia@10 4
## 3 Zambia@2 8
## 4 Zambia@3 6
## 5 Zambia@4 4
## 6 Zambia@5 6
## 7 Zambia@6 2
## 8 Zambia@7 5
## 9 Zambia@8 4
## 10 Zambia@9 4
replacements_1 <- readLines("output/zambia_replacement.txt",warn=FALSE)
replacements_1 <- replacements_1[replacements_1!=""]
replacements_2 <- replacements_1 %>%
strsplit(" ") %>%
lapply(function(x) {
if (length(x)==1) return(data.frame(ID=x[1],Full="Full"))
if (x[2]=="a") return(data.frame(ID=x[1],Full="Almost Full"))
return(NULL)
}) %>%
do.call(rbind,.) %>%
filter(Full=="Full")
replacements_2$unit3 <- replacements_2[,1] %>%
strsplit("@") %>%
lapply(function(x) paste(x[1],x[2],x[3],sep="@")) %>%
do.call(rbind,.)
replacements_3 <- replacements_2 %>%
group_by(unit3) %>%
summarise(Full=n()) %>%
filter(Full>1)
replacements_3 %>%
mutate(bin=strsplit(unit3,"@") %>%
lapply(function(x) paste(x[1],x[2],sep="@")) %>%
unlist()) %>%
group_by(bin) %>%
summarise(n())
## # A tibble: 10 x 2
## bin `n()`
## <chr> <int>
## 1 Zambia@1 4
## 2 Zambia@10 4
## 3 Zambia@2 8
## 4 Zambia@3 6
## 5 Zambia@4 4
## 6 Zambia@5 6
## 7 Zambia@6 2
## 8 Zambia@7 5
## 9 Zambia@8 4
## 10 Zambia@9 4
zambia_replacement_units <- readLines(
"output/zambia_replacement.txt",warn=FALSE) %>%
strsplit("\n") %>%
unlist() %>%
strsplit(" ") %>%
lapply(function(x) {
if (length(x)==1) return(data.frame(ID=x[1],Full="Full"))
if (x[2]=="a") return(data.frame(ID=x[1],Full="Almost Empty"))
return(NULL)
}) %>%
do.call(rbind,.) %>%
mutate(bin = sapply(ID,function(x) {
strsplit(x,"@") %>%
lapply(function(y) paste(y[1],y[2],sep="@")) %>%
unlist()}),
unit3 = sapply(ID,function(x) {
strsplit(x,"@") %>%
lapply(function(y) paste(y[1],y[2],y[3],sep="@")) %>%
unlist()
}))
zambia_sample_200_complete <-
bind(zambia_sample_200[zambia_sample_200$pick=="Sample"&
(zambia_sample_200$Name%in%
c(zambia_checking$ID,
zambia_checking$unit3)|
zambia_sample_200$type=="Bin"),],
zambia_sample_200[zambia_sample_200$pick!="Sample"&
zambia_sample_200$Name%in%
c(zambia_replacement_units$ID,
zambia_replacement_units$bin,
zambia_replacement_units$unit3),])
zambia_sample_200_complete$almost_empty <- "Full"
zambia_sample_200_complete$almost_empty[
zambia_sample_200_complete$Name%in%
c(zambia_checking$ID[zambia_checking$Full == "Almost Full"],
zambia_replacement_units$ID[zambia_replacement_units$Full=="Almost Empty"])] <-
"Almost Empty"
zambia_sample_200_complete$Description <-
paste0(zambia_sample_200_complete$Description,"; Replacement Status: ",
zambia_sample_200_complete$almost_empty)
zambia_sample_200_complete <-
zambia_sample_200_complete[order(zambia_sample_200_complete$Name),]
overl <- over(zambia_sample_200_complete[zambia_sample_200_complete$type=="Unit_2",],
zambia_fdgs) %>%
is.na %>%
`!` %>%
which() %>% sapply(function(x)
zambia_sample_200_complete$Name[
which(zambia_sample_200_complete$type=="Unit_2")[x]])
zambia_5k_1k_sample_200_complete <-
zambia_sample_200_complete[!zambia_sample_200_complete$Name%in%overl,]
writeOGR(zambia_5k_1k_sample_200_complete,
"output/zambia_5k_1k_sample_200_complete",
driver = "KML",
layer = "zambia",overwrite_layer = TRUE)
## Warning in fld_names == attr(res, "ofld_nms"): longer object length is not
## a multiple of shorter object length
## Bin Sampled To Sample
## 1 1 9 10
## 2 2 12 12
## 3 3 12 12
## 4 4 9 9
## 5 5 24 23
## 6 6 6 6
## 7 7 7 7
## 8 8 4 4
## 9 9 7 7
## 10 10 10 10
## Bin Sampled To Sample
## 1 1 18 18
## 2 2 16 16
## 3 3 26 26
## 4 4 13 13
## 5 5 20 20
## 6 6 22 22
## 7 7 18 18
## 8 8 17 17
## Bin Sampled To Sample
## 1 1 24 24
## 2 2 45 45
## 3 3 33 33
## 4 4 24 24
## 5 5 4 4
## 6 6 7 7
## 7 7 7 7
## 8 8 6 6
## Bin Sampled To Sample
## 1 1 17 17
## 2 2 46 46
## 3 3 22 22
## 4 4 24 23
## 5 5 10 10
## 6 6 22 22
## 7 7 9 8
## 8 8 2 2
maw_check <- readLines("output/malawi_check.txt",warn=FALSE)
maw_check <- maw_check[maw_check!=""]
malawi_checking <- maw_check %>%
strsplit(" ") %>%
lapply(function(x) {
if (length(x)==1) return(data.frame(ID=x[1],Full="Full"))
if (x[2]=="a") return(data.frame(ID=x[1],Full="Almost Full"))
return(NULL)
}) %>%
do.call(rbind,.)
malawi_checking$unit3 <- malawi_checking[,1] %>%
strsplit("@") %>%
lapply(function(x) paste(x[1],x[2],x[3],sep="@")) %>%
unlist()
malawi_checking$unit2 <- malawi_checking[,1] %>%
strsplit("@") %>%
lapply(function(x) paste(x[1],x[2],sep="@")) %>%
unlist()
maw_to_be_replaced <- malawi_checking %>%
filter(Full=="Full") %>%
group_by(unit3) %>%
summarise(Full=n()) %>%
filter(Full<2)
empty_units <- maw_check[grepl("repl",maw_check)] %>% gsub(" repl","",.)
# no units to replace
# zambia_replacement_units <- readLines(
# "output/zambia_replacement.txt",warn=FALSE) %>%
# strsplit("\n") %>%
# unlist() %>%
# strsplit(" ") %>%
# lapply(function(x) {
# if (length(x)==1) return(data.frame(ID=x[1],Full="Full"))
# if (x[2]=="a") return(data.frame(ID=x[1],Full="Almost Empty"))
# return(NULL)
# }) %>%
# do.call(rbind,.) %>%
# mutate(bin = sapply(ID,function(x) {
# strsplit(x,"@") %>%
# lapply(function(y) paste(y[1],y[2],sep="@")) %>%
# unlist()}),
# unit3 = sapply(ID,function(x) {
# strsplit(x,"@") %>%
# lapply(function(y) paste(y[1],y[2],y[3],sep="@")) %>%
# unlist()
# }))
malawi_sample_200_complete <-
malawi_sample_200[malawi_sample_200$pick=="Sample"&
(malawi_sample_200$Name%in%
c(malawi_checking$ID,
malawi_checking$unit3,
malawi_checking$unit2)|
malawi_sample_200$type=="Bin"),]
malawi_sample_200_complete$almost_empty <- "Full"
malawi_sample_200_complete$almost_empty[
malawi_sample_200_complete$Name%in%
malawi_checking$ID[malawi_checking$Full == "Almost Full"]] <-
"Almost Empty"
malawi_sample_200_complete$Description <-
paste0(malawi_sample_200_complete$Description,"; Replacement Status: ",
malawi_sample_200_complete$almost_empty)
malawi_sample_200_complete <-
malawi_sample_200_complete[order(malawi_sample_200_complete$Name),]
if (over(malawi_sample_200_complete[malawi_sample_200_complete$type=="Unit_2",],malawi_fdgs) %>%
is.na %>%
`!` %>%
which() %>%
length() == 0) cat("No overlap between sample and FDGs.") else cat("There are units that overlap with the FDGs.")
## There are units that overlap with the FDGs.
overl <- over(malawi_sample_200_complete[malawi_sample_200_complete$type=="Unit_2",],
malawi_fdgs) %>%
is.na %>%
`!` %>%
which() %>% sapply(function(x)
malawi_sample_200_complete$Name[
which(malawi_sample_200_complete$type=="Unit_2")[x]])
#
if (length(overl)>0) malawi_5k_1k_sample_200_complete <-
malawi_sample_200_complete[!malawi_sample_200_complete$Name%in%overl,]
#
overl
## 15195
## "Malawi@7@6@2"
writeOGR(malawi_sample_200_complete,
"output/malawi_5k_1k_sample_200_complete.kml",
driver = "KML",
layer = "malawi",overwrite_layer = TRUE)
## Warning in fld_names == attr(res, "ofld_nms"): longer object length is not
## a multiple of shorter object length