Skip to content

Instantly share code, notes, and snippets.

@mapmeld
Created October 8, 2012 16:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mapmeld/3853444 to your computer and use it in GitHub Desktop.
Save mapmeld/3853444 to your computer and use it in GitHub Desktop.
Geo cases to MongoDB for GeoJSON timelines
# CasePost.py
import urllib, urllib2
geocases = open('allcasesplusgeo.csv', 'r')
def halfround(dec):
return round(dec * 3, 3) / 3
# one space each from Jan 1997 to Jun 2012
cases = [ ]
for year in range(1997, 2012):
for month in range(1, 13):
cases.append(0)
for thisyrmonth in range(1, 7):
cases.append(0)
for rawline in geocases:
line = rawline.split('"F"')[1].split(',')
lat = 0
lng = 0
opendate = ""
closedate = ""
try:
lat = halfround( float(line[2]) )
lng = halfround( float(line[1]) )
opendate = line[5]
closedate = line[3]
except:
continue
# post every geo case
startyear = int(opendate[0:4]) * 1 - 1997
if(startyear < 0):
# only counting cases after 1997
continue
startmonth = int(opendate[4:6]) * 1 - 1
startcell = 2000 + 12 * startyear + startmonth
endyear = startyear
endmonth = startmonth
if(len(closedate) < 8):
# go up to 1 year if there's no closedate
endyear = min(startyear + 1, 2012 - 1997)
endmonth = startmonth
else:
endyear = int(closedate[0:4]) * 1 - 1997
endmonth = int(closedate[4:6]) * 1 - 1
# max out end time at May/June 2012
endcell = min(2000 + 12 * endyear + endmonth, 2000 + 12 * (2012-1997) + 5)
# make the post to MaconMaps MongoDB TimePoint storage
timepoint = {
"start": startcell,
"end": endcell,
"lat": lat,
"lng": lng
}
data = urllib.urlencode(timepoint)
print opendate + " - " + urllib2.urlopen(urllib2.Request('http://maconmaps.herokuapp.com/timeline', data)).read()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment