Last active
August 29, 2015 13:56
-
-
Save phreid/8979852 to your computer and use it in GitHub Desktop.
Migration map
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
import csv | |
import re | |
from pygeocoder import Geocoder | |
infile = open('mig_data.csv') | |
outfile = open('geo_data.csv','wb') | |
csvfile = list(csv.DictReader(infile)) | |
fieldnames = ['origin_long', 'dest_long', 'dest_lat', 'MIGMOVE', | |
'Ref_Date', 'Coordinate','tempdest', 'Vector', 'Value', | |
'GEODEST','tempgeo', 'GEO', 'origin_lat'] | |
csvwriter = csv.DictWriter(outfile, fieldnames=fieldnames) | |
unique = {} | |
for line in csvfile: | |
# Non-robust way to get around Unicode import errors from French characters. | |
line['GEO'] = line['GEO'].replace('\xe9','e') | |
line['GEO'] = line['GEO'].replace('\xe8','e') | |
line['GEODEST'] = line['GEODEST'].replace('\xe9','e') | |
line['GEODEST'] = line['GEODEST'].replace('\xe8','e') | |
# Pasting " City, Canada" makes sure we get the right coordinates | |
line['tempgeo'] = re.split('[,\-/]',line['GEO'])[0] + " City, Canada" | |
line['tempdest'] = re.split('[,\-/]',line['GEODEST'])[0] + " City, Canada" | |
# After the previous steps we end up with "Non City, Canada" records (from | |
# Non-Census Metropolitan Areas | |
if (line['tempgeo'] not in unique.keys() or line['tempdest'] not in unique.keys()) \ | |
and line['tempgeo'] != "Non City, Canada" and line['tempdest'] != "Non City, Canada": | |
unique[line['tempgeo']] = Geocoder.geocode(line['tempgeo']).coordinates | |
unique[line['tempdest']] = Geocoder.geocode(line['tempdest']).coordinates | |
csvwriter.writeheader() | |
for line in csvfile: | |
geo = line['tempgeo'] | |
geodest = line['tempdest'] | |
if geo != "Non City, Canada" and geodest != "Non City, Canada": | |
line['origin_lat'] = unique[geo][0] | |
line['origin_long'] = unique[geo][1] | |
line['dest_lat'] = unique[geodest][0] | |
line['dest_long'] = unique[geodest][1] | |
csvwriter.writerow(line) | |
infile.close() | |
outfile.close() |
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
library(mapdata) | |
library(geosphere) | |
library(stringr) | |
setwd("~") | |
geodata = read.csv("geo_data.csv") | |
geodata$Value = as.numeric(as.character(geodata$Value)) | |
geodata = cbind(as.data.frame(str_split_fixed(geodata$GEO,",",2)),geodata) | |
geodata = cbind(as.data.frame(str_split_fixed(geodata$GEODEST,",",2)),geodata) | |
names(geodata)[1] = "dest_city" | |
names(geodata)[2] = "dest_province" | |
names(geodata)[3] = "origin_city" | |
names(geodata)[4] = "origin_province" | |
keepcols = c("origin_city","dest_city","MIGMOVE","Value", | |
"origin_lat","origin_long","dest_lat","dest_long", "Ref_Date") | |
geodata = geodata[keepcols] | |
majcmas = c("Vancouver","Calgary","Edmonton","Toronto","Ottawa-Gatineau","Montreal") | |
par(mfrow=c(2,3),mar=c(1,1,1,1),oma=c(0,0,0,0)) | |
for (c in majcmas){ | |
map('worldHires','Canada',col="lightgrey",border="white", fill=TRUE,ylim=c(35,70)) | |
gsub = geodata[geodata$dest_city == c & geodata$MIGMOVE == "Net-migration" | |
& geodata$Ref_Date == "2011",] | |
dest_long = gsub$dest_long[1] | |
dest_lat = gsub$dest_lat[1] | |
for (i in 1:length(gsub$origin_city)){ | |
city1 = gsub[i,] | |
gc = gcIntermediate(c(city1$origin_long,city1$origin_lat),c(dest_long,dest_lat),n=50,addStartEnd=TRUE) | |
if (gsub[i,]$Value < 0) lines(gc,col="#0078BF",lwd=0.5) | |
else lines(gc,col="red",lwd=0.5) | |
}} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment