def project(lon, lat): def to_feature(distance, lon, lat): return point([float(lon), float(lat)], { 'dist': distance, }) json.dump( feature_collection([ point([float(lon), float(lat)], { 'marker-color': '#3ba048', }), ] + list(starmap(to_feature, lua('project_point', 150, lon, lat)))), open('./build/projection.json', 'w'))
def ways_from_gps(longitude, latitude): def get_coordinates(way): return [ list(map(float, coords)) for coords in lua('nodes_from_way', way) ] def ans_to_json(way, nearestnode): return line_string(get_coordinates(way)) json.dump(feature_collection( list(starmap( ans_to_json, lua('ways_from_gps', RADIUS, longitude, latitude) )) + [point([float(longitude), float(latitude)])] ), open('./build/ways_from_gps.geojson', 'w'), indent=2)
def to_feature(distance, lon, lat): return point([float(lon), float(lat)], { 'dist': distance, })
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'))