class RelationsBasedRouting(object): def __init__(self): #print ('reached constructor') self.entity_service = EntityService() def connectRoutes(self): MAX_BUCKET_SIZE = 500 bucket = [] pedestrian_routes = self.entity_service.getAllDocuments(dbRelationsCollection) for i in range(0,pedestrian_routes.count()): src = pedestrian_routes[i] print (src['osmId']) src_ways = set(pedestrian_routes[i]['refs']) for j in range(0,pedestrian_routes.count()): if pedestrian_routes[j]['osmId']!=pedestrian_routes[i]['osmId']: common_nodes = list(src_ways.intersection(pedestrian_routes[j]['refs'])) if(len(common_nodes)>0): result={} result['from'] = src['osmId'] result['to'] = pedestrian_routes[j]['osmId'] result['commonNodes'] = common_nodes if len(bucket) == MAX_BUCKET_SIZE: self.entity_service.addDocuments(dbCRoute, bucket) print ("bulk update of "+str(MAX_BUCKET_SIZE)+" is completed") del bucket[:] #print (bucket) bucket.append(result) #else: #print ("common streets not found between "+str(src["osmId"])+" and "+str(pedestrian_routes[j]['osmId'])) if len(bucket)>0: self.entity_service.addDocuments(dbCRoute, bucket) print ("Residual bulk update in progress") del bucket[:] def addDistinctStreets(self): #print ('here') cRouteService = Registry.getInstance().getService("cRoutes_service") #print ("reached") streets=cRouteService.getDistinctStreets() #print (streets) coords_service = CoordsService() ways_collection = fsp2.PFinderConfig.dbWaysCollection for street in streets: target_street = self.entity_service.getDocument(ways_collection, {'osmId':street}) if target_street== None: continue result = {} result['targetNode'] = coords_service.getCoordinate(target_street['nodes'][0]) result['street'] = target_street #print (result) self.entity_service.addDocument(dbCRoutesStreet, result)
class GraphEntityService(object): def __init__(self): self.entityService = EntityService() def serialzeGraph(self,graph): self.entityService.addDocument(dbGraph, graph) def getGraph(self): return self.entityService.getAllDocuments(dbGraph) def getGraphEdges(self): query = {"edge":{"$ne":"null"}} return self.entityService.getDocuments(dbGraph, query) def getNodeEdges(self,osmId): query = {"edge":{"$ne":None},"from":osmId} #print (query) return self.entityService.getDocuments(dbGraph, query) def getNodebyOSMId(self,osmId): query = {"from":osmId} return self.entityService.getDocument(dbGraph, query)
class GraphEntityService(object): def __init__(self): self.entityService = EntityService() def serialzeGraph(self, graph): self.entityService.addDocument(dbGraph, graph) def getGraph(self): return self.entityService.getAllDocuments(dbGraph) def getGraphEdges(self): query = {"edge": {"$ne": "null"}} return self.entityService.getDocuments(dbGraph, query) def getNodeEdges(self, osmId): query = {"edge": {"$ne": None}, "from": osmId} #print (query) return self.entityService.getDocuments(dbGraph, query) def getNodebyOSMId(self, osmId): query = {"from": osmId} return self.entityService.getDocument(dbGraph, query)
class ClusterGraphEntityService(object): def __init__(self): self.cGraphCollection = dbClusterGraph self.entityService = EntityService() def addClusterGraphEdge(self,doc): self.entityService.addDocument(self.cGraphCollection, doc) def addClusterEdge(self,node): self.entityService.addDocument(dbClusterEdges, node) def getClusterEdges(self): return self.entityService.getAllDocuments(dbClusterEdges) def getClusterCenters(self): query ={"targetNodeCluster":{"$eq":None}} return self.entityService.getDocuments(dbClusterEdges,query) def isClusterCenter(self,osmId): query = {} def getClusterEdges2(self,targetNodeCluster): query ={"targetNodeCluster":{"$eq":targetNodeCluster}} return self.entityService.getDocuments(dbClusterEdges,query) def getStreetfromNode(self,nodeId): query = {"targetNode.osmId":nodeId} #print (query) return self.entityService.getDocument(dbClusterEdges, query)
class EdgeBasedGraph(object): def __init__(self): self.entityService = EntityService() self.waysCollection = dbWaysCollection self.coordsService = CoordsService() def parseEdgesForConnectivity(self): edges=self.entityService.getAllDocuments(self.waysCollection) counter = 0 for i in range(0,100): tempNodes = edges[i]['nodes'] #self.calculateHaversian(edges[i],tempNodes,counter) counter=counter+len(tempNodes) self.findMinEdgeNode(i,tempNodes[len(tempNodes)-1],edges,counter,100) def calculateHaversian(self,nodesEdge,nodes,counter): for i in range(0,len(nodes)-1): fromNode = self.coordsService.getCoordinate(nodes[i]) toNode = self.coordsService.getCoordinate(nodes[i+1]) dist=Haversian.getInstance().calculateDistance(Coordinates(fromNode['lng'],fromNode['lat']),Coordinates(toNode['lng'],toNode['lat'])) graphNode = {} graphNode['index']=(i+counter,(i+1)+counter) graphNode['from'] = fromNode graphNode['to'] = toNode #graphNode['edge']= nodesEdge graphNode['dist']=dist self.entityService.addDocument("graph1", graphNode) graphNode1 = {} graphNode1['index']=((i+1)+counter,i+counter) graphNode1['from'] = toNode graphNode1['to'] = fromNode graphNode1['dist']=dist self.entityService.addDocument("graph1", graphNode1) #return len(nodes) def findMinEdgeNode(self,targetEdge,osmId,edges,counter,upper_limit): minDist = INFINITY query = {"from.osmId":{"$eq":osmId}} targetNodeCoords = self.entityService.getDocument("graph2", query)['from'] #print(targetNodeCoords) minFirstNodeCoords = None nodesCount = 0 for i in range(0,upper_limit): if targetEdge == i: nodesCount += len(edges[i]['nodes']) continue firstNode = edges[i]['nodes'][0] #print (firstNode) query={"from.osmId":{"$eq":firstNode}} if firstNode!=osmId: firstNodeCoords = self.entityService.getDocument("graph2",query)["from"] dist = Haversian.getInstance().calculateDistance(Coordinates(firstNodeCoords['lng'],firstNodeCoords['lat']),Coordinates(targetNodeCoords['lng'],targetNodeCoords['lat'])) if dist < minDist: minDist = dist minFirstNodeCoords= firstNodeCoords index = nodesCount nodesCount += len(edges[i]['nodes']) #print (minDist) graphNode = {} graphNode['index']=(counter-1,index) graphNode['from'] = targetNodeCoords graphNode['to'] = minFirstNodeCoords #graphNode['edge']= nodesEdge graphNode['dist']=minDist #print (graphNode) self.entityService.addDocument("graph2", graphNode) graphNode = {} graphNode['index']=(index,counter-1) graphNode['from'] = minFirstNodeCoords graphNode['to'] = targetNodeCoords graphNode['dist']=minDist #print (graphNode) self.entityService.addDocument("graph2", graphNode)
class ClusterGraph(object): def __init__(self, K): self.no_of_clusters = K self.clusters_center = [None] * K self.cluster_graph_collection = "cGraph_V1" self.clusters_collection = "cEdges_V1" self.entityService = EntityService() self.coodrdsService = CoordsService() self.cGraphService = ClusterGraphEntityService() def identifyClusters(self): N = self.entityService.getNumberOfDocuments("ways1") for i in range(0, self.no_of_clusters): r = math.floor(random() * N) randomEdge = self.entityService.getRandomDocument("ways1", {}, r) edgeNode = randomEdge[0]['nodes'][0] edgeCoords = self.coodrdsService.getCoordinate(edgeNode) cEdgeNode = {} cEdgeNode['street'] = randomEdge[0] cEdgeNode['targetNode'] = edgeCoords self.entityService.addDocument(self.clusters_collection, cEdgeNode) def findMinEdgeNode(self, targetEdge, start_index, edges): minDist = INFINITY #query = {"from.osmId":{"$eq":osmId}} targetNodeCoords = targetEdge[ 'targetNode'] #self.entityService.getDocument("graph2", query)['from'] #print(targetNodeCoords) minFirstNodeCoords = None for i in range(start_index, edges.count()): firstNode = edges[i]['targetNode'] if edges[i]['street']['osmId'] != targetEdge['street']['osmId']: dist = Haversian.getInstance().calculateDistance( Coordinates(firstNode['lng'], firstNode['lat']), Coordinates(targetNodeCoords['lng'], targetNodeCoords['lat'])) if dist < minDist: minDist = dist minFirstNodeCoords = firstNode result = {} result['from'] = targetNodeCoords result['to'] = minFirstNodeCoords result['distance'] = minDist return result #print (minDist) #print (graphNode) def addGraphEdge(self, targetNodeCoords, minFirstNodeCoords, minDist): graphNode = {} #graphNode['index']=(counter-1,index) graphNode['from'] = targetNodeCoords graphNode['to'] = minFirstNodeCoords #graphNode['edge']= nodesEdge graphNode['dist'] = minDist #print (graphNode) #self.entityService.addDocument(self.cluster_graph_collection, graphNode) self.cGraphService.addClusterGraphEdge(graphNode) graphNode = {} #graphNode['index']=(index,counter-1) graphNode['from'] = minFirstNodeCoords graphNode['to'] = targetNodeCoords graphNode['dist'] = minDist self.cGraphService.addClusterGraphEdge(graphNode) #self.entityService.addDocument(self.cluster_graph_collection, graphNode) def connectClusters(self): streets = self.entityService.getAllDocuments(self.clusters_collection) for i in range(0, streets.count() - 1): node = self.findMinEdgeNode(streets[i], (i + 1), streets) #processedEdges) #node = self.findMinEdgeNode(streets[i], 0,streets)#processedEdges) self.addGraphEdge(node['from'], node['to'], node['distance']) #processedEdges.remove(street) def performEdgeClustering(self): cluster_centers = self.cGraphService.getClusterCenters() cluster_centers_copy = cluster_centers.clone() print(cluster_centers.count()) streets = self.entityService.getAllDocuments(dbWaysCollection) for i in range(0, 10): if self.isClusterCenter(streets[i]['osmId'], cluster_centers_copy): print( "Encountered cluster center, skipping cluster assignment") continue targetEdge = {} targetEdge['street'] = streets[ i] #self.entityService.getDocument("graph2",{"from.osmId":{"$eq":streets[i]['nodes'][0]}}) tempNode = self.entityService.getDocument( "graph2", {"from.osmId": { "$eq": streets[i]['nodes'][0] }}) if tempNode == None: tempNode = self.coodrdsService.getCoordinate( streets[i]['nodes'][0]) else: tempNode = tempNode['from'] targetEdge['targetNode'] = tempNode targetEdge['targetNodeCluster'] = self.findMinEdgeNode( targetEdge, 0, cluster_centers)['to']['osmId'] self.cGraphService.addClusterEdge(targetEdge) def connectEdgeswithInClusters(self): cluster_centers = self.cGraphService.getClusterCenters() for i in range(0, cluster_centers.count()): cluster_edges = self.cGraphService.getClusterEdges2( cluster_centers[i]['targetNode']['osmId']) if (cluster_edges.count() == 0): print( "skipping edge cluster connection, no edge exists in cluster" ) continue for j in range(0, cluster_edges.count() - 1): node = self.findMinEdgeNode(cluster_edges[j], (j + 1), cluster_edges) #node = self.findMinEdgeNode(cluster_edges[j], 0,cluster_edges) self.addGraphEdge(node['from'], node['to'], node['distance']) #print (node) node = self.findMinEdgeNode(cluster_centers[i], 0, cluster_edges) self.addGraphEdge(node['from'], node['to'], node['distance']) #print ("--------------------------") def isClusterCenter(self, osmId, centers): return any(obj['street']['osmId'] == osmId for obj in centers)