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)


Calling Google Maps API for 235 Albany Street, Cambridge
it is located at -71.101214/42.358698
Calling Google Maps API for 31 Ames Street, Cambridge
it is located at -71.088687/42.361884
3476 edges found within miniGraph
the closest node found to startPt is [-71.100124, 42.3589794], with dist 94.8850617077
the closest node found to endPt is [-71.088876, 42.3619774], with dist 18.6942514381
shortest Path dist between the points are 1131.05041024
The shortest path dist 1131.05041024 is already shorter than targetDis
current score is (8.800097764772044, 0.17124120956190425, 0.0, 1.8884140509144833)
The actual path dis is 1131.05041024

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]:
(-71.115000000000009, -71.075000000000003, 42.350000000000001, 42.375)

In [9]:
print 'worst score is '+str(myPath['worstScore'])


worst score is (8.800097764772044, 0.3881080632296259, 0.0, 4.0570825875917)

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]:
(-71.109999999999999,
 -71.075000000000003,
 42.352000000000004,
 42.368000000000002)

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')


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-3-d31163f1279f> in <module>()
----> 1 fig = plt.figure(figsize=(20, 20),dpi=dpi)
      2 ax = fig.gca()
      3 miniWorldPlot(myPath['miniGraph'])
      4 onepath = myPath['pathDict'][1]
      5 path = onepath['path']

NameError: name 'dpi' is not defined

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]:
(-71.109999999999999,
 -71.075000000000003,
 42.352000000000004,
 42.368000000000002)

In [ ]: