In [8]:
import psycopg2
from Vertex import Vertex
from Graph import Graph
from GeoCode import GeoCode
from distCal import lonlat2ID, cor2ID, distanceCal, calPathDis, R
def createMiniWorld(miniGraph, startPt, endPt):
#define the boundary of the miniworld
padding = 0.01
minLng = min(startPt[0], endPt[0])-padding
maxLng = max(startPt[0], endPt[0])+padding
minLat = min(startPt[1], endPt[1])-padding
maxLat = max(startPt[1], endPt[1])+padding
try:
conn = psycopg2.connect("dbname='CamBosRoad' user = 'postgres' host = 'localhost' password='p2k007mm'")
cur = conn.cursor()
except:
print 'connection problem'
cur.execute("SELECT * FROM Edges WHERE fnodelng>{} and fnodelat>{} \
and fnodelng<{} and fnodelat<{} \
".format(minLng, minLat, maxLng, maxLat))
miniDB = cur.fetchall()
#for data in miniDB:
# print data
print ('{} edges found within miniGraph').format(len(miniDB))
for edge in miniDB:
fnode = (edge[0], edge[1])
tnode = (edge[2], edge[3])
miniGraph.addEdge_directional(fnode, tnode, edge[4], edge[5])
return [[minLat, minLng],[maxLat, maxLng]]
def PathTestMashUp(startPt, endPt, targetDis):
startCor = GeoCode(startPt);
endCor = GeoCode(endPt)
miniGraph = Graph()
bounds = createMiniWorld(miniGraph, startCor, endCor)
startNode = [0, 0]
dist = miniGraph.findNearestNode(startCor, startNode)
print 'the closest node found to startPt is '+ str(startNode) +', with dist '+str(dist)
endNode = [0, 0]
dist = miniGraph.findNearestNode(endCor, endNode)
print 'the closest node found to endPt is '+str(endNode) +', with dist '+str(dist)
startNode = cor2ID(startNode)
endNode = cor2ID(endNode)
#myPath = miniGraph.Dijkstra(startNode, endNode)
myPath = miniGraph.MonteCarloBestPath(startNode, endNode, targetDis)
print 'The actual path dis is '+ str(myPath['dist'])
return {'miniGraph': miniGraph,
'startPt':startCor,
'endPt':endCor,
'startNode':startNode,
'endNode':endNode,
'dist': myPath['dist'],
'path': myPath['path'],
}
In [9]:
myPath = PathTestMashUp('235 Albany Street, Cambridge', '31 Ames Street, Cambridge', 800)
#myPath = PathTestMashUp('Kendal/MIT, Cambridge', 'Central Square, Cambridge', 3000)
#myPath = PathTestMashUp('Chinatown, Boston', 'Musuem of Fine Arts, Boston', 4000)
In [11]:
padding = .01
minX = -71.1013
minY = 42.3530
maxX = -71.0759
maxY = 42.3681
def miniWorldPlot(miniGraph):
for v in miniGraph.vertList:
for u in miniGraph.vertList[v].getConnections():
#for demo purpuse only
if (v[1]<minY or u[1]<minY):
continue
#if (v[1]<minY-padding and u[1]<minY-padding) or (u[1]>maxY+padding and v[1]>maxY+padding):
# continue
x = (v[0], u[0])
y = (v[1], u[1])
ax.plot(x, y, 'c.-')
import matplotlib.pyplot as plt
fig = plt.figure(figsize=(20, 20),dpi=800)
ax = fig.gca()
miniWorldPlot(myPath['miniGraph'])
startPt = myPath['startPt']
ax.plot(startPt[0], startPt[1],'r+')
endPt = myPath['endPt']
ax.plot(endPt[0], endPt[1],'r+')
startNode = myPath['startNode']
ax.plot(startNode[0], startNode[1],'ro')
endNode = myPath['endNode']
ax.plot(endNode[0], endNode[1],'ro')
'''
colorStr = 'ygbkrm'
ix=0
path = myPath['path']
x = [i for i,j in path]
y = [j for i,j in path]
ax.plot(x, y, 'ro-')
print 'worst dis is '+str(myPath['dist'])
'''
ax.axis('scaled')
Out[11]:
In [9]:
print 'worst score is '+str(myPath['worstScore'])
In [19]:
fig = plt.figure(figsize=(20, 20),dpi=dpi)
ax = fig.gca()
miniWorldPlot(myPath['miniGraph'])
onepath = myPath['pathDict'][0]
path = onepath['path']
x = [i for i,j in path]
y = [j for i,j in path]
ax.plot(x, y, 'ro-')
ax.axis('scaled')
Out[19]:
In [3]:
fig = plt.figure(figsize=(20, 20),dpi=dpi)
ax = fig.gca()
miniWorldPlot(myPath['miniGraph'])
onepath = myPath['pathDict'][1]
path = onepath['path']
x = [i for i,j in path]
y = [j for i,j in path]
ax.plot(x, y, 'ro-')
ax.axis('scaled')
In [22]:
fig = plt.figure(figsize=(20, 20),dpi=dpi)
ax = fig.gca()
miniWorldPlot(myPath['miniGraph'])
onepath = myPath['pathDict'][5]
path = onepath['path']
x = [i for i,j in path]
y = [j for i,j in path]
ax.plot(x, y, 'ro-')
ax.axis('scaled')
Out[22]:
In [ ]: