Last active
December 26, 2015 10:39
-
-
Save mhl/7138330 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
lon | lat | |
---|---|---|
0.119 | 52.205 | |
-3.188889 | 55.953056 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# A simple example of taking coordinates from a CSV file and looking | |
# up areas around those points in MapIt. This assumes that you're | |
# looking for the 'OLG' type in MapIt and that your coordinates are in | |
# WGS 84 - change the 'srid' and 'mapit.type' variable if that isn't | |
# right. This just prints out the results. | |
library(RCurl) | |
library(rjson) | |
coords <- read.csv('coords.csv') | |
srid <- "4326" | |
# OLG is Lower Layer Super Output Area, Generalised | |
mapit.type <- "OLG" | |
find_areas <- function(coord) { | |
lon <- coord[1] | |
lat <- coord[2] | |
coord.string <- paste(lon, ',', lat, sep="") | |
url <- paste("http://mapit.mysociety.org/point/", | |
srid, | |
"/", | |
coord.string, | |
'.json?type=', | |
mapit.type, | |
sep="") | |
results <- fromJSON(getURL(url)) | |
# Sleep for just over a second to avoid hitting the MapIt rate | |
# limiting: | |
Sys.sleep(1.1) | |
if (length(results) == 0) { | |
print(paste("No", mapit.type, "found around", coord.string)) | |
} else { | |
for (area.id in names(results)) { | |
result <- results[[area.id]] | |
print(paste(coord.string, | |
" is in ", | |
result$name, | |
" (ONS: ", result$codes$ons, ")", | |
sep="")) | |
} | |
} | |
} | |
apply(coords, 1, find_areas) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment