Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import simplejson
from urllib.request import urlopen
import urllib.request
import urllib.parse
from neo4j.v1 import GraphDatabase
import time
uri = "bolt://localhost:7687"
driver = GraphDatabase.driver(uri, auth=("neo4j", "changeYourPassword"))
GEOCODE_BASE_URL = 'https://maps.googleapis.com/maps/api/geocode/json'
GEOCODE_ROUTE_URL = 'https://maps.googleapis.com/maps/api/directions/json?origin=Stafford,VA&destination='
GEOCODE_ROUTE_START_END_URL = 'https://maps.googleapis.com/maps/api/directions/json?origin='
GEOCODE_API_KEY = 'yourGoogleAPIKey'
base64string = 'your Neo4j password in base64'
def geocode(fullAddress):
def geocode2(startAddress, endAddress):
# url = GEOCODE_BASE_URL + '?' + urllib.urlencode(geo_args)
url = GEOCODE_ROUTE_START_END_URL + urllib.parse.quote(startAddress) + '&destination=' + urllib.parse.quote(endAddress) + '&key=' + GEOCODE_API_KEY
request = urllib.request.Request(url)
request.add_header("Authorization", "Basic %s" % base64string)
result = simplejson.load(urlopen(request))
totalTravelTime = 0
# print simplejson.dumps([s['duration'] for s in result['routes'][0]['legs'][0]['steps']], indent=2)
print (len(result['routes']))
#print(result)
i=1
with driver.session() as session:
for y in result['routes'][0]['legs']:
endlat=y['end_location']['lat']
endlng=y['end_location']['lng']
startlat=y['start_location']['lat']
startlng=y['start_location']['lng']
# print(str(endlat) + "," + str(endlng))
numSteps = len(result['routes'][0]['legs'][0]['steps'])
# print(numSteps)
for s in result['routes'][0]['legs'][0]['steps']:
totalTravelTime = totalTravelTime + int(s['duration']['value'])
turnLat = s['end_location']['lat']
turnLng = s['end_location']['lng']
if i < 2:
neoQuery = "CALL com.dfauth.h3.lineBetweenLocations(" + str(startlat) + "," + str(startlng) + "," + str(turnLat) + "," + str(turnLng) +") yield nodes unwind nodes as locNode match (locNode)<-[:HAS_PRACTICE_AT]-(p:Provider)-[:HAS_SPECIALTY]->(t:TaxonomyCode) return distinct locNode.Address1 + ' ' + locNode.CityName +', ' + locNode.StateName as locationAddress, locNode.latitude as latitude, locNode.longitude as longitude, coalesce(p.BusinessName,p.FirstName + ' ' + p.LastName) as practiceName, p.NPI as NPI order by locationAddress;"
# print(neoQuery)
result = session.run(neoQuery)
for record in result:
print(record["locationAddress"] + " " + record["practiceName"])
if i > 1 and i < numSteps:
neoQuery = "CALL com.dfauth.h3.lineBetweenLocations(" + str(prevLat) + "," + str(prevLng) + "," + str(turnLat) + "," + str(turnLng) +") yield nodes unwind nodes as locNode match (locNode)<-[:HAS_PRACTICE_AT]-(p:Provider)-[:HAS_SPECIALTY]->(t:TaxonomyCode) return distinct locNode.Address1 + ' ' + locNode.CityName +', ' + locNode.StateName as locationAddress, locNode.latitude as latitude, locNode.longitude as longitude, coalesce(p.BusinessName,p.FirstName + ' ' + p.LastName) as practiceName, p.NPI as NPI order by locationAddress;"
result = session.run(neoQuery)
for record in result:
print(record["locationAddress"] + " " + record["practiceName"])
if i > (numSteps-1):
# print(i)
neoQuery = "CALL com.dfauth.h3.lineBetweenLocations(" + str(prevLat) + "," + str(prevLng) + "," + str(endlat) + "," + str(endlng) +") yield nodes unwind nodes as locNode match (locNode)<-[:HAS_PRACTICE_AT]-(p:Provider)-[:HAS_SPECIALTY]->(t:TaxonomyCode) return distinct locNode.Address1 + ' ' + locNode.CityName +', ' + locNode.StateName as locationAddress, locNode.latitude as latitude, locNode.longitude as longitude, coalesce(p.BusinessName,p.FirstName + ' ' + p.LastName) as practiceName, p.NPI as NPI order by locationAddress;"
# print(neoQuery)
result = session.run(neoQuery)
for record in result:
print(record["locationAddress"] + " " + record["practiceName"])
prevLat = turnLat
prevLng = turnLng
i += 1
print("Total Travel Time in minutes: " + str(float(float(totalTravelTime)/60)))
session.close()
def travelTime(postalCode, taxonomyCode):
print("My zip code is: " + postalCode)
print("My desired physician taxonomy is: " + taxonomyCode)
url=NEAREST_PHYSICIAN_URL + postalCode + '/' + taxonomyCode
request = urllib2.Request(url)
request.add_header("Authorization", "Basic %s" % base64string)
result = simplejson.load(urllib2.urlopen(request))
for s in result['Providers']:
print("Nearest Address is " + s['FullAddress'])
geocode(s['FullAddress'])
if __name__ == '__main__':
#travelTime('22554','101YA0400X')
geocode2('700 Courthouse Road, Stafford, VA 22554','50 N. Stafford Complex, Center St Suite 107, Stafford, VA 22556')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.