Skip to content

Instantly share code, notes, and snippets.

@mapmeld
Created August 24, 2012 19:34
Show Gist options
  • Save mapmeld/3454788 to your computer and use it in GitHub Desktop.
Save mapmeld/3454788 to your computer and use it in GitHub Desktop.
Upload housing cases into Neo4j graph database. Link them to their streets
# HouseNet.py
# upload housing cases into the graph database
import urllib, urllib2
cases = open('PROP1Cleanest.csv', 'r')
streetids = { }
skipto = "1903 houstonave";
skipcount = 0
for line in cases:
# add cases to neo4j
streetslug = line[1: line.find(',')]
streetslug = streetslug.lower().replace(' ','').replace('street','st').replace('avenue','ave').replace('lane','ln').replace('first','1st').replace('trail','trl').replace('place','pl').replace('road','rd').replace('cove','cv').replace('terrace','ter').replace('court','ct').replace('circle','cir').replace('second','2nd').replace('third','3rd').replace('fourth','4th').replace('fifth','5th').replace('sixth','6th').replace('seventh','7th').replace('eighth','8th').replace('ninth','9th')
# read in other values
remainder = line[line.find(',') : len(line)]
remainder = remainder[remainder.find('"') + 3 : len(remainder)]
remainder = remainder.split(',')
number = remainder[0][0 : remainder[0].find('"')]
closedate = remainder[5].replace(' ','')
opendate = remainder[7].replace(' ','')
action = remainder[9].replace('"','')
# skippable
if(skipcount > 0):
if(number + " " + streetslug == skipto):
skipcount = skipcount - 1
continue
# get street id from slug, so it can be followed
streetid = "-1"
if(streetids.has_key(streetslug)):
streetid = streetids[streetslug]
else:
streetid = urllib2.urlopen('http://localhost:3000/streetname/' + streetslug).read()
if(streetid == '-1'):
# street name not recognized
print "did not find " + streetslug
continue
streetids[streetslug] = streetid
values = {
"closedate": closedate,
"opendate": opendate,
"action": action,
"number": number
}
# create the point
data = urllib.urlencode(values)
pointid = urllib2.urlopen(urllib2.Request('http://localhost:3000/points', data)).geturl().split('points/')[1]
# have point follow street
values = {
"streetid": streetid
}
data = urllib.urlencode(values)
urllib2.urlopen(urllib2.Request('http://localhost:3000/points/' + pointid + '/follow', data)).read()
print number + " " + streetslug + " at " + pointid
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment