Ejemplo n.º 1
0
def route_direct(file_name,
                 source_id=None,
                 target_id=None,
                 out_file=None,
                 overpass_url=None,
                 disk_cache=False,
                 visualize=False):
    from limic.util import start, end, status, file_size, load_pickled, save_pickled, save_path, options, replace
    if disk_cache:
        start("Using disk cache", file_name)
        set_option('disk_cache', file_name)
    from limic.overpass import region, set_server
    if disk_cache:
        status("OK")
    from os.path import exists
    if not disk_cache and exists(file_name):
        start("Loading", file_name)
        region.backend._cache = load_pickled(file_name)
        end('')
        file_size(file_name)
    len_cache = len(region.backend._cache)
    start("Routing using direct algorithm")
    set_server(overpass_url)
    path = astar_direct(source_id, target_id)
    end()
    start("Saving path to", out_file)
    save_path(path, out_file, visualize)
    end()
    if not disk_cache and len_cache != len(region.backend._cache):
        file_name_tmp = file_name + ".tmp"
        start("Saving to", file_name, "via", file_name_tmp)
        save_pickled(file_name_tmp, region.backend._cache)
        replace(file_name_tmp, file_name)
        end('')
        file_size(file_name)
Ejemplo n.º 2
0
def prune_nx(file_name_in, file_name_out, polygon, overpass_url):
    from limic.util import start, end, file_size, status, save_pickled, load_pickled, check_overwrite
    if not check_overwrite(file_name_in, file_name_out):
        return
    start("Loading from", file_name_in)
    g = load_pickled(file_name_in)
    end('')
    file_size(file_name_in)
    polygon = list(map(float, polygon))
    polygon = list(zip(polygon[::2], polygon[1::2]))
    if not overpass_url:
        from limic.util import kdtree, nodes_in_geometry
        start("Building kd-tree from nodes")
        tree = kdtree(g.nodes(), get_latlon=lambda x: (x[1], x[2]))
        end()
        start("Querying tree for nodes in polygon")
        nodes = nodes_in_geometry(tree, polygon)
    else:
        from limic.overpass import nodes_in_geometry, set_server
        start("Query server for nodes in polygon")
        set_server(overpass_url)
        nodes = nodes_in_geometry(polygon)
    end('')
    status(len(nodes))
    start("Pruning graph")
    h = prune_ids_nx(g, nodes)
    end()
    start("Saving to", file_name_out)
    save_pickled(file_name_out, h)
    end('')
    file_size(file_name_out)
Ejemplo n.º 3
0
def prune_gt(file_name_in, file_name_out, polygon, overpass_url):
    from limic.util import start, end, file_size, status, save_gt, load_gt, check_overwrite
    from limic.overpass import nodes_in_geometry, set_server
    if not check_overwrite(file_name_in, file_name_out):
        return
    start("Loading from", file_name_in)
    g = load_gt(file_name_in)
    end('')
    file_size(file_name_in)
    start("Query server for nodes in polygon")
    set_server(overpass_url)
    nodes = nodes_in_geometry(zip(polygon[::2], polygon[1::2]))
    end('')
    status(len(nodes))
    start("Pruning graph")
    h = prune_ids_gt(g, nodes)
    end()
    start("Saving to", file_name_out)
    save_gt(file_name_out, h)
    end('')
    file_size(file_name_out)
Ejemplo n.º 4
0
def fill(overpass_url,
         file_name=None,
         area=None,
         around=1000,
         eps=0.01,
         safe_dist=100,
         penalize=20,
         max_workers=None):
    from limic.overpass import set_server, pylon, region, get_towers_by_area
    from limic.util import start, end, file_size, status, load_pickled, save_pickled, options, replace, options
    from networkx import Graph, relabel_nodes
    from os import cpu_count
    from os.path import exists
    from concurrent.futures import ThreadPoolExecutor, wait
    from signal import signal, SIGINT
    if not area and not file_name:
        if options.parser:
            options.parser.error("specify at least one of --area or CACHE")
        else:
            status("ERROR: specify at least area or cache name!")
            from sys import exit
            exit(-1)
    if not area:
        area = file_name.split(".")[1]
    if not file_name:
        file_name = "cache." + area
    if not max_workers:
        max_workers = cpu_count() * 4
    start("Number of workers")
    status(max_workers)
    if exists(file_name):
        start("Loading", file_name)
        region.backend._cache = load_pickled(file_name)
        end('')
        file_size(file_name)
    len_cache = len(region.backend._cache)
    start("Querying overpass for", area)
    set_server(overpass_url)
    towers = get_towers_by_area(area)
    end()
    fs = []
    executor = ThreadPoolExecutor(max_workers=max_workers)
    interrupt = 0

    def shutdown(sig, frame):
        nonlocal interrupt
        interrupt += 1
        print("Shutting down ...")
        for f in fs:
            f.cancel()
        print("Cancelled all futures ...")
        running = len(fs)
        total = running
        while running:
            print("Waiting for", running, "processes to shut down ...")
            wait(fs, timeout=60)
            running = sum(0 if f.done() else 1 for f in fs)
        if len_cache != len(region.backend._cache):
            file_name_tmp = file_name + "." + str(interrupt)
            start("Emergency saving to", file_name_tmp)
            save_pickled(file_name_tmp, region.backend._cache)
            end('')
            file_size(file_name_tmp)

    signal(SIGINT, shutdown)
    options.failed = True
    while options.failed:
        options.failed = False
        for tower in towers:
            fs.append(
                executor.submit(cache_tower, tower, around, eps, safe_dist,
                                penalize))
        running = len(fs)
        total = running
        while running:
            print("Waiting for", running, "out of", total, "processes ...")
            wait(fs, timeout=60)
            running = sum(0 if f.done() else 1 for f in fs)
    if len_cache != len(region.backend._cache):
        file_name_tmp = file_name + ".tmp"
        start("Saving to", file_name, "via", file_name_tmp)
        save_pickled(file_name_tmp, region.backend._cache)
        replace(file_name_tmp, file_name)
        end('')
        file_size(file_name)