def triangles(): coords = loadcoords() features = [] newlinecoords = [] for n1, n2, n3 in zip(coords[0:-2], coords[1: -1], coords[2:]): # features.append(polygon([n1, n2, n3, n1])) mid = [ (n1[0]+n2[0])/2, (n1[1]+n2[1])/2, ] prop = 2/3 newlinecoords.append([ n3[0] + (mid[0] - n3[0])*prop, n3[1] + (mid[1] - n3[1])*prop, ]) features.append(line_string([coords[0]] + newlinecoords + [coords[-1]])) features.append(line_string(coords)) json.dump( feature_collection(features), open('./build/triangles.geojson', 'w') )
def osrmresponse(responsefile): def to_coordinate(tracepoint): return tracepoint['location'] data = json.load(open(responsefile)) json.dump(feature_collection([ line_string(map(to_coordinate, data['tracepoints'])), ]), sys.stdout)
def a_star(fromnode, tonode, skip_node=None): loadlua() route = lua('a_star', fromnode, tonode, skip_node) if route == 0: return 'Route not found' json.dump(feature_collection([ line_string(list(map(float, coords)) for coords in route[1]) ]), open('./build/a_star.geojson', 'w')) return route
def ans_to_json(way, nearestnode): return line_string(get_coordinates(way))
def mapmatch(layers): coordinates = loadcoords() closest_ways = [ lua('ways_from_gps', RADIUS, *coords) for coords in coordinates ] parents = dict() for layer in range(1, min(int(layers), len(coordinates))): print('processing layer {}'.format(layer)) total_links = len(closest_ways[layer-1])*len(closest_ways[layer]) count = 0 best_of_layer = None best_of_layer_cost = INF for wayt, nearestnodet in closest_ways[layer]: # t for to best_cost = INF best_parent = None best_path = None for wayf, nearestnodef in closest_ways[layer-1]: # f for from parent = parents.get(Node.hash(layer-1, wayf)) if layer > 1 and parent is None: # these nodes are not useful as their parents couldn't be # routed continue try: length, path = lua( 'a_star', nearestnodef, nearestnodet, parent.skip_node if parent is not None else None ) except TypeError: continue # no route from start to end # difference between path length and great circle distance # between the two gps points curcost = log(abs( length - d(*(coordinates[layer-1]+coordinates[layer])) )) # distance between start of path and first gps point curcost += d(*(coordinates[layer-1] + list(map( float, red.geopos('base:nodehash', nearestnodef)[0] )))) # distance between end of path and second gps point curcost += d(*(coordinates[layer] + list(map( float, red.geopos('base:nodehash', nearestnodet)[0] )))) if parent: curcost += parent.cost if curcost < best_cost: best_cost = curcost best_parent = parent best_path = path count += 1 print('processed {} of {} links for this layer'.format( count, total_links), end='\r', flush=True ) try: if len(best_path) >= 2: skip_node = best_path[-2][2] elif len(best_path) == 1: skip_node = best_parent.skip_node if best_parent else None except TypeError: continue # No feasible route to get here newnode = Node( layer=layer, way=wayt, cost=best_cost, path=best_path, parent=best_parent, skip_node=skip_node, ) parents[Node.hash(layer, wayt)] = newnode if newnode.cost < best_of_layer_cost: best_of_layer = newnode best_of_layer_cost = newnode.cost print() curnode = best_of_layer lines = [] while curnode is not None: if len(curnode.path) == 1: lines.append(point(map(float, curnode.path[0]))) else: lines.append(line_string( (list(map(float, pos)) for pos in curnode.path), { 'layer': curnode.layer, 'way': curnode.way.decode('utf8'), } )) curnode = curnode.parent json.dump(feature_collection(lines + [line_string(coordinates, { 'stroke': '#000000', 'stroke-width': 4, 'stroke-opacity': .5, })]), open('./build/result.geojson', 'w'))
def compute(): from hashids import Hashids data = json.load(open('./data/route.geojson')) coordinates = data['features'][0]['geometry']['coordinates'] hashes = Hashids(salt='a salt', min_length=6, alphabet='0123456789ABCDEF') # Using dijskra to find the best route match heap = [Edge()] path = None while len(heap) > 0: edge = heappop(heap) print(edge) # add to the heap the nodes reachable from current node for way, dist, nearestnode in lua('ways_from_gps', 150, edge.to_layer, *coordinates[edge.to_layer]): weigth = float(dist) + edge.weigth if edge.from_street: # compare the great circle distance between gps positions with # the shortest path length from street 1 to street 2 # gpsdist = distance(*(coordinates[edge.to_layer] + coordinates[edge.to_layer+1])) # weigth += abs(gpsdist - lua('a_star', edge.to_nearestnode, nearestnode)) pass newedge = Edge( weigth=weigth, to_layer=edge.to_layer + 1, from_street=edge.to_street, to_street=way.decode('utf8'), from_nearestnode=edge.to_nearestnode, to_nearestnode=nearestnode, parent=edge, ) heappush(heap, newedge) # last GPS position visited, route finished # if edge.to_layer == len(coordinates)-1: if edge.to_layer == 3: break curedge = edge streets = set() while curedge != None: if curedge.from_street: streets.add(curedge.from_street) if curedge.to_street: streets.add(curedge.to_street) curedge = curedge.parent json.dump( feature_collection([ line_string( list(map(lambda x: float(x), nodepos)) for nodepos in lua('nodes_from_way', street)) for street in streets ] + [ line_string(coordinates, { 'stroke': '#000000', 'stroke-width': 4, 'stroke-opacity': .5, }) ]), open('./build/result.geojson', 'w'))