-
Notifications
You must be signed in to change notification settings - Fork 0
/
.Rhistory
executable file
·512 lines (512 loc) · 30.8 KB
/
.Rhistory
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
cauth_lut <- read.csv(paste(getwd(),"/CombinedAuth/cauth.csv", sep = ""), sep="|")
cauth_lut$ladid <- cauth_lut[cauth_lut$]
cauth_lut$ladid <- lookup_lad[match(cauth_lut$lad,lookup_lad$LAD11NM),1]
cauth_lut <- read.csv(paste(getwd(),"/CombinedAuth/cauth.csv", sep = ""), sep="|")
cauth_lut$ladid <- lookup_lad[match(cauth_lut$lad,lookup_lad$LAD11NM),1]
cauth_lut <- read.csv(paste(getwd(),"/CombinedAuth/cauth.csv", sep = ""), sep="|")
cauth_lut$ladid <- lookup_lad[match(cauth_lut$lad,lookup_lad$LAD11NM),1]
cauth_lut <- read.csv(paste(getwd(),"/CombinedAuth/cauth.csv", sep = ""), sep="|")
cauth_lut$ladid <- lookup_lad[match(cauth_lut$lad,lookup_lad$LAD11NM),1]
cauth_lut <- read.csv(paste(getwd(),"/CombinedAuth/cauth.csv", sep = ""), sep="|")
cauth_lut$ladid <- lookup_lad[match(cauth_lut$lad,lookup_lad$LAD11NM),1]
cauth_lut <- read.csv(paste(getwd(),"/CombinedAuth/cauth.csv", sep = ""), sep="|")
cauth_lut$ladid <- lookup_lad[match(cauth_lut$lad,lookup_lad$LAD11NM),1]
cauth_lut <- read.csv(paste(getwd(),"/CombinedAuth/cauth.csv", sep = ""), sep="|")
cauth_lut$ladid <- lookup_lad[match(cauth_lut$lad,lookup_lad$LAD11NM),1]
cauth_lut <- read.csv(paste(getwd(),"/CombinedAuth/cauth.csv", sep = ""), sep="|")
cauth_lut$ladid <- lookup_lad[match(cauth_lut$lad,lookup_lad$LAD11NM),1]
cauth_lut <- read.csv(paste(getwd(),"/CombinedAuth/cauth.csv", sep = ""), sep="|")
cauth_lut$ladid <- lookup_lad[match(cauth_lut$lad,lookup_lad$LAD11NM),1]
cauth_lut <- read.csv(paste(getwd(),"/CombinedAuth/cauth.csv", sep = ""), sep="|")
cauth_lut$ladid <- lookup_lad[match(cauth_lut$lad,lookup_lad$LAD11NM),1]
cauth_lut <- read.csv(paste(getwd(),"/CombinedAuth/cauth.csv", sep = ""), sep="|")
cauth_lut$ladid <- lookup_lad[match(cauth_lut$lad,lookup_lad$LAD11NM),1]
cauth_lut <- read.csv(paste(getwd(),"/CombinedAuth/cauth.csv", sep = ""), sep="|")
cauth_lut$ladid <- lookup_lad[match(cauth_lut$lad,lookup_lad$LAD11NM),1]
cauth_lut
write.table(cauth_lut, file = paste(getwd(),"/CombinedAuth/cauth_lut.csv", sep = ""), row.names=FALSE, quote = FALSE, na="", sep="|")
con <- odbcConnect("cdrcdb", uid = "kd", pwd= pwd)
csv_types <- "cauthid varchar, cauth varchar, lad varchar, ladid varchar"
import_csv(con,paste(getwd(),"/CombinedAuth", sep = ""), "cauth_lut","cauth_lut.csv",csv_types,"|")
odbcQuery(con, "DROP TABLE IF EXISTS lad_cauth;")
odbcQuery(con, "CREATE TABLE lad_cauth AS SELECT * FROM lads RIGHT JOIN cauth_lut ON (cauth_lut.ladid = lads.code);")
odbcQuery(con, "DROP TABLE IF EXISTS cauth_fin;")
odbcQuery(con, "CREATE TABLE cauth_fin AS SELECT ST_UnaryUnion(ST_Collect(geom)), lad_cauth.cauthid, lad_cauth.cauth FROM lad_cauth GROUP BY lad_cauth.cauth, lad_cauth.cauthid;")
cauth_lut$n_ladid <- ifelse(cauth_lut$ladid == "E08000020", "E08000037", ifelse(cauth_lut$ladid == "E06000048", "E08000057",as.character(cauth_lut$ladid)))
write.table(cauth_lut, file = paste(getwd(),"/CombinedAuth/cauth_lut.csv", sep = ""), row.names=FALSE, quote = FALSE, na="", sep="|")
csv_types <- "cauthid varchar, cauth varchar, lad varchar, ladid varchar, n_ladid varchar"
odbcQuery(con, "DROP TABLE IF EXISTS cauth_lut;")
import_csv(con,paste(getwd(),"/CombinedAuth", sep = ""), "cauth_lut","cauth_lut.csv",csv_types,"|")
odbcQuery(con, "DROP TABLE IF EXISTS lad_cauth;")
odbcQuery(con, "CREATE TABLE lad_cauth AS SELECT * FROM lads RIGHT JOIN cauth_lut ON (cauth_lut.ladid = lads.code);")
odbcQuery(con, "DROP TABLE IF EXISTS cauth_fin;")
odbcQuery(con, "CREATE TABLE cauth_fin AS SELECT ST_UnaryUnion(ST_Collect(geom)), lad_cauth.cauthid, lad_cauth.cauth FROM lad_cauth GROUP BY lad_cauth.cauth, lad_cauth.cauthid;")
odbcQuery(con, "DROP TABLE IF EXISTS lad_cauth;")
odbcQuery(con, "CREATE TABLE lad_cauth AS SELECT * FROM lads RIGHT JOIN cauth_lut ON (cauth_lut.n_ladid = lads.code);")
odbcQuery(con, "DROP TABLE IF EXISTS cauth_fin;")
odbcQuery(con, "CREATE TABLE cauth_fin AS SELECT ST_UnaryUnion(ST_Collect(geom)), lad_cauth.cauthid, lad_cauth.cauth FROM lad_cauth GROUP BY lad_cauth.cauth, lad_cauth.cauthid;")
cauth_lut$n_ladid <- ifelse(cauth_lut$ladid == "E08000020", "E08000037", ifelse(cauth_lut$ladid == "E06000048", "E06000057",as.character(cauth_lut$ladid)))
write.table(cauth_lut, file = paste(getwd(),"/CombinedAuth/cauth_lut.csv", sep = ""), row.names=FALSE, quote = FALSE, na="", sep="|")
csv_types <- "cauthid varchar, cauth varchar, lad varchar, ladid varchar, n_ladid varchar"
odbcQuery(con, "DROP TABLE IF EXISTS cauth_lut;")
import_csv(con,paste(getwd(),"/CombinedAuth", sep = ""), "cauth_lut","cauth_lut.csv",csv_types,"|")
odbcQuery(con, "DROP TABLE IF EXISTS lad_cauth;")
odbcQuery(con, "CREATE TABLE lad_cauth AS SELECT * FROM lads RIGHT JOIN cauth_lut ON (cauth_lut.n_ladid = lads.code);")
odbcQuery(con, "DROP TABLE IF EXISTS cauth_fin;")
odbcQuery(con, "CREATE TABLE cauth_fin AS SELECT ST_UnaryUnion(ST_Collect(geom)), lad_cauth.cauthid, lad_cauth.cauth FROM lad_cauth GROUP BY lad_cauth.cauth, lad_cauth.cauthid;")
system(paste0('pgsql2shp -f ', getwd(), '/CombinedAuth/data/CombinedAuth.shp -h localhost -u ', username, ' -P ', pwd, ' ', db_name,' "SELECT * FROM cauth_fin"'))
var_desc <- read.csv("m_code/ckan/var_desc.csv")
pg_tables <- unique(var_desc$DatasetId)
pg_tables <- data.frame(pg_tables)
pg_cols <- paste(var_desc$ColumnVariableCode, ifelse(var_desc$ColumnVariableMeasurementUnit == "Sum", "decimal",
ifelse(var_desc$ColumnVariableMeasurementUnit == "Ratio", "decimal",
ifelse(var_desc$ColumnVariableMeasurementUnit == "Percentage", "decimal",
ifelse(var_desc$ColumnVariableMeasurementUnit == "Years", "decimal",
ifelse(var_desc$ColumnVariableMeasurementUnit == "Average", "decimal","integer"))))), sep = " ")
# Create CombinedAuth directories
for (i in leps_ids[,1]){
print(i)
lep_tb_path <- paste0("/media/kd/Data/temp/CombinedAuth/CombAuth",as.character(i))
dir.create(lep_tb_path)
}
for (i in leps_ids[,1]){
print(i)
lep_tb_path <- paste0("/media/kd/Data/temp/CombinedAuth/CombAuth",as.character(i),"/tables/")
dir.create(lep_tb_path)
}
for (i in leps_ids[,1]){
print(i)
lep_tb_path <- paste0("/media/kd/Data/temp/CombinedAuth/CombAuth",as.character(i),"/shapefiles/")
dir.create(lep_tb_path)
}
cauthids <- unique(cauth_lut[,c("cauthid", "cauth")])
View(cauthids)
# Create CombinedAuth directories
for (i in cauthids[,1]){
print(i)
cauth_tb_path <- paste0("/media/kd/Data/temp/CombinedAuth/CombAuth",as.character(i))
dir.create(cauth_tb_path)
}
for (i in cauthids[,1]){
print(i)
cauth_tb_path <- paste0("/media/kd/Data/temp/CombinedAuth/CombAuth",as.character(i),"/tables/")
dir.create(cauth_tb_path)
}
for (i in cauthids[,1]){
print(i)
cauth_tb_path <- paste0("/media/kd/Data/temp/CombinedAuth/CombAuth",as.character(i),"/shapefiles/")
dir.create(cauth_tb_path)
}
v_lads <- data.frame(cauth_lut[cauth_lut$cauthid == i,4])
v_lads <- v_lads[complete.cases(v_lads),]
v_lads <- data.frame(v_lads)
i <-"1"
v_lads <- data.frame(cauth_lut[cauth_lut$cauthid == i,4])
v_lads <- v_lads[complete.cases(v_lads),]
v_lads <- data.frame(v_lads)
View(v_lads)
# Extract Census tables
for (i in cauthids[,1]){
print(i)
cauth_tb_path <- paste0("/media/kd/Data/temp/CombinedAuth/CombAuth",as.character(i), "/tables")
v_lads <- data.frame(cauth_lut[cauth_lut$cauthid == i,4])
v_lads <- v_lads[complete.cases(v_lads),]
v_lads <- data.frame(v_lads)
field_value_oa <- as.character(unique(lookup[ which( lookup$LAD11CD %in% v_lads$v_lads ),1]))
field_value_lsoa <- as.character(unique(lookup[ which( lookup$LAD11CD %in% v_lads$v_lads ),2]))
field_value_msoa <- as.character(unique(lookup[ which( lookup$LAD11CD %in% v_lads$v_lads ),4]))
for (x in 1:nrow(pg_tables[1])){
tb <- as.character(tolower(pg_tables$pg_tables[x]))
get_subset_table(paste0(tb,"_oa"), "geographycode", field_value_oa, paste0(cauth_tb_path,"/",toupper(tb),"_oa11.csv"),con)
get_subset_table(paste0(tb,"_lsoa"), "geographycode", field_value_lsoa, paste0(cauth_tb_path,"/",toupper(tb),"_lsoa11.csv"),con)
get_subset_table(paste0(tb,"_msoa"), "geographycode", field_value_msoa, paste0(cauth_tb_path,"/",toupper(tb),"_msoa11.csv"),con)
}
}
# Extract LEP's Shapefiles for OA,LSOA & MSOA geographies
for (i in cauthids[,1]){
print(i)
cauth_tb_path <- paste0("/media/kd/Data/temp/CombinedAuth/CombAuth",as.character(i), "/shapefiles")
v_lads <- data.frame(cauth_lut[cauth_lut$cauthid == i,4])
v_lads <- v_lads[complete.cases(v_lads),]
v_lads <- data.frame(v_lads)
field_value_oa <- as.character(unique(lookup[ which( lookup$LAD11CD %in% v_lads$v_lads ),1]))
field_value_lsoa <- as.character(unique(lookup[ which( lookup$LAD11CD %in% v_lads$v_lads ),2]))
field_value_msoa <- as.character(unique(lookup[ which( lookup$LAD11CD %in% v_lads$v_lads ),4]))
tb <- tolower(paste0("CombAuth",as.character(i)))
get_subset_geo(paste0("oa11"), "oa11cd", field_value_oa, paste0(cauth_tb_path,"/",toupper(tb),"_oa11.shp"),con,pwd)
get_subset_geo(paste0("lsoa11"), "lsoa11cd", field_value_lsoa, paste0(cauth_tb_path,"/",toupper(tb),"_lsoa11.shp"),con,pwd)
get_subset_geo(paste0("msoa11"), "msoa11cd", field_value_msoa, paste0(cauth_tb_path,"/",toupper(tb),"_msoa11.shp"),con,pwd)
}
source("/media/kd/Data/Dropbox/Repos/r_source/postgis_functions.r")
np_lut <- read.csv(paste(getwd(),"/NPowerhouse/np.csv", sep = ""), sep="|")
np_lut$ladid <- lookup_lad[match(np_lut$lad,lookup_lad$LAD11NM),1]
np_lut$n_ladid <- ifelse(np_lut$ladid == "E08000020", "E08000037", ifelse(np_lut$ladid == "E06000048", "E06000057",as.character(np_lut$ladid)))
write.table(np_lut, file = paste(getwd(),"/NPowerhouse/np_lut.csv", sep = ""), row.names=FALSE, quote = FALSE, na="", sep="|")
View(np_lut)
np_lut <- read.csv(paste(getwd(),"/NPowerhouse/np.csv", sep = ""), sep="|")
np_lut$ladid <- lookup_lad[match(np_lut$lad,lookup_lad$LAD11NM),1]
np_lut$n_ladid <- ifelse(np_lut$ladid == "E08000020", "E08000037", ifelse(np_lut$ladid == "E06000048", "E06000057",as.character(np_lut$ladid)))
write.table(np_lut, file = paste(getwd(),"/NPowerhouse/np_lut.csv", sep = ""), row.names=FALSE, quote = FALSE, na="", sep="|")
np_lut <- read.csv(paste(getwd(),"/NPowerhouse/np.csv", sep = ""), sep="|")
np_lut$ladid <- lookup_lad[match(np_lut$lad,lookup_lad$LAD11NM),1]
np_lut$n_ladid <- ifelse(np_lut$ladid == "E08000020", "E08000037", ifelse(np_lut$ladid == "E06000048", "E06000057",as.character(np_lut$ladid)))
write.table(np_lut, file = paste(getwd(),"/NPowerhouse/np_lut.csv", sep = ""), row.names=FALSE, quote = FALSE, na="", sep="|")
npids <- unique(np_lut[,c("npid", "np")])
View(np_lut)
View(npids)
con <- odbcConnect("cdrcdb", uid = "kd", pwd= pwd)
csv_types <- "npid varchar, np varchar, lad varchar, ladid varchar, n_ladid varchar"
odbcQuery(con, "DROP TABLE IF EXISTS np_lut;")
import_csv(con,paste(getwd(),"/NPowerhouse", sep = ""), "np_lut","np_lut.csv",csv_types,"|")
odbcQuery(con, "DROP TABLE IF EXISTS lad_np;")
odbcQuery(con, "CREATE TABLE lad_np AS SELECT * FROM lads RIGHT JOIN np_lut ON (np_lut.n_ladid = lads.code);")
odbcQuery(con, "DROP TABLE IF EXISTS np_fin;")
odbcQuery(con, "CREATE TABLE np_fin AS SELECT ST_UnaryUnion(ST_Collect(geom)), lad_np.npid, lad_np.np FROM lad_np GROUP BY lad_np.np, lad_np.npid;")
system(paste0('pgsql2shp -f ', getwd(), '/NPowerhouse/data/NPowerhouse.shp -h localhost -u ', username, ' -P ', pwd, ' ', db_name,' "SELECT * FROM np_fin"'))
for (i in npids[,1]){
print(i)
np_tb_path <- paste0("/media/kd/Data/temp/NPowerhouse/NP",as.character(i))
dir.create(np_tb_path)
}
for (i in npids[,1]){
print(i)
np_tb_path <- paste0("/media/kd/Data/temp/NPowerhouse/NP",as.character(i),"/tables/")
dir.create(np_tb_path)
}
for (i in npids[,1]){
print(i)
np_tb_path <- paste0("/media/kd/Data/temp/NPowerhouse/NP",as.character(i),"/shapefiles/")
dir.create(np_tb_path)
}
for (i in npids[,1]){
print(i)
np_tb_path <- paste0("/media/kd/Data/temp/NPowerhouse/NP",as.character(i))
dir.create(np_tb_path)
}
for (i in npids[,1]){
print(i)
np_tb_path <- paste0("/media/kd/Data/temp/NPowerhouse/NP",as.character(i),"/tables/")
dir.create(np_tb_path)
}
for (i in npids[,1]){
print(i)
np_tb_path <- paste0("/media/kd/Data/temp/NPowerhouse/NP",as.character(i),"/shapefiles/")
dir.create(np_tb_path)
}
for (i in npids[,1]){
print(i)
np_tb_path <- paste0("/media/kd/Data/temp/NPowerhouse/NP",as.character(i), "/shapefiles")
v_lads <- data.frame(np_lut[np_lut$npid == i,4])
v_lads <- v_lads[complete.cases(v_lads),]
v_lads <- data.frame(v_lads)
field_value_oa <- as.character(unique(lookup[ which( lookup$LAD11CD %in% v_lads$v_lads ),1]))
field_value_lsoa <- as.character(unique(lookup[ which( lookup$LAD11CD %in% v_lads$v_lads ),2]))
field_value_msoa <- as.character(unique(lookup[ which( lookup$LAD11CD %in% v_lads$v_lads ),4]))
tb <- tolower(paste0("CombAuth",as.character(i)))
get_subset_geo(paste0("oa11"), "oa11cd", field_value_oa, paste0(np_tb_path,"/",toupper(tb),"_oa11.shp"),con,pwd)
get_subset_geo(paste0("lsoa11"), "lsoa11cd", field_value_lsoa, paste0(np_tb_path,"/",toupper(tb),"_lsoa11.shp"),con,pwd)
get_subset_geo(paste0("msoa11"), "msoa11cd", field_value_msoa, paste0(np_tb_path,"/",toupper(tb),"_msoa11.shp"),con,pwd)
}
for (i in npids[,1]){
print(i)
np_tb_path <- paste0("/media/kd/Data/temp/NPowerhouse/NP",as.character(i), "/tables")
v_lads <- data.frame(np_lut[np_lut$npid == i,4])
v_lads <- v_lads[complete.cases(v_lads),]
v_lads <- data.frame(v_lads)
field_value_oa <- as.character(unique(lookup[ which( lookup$LAD11CD %in% v_lads$v_lads ),1]))
field_value_lsoa <- as.character(unique(lookup[ which( lookup$LAD11CD %in% v_lads$v_lads ),2]))
field_value_msoa <- as.character(unique(lookup[ which( lookup$LAD11CD %in% v_lads$v_lads ),4]))
for (x in 1:nrow(pg_tables[1])){
tb <- as.character(tolower(pg_tables$pg_tables[x]))
get_subset_table(paste0(tb,"_oa"), "geographycode", field_value_oa, paste0(np_tb_path,"/",toupper(tb),"_oa11.csv"),con)
get_subset_table(paste0(tb,"_lsoa"), "geographycode", field_value_lsoa, paste0(np_tb_path,"/",toupper(tb),"_lsoa11.csv"),con)
get_subset_table(paste0(tb,"_msoa"), "geographycode", field_value_msoa, paste0(np_tb_path,"/",toupper(tb),"_msoa11.csv"),con)
}
}
library(whisker)
data <- list(grantNo = "ES/L011840/1",
titl = "",
AuthEnty = c("Alex Singleton, University of Liverpool", "Konstantinos Daras, University of Liverpool"),
depositr = "Alex Singleton, University of Liverpool",
fundAg = "Economic and Social Research Council",
producer= "ESRC Consumer Data Research Centre",
copyright = "The following attribution statements must be used to acknowledge ONS and OS copyright and source in use of these datasets:
Contains National Statistics data Crown copyright and database right 2016;
Contains Ordnance Survey data Crown copyright and database right 2016",
dataCollector = c("Office for National Statistics", "Ordnance Survey"),
dataSrc = c("Census 2011"),
topcClas = c("Transport", "Health","Ethnicity", "Energy", "Demographics","Housing"),
keyword = "",
abstract = "",
timePrd = "2011",
collDate = "27/03/11",
nation = "",
geogCover = "",
geogUnit = c("Output Area", "Lower Super Output Area", "Middle Super Output Area"),
anlyUnit = c("Area", "Household", "Person"),
dataKind = c("Count","Sum","Ratio", "Percent"),
accsPlac = c("Consumer Data Research Centre", "UK Data Service"),
contact = "[email protected]",
relPubl = "",
serName = "CDRC 2011 Census Data Packs"
)
setwd("/media/kd/Data/Dropbox/Repos/rds")
template <- readLines("/media/kd/Data/Dropbox/Repos/rds/m_code/ckan/xml_doc.xml")
cauth_lut <- read.csv(paste(getwd(),"/LEPs/cauth_lut.csv", sep = ""), sep="|")
cauth_ids <- unique(cauth_lut[,c("cauthid", "cauth")])
setwd("/media/kd/Data/temp/CombinedAuth")
fromfile1 <- "/media/kd/Data/Dropbox/Repos/rds/m_code/ckan/datasets_description.csv"
fromfile2 <- "/media/kd/Data/Dropbox/Repos/rds/m_code/ckan/var_desc.csv"
setwd("/media/kd/Data/Dropbox/Repos/rds")
template <- readLines("/media/kd/Data/Dropbox/Repos/rds/m_code/ckan/xml_doc.xml")
cauth_lut <- read.csv(paste(getwd(),"/CombinedAuth/cauth_lut.csv", sep = ""), sep="|")
cauth_ids <- unique(cauth_lut[,c("cauthid", "cauth")])
setwd("/media/kd/Data/temp/CombinedAuth")
fromfile1 <- "/media/kd/Data/Dropbox/Repos/rds/m_code/ckan/datasets_description.csv"
fromfile2 <- "/media/kd/Data/Dropbox/Repos/rds/m_code/ckan/var_desc.csv"
for (i in 1:nrow(cauth_ids)){
code <- paste0("CombAuth", cauth_ids[i,"cauthid"])
name <- cauth_ids[i,"cauth"]
data$keyword <- c("Census", "2011", code)
data$titl <- paste("CDRC 2011 Census Data Packs for Combined Authority: ", name, " (", code, ")", sep="")
data$abstract <- paste("This census data pack provides 2011 Census estimates for the 'Key Statistic' and 'Quick Statistic' tables within the Combined Authority: ",
name, " (", code, ")",
" at the Output Area, Lower Super Output Area and Middle Super Output Area scale. The estimates are as at census day, 27 March 2011.",sep="")
data$nation <- "England"
data$geogCover <- name
readme <- file(paste(getwd(), code, "readme.txt", sep="/"))
writeLines(c(paste("CDRC 2011 Census Geodata pack - ", name, " (", code, ")", ":\n", sep=""),
"+ Abstract",
data$abstract,
"\n",
"+ Contents",
"\t - readme.txt: Information about the CDRC Geodata pack",
"\t - metadata.xml: Metadata",
"\t - datasets_description.csv: Description of the 2011 Census Key Statistic and Quick Statistic datasets",
"\t - variables_description.csv: Description of the 2011 Census Key Statistic and Quick Statistic variables",
paste("\t - tables: Folder containing the OA, LSOA and MSOA 2011 Census data for the Combined Authority: ", name, " (", code, ")", sep=""),
paste("\t - shapefiles: Folder containing the OA, LSOA and MSOA digital boundaries as shapefiles for the Combined Authority: ", name, " (", code, ")", sep=""),
"\n",
"+ Statistical Disclosure Control",
"In order to protect against disclosure of personal information from the 2011 Census, there has been swapping of records in the Census database between different geographic areas and so some counts will be affected.",
"In the main, the greatest effects will be at the lowest geographies, since the record swapping is targeted towards those households with unusual characteristics in small areas.",
"More details on the ONS Census disclosure control strategy may be found on the Statistical Disclosure Control page on the ONS website:",
"http://www.ons.gov.uk/ons/guide-method/census/2011/census-data/2011-census-prospectus/new-developments-for-2011-census-results/statistical-disclosure-control/index.html","\n",
"+ Citation and Copyright",
"The following attribution statements must be used to acknowledge ONS and OS copyright and source in use of these datasets:",
"Contains National Statistics data Crown copyright and database right 2016",
"Contains Ordnance Survey data Crown copyright and database right 2016\n",
"+ Funding",
paste("Funded by: Economic and Social Research Council", data$grantNo)),
readme)
close(readme)
new_xml <- whisker.render(template, data)
writeLines(new_xml, paste(getwd(), "/", code, "/metadata.xml", sep=""))
tofile1 <- paste(getwd(), "/", code, "/datasets_description.csv", sep="")
tofile2 <- paste(getwd(), "/", code, "/variables_description.csv", sep="")
file.copy(fromfile1, tofile1, overwrite = TRUE, recursive = FALSE, copy.mode = TRUE, copy.date = FALSE)
file.copy(fromfile2, tofile2, overwrite = TRUE, recursive = FALSE, copy.mode = TRUE, copy.date = FALSE)
}
library(whisker)
data <- list(grantNo = "ES/L011840/1",
titl = "",
AuthEnty = c("Alex Singleton, University of Liverpool", "Konstantinos Daras, University of Liverpool"),
depositr = "Alex Singleton, University of Liverpool",
fundAg = "Economic and Social Research Council",
producer= "ESRC Consumer Data Research Centre",
copyright = "The following attribution statements must be used to acknowledge ONS and OS copyright and source in use of these datasets:
Contains National Statistics data Crown copyright and database right 2016;
Contains Ordnance Survey data Crown copyright and database right 2016",
dataCollector = c("Office for National Statistics", "Ordnance Survey"),
dataSrc = c("Census 2011"),
topcClas = c("Transport", "Health","Ethnicity", "Energy", "Demographics","Housing"),
keyword = "",
abstract = "",
timePrd = "2011",
collDate = "27/03/11",
nation = "",
geogCover = "",
geogUnit = c("Output Area", "Lower Super Output Area", "Middle Super Output Area"),
anlyUnit = c("Area", "Household", "Person"),
dataKind = c("Count","Sum","Ratio", "Percent"),
accsPlac = c("Consumer Data Research Centre", "UK Data Service"),
contact = "[email protected]",
relPubl = "",
serName = "CDRC 2011 Census Data Packs"
)
setwd("/media/kd/Data/Dropbox/Repos/rds")
template <- readLines("/media/kd/Data/Dropbox/Repos/rds/m_code/ckan/xml_doc.xml")
np_lut <- read.csv(paste(getwd(),"/NPowerhouse/np_lut.csv", sep = ""), sep="|")
np_ids <- unique(np_lut[,c("npid", "np")])
setwd("/media/kd/Data/temp/NPowerhouse")
fromfile1 <- "/media/kd/Data/Dropbox/Repos/rds/m_code/ckan/datasets_description.csv"
fromfile2 <- "/media/kd/Data/Dropbox/Repos/rds/m_code/ckan/var_desc.csv"
for (i in 1:nrow(np_ids)){
code <- paste0("NP", np_ids[i,"cnpid"])
name <- np_ids[i,"np"]
data$keyword <- c("Census", "2011", code)
data$titl <- paste("CDRC 2011 Census Data Packs for Northern Powerhouse Region: ", name, " (", code, ")", sep="")
data$abstract <- paste("This census data pack provides 2011 Census estimates for the 'Key Statistic' and 'Quick Statistic' tables within the Northern Powerhouse Region: ",
name, " (", code, ")",
" at the Output Area, Lower Super Output Area and Middle Super Output Area scale. The estimates are as at census day, 27 March 2011.",sep="")
data$nation <- "England"
data$geogCover <- name
readme <- file(paste(getwd(), code, "readme.txt", sep="/"))
writeLines(c(paste("CDRC 2011 Census Geodata pack - ", name, " (", code, ")", ":\n", sep=""),
"+ Abstract",
data$abstract,
"\n",
"+ Contents",
"\t - readme.txt: Information about the CDRC Geodata pack",
"\t - metadata.xml: Metadata",
"\t - datasets_description.csv: Description of the 2011 Census Key Statistic and Quick Statistic datasets",
"\t - variables_description.csv: Description of the 2011 Census Key Statistic and Quick Statistic variables",
paste("\t - tables: Folder containing the OA, LSOA and MSOA 2011 Census data for the Northern Powerhouse Region: ", name, " (", code, ")", sep=""),
paste("\t - shapefiles: Folder containing the OA, LSOA and MSOA digital boundaries as shapefiles for the Northern Powerhouse Region: ", name, " (", code, ")", sep=""),
"\n",
"+ Statistical Disclosure Control",
"In order to protect against disclosure of personal information from the 2011 Census, there has been swapping of records in the Census database between different geographic areas and so some counts will be affected.",
"In the main, the greatest effects will be at the lowest geographies, since the record swapping is targeted towards those households with unusual characteristics in small areas.",
"More details on the ONS Census disclosure control strategy may be found on the Statistical Disclosure Control page on the ONS website:",
"http://www.ons.gov.uk/ons/guide-method/census/2011/census-data/2011-census-prospectus/new-developments-for-2011-census-results/statistical-disclosure-control/index.html","\n",
"+ Citation and Copyright",
"The following attribution statements must be used to acknowledge ONS and OS copyright and source in use of these datasets:",
"Contains National Statistics data Crown copyright and database right 2016",
"Contains Ordnance Survey data Crown copyright and database right 2016\n",
"+ Funding",
paste("Funded by: Economic and Social Research Council", data$grantNo)),
readme)
close(readme)
new_xml <- whisker.render(template, data)
writeLines(new_xml, paste(getwd(), "/", code, "/metadata.xml", sep=""))
tofile1 <- paste(getwd(), "/", code, "/datasets_description.csv", sep="")
tofile2 <- paste(getwd(), "/", code, "/variables_description.csv", sep="")
file.copy(fromfile1, tofile1, overwrite = TRUE, recursive = FALSE, copy.mode = TRUE, copy.date = FALSE)
file.copy(fromfile2, tofile2, overwrite = TRUE, recursive = FALSE, copy.mode = TRUE, copy.date = FALSE)
}
View(npids)
library(whisker)
data <- list(grantNo = "ES/L011840/1",
titl = "",
AuthEnty = c("Alex Singleton, University of Liverpool", "Konstantinos Daras, University of Liverpool"),
depositr = "Alex Singleton, University of Liverpool",
fundAg = "Economic and Social Research Council",
producer= "ESRC Consumer Data Research Centre",
copyright = "The following attribution statements must be used to acknowledge ONS and OS copyright and source in use of these datasets:
Contains National Statistics data Crown copyright and database right 2016;
Contains Ordnance Survey data Crown copyright and database right 2016",
dataCollector = c("Office for National Statistics", "Ordnance Survey"),
dataSrc = c("Census 2011"),
topcClas = c("Transport", "Health","Ethnicity", "Energy", "Demographics","Housing"),
keyword = "",
abstract = "",
timePrd = "2011",
collDate = "27/03/11",
nation = "",
geogCover = "",
geogUnit = c("Output Area", "Lower Super Output Area", "Middle Super Output Area"),
anlyUnit = c("Area", "Household", "Person"),
dataKind = c("Count","Sum","Ratio", "Percent"),
accsPlac = c("Consumer Data Research Centre", "UK Data Service"),
contact = "[email protected]",
relPubl = "",
serName = "CDRC 2011 Census Data Packs"
)
setwd("/media/kd/Data/Dropbox/Repos/rds")
template <- readLines("/media/kd/Data/Dropbox/Repos/rds/m_code/ckan/xml_doc.xml")
np_lut <- read.csv(paste(getwd(),"/NPowerhouse/np_lut.csv", sep = ""), sep="|")
np_ids <- unique(np_lut[,c("npid", "np")])
setwd("/media/kd/Data/temp/NPowerhouse")
fromfile1 <- "/media/kd/Data/Dropbox/Repos/rds/m_code/ckan/datasets_description.csv"
fromfile2 <- "/media/kd/Data/Dropbox/Repos/rds/m_code/ckan/var_desc.csv"
for (i in 1:nrow(np_ids)){
code <- paste0("NP", np_ids[i,"npid"])
name <- np_ids[i,"np"]
data$keyword <- c("Census", "2011", code)
data$titl <- paste("CDRC 2011 Census Data Packs for Northern Powerhouse Region: ", name, " (", code, ")", sep="")
data$abstract <- paste("This census data pack provides 2011 Census estimates for the 'Key Statistic' and 'Quick Statistic' tables within the Northern Powerhouse Region: ",
name, " (", code, ")",
" at the Output Area, Lower Super Output Area and Middle Super Output Area scale. The estimates are as at census day, 27 March 2011.",sep="")
data$nation <- "England"
data$geogCover <- name
readme <- file(paste(getwd(), code, "readme.txt", sep="/"))
writeLines(c(paste("CDRC 2011 Census Geodata pack - ", name, " (", code, ")", ":\n", sep=""),
"+ Abstract",
data$abstract,
"\n",
"+ Contents",
"\t - readme.txt: Information about the CDRC Geodata pack",
"\t - metadata.xml: Metadata",
"\t - datasets_description.csv: Description of the 2011 Census Key Statistic and Quick Statistic datasets",
"\t - variables_description.csv: Description of the 2011 Census Key Statistic and Quick Statistic variables",
paste("\t - tables: Folder containing the OA, LSOA and MSOA 2011 Census data for the Northern Powerhouse Region: ", name, " (", code, ")", sep=""),
paste("\t - shapefiles: Folder containing the OA, LSOA and MSOA digital boundaries as shapefiles for the Northern Powerhouse Region: ", name, " (", code, ")", sep=""),
"\n",
"+ Statistical Disclosure Control",
"In order to protect against disclosure of personal information from the 2011 Census, there has been swapping of records in the Census database between different geographic areas and so some counts will be affected.",
"In the main, the greatest effects will be at the lowest geographies, since the record swapping is targeted towards those households with unusual characteristics in small areas.",
"More details on the ONS Census disclosure control strategy may be found on the Statistical Disclosure Control page on the ONS website:",
"http://www.ons.gov.uk/ons/guide-method/census/2011/census-data/2011-census-prospectus/new-developments-for-2011-census-results/statistical-disclosure-control/index.html","\n",
"+ Citation and Copyright",
"The following attribution statements must be used to acknowledge ONS and OS copyright and source in use of these datasets:",
"Contains National Statistics data Crown copyright and database right 2016",
"Contains Ordnance Survey data Crown copyright and database right 2016\n",
"+ Funding",
paste("Funded by: Economic and Social Research Council", data$grantNo)),
readme)
close(readme)
new_xml <- whisker.render(template, data)
writeLines(new_xml, paste(getwd(), "/", code, "/metadata.xml", sep=""))
tofile1 <- paste(getwd(), "/", code, "/datasets_description.csv", sep="")
tofile2 <- paste(getwd(), "/", code, "/variables_description.csv", sep="")
file.copy(fromfile1, tofile1, overwrite = TRUE, recursive = FALSE, copy.mode = TRUE, copy.date = FALSE)
file.copy(fromfile2, tofile2, overwrite = TRUE, recursive = FALSE, copy.mode = TRUE, copy.date = FALSE)
}
# Extract Northern Powerhouse's shapefiles for OA,LSOA & MSOA geographies
for (i in npids[,1]){
print(i)
np_tb_path <- paste0("/media/kd/Data/temp/NPowerhouse/NP",as.character(i), "/shapefiles")
v_lads <- data.frame(np_lut[np_lut$npid == i,4])
v_lads <- v_lads[complete.cases(v_lads),]
v_lads <- data.frame(v_lads)
field_value_oa <- as.character(unique(lookup[ which( lookup$LAD11CD %in% v_lads$v_lads ),1]))
field_value_lsoa <- as.character(unique(lookup[ which( lookup$LAD11CD %in% v_lads$v_lads ),2]))
field_value_msoa <- as.character(unique(lookup[ which( lookup$LAD11CD %in% v_lads$v_lads ),4]))
tb <- tolower(paste0("NP",as.character(i)))
get_subset_geo(paste0("oa11"), "oa11cd", field_value_oa, paste0(np_tb_path,"/",toupper(tb),"_oa11.shp"),con,pwd)
get_subset_geo(paste0("lsoa11"), "lsoa11cd", field_value_lsoa, paste0(np_tb_path,"/",toupper(tb),"_lsoa11.shp"),con,pwd)
get_subset_geo(paste0("msoa11"), "msoa11cd", field_value_msoa, paste0(np_tb_path,"/",toupper(tb),"_msoa11.shp"),con,pwd)
}
zip(paste0(getwd(),"/NP1.zip"), files = "NP1")
for (np in npids$npid){
zip(paste(getwd(),"/NP", np,".zip",sep=""), files=paste0("NP",lad))
}
for (np in npids$npid){
zip(paste(getwd(),"/NP", np,".zip",sep=""), files=paste0("NP",np))
}
setwd("/media/kd/Data/temp/CombinedAuth")
############################################### zip folders #################################################
for (cauth in cauthids$cauthid){
zip(paste(getwd(),"/CombAuth", cauth,".zip",sep=""), files=paste0("CombAuth",cauth))
}
setwd("/media/kd/Data/Dropbox/Repos/rds")
setwd("/media/kd/Data/temp/LEPs")
leps_lut <- read.csv(paste(getwd(),"/LEPs/leps.csv", sep = ""), sep="|")
leps_ids <- read.csv(paste(getwd(),"/LEPs/lepids.csv", sep = ""), sep="|", colClasses = c("character", "character"))
leps_lut$lepid <- leps_ids[match(leps_lut$lep, leps_ids$lepname),1]
lookup_lep <- unique(leps_lut[,c("lepid", "lep")])
lookup_lep[,1] <- as.character(lookup_lep[,1])
lookup_lep[,2] <- as.character(lookup_lep[,2])
setwd("/media/kd/Data/Dropbox/Repos/rds")
template <- readLines("/media/kd/Data/Dropbox/Repos/rds/m_code/ckan/xml_doc.xml")
leps_lut <- read.csv(paste(getwd(),"/LEPs/leps.csv", sep = ""), sep="|")
leps_ids <- read.csv(paste(getwd(),"/LEPs/lepids.csv", sep = ""), sep="|", colClasses = c("character", "character"))
leps_lut$lepid <- leps_ids[match(leps_lut$lep, leps_ids$lepname),1]
lookup_lep <- unique(leps_lut[,c("lepid", "lep")])
lookup_lep[,1] <- as.character(lookup_lep[,1])
lookup_lep[,2] <- as.character(lookup_lep[,2])
setwd("/media/kd/Data/temp/LEPs")
############################################### zip folders #################################################
for (lep in lookup_lep$lepid){
zip(paste(getwd(),"/LEP", lep,".zip",sep=""), files=paste0("LEP",lep))
}