Skip to content

Instantly share code, notes, and snippets.

@giacecco
Last active August 29, 2015 14:25
Embed
What would you like to do?
require(dplyr)
require(RPostgreSQL)
target_town <- "BERKHAMSTED"
# I fetch all postcodes and populated_places from OS' Open Names
os_on_postcodes <- tbl(src_postgres("olaf"), sql("SELECT name1, populated_place FROM os_open_names WHERE local_type = 'Postcode' AND populated_place != ''"))
# I filter by target town
# TODO: what to do about those parts of town like Northchurch to Berkhamsted that appear as
# "independent" in OS Open Names but are commonly considered part of the other, e.g. to the point
# of being inside the same bounding box according to OS Open Names itself?
target_town_postcodes <- collect(os_on_postcodes %>% filter(toupper(populated_place) == target_town))$name1
> target_town_postcodes
[1] "HP4 1DS" "HP4 2HQ" "HP4 3LS" "HP4 3QB" "HP4 3SB" "HP4 2FY" "HP4 3PY" "HP4 2AH" "HP4 2EE" "HP4 3SG"
[11] "HP4 1BL" "HP4 2HW" "HP4 2TR" "HP4 2FN" "HP4 3JD" "HP4 1BZ" "HP4 3TS" "HP4 3ER" "HP4 3PA" "HP4 3BS"
[21] "HP4 3HR" "HP4 3WZ" "HP4 3DQ" "HP4 1EU" "HP4 2EY" "HP4 2LF" "HP4 3NY" "HP4 1JR" "HP4 3SN" "HP4 1HF"
[31] "HP4 3QN" "HP4 1DQ" "HP4 2AZ" "HP4 1EX" "HP4 3NZ" "HP4 1AT" "HP4 1LJ" "HP4 1FB" "HP4 3NU" "HP4 3GY"
[41] "HP4 3BG" "HP4 3RR" "HP4 3DY" "HP4 2AQ" "HP4 3TQ" "HP4 2DL" "WD23 3HJ" "HP4 2EH" "HP4 3LX" "HP4 1JL"
[51] "HP4 3SY" "HP4 1SA" "HP4 3NS" "HP4 3RF" "HP4 3RZ" "HP4 3FN" "HP4 3PJ" "HP4 2JE" "HP4 3RG" "HP4 1BY"
[61] "HP4 1HQ" "HP4 2JQ" "HP4 1HR" "HP4 3HL" "HP4 1EQ" "HP4 3HH" "HP4 1HU" "HP4 3DH" "HP4 1JU" "HP4 2EG"
[71] "HP4 2FQ" "HP4 2JN" "HP4 3XA" "HP4 1DW" "HP4 3PS" "HP4 3QW" "HP4 3QP" "HP4 1BW" "HP4 2LE" "HP4 3XD"
[81] "HP4 3LE" "HP4 3DU" "HP4 1EZ" "HP4 3QF" "HP4 3YY" "HP4 2BJ" "HP4 3RD" "HP4 2BW" "HP4 3EQ" "HP4 3QY"
[91] "HP4 1SF" "HP4 3DX" "HP4 2LG" "HP4 2NG" "HP4 1EG" "HP4 3LH" "HP4 3PD" "HP4 3QR" "HP4 1EE" "HP4 3LQ"
[101] "HP4 3TN" "HP4 3XG" "HP4 1AH" "HP4 3AS" "HP4 1SD" "HP4 3EL" "HP4 1DB" "HP4 1JX" "HP4 3HY" "HP4 3NJ"
[111] "HP4 3NH" "HP4 1TE" "HP4 3NN" "HP4 1HD" "HP4 1DD" "HP4 3ST" "HP4 3EP" "HP4 3RS" "HP4 2DT" "HP4 3XU"
[121] "HP4 3UU" "HP4 2BN" "HP4 1SX" "HP4 2BT" "HP4 3RH" "HP4 1FG" "HP4 2LH" "HP4 2BB" "HP4 3LL" "HP4 9BG"
[131] "HP4 2EJ" "HP4 2DR" "HP4 2LW" "HP4 2DJ" "HP4 2DU" "HP4 3HU" "HP4 1ED" "HP4 3LP" "HP4 3QT" "HP4 3RX"
[141] "HP4 2PR" "HP4 3JE" "HP4 3LJ" "HP4 3HT" "HP4 1AQ" "HP4 3YP" "HP4 3PN" "HP4 3RW" "HP4 3SF" "HP4 1BB"
[151] "HP4 1JA" "HP4 1LW" "HP4 3DB" "HP4 3PQ" "HP4 1LN" "HP4 3XR" "HP4 3TF" "HP4 1TH" "HP4 1JW" "HP4 2YD"
[161] "HP4 3BJ" "HP4 1AJ" "HP4 3XE" "HP4 2AF" "HP4 3JH" "HP4 1TP" "HP4 1DZ" "HP4 3AX" "HP4 2DZ" "HP4 2YF"
[171] "HP4 3DW" "HP4 9AR" "HP4 3RP" "HP4 3EY" "HP4 1FL" "HP4 1HH" "HP4 3AP" "HP4 2ED" "HP4 1HS" "HP4 2LP"
[181] "HP4 3EZ" "HP4 3RJ" "HP4 1JP" "HP4 2BA" "HP4 3LU" "HP4 2BU" "HP4 1AF" "HP4 3ND" "HP4 1EY" "HP4 3EF"
[191] "HP4 3NF" "HP4 3XH" "HP4 3NP" "HP4 3DN" "HP4 3AT" "HP4 3QX" "HP4 3TP" "HP4 2PF" "HP4 3PW" "HP4 3XN"
[201] "HP4 1GE" "HP4 3HQ" "HP4 3NA" "HP4 3AF" "HP4 3DZ" "HP4 2JW" "HP4 3FQ" "HP4 3XB" "HP4 3HP" "HP4 2FF"
[211] "HP4 1WY" "HP4 3JB" "HP4 2HT" "HP4 1FF" "HP4 1DT" "HP4 3NR" "HP4 3HE" "HP4 3NQ" "HP4 2WD" "HP4 3PE"
[221] "HP4 2AX" "HP4 2EZ" "HP4 1JH" "HP4 2UB" "HP4 1JF" "HP4 3SX" "HP4 2DE" "HP4 3GB" "HP4 2BY" "HP4 2JJ"
[231] "HP4 2FG" "HP4 3XJ" "HP4 1JD" "HP4 2BE" "HP4 2AS" "HP4 3YX" "HP4 3BT" "HP4 2NB" "HP4 1HA" "HP4 2EN"
[241] "HP4 1DJ" "HP4 2FE" "HP4 3FY" "HP4 3ET" "HP4 3XP" "HP4 2XD" "HP4 3BU" "HP4 3JG" "HP4 2WB" "HP4 3YA"
[251] "HP4 2JS" "HP4 1AY" "HP4 2UA" "HP4 3AD" "HP4 9BN" "HP4 1LH" "HP4 3NG" "HP4 2DD" "HP4 3YD" "HP4 3JX"
[261] "HP4 3AH" "HP4 2LD" "HP4 3TT" "HP4 2EQ" "HP4 3BB" "HP4 1LR" "HP4 2NW" "HP4 3AQ" "HP4 3LZ" "HP4 3NB"
[271] "HP4 3LF" "HP4 2LN" "HP4 3ZE" "HP4 2HJ" "HP4 2ST" "HP4 1DL" "HP4 9AW" "HP4 3DG" "HP4 2DX" "HP4 1AZ"
[281] "HP4 1EA" "HP4 3FA" "HP4 3BH" "HP4 3PF" "HP4 3HB" "HP4 9AS" "HP4 3QD" "HP4 2JU" "HP4 1AL" "HP4 3AN"
[291] "HP4 1BJ" "HP4 3QE" "HP4 3SP" "HP4 3QS" "HP4 1WR" "HP4 1JJ" "HP4 1AD" "HP4 2AR" "HP4 1AB" "HP4 2NA"
[301] "HP4 1FN" "HP4 1TF" "HP4 2HG" "HP4 3LR" "HP4 1AE" "HP4 3AL" "HP4 2NZ" "HP4 3HD" "HP4 2AG" "HP4 3LW"
[311] "HP4 1FD" "HP4 3JP" "HP4 3BQ" "HP4 3FW" "HP4 3LA" "HP4 3RT" "HP4 3FG" "HP4 3LN" "HP4 2DA" "HP4 2HN"
[321] "HP4 1HB" "HP4 2LB" "HP4 3RU" "HP4 1AG" "HP4 2HR" "HP4 2EX" "HP4 1LL" "HP4 3TH" "HP4 2DG" "HP4 2DS"
[331] "HP4 2JR" "HP4 2JG" "HP4 2JT" "HP4 3PZ" "HP4 1BS" "HP4 9BW" "HP4 2FS" "HP4 2DW" "HP4 2BP" "HP4 3JR"
[341] "HP4 2AA" "HP4 1JS" "HP4 2AE" "HP4 3QH" "HP4 3BD" "HP4 3JA" "HP4 2PS" "HP4 3WF" "HP4 3YZ" "HP4 2AJ"
[351] "HP4 2BL" "HP4 2XF" "HP4 3SS" "HP4 3JU" "HP4 1SZ" "HP4 3RA" "HP4 2HX" "HP4 2JH" "HP4 9AG" "HP4 3SL"
[361] "HP4 3AJ" "HP4 3NX" "HP4 3JF" "HP4 2HL" "HP4 3BL" "HP4 3PG" "HP4 3QA" "HP4 1BX" "HP4 2JZ" "HP4 1ZZ"
[371] "HP4 1HT" "HP4 3TG" "HP4 3TR" "HP4 2DF" "HP4 3EU" "HP4 1BU" "HP4 3DT" "HP4 2LR" "HP4 3QQ" "HP4 3GW"
[381] "HP4 3JS" "HP4 2LQ" "HP4 1DF" "HP4 1AW" "HP4 2BQ" "HP4 1FH" "HP4 3SH" "HP4 3AY" "HP4 2JF" "HP4 3PB"
[391] "HP4 1AR" "HP4 3RE" "HP4 1ES" "HP4 1EH" "HP4 2JL" "HP4 3XZ" "HP4 3AB" "HP4 1SE" "HP4 1HN" "HP4 2PQ"
[401] "HP4 3HA" "HP4 3EN" "HP4 3YS" "HP4 2EL" "HP4 2ZW" "HP4 2AL" "HP4 3PR" "HP4 2DP" "HP4 1TJ" "HP4 3TE"
[411] "HP4 2HB" "HP4 3NT" "HP4 3RQ" "HP4 3RL" "HP4 2XT" "HP4 3PP" "HP4 3AE" "HP4 3LB" "HP4 3EJ" "HP4 3AR"
[421] "HP4 2DH" "HP4 2EB" "HP4 3DE" "HP4 1TB" "HP4 1DP" "HP4 3EG" "HP4 2LL" "HP4 3DS" "HP4 3QL" "HP4 3JY"
[431] "HP4 3UG" "HP4 3JJ" "HP4 1DA" "HP4 1JQ" "HP4 3AG" "HP4 3JN" "HP4 3NW" "HP4 3DA" "HP4 1AA" "HP4 1ET"
[441] "HP4 3NE" "HP4 2HE" "HP4 3DJ" "HP4 3YE" "HP4 9AX" "HP4 3SZ" "HP4 1EF" "HP4 3PX" "HP4 1FE" "HP4 3YF"
[451] "HP4 3NL" "HP4 1BE" "HP4 1BT" "HP4 1DU" "HP4 2HD" "HP4 3RN" "HP4 1HP" "HP4 2FT" "HP4 3SE" "HP4 1HZ"
[461] "HP4 1JZ" "HP4 2FX" "HP4 3EH" "HP4 3QZ" "HP4 3TX" "HP4 3BA" "HP4 3EX" "HP4 1JB" "HP4 3JQ" "HP4 2PB"
[471] "HP4 2EP" "HP4 2WF" "HP4 2HY" "HP4 1JG" "HP4 1HX" "HP4 2LJ" "HP4 1DY" "HP4 3UT" "HP4 3PT" "HP4 3HZ"
[481] "HP4 2TS" "HP4 3RB" "HP4 2JP" "HP4 1BP" "HP4 1HE" "HP4 3UJ" "HP4 3QG" "HP4 3TU" "HP4 1JE" "HP4 3PL"
[491] "HP4 3JT" "HP4 1JY" "HP4 2HU" "HP4 1DN" "HP4 1TD" "HP4 2BS" "HP4 1HJ" "HP4 3TA" "HP4 3EW" "HP4 3BZ"
[501] "HP4 1YP" "HP4 3SJ" "HP4 3QU" "HP4 3DL" "HP4 2EA" "HP4 3HX" "HP4 2HZ" "HP4 1SB" "HP4 2BX" "HP4 1HL"
[511] "HP4 2EU" "HP4 2HH" "HP4 1AP" "HP4 2HP" "HP4 2WQ" "HP4 2HF" "HP4 3YU" "HP4 1HY" "HP4 1AS" "HP4 2WA"
[521] "HP4 2EF" "HP4 3BW" "HP4 3JZ" "HP4 3BN" "HP4 1DH" "HP4 3BX" "HP4 3LG" "HP4 3PH" "HP4 2WG" "HP4 3SA"
[531] "HP4 1DR" "HP4 3SR" "HP4 1JN" "HP4 3EA" "HP4 2AB" "HP4 3DR" "HP4 3LD" "HP4 3BP" "HP4 1FJ" "HP4 3BY"
[541] "HP4 3FT" "HP4 3SW" "HP4 3YT" "HP4 3YL" "HP4 1SY" "HP4 9BF" "HP4 3ED" "HP4 3AW" "HP4 3SQ" "HP4 1DX"
[551] "HP4 3XL" "HP4 3EE" "HP4 3DD" "HP4 2DQ" "HP4 3AA" "HP4 1EB" "HP4 2HS" "HP4 3AZ" "HP4 3DF" "HP4 2GZ"
[561] "HP4 2GS" "HP4 2UJ" "HP4 2PA" "HP4 2FU" "HP4 2NY" "HP4 2QB" "HP4 2NL" "HP4 2PD" "HP4 2NF" "HP4 2PH"
[571] "HP4 2GT" "HP4 2GY" "HP4 2NN" "HP4 2NX" "HP4 2GW" "HP4 2NP" "HP4 2PL" "HP4 2PN" "HP4 2FW" "HP4 2LA"
[581] "HP4 2ET" "HP4 2EW" "HP4 2PG" "HP4 2NS" "HP4 2QD" "HP4 2ND" "HP4 2GU" "HP4 2NJ" "HP4 2NU" "HP4 2GR"
[591] "HP4 2ER" "HP4 2NE" "HP4 2PU" "HP4 2NT" "HP4 2PT" "HP4 2HA" "HP4 2PP" "HP4 2FZ" "HP4 2PJ" "HP4 2GX"
[601] "HP4 2PE" "HP4 2LS" "HP4 2GP" "HP4 2NH" "HP4 2ES" "HP4 2NR" "HP4 2BG" "HP4 3UR" "HP4 3ES" "HP4 1TA"
[611] "HP4 2WE"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment