-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmap-practices.R
63 lines (53 loc) · 2.22 KB
/
map-practices.R
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
## Taking input from a Practices Reference File (environment object: 'practices') this script
## finds names and codes for constituencies, NI councils, wards and NISRA super output areas, as
## well as latitudes and longitudes for each practice, in the new object 'postcode_details'
## Powered by the MapIt API from MySociety.
## See http://mapit.mysociety.org/ for licensing terms for the use of this service,
## and for the licensing of postcode data.
## If you are looking only for coordinates, you may find it easier to use the NISRA source:
## http://www.ninis2.nisra.gov.uk/Download/People%20and%20Places/GP%20Practices.ods [Licence: OGL]
library(httr)
url <- 'http://mapit.mysociety.org/postcode/'
postcode_details <- data.frame(practice = unique(practices$PracNo), pcd1 = NA)
postcode_details$pcd1 = practices$Postcode[match(postcode_details$practice,practices$PracNo)]
postcode_details <- within(postcode_details, pcd2 <- gsub(" ","",pcd1))
addcols <- c("wmc_name", "wmc_code", "lgd_name", "lgd_code", "soa_name",
"soa_code", "ward_name", "ward_code", "lat", "lon")
for (i in addcols){
postcode_details[,i] <- NA
}
for (r in 1:nrow(postcode_details)){
Sys.sleep(0.4)
p <- postcode_details[r,]$pcd2
response <- GET(paste0(url,p))
content <- content(response)
areas <- content$areas
for (x in 1:length(areas)){
if(areas[[x]]$type == 'WMC'){
wmc_name <- areas[[x]]$name
wmc_code <- areas[[x]]$codes$gss
}
if(areas[[x]]$type == 'LGD'){
lgd_name <- areas[[x]]$name
lgd_code <- areas[[x]]$codes$gss
}
if(areas[[x]]$type == 'OLF'){
soa_name <- areas[[x]]$name
soa_code <- areas[[x]]$codes$ons
}
if(areas[[x]]$type == 'LGW'){
ward_name <- areas[[x]]$name
ward_code <- areas[[x]]$codes$gss
}
}
postcode_details[r,]$wmc_name <- wmc_name
postcode_details[r,]$wmc_code <- wmc_code
postcode_details[r,]$lgd_name <- lgd_name
postcode_details[r,]$lgd_code <- lgd_code
postcode_details[r,]$soa_name <- soa_name
postcode_details[r,]$soa_code <- soa_code
postcode_details[r,]$ward_name <- ward_name
postcode_details[r,]$ward_code <- ward_code
postcode_details[r,]$lat <- content$wgs84_lat
postcode_details[r,]$lon <- content$wgs84_lon
}