Created
March 5, 2021 08:31
-
-
Save MichaelChirico/38ef2e6082194a8e7cc85b958674636b to your computer and use it in GitHub Desktop.
Count the number of MRT/LRT stations in each postal district
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
library(sp) | |
library(rgdal) | |
# better to download & read the files programmatically, but something | |
# funky is happening on that route, not bothering to debug too deeply. | |
# for reproducibility: | |
# (1) Click Download at the URL | |
# (2) Unzip the file; it contains two more zip directories, one KML and one SHP | |
# (3) Unzip the .shp zip to ~/Downloads/mrt | |
# via https://data.gov.sg/dataset/master-plan-2014-rail-station | |
mrt = rgdal::readOGR("~/Downloads/mrt", "G_MP14_RAIL_STN_PL") | |
# NB: I hand-drew this in the Maps interface & exported to KML | |
districts = rgdal::readOGR("~/Downloads/sg_postal_districts.kml", "sg_postal_districts") | |
# convert to same coordinate system as mrt | |
districts = spTransform(districts, mrt@proj4string) | |
districts_df <- data.frame(district = districts$Name, nbhds = districts$neighborhoods) | |
counts_mrt_lrt <- table(over(mrt, districts)$Name) | |
# defaults to 0 (will be missing from counts if there are no matches) | |
districts_df$n_mrt_lrt_stations = 0L | |
districts_df[ | |
match(names(counts_mrt_lrt), districts_df$district), | |
"n_mrt_lrt_stations"] = as.integer(counts_mrt_lrt) | |
counts_mrt <- table(over(mrt[mrt$TYPE == "MRT", ], districts)$Name) | |
districts_df$n_mrt_stations = 0L | |
districts_df[ | |
match(names(counts_mrt), districts_df$district), | |
"n_mrt_stations"] = as.integer(counts_mrt) | |
write.csv( | |
districts_df, | |
'~/n_mrt_stations_by_district.csv' | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment