Created
October 10, 2017 21:11
-
-
Save shanealynn/1f60f61bbe7c37fac0891e9893d27473 to your computer and use it in GitHub Desktop.
Find small areas for PPR latitudes and longitudes
This file contains 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
# Overlay the small areas from the census data | |
# load small area files - remember this needs to be in GPS form for matching. | |
map_data <- readShapePoly('Census2011_Small_Areas_generalised20m/small_areas_gps.shp') | |
# Assign a small area and electoral district to each property with a GPS coordinate. | |
# The assignment of points to polygons is done using the sp::over() function. | |
# Inputs are a SpatialPoints (house locations) set, and SpatialPolygons (boundary shapes) | |
spatial_points <- SpatialPointsDataFrame(coords = ppr_data[!is.na(latitude),.(longitude,latitude)], data=ppr_data[!is.na(latitude), .(input_string, postcode)]) | |
polygon_overlap <- over(spatial_points, map_data) | |
# Now we can merge the Small Area / Electoral District IDs back onto the ppr_data. | |
ppr_data[!is.na(latitude), geo_county:=polygon_overlap$COUNTYNAME] | |
ppr_data$geo_county = str_replace(ppr_data$geo_county, pattern = " County", replacement = "") | |
ppr_data[!is.na(latitude), electoral_district:=polygon_overlap$EDNAME] | |
ppr_data[!is.na(latitude), electoral_district_id:=polygon_overlap$CSOED] | |
ppr_data[!is.na(latitude), region:=polygon_overlap$NUTS3NAME] | |
ppr_data[!is.na(latitude), small_area:=polygon_overlap$SMALL_AREA] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment