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()
class CRoutesService(object): def __init__(self): self.cRoutesCollection = dbCRoute self.relationsCollection = dbRelationsCollection self.entityService = EntityService()#Registry.getInstance().getService("entity_service") def getDistinctRoutes(self): return self.entityService.getDistinctDocuments(self.cRoutesCollection,"from") def getDistinctStreets(self): return self.entityService.getDistinctDocuments(self.cRoutesCollection,"commonNodes") def getCommonStreetsBetweenRoutes(self,src,dest): query = {"from":src,"to":dest} return self.entityService.getDocument(self.cRoutesCollection,query) def getRouteInformation(self,osmId): query = {'osmId':osmId} return self.entityService.getDocument(dbRelationsCollection, query) def getRoutefromStreet(self,street): query = {"commonNodes":{"$in":[street]}} return self.entityService.getDocument(self.cRoutesCollection, query) def getcRoutesStreets(self,query): attrubutes_exclusion = {"_id":0,"street.nodes":0} return self.entityService.getDocuments2(dbCRoutesStreet, query,attrubutes_exclusion) def getcRoutesStreetsWithNames(self): attrubutes_exclusion = {"_id":0,"street.nodes":0} query = {"street.meta_data.name":{"$exists":True}} return self.entityService.getDocuments2(dbCRoutesStreet, query,attrubutes_exclusion) def getRoutefromStreet2(self,street): command = OrderedDict() command['distinct']=self.cRoutesCollection command['key']="from" command['query']={"commonNodes":{"$in":[street]}} return self.entityService.runCommand(command)
def processRelationBasedFSPResult(self,result,src_street,dest_street): if not('shortest_path' in result): raise InvalidMessageException("Invalid result from Shortest path, KeyError:'shortest_path' not found") path = deepcopy(result['shortest_path']) cRouteService = CRoutesService()#Registry.getInstance().getService("cRoutes_service") entityService = EntityService() coords_service = CoordsService() ways_collection = fsp2.PFinderConfig.dbWaysCollection i = len(path)-1 final_set = set() sum = 0 final_result = {} final_result['points'] = [] final_result['routes']= [] prev_point = entityService.getDocument(dbCRoutesStreet,{'street.osmId':src_street})['targetNode'] final_result['points'].append(prev_point) while(i>=1): common_streets = cRouteService.getCommonStreetsBetweenRoutes(path[i],path[i-1])['commonNodes'] target_street = entityService.getDocument(dbCRoutesStreet, {'street.osmId':common_streets[0]}) if target_street== None: i=i-1 continue point = target_street['targetNode'] sum = sum + Haversian.getInstance().calculateDistance(Coordinates(prev_point['lng'],prev_point['lat']),Coordinates(point['lng'],point['lat']) ) #dest = coords_service.getCoordinate(target_street['nodes'][len(target_street['nodes'])-1]) #sum = sum+Haversian.getInstance().calculateDistance(Coordinates(src['lng'],src['lat']),Coordinates(dest['lng'],dest['lat']) ) final_result['points'].append(point) #final_result['points'].append(dest) temp_route = cRouteService.getRouteInformation(path[i]) final_result['routes'].append({"osmId":temp_route['osmId'],"metaData":temp_route['metaData']}) prev_point = point #min_nodes = len(target_street['nodes']) #for j in range(1,len(common_streets)): #print (common_streets[i]) # street = entityService.getDocument(ways_collection, {'osmId':common_streets[j]}) # if street== None: # continue # if len(street['nodes'])<min_nodes: # min_nodes = len(street['nodes']) # target_street= street #print (target_street) #print (common_streets) i=i-1 final_result['points'].append(entityService.getDocument(dbCRoutesStreet,{'street.osmId':dest_street})['targetNode']) final_result['routes'].append(cRouteService.getRouteInformation(path[0])) if sum==0: point = entityService.getDocument(dbCRoutesStreet,{'street.osmId':dest_street})['targetNode'] sum=sum+Haversian.getInstance().calculateDistance(Coordinates(prev_point['lng'],prev_point['lat']),Coordinates(point['lng'],point['lat']) ) final_result['distance'] = sum return final_result
def __init__(self): self.registry = {} self.registry[RegistryServices.NODES_SERVICE] = NodeService() self.registry[ RegistryServices.CGRAPH_SERVICE] = ClusterGraphEntityService() self.registry[RegistryServices.FSP_SERVICE] = FSPService() self.registry[RegistryServices.GHOPER_SERVICE] = GraphHopperService() self.registry[RegistryServices.ENTITY_SERVICE] = EntityService() self.registry[RegistryServices.CROUTES_SERVICE] = CRoutesService()
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 CRoutesService(object): def __init__(self): self.cRoutesCollection = dbCRoute self.relationsCollection = dbRelationsCollection self.entityService = EntityService( ) #Registry.getInstance().getService("entity_service") def getDistinctRoutes(self): return self.entityService.getDistinctDocuments(self.cRoutesCollection, "from") def getDistinctStreets(self): return self.entityService.getDistinctDocuments(self.cRoutesCollection, "commonNodes") def getCommonStreetsBetweenRoutes(self, src, dest): query = {"from": src, "to": dest} return self.entityService.getDocument(self.cRoutesCollection, query) def getRouteInformation(self, osmId): query = {'osmId': osmId} return self.entityService.getDocument(dbRelationsCollection, query) def getRoutefromStreet(self, street): query = {"commonNodes": {"$in": [street]}} return self.entityService.getDocument(self.cRoutesCollection, query) def getcRoutesStreets(self, query): attrubutes_exclusion = {"_id": 0, "street.nodes": 0} return self.entityService.getDocuments2(dbCRoutesStreet, query, attrubutes_exclusion) def getcRoutesStreetsWithNames(self): attrubutes_exclusion = {"_id": 0, "street.nodes": 0} query = {"street.meta_data.name": {"$exists": True}} return self.entityService.getDocuments2(dbCRoutesStreet, query, attrubutes_exclusion) def getRoutefromStreet2(self, street): command = OrderedDict() command['distinct'] = self.cRoutesCollection command['key'] = "from" command['query'] = {"commonNodes": {"$in": [street]}} return self.entityService.runCommand(command)
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 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)
def __init__(self): self.cGraphCollection = dbClusterGraph self.entityService = EntityService()
def __init__(self): self.entityService = EntityService()
def __init__(self): self.entityService = EntityService() self.waysCollection = dbWaysCollection self.coordsService = CoordsService()
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)
def __init__(self): self.entityService = EntityService()
class ShortestFSP(object): def __init__(self,src,dest): self.graph = {} self.graphService = GraphEntityService() self.src = src self.dest = dest self.entityService = EntityService() def readGraph(self): self.graph = self.graphService.getGraph() def processGraph(self): queue = deque([]) #numEdges = 100#int(math.sqrt(self.graph.count())) #nodes_edges = 300 #dist = [float(99999000)]*(numEdges*nodes_edges) #srcNode = self.entityService.getDocument("graph2",{"from.osmId":{"$eq":self.src}})#self.graphService.getNodebyOSMId(self.src) #destNode = self.entityService.getDocument("graph2",{"from.osmId":{"$eq":self.dest}})#self.graphService.getNodebyOSMId(self.dest) srcNode = self.entityService.getDocument("cRoutes",{"from":{"$eq":self.src}})#self.graphService.getNodebyOSMId(self.src) destNode = self.entityService.getDocument("cRoutes",{"from":{"$eq":self.dest}})#self.graphService.getNodebyOSMId(self.dest) #distance from src node to itself is zero #print (srcNode) #print (destNode) #dist[int(srcNode['index'][0])] = 0 visited = {} dist = {} #dist[srcNode['from']['osmId']] = 0 # Enqueue src Node queue.append(srcNode) prev={}#[None]*(numEdges*nodes_edges) while len(queue) != 0: #print (queue) u = queue.popleft() uOsmId = u['from'] #print (uOsmId) if uOsmId == destNode['from']: print ("reached") #print (dist[destNode['index'][0]]) #return break #print(self.graphService.getNodeEdges(u['from']).count()) for edge in self.entityService.getDocuments("cRoutes",{"from":{"$eq":uOsmId}}):#self.graphService.getNodeEdges(u['from']): #print (edge) toOsmId = edge['to'] #print (toOsmId) #if not(toOsmId in dist): #dist[toOsmId] = INFINITY #temp = dist[uOsmId]+edge['dist'] #print (temp) '''dist[int(edge['index'][1])]''' if (not(toOsmId in visited) or (visited[toOsmId]==False or visited[toOsmId]== None)): #dist[int(edge['index'][1])] = temp #prev[int(edge['index'][1])] = u #dist[toOsmId] = temp prev[toOsmId] = uOsmId visited[toOsmId] = True #if not(any(obj['to']['osmId']== edge['to']['osmId'] for obj in queue)): #print (edge['index'][1]) queue.append(self.entityService.getDocument("cRoutes",{"from":{"$eq":edge['to']}})) # Stack with shortest path #print (dist) #print (prev) path = [] if self.src == self.dest: path.append(self.src) else: x = self.dest path.append(x) while prev[x]!=self.src: path.append(prev[x]) x= prev[x] path.append(prev[x]) result = {} result['shortest_path']=path #print (result) #result ['distance']=dist[self.dest] #print (result) return result def calculateDistance(self,result): routes = result['shortest_path']
def __init__(self): self.cRoutesCollection = dbCRoute self.relationsCollection = dbRelationsCollection self.entityService = EntityService()#Registry.getInstance().getService("entity_service")
def processRelationBasedFSPResult(self, result, src_street, dest_street): if not ('shortest_path' in result): raise InvalidMessageException( "Invalid result from Shortest path, KeyError:'shortest_path' not found" ) path = deepcopy(result['shortest_path']) cRouteService = CRoutesService( ) #Registry.getInstance().getService("cRoutes_service") entityService = EntityService() coords_service = CoordsService() ways_collection = fsp2.PFinderConfig.dbWaysCollection i = len(path) - 1 final_set = set() sum = 0 final_result = {} final_result['points'] = [] final_result['routes'] = [] prev_point = entityService.getDocument( dbCRoutesStreet, {'street.osmId': src_street})['targetNode'] final_result['points'].append(prev_point) while (i >= 1): common_streets = cRouteService.getCommonStreetsBetweenRoutes( path[i], path[i - 1])['commonNodes'] target_street = entityService.getDocument( dbCRoutesStreet, {'street.osmId': common_streets[0]}) if target_street == None: i = i - 1 continue point = target_street['targetNode'] sum = sum + Haversian.getInstance().calculateDistance( Coordinates(prev_point['lng'], prev_point['lat']), Coordinates(point['lng'], point['lat'])) #dest = coords_service.getCoordinate(target_street['nodes'][len(target_street['nodes'])-1]) #sum = sum+Haversian.getInstance().calculateDistance(Coordinates(src['lng'],src['lat']),Coordinates(dest['lng'],dest['lat']) ) final_result['points'].append(point) #final_result['points'].append(dest) temp_route = cRouteService.getRouteInformation(path[i]) final_result['routes'].append({ "osmId": temp_route['osmId'], "metaData": temp_route['metaData'] }) prev_point = point #min_nodes = len(target_street['nodes']) #for j in range(1,len(common_streets)): #print (common_streets[i]) # street = entityService.getDocument(ways_collection, {'osmId':common_streets[j]}) # if street== None: # continue # if len(street['nodes'])<min_nodes: # min_nodes = len(street['nodes']) # target_street= street #print (target_street) #print (common_streets) i = i - 1 final_result['points'].append( entityService.getDocument( dbCRoutesStreet, {'street.osmId': dest_street})['targetNode']) final_result['routes'].append( cRouteService.getRouteInformation(path[0])) if sum == 0: point = entityService.getDocument( dbCRoutesStreet, {'street.osmId': dest_street})['targetNode'] sum = sum + Haversian.getInstance().calculateDistance( Coordinates(prev_point['lng'], prev_point['lat']), Coordinates(point['lng'], point['lat'])) final_result['distance'] = sum return final_result
def __init__(self): #print ('reached constructor') self.entity_service = EntityService()
def __init__(self): self.cRoutesCollection = dbCRoute self.relationsCollection = dbRelationsCollection self.entityService = EntityService( ) #Registry.getInstance().getService("entity_service")
def __init__(self,src,dest): self.graph = {} self.graphService = GraphEntityService() self.src = src self.dest = dest self.entityService = EntityService()
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)