-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiva_gis.qmd
194 lines (143 loc) · 7.44 KB
/
diva_gis.qmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
---
title: "GIS Data to ODK itemlist"
author: "Chrissy h Roberts"
---
## Background
Using Administrative data from \[DIVA-GIS\](www.diva-gis.org), this script will convert the geographical administrative data (levels 0-3) for one or more country and output an itemsets.csv file that works with ODK and preserves accented characters (tested only on roman characters). The primary purpose here is to create a list that is compatible with the cascading select system that ODK uses to filter down to more granular responses in questions where you'd want to capture district \> area \> village or similar.
This also removes accents from internal variables like the XLSForm 'name' column.
Accents will appear on screen, but won't be preserved in data frames. This is desirable because working with mixed data that may or may not include accents is a pain.
## Data
You will need to get administrative data from \[DIVA GIS\](http://www.diva-gis.org/datadown#) for each of the countries you want to include.
Unzip this data to a folder in the same directory as this script,
**This example uses Uganda and Democratic Republic of Congo.**
## Libraries
```{r}
library(knitr)
```
### Create a folder to house data
```{r}
if(!dir.exists("data/divadownload/")){dir.create("data/divadownload")}
system("rm -rf data/divadownload/*")
```
### Define target countries
Specify ISO codes (3 digit) to tell R which data sets to include. A full list of ISO codes is available \[here\](#https://en.wikipedia.org/wiki/ISO_3166-1_alpha-3 )
```{r}
countries<-c("UGA","COD")
```
## Download and bind data
For each country, find the data set in the folder structure and load up then bind together
```{r}
options(timeout=120)
for(i in 1:length(countries))
{
#download the data set for country i
download.file(url = paste("http://biogeo.ucdavis.edu/data/diva/adm/",countries[i],"_adm.zip",sep=""),destfile =paste("data/divadownload/",countries[i],"_adm.zip",sep=""))
#unzip the data for country i
system (paste("unzip data/divadownload/",countries[i],"_adm.zip -d data/divadownload/",countries[i],sep=""))
#read the level 3 data for country i
level3data<-read.csv(list.files(pattern = paste(countries[i],"_adm3.csv",sep=""),full.names = T,recursive = T))
#select only the relevant columns
level3data<-level3data[,c("NAME_0","NAME_1","NAME_2","NAME_3","ISO")]
#for the first country, create a new df
if(i==1){full.data.output<-level3data}
#for countries 2 to n, bind data
if(i!=1){full.data.output<-rbind(full.data.output,level3data)}
}
```
## Define function to remove accents
```{r}
#define function to remove accents from text
removeAccents<-function(x)
{
a <- c('À', 'Á', 'Â', 'Ã', 'Ä', 'Å', 'Æ', 'Ç', 'È', 'É', 'Ê', 'Ë', 'Ì', 'Í', 'Î', 'Ï', 'Ð', 'Ñ', 'Ò', 'Ó', 'Ô', 'Õ', 'Ö', 'Ø', 'Ù', 'Ú', 'Û', 'Ü', 'Ý', 'ß', 'à', 'á', 'â', 'ã', 'ä', 'å', 'æ', 'ç', 'è', 'é', 'ê', 'ë', 'ì', 'í', 'î', 'ï', 'ñ', 'ò', 'ó', 'ô', 'õ', 'ö', 'ø', 'ù', 'ú', 'û', 'ü', 'ý', 'ÿ', 'Ā', 'ā', 'Ă', 'ă', 'Ą', 'ą', 'Ć', 'ć', 'Ĉ', 'ĉ', 'Ċ', 'ċ', 'Č', 'č', 'Ď', 'ď', 'Đ', 'đ', 'Ē', 'ē', 'Ĕ', 'ĕ', 'Ė', 'ė', 'Ę', 'ę', 'Ě', 'ě', 'Ĝ', 'ĝ', 'Ğ', 'ğ', 'Ġ', 'ġ', 'Ģ', 'ģ', 'Ĥ', 'ĥ', 'Ħ', 'ħ', 'Ĩ', 'ĩ', 'Ī', 'ī', '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 'e', 'E', 'e', 'E', 'e', 'E', 'e', 'G', 'g', 'G', 'g', 'G', 'g', 'G', 'g', 'H', 'h', 'H', 'h', 'I', 'i', 'I', 'i', 'I', 'i', 'I', 'i', 'I', 'i', 'IJ', 'ij', 'J', 'j', 'K', 'k', 'L', 'l', 'L', 'l', 'L', 'l', 'L', 'l', 'l', 'l', 'N', 'n', 'N', 'n', 'N', 'n', 'n', 'O', 'o', 'O', 'o', 'O', 'o', 'OE', 'oe', 'R', 'r', 'R', 'r', 'R', 'r', 'S', 's', 'S', 's', 'S', 's', 'S', 's', 'T', 't', 'T', 't', 'T', 't', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'W', 'w', 'Y', 'y', 'Y', 'Z', 'z', 'Z', 'z', 'Z', 'z', 's', 'f', 'O', 'o', 'U', 'u', 'A', 'a', 'I', 'i', 'O', 'o', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'A', 'a', 'AE', 'ae', 'O', 'o');
for(i in 1:length(a))
{
x<-gsub(x = x,pattern = a[i],replacement = b[i])
}
return(x)
}
```
## Tidy data
```{r}
#Set correct names so that label is descriptive country name
names(full.data.output)[which(names(full.data.output)=="NAME_0")]<-"label"
#set correct names so that ISO code is NAME_0 (level 0 country data)
names(full.data.output)[which(names(full.data.output)=="ISO")]<-"NAME_0"
#get rid of whitespace and dots and so on [might need to add more]
full.data.output<-full.data.output[,c("NAME_0","NAME_1","NAME_2","NAME_3","label")]
full.data.output$NAME_1<-gsub(full.data.output$NAME_1,pattern = "/| |'|//.",replacement = "_")
full.data.output$NAME_2<-gsub(full.data.output$NAME_2,pattern = "/| |'|//.",replacement = "_")
full.data.output$NAME_3<-gsub(full.data.output$NAME_3,pattern = "/| |'|//.",replacement = "_")
#find level zero and blank out levels 1,2,3
level0<-full.data.output
level0[,2:4]<-""
level0<-unique(level0)
level0$list_name<-"NAME_0"
#find level one and blank out levels 2,3
level1<-full.data.output[,c(2,1,3,4,5)]
level1[,3:4]<-""
level1<-unique(level1)
level1$list_name<-"NAME_1"
level1$label<-level1$NAME_1
x<-level1
x$NAME_1<-""
x$label<-""
x<-unique(x)
x$NAME_1<-"Other"
x$label<-"Other"
level1<-rbind(level1,x)
rm(x)
#find level two and blank out level 3
level2<-full.data.output[,c(3,1,2,4,5)]
level2[,4]<-""
level2<-unique(level2)
level2$list_name<-"NAME_2"
level2$label<-level2$NAME_2
x<-level2
x$NAME_2<-""
x$label<-""
x<-unique(x)
x$NAME_2<-"Other"
x$label<-"Other"
level2<-rbind(level2,x)
rm(x)
#find level three
level3<-full.data.output[,c(4,1,2,3,5)]
level3<-unique(level3)
level3$list_name<-"NAME_3"
level3$label<-level3$NAME_3
x<-level3
x$NAME_3<-""
x$label<-""
x<-unique(x)
x$NAME_3<-"Other"
x$label<-"Other"
level3<-rbind(level3,x)
rm(x)
#put it all together
output<-level0
output<-rbind(output,level1)
output<-rbind(output,level2)
output<-rbind(output,level3)
# remove accents from name, levels 0-3, leaving them only in label.
output<-output[,c("list_name","NAME_0","NAME_1","NAME_2","NAME_3","label")]
output$name<-output$label
output$name<-removeAccents(output$name)
output$NAME_0<-removeAccents(output$NAME_0)
output$NAME_1<-removeAccents(output$NAME_1)
output$NAME_2<-removeAccents(output$NAME_2)
output$NAME_3<-removeAccents(output$NAME_3)
```
## Show output
```{r,message=F}
kable(head(output,100))
```
## Write output to itemsets.csv
```{r}
write.csv(x = output,file = "output/diva_itemsets.csv",quote = F,row.names = F)
```
## Delete the raw data
```{r}
system("rm -rf data/divadownload/")
```