예제 #1
0
def example1():
    map_con = InMemMap("mymap",
                       graph={
                           "A": ((1, 1), ["B", "C", "X"]),
                           "B": ((1, 3), ["A", "C", "D", "K"]),
                           "C": ((2, 2), ["A", "B", "D", "E", "X", "Y"]),
                           "D": ((2, 4), ["B", "C", "F", "E", "K", "L"]),
                           "E": ((3, 3), ["C", "D", "F", "Y"]),
                           "F": ((3, 5), ["D", "E", "L"]),
                           "X": ((2, 0), ["A", "C", "Y"]),
                           "Y": ((3, 1), ["X", "C", "E"]),
                           "K": ((1, 5), ["B", "D", "L"]),
                           "L": ((2, 6), ["K", "D", "F"])
                       },
                       use_latlon=False)

    path = [(0.8, 0.7), (0.9, 0.7), (1.1, 1.0), (1.2, 1.5), (1.2, 1.6),
            (1.1, 2.0), (1.1, 2.3), (1.3, 2.9), (1.2, 3.1), (1.5, 3.2),
            (1.8, 3.5), (2.0, 3.7), (2.3, 3.5), (2.4, 3.2), (2.6, 3.1),
            (2.9, 3.1), (3.0, 3.2), (3.1, 3.8), (3.0, 4.0), (3.1, 4.3),
            (3.1, 4.6), (3.0, 4.9)]

    matcher = DistanceMatcher(map_con,
                              max_dist=2,
                              obs_noise=1,
                              min_prob_norm=0.5)
    states, _ = matcher.match(path)
    nodes = matcher.path_pred_onlynodes

    print("States\n------")
    print(states)
    print("Nodes\n------")
    print(nodes)
    print("")
    matcher.print_lattice_stats()
예제 #2
0
def test_path2_dist():
    mapdb, path1, path2, path_sol = setup_map()

    matcher = DistanceMatcher(mapdb,
                              max_dist_init=1,
                              min_prob_norm=0.5,
                              obs_noise=0.5,
                              dist_noise=0.5,
                              non_emitting_states=True)
    matcher.match(path2, unique=True)
    path_pred = matcher.path_pred_onlynodes
    if directory:
        from leuvenmapmatching import visualization as mmviz
        matcher.print_lattice_stats()
        matcher.print_lattice()
        # with (directory / 'lattice_path2.gv').open('w') as ofile:
        #     matcher.lattice_dot(file=ofile)
        mmviz.plot_map(mapdb,
                       matcher=matcher,
                       show_labels=True,
                       show_matching=True,
                       filename=str(directory /
                                    "test_nonemitting_test_path2_dist.png"))
    assert path_pred == path_sol, "Nodes not equal:\n{}\n{}".format(
        path_pred, path_sol)
예제 #3
0
def test_bug2():
    this_path = Path(os.path.realpath(__file__)).parent / "rsrc" / "bug2"
    edges_fn = this_path / "edgesrl.csv"
    nodes_fn = this_path / "nodesrl.csv"
    path_fn = this_path / "path.csv"

    logger.debug(f"Reading map ...")
    mmap = SqliteMap("road_network", use_latlon=True, dir=this_path)

    path = []
    with path_fn.open("r") as path_f:
        reader = csv.reader(path_f, delimiter=',')
        for row in reader:
            lat, lon = [float(coord) for coord in row]
            path.append((lat, lon))
    node_cnt = 0
    with nodes_fn.open("r") as nodes_f:
        reader = csv.reader(nodes_f, delimiter=',')
        for row in reader:
            nid, lonlat, _ = row
            nid = int(nid)
            lon, lat = [float(coord) for coord in lonlat[1:-1].split(",")]
            mmap.add_node(nid, (lat, lon), ignore_doubles=True, no_index=True, no_commit=True)
            node_cnt += 1
    edge_cnt = 0
    with edges_fn.open("r") as edges_f:
        reader = csv.reader(edges_f, delimiter=',')
        for row in reader:
            _eid, nid1, nid2, pid = [int(val) for val in row]
            mmap.add_edge(nid1, nid2, edge_type=0, path=pid, no_index=True, no_commit=True)
            edge_cnt += 1
    logger.debug(f"... done: {node_cnt} nodes and {edge_cnt} edges")
    logger.debug("Indexing ...")
    mmap.reindex_nodes()
    mmap.reindex_edges()
    logger.debug("... done")

    matcher = DistanceMatcher(mmap, min_prob_norm=0.001,
                              max_dist=200, obs_noise=4.07,
                              non_emitting_states=True)
    # path = path[:2]
    nodes, idx = matcher.match(path, unique=True)
    path_pred = matcher.path_pred
    if directory:
        import matplotlib.pyplot as plt
        matcher.print_lattice_stats()
        logger.debug("Plotting post map ...")
        fig = plt.figure(figsize=(100, 100))
        ax = fig.get_axes()
        mm_viz.plot_map(mmap, matcher=matcher, use_osm=True, ax=ax,
                        show_lattice=False, show_labels=True, show_graph=False, zoom_path=True,
                        show_matching=True)
        plt.savefig(str(directory / "test_bug1.png"))
        plt.close(fig)
        logger.debug("... done")
예제 #4
0
def test_path2_proj():
    prepare_files()
    map_con_latlon = create_map_from_xml(osm2_fn)
    map_con = map_con_latlon.to_xy()
    track = [map_con.latlon2yx(p[0], p[1]) for p in gpx_to_path(track2_fn)]
    matcher = DistanceMatcher(map_con,
                              max_dist=300,
                              max_dist_init=25,
                              min_prob_norm=0.0001,
                              non_emitting_length_factor=0.95,
                              obs_noise=50,
                              obs_noise_ne=50,
                              dist_noise=50,
                              max_lattice_width=5,
                              non_emitting_states=True)
    states, last_idx = matcher.match(track, unique=False)
    nodes = matcher.path_pred_onlynodes
    if directory:
        matcher.print_lattice_stats()
        mm_viz.plot_map(map_con,
                        matcher=matcher,
                        path=track,
                        use_osm=False,
                        show_graph=True,
                        show_matching=True,
                        show_labels=5,
                        filename=str(directory /
                                     "test_path_latlon_path2_proj.png"))
    nodes_sol = [
        2634474831, 1096512242, 3051083902, 1096512239, 1096512241, 1096512240,
        1096508366, 1096508372, 16483861, 1096508360, 159656075, 1096508382,
        16483862, 3051083898, 16526535, 3060597381, 3060515059, 16526534,
        16526532, 1274158119, 16526540, 3060597377, 16526541, 16424220,
        1233373340, 613125597, 1076057753
    ]
    nodes_sol2 = [
        1096512242, 3051083902, 1096512239, 1096512241, 1096512240, 159654664,
        1096508373, 1096508381, 16483859, 1096508369, 159654663, 1096508363,
        16483862, 3051083898, 16526535, 3060597381, 3060515059, 16526534,
        16526532, 611867918, 3060725817, 16483866, 3060725817, 611867918,
        16526532, 1274158119, 16526540, 3060597377, 16526541, 16424220,
        1233373340, 613125597, 1076057753
    ]
    assert (nodes
            == nodes_sol) or (nodes
                              == nodes_sol2), f"Nodes do not match: {nodes}"
def test_bug1():
    map_con = SqliteMap("map", use_latlon=True)
    map_con.add_nodes([(1, (47.590439915657, -122.238368690014)),
                       (2, (47.5910192728043, -122.239519357681)),
                       (3, (47.5913706421852, -122.240168452263))])
    map_con.add_edges([(1, 2), (2, 3)])
    path = [
        # (47.59043333, -122.2384167),
        (47.59058333, -122.2387),
        (47.59071667, -122.2389833),
        (47.59086667, -122.2392667),
        (47.59101667, -122.23955),
        (47.59115, -122.2398333)
    ]
    path_sol = [(1, 2), (2, 3)]
    matcher = DistanceMatcher(map_con,
                              min_prob_norm=0.001,
                              max_dist=200,
                              obs_noise=4.07,
                              non_emitting_states=True)
    matcher.match(path, unique=True)
    path_pred = matcher.path_pred
    if directory:
        import matplotlib.pyplot as plt
        matcher.print_lattice_stats()
        logger.debug("Plotting post map ...")
        fig = plt.figure(figsize=(100, 100))
        ax = fig.get_axes()
        mm_viz.plot_map(map_con,
                        matcher=matcher,
                        use_osm=True,
                        ax=ax,
                        show_lattice=False,
                        show_labels=True,
                        show_graph=True,
                        zoom_path=True,
                        show_matching=True)
        plt.savefig(str(directory / "test_newson_bug1.png"))
        plt.close(fig)
        logger.debug("... done")
    assert path_pred == path_sol, f"Edges not equal:\n{path_pred}\n{path_sol}"
예제 #6
0
def example2():
    path = [(1, 0), (7.5, 0.65), (10.1, 1.9)]
    mapdb = InMemMap("mymap",
                     graph={
                         "A": ((1, 0.00), ["B"]),
                         "B": ((3, 0.00), ["A", "C"]),
                         "C": ((4, 0.70), ["B", "D"]),
                         "D": ((5, 1.00), ["C", "E"]),
                         "E": ((6, 1.00), ["D", "F"]),
                         "F": ((7, 0.70), ["E", "G"]),
                         "G": ((8, 0.00), ["F", "H"]),
                         "H": ((10, 0.0), ["G", "I"]),
                         "I": ((10, 2.0), ["H"])
                     },
                     use_latlon=False)
    matcher = DistanceMatcher(mapdb,
                              max_dist_init=0.2,
                              obs_noise=1,
                              obs_noise_ne=10,
                              non_emitting_states=True,
                              only_edges=True)
    states, _ = matcher.match(path)
    nodes = matcher.path_pred_onlynodes

    print("States\n------")
    print(states)
    print("Nodes\n------")
    print(nodes)
    print("")
    matcher.print_lattice_stats()

    mmviz.plot_map(mapdb,
                   matcher=matcher,
                   show_labels=True,
                   show_matching=True,
                   filename="output.png")
예제 #7
0
def test_bug2():
    this_path = Path(os.path.realpath(__file__)).parent / "rsrc" / "bug2"
    edges_fn = this_path / "edgesrl.csv"
    nodes_fn = this_path / "nodesrl.csv"
    path_fn = this_path / "path.csv"
    zip_fn = this_path / "leuvenmapmatching_testdata.zip"

    if not (edges_fn.exists() and nodes_fn.exists() and path_fn.exists()):
        import requests
        url = 'https://people.cs.kuleuven.be/wannes.meert/leuvenmapmatching/leuvenmapmatching_testdata.zip'
        logger.debug("Download testfiles from kuleuven.be")
        r = requests.get(url, stream=True)
        with zip_fn.open('wb') as ofile:
            for chunk in r.iter_content(chunk_size=1024):
                if chunk:
                    ofile.write(chunk)
        import zipfile
        logger.debug("Unzipping leuvenmapmatching_testdata.zip")
        with zipfile.ZipFile(str(zip_fn), "r") as zip_ref:
            zip_ref.extractall(str(zip_fn.parent))

    logger.debug(f"Reading map ...")
    mmap = SqliteMap("road_network", use_latlon=True, dir=this_path)

    path = []
    with path_fn.open("r") as path_f:
        reader = csv.reader(path_f, delimiter=',')
        for row in reader:
            lat, lon = [float(coord) for coord in row]
            path.append((lat, lon))
    node_cnt = 0
    with nodes_fn.open("r") as nodes_f:
        reader = csv.reader(nodes_f, delimiter=',')
        for row in reader:
            nid, lonlat, _ = row
            nid = int(nid)
            lon, lat = [float(coord) for coord in lonlat[1:-1].split(",")]
            mmap.add_node(nid, (lat, lon), ignore_doubles=True, no_index=True, no_commit=True)
            node_cnt += 1
    edge_cnt = 0
    with edges_fn.open("r") as edges_f:
        reader = csv.reader(edges_f, delimiter=',')
        for row in reader:
            _eid, nid1, nid2, pid = [int(val) for val in row]
            mmap.add_edge(nid1, nid2, edge_type=0, path=pid, no_index=True, no_commit=True)
            edge_cnt += 1
    logger.debug(f"... done: {node_cnt} nodes and {edge_cnt} edges")
    logger.debug("Indexing ...")
    mmap.reindex_nodes()
    mmap.reindex_edges()
    logger.debug("... done")

    matcher = DistanceMatcher(mmap, min_prob_norm=0.001,
                              max_dist=200, obs_noise=4.07,
                              non_emitting_states=True)
    # path = path[:2]
    nodes, idx = matcher.match(path, unique=True)
    path_pred = matcher.path_pred
    if directory:
        import matplotlib.pyplot as plt
        matcher.print_lattice_stats()
        logger.debug("Plotting post map ...")
        fig = plt.figure(figsize=(100, 100))
        ax = fig.get_axes()
        mm_viz.plot_map(mmap, matcher=matcher, use_osm=True, ax=ax,
                        show_lattice=False, show_labels=True, show_graph=False, zoom_path=True,
                        show_matching=True)
        plt.savefig(str(directory / "test_bug1.png"))
        plt.close(fig)
        logger.debug("... done")
예제 #8
0
from leuvenmapmatching.matcher.distance import DistanceMatcher
from leuvenmapmatching.map.inmem import InMemMap
from leuvenmapmatching import visualization as mmviz

path = [(1, 0), (7.5, 0.65), (10.1, 1.9)]
mapdb = InMemMap("mymap", graph={
    "A": ((1, 0.00), ["B"]),
    "B": ((3, 0.00), ["A", "C"]),
    "C": ((4, 0.70), ["B", "D"]),
    "D": ((5, 1.00), ["C", "E"]),
    "E": ((6, 1.00), ["D", "F"]),
    "F": ((7, 0.70), ["E", "G"]),
    "G": ((8, 0.00), ["F", "H"]),
    "H": ((10, 0.0), ["G", "I"]),
    "I": ((10, 2.0), ["H"])
}, use_latlon=False)
matcher = DistanceMatcher(mapdb, max_dist_init=0.2, obs_noise=1, obs_noise_ne=10,
                          non_emitting_states=True, only_edges=True)
states, _ = matcher.match(path)
nodes = matcher.path_pred_onlynodes

print("States\n------")
print(states)
print("Nodes\n------")
print(nodes)
print("")
matcher.print_lattice_stats()

mmviz.plot_map(mapdb, matcher=matcher,
              show_labels=True, show_matching=True
              filename="output.png")
def test_bug2():
    from leuvenmapmatching.util.openstreetmap import locations_to_map
    map_con = SqliteMap("map", use_latlon=True, dir=directory)
    path = [(50.87205, 4.66089), (50.874550000000006, 4.672980000000001),
            (50.87538000000001, 4.67698),
            (50.875800000000005, 4.6787600000000005),
            (50.876520000000006, 4.6818),
            (50.87688000000001, 4.683280000000001), (50.87814, 4.68733),
            (50.87832, 4.68778), (50.87879, 4.68851),
            (50.87903000000001, 4.68895),
            (50.879560000000005, 4.689170000000001),
            (50.87946, 4.6900900000000005),
            (50.879290000000005, 4.6909600000000005),
            (50.87906, 4.6921800000000005), (50.87935, 4.6924),
            (50.879720000000006, 4.69275), (50.88002, 4.6930700000000005),
            (50.880430000000004, 4.693440000000001),
            (50.880660000000006, 4.69357),
            (50.880660000000006, 4.6936100000000005),
            (50.88058, 4.694640000000001), (50.88055000000001, 4.69491),
            (50.88036, 4.696160000000001), (50.88009, 4.697550000000001),
            (50.87986, 4.6982800000000005),
            (50.879720000000006, 4.698790000000001),
            (50.87948, 4.699730000000001),
            (50.87914000000001, 4.6996400000000005),
            (50.87894000000001, 4.6995000000000005),
            (50.878800000000005, 4.699350000000001),
            (50.8785, 4.6991000000000005), (50.87841, 4.6990300000000005)]
    locations_to_map(path, map_con, filename=directory / "osm.xml")
    path_sol = [(5777282112, 2633552218), (2633552218, 5777282111),
                (5777282111, 5777282110), (5777282110, 1642021707),
                (1642021707, 71361087), (71361087, 71364203),
                (71364203, 1151697757), (1151697757, 1647339017),
                (1647339017, 1647339030), (1647339030, 2058510349),
                (2058510349, 2633552212), (2633552212, 1380538577),
                (1380538577, 1439572271), (1439572271, 836434313),
                (836434313, 2633771041), (2633771041, 5042874484),
                (5042874484, 5042874485), (5042874485, 2518922583),
                (2518922583, 2659762546), (2659762546, 5777282063),
                (5777282063, 2633771037), (2633771037, 2633771035),
                (2633771035, 2633771033), (2633771033, 1151668705),
                (1151668705, 2633771094), (2633771094, 1151668722),
                (1151668722, 1151668724), (1151668724, 5543948222),
                (5543948222, 2058481517), (2058481517, 16933576),
                (16933576, 5543948221), (5543948221, 2518923620),
                (2518923620, 5543948020), (5543948020, 5543948019),
                (5543948019, 18635886), (18635886, 18635887),
                (18635887, 1036909153), (1036909153, 2658942230),
                (2658942230, 1001099975), (1001099975, 16933574),
                (16933574, 1125604152), (1125604152, 5543948238),
                (5543948238, 1125604150), (1125604150, 1125604148),
                (1125604148, 2634195334), (2634195334, 2087854243),
                (2087854243, 5543948237), (5543948237, 160226603),
                (160226603, 180130266), (180130266, 5543948227),
                (5543948227, 5543948226), (5543948226, 1195681902),
                (1195681902, 101135392), (101135392, 2606704673),
                (2606704673, 18635977), (18635977, 1026111708),
                (1026111708, 1026111631), (1026111631, 16571375),
                (16571375, 2000680621), (2000680621, 999580042),
                (999580042, 16571370), (16571370, 2000680620),
                (2000680620, 5078692402), (5078692402, 5543948008),
                (5543948008, 16571371), (16571371, 999579936),
                (999579936, 2639836143), (2639836143, 5543948014),
                (5543948014, 5222992316), (5222992316, 30251323),
                (30251323, 159701080), (159701080, 3173217124),
                (3173217124, 1165209673), (1165209673, 1380538689),
                (1380538689, 2878334668), (2878334668, 2871137399),
                (2871137399, 2876902981), (2876902981, 2873624508),
                (2873624508, 2873624509), (2873624509, 2899666507),
                (2899666507, 2899666518), (2899666518, 2899666513),
                (2899666513, 2903073945), (2903073945, 2903073951),
                (2903073951, 1380538681), (1380538681, 2914810627),
                (2914810627, 2914810618), (2914810618, 2914810607),
                (2914810607, 2914810604), (2914810604, 2914810483),
                (2914810483, 2914810462), (2914810462, 2914810464),
                (2914810464, 1312433523), (1312433523, 20918594),
                (20918594, 2634267817), (2634267817, 2967425445),
                (2967425445, 3201523879), (3201523879, 157217466),
                (157217466, 2963305939), (2963305939, 3201523877),
                (3201523877, 3889275909), (3889275909, 3889275897),
                (3889275897, 157255077), (157255077, 30251882),
                (30251882, 157245624), (157245624, 1150903673),
                (1150903673, 4504936404)]
    matcher = DistanceMatcher(map_con,
                              min_prob_norm=0.001,
                              max_dist=200,
                              obs_noise=4.07,
                              non_emitting_states=True)
    nodes, idx = matcher.match(path, unique=True)
    path_pred = matcher.path_pred
    if directory:
        import matplotlib.pyplot as plt
        matcher.print_lattice_stats()
        logger.debug("Plotting post map ...")
        fig = plt.figure(figsize=(100, 100))
        ax = fig.get_axes()
        mm_viz.plot_map(map_con,
                        matcher=matcher,
                        use_osm=True,
                        ax=ax,
                        show_lattice=False,
                        show_labels=True,
                        show_graph=False,
                        zoom_path=True,
                        show_matching=True)
        plt.savefig(str(directory / "test_newson_bug1.png"))
        plt.close(fig)
        logger.debug("... done")
    assert path_pred == path_sol, f"Edges not equal:\n{path_pred}\n{path_sol}"
def test_route():
    if directory:
        import matplotlib.pyplot as plt
    else:
        plt = None
    paths, map_con, route = load_data()
    route = [(lat, lon) for lat, lon in route]
    zoom_path = True
    # zoom_path = slice(2645, 2665)
    slice_route = None
    # slice_route = slice(650, 750)
    # slice_route = slice(2657, 2662)  # First location where some observations are missing
    # slice_route = slice(2770, 2800)  # Observations are missing
    # slice_route = slice(2910, 2950)  # Interesting point
    # slice_route = slice(2910, 2929)  # Interesting point
    # slice_route = slice(6825, 6833)  # Outlier observation
    slice_route = slice(6300, )

    # if directory is not None:
    #     logger.debug("Plotting pre map ...")
    #     mm_viz.plot_map(map_con_latlon, path=route_latlon, use_osm=True,
    #                     show_lattice=False, show_labels=False, show_graph=False, zoom_path=zoom_path,
    #                     filename=str(directory / "test_newson_route.png"))
    #     logger.debug("... done")

    matcher = DistanceMatcher(map_con,
                              min_prob_norm=0.0001,
                              max_dist=200,
                              dist_noise=15,
                              dist_noise_ne=30,
                              obs_noise=30,
                              obs_noise_ne=150,
                              non_emitting_states=True)

    if slice_route is None:
        pkl_fn = this_path / "nodes_pred.pkl"
        if pkl_fn.exists():
            with pkl_fn.open("rb") as pkl_file:
                logger.debug(f"Reading predicted nodes from pkl file")
                route_nodes = pickle.load(pkl_file)
        else:
            matcher.match(route)
            route_nodes = matcher.path_pred_onlynodes
            with pkl_fn.open("wb") as pkl_file:
                pickle.dump(route_nodes, pkl_file)
        from leuvenmapmatching.util.evaluation import route_mismatch_factor
        print(route_nodes[:10])
        # route_edges = map_con.nodes_to_paths(route_nodes)
        # print(route_edges[:10])
        grnd_paths, _ = zip(*paths)
        print(grnd_paths[:10])
        route_paths = map_con.nodes_to_paths(route_nodes)
        print(route_paths[:10])

        logger.debug(f"Compute route mismatch factor")
        factor, cnt_matches, cnt_mismatches, total_length, mismatches, _, _ = \
            route_mismatch_factor(map_con, route_paths, grnd_paths,window=None, keep_mismatches=True)
        logger.debug(
            f"factor = {factor}, "
            f"cnt_matches = {cnt_matches}/{cnt_mismatches} of {len(grnd_paths)}/{len(route_paths)}, "
            f"total_length = {total_length}\n"
            f"mismatches = " + " | ".join(str(v) for v in mismatches))
    else:
        _, last_idx = matcher.match(route[slice_route])
        logger.debug(f"Last index = {last_idx}")

    # matcher.match(route[2657:2662])  # First location where some observations are missing
    # matcher.match(route[2770:2800])  # Observations are missing
    # matcher.match(route[2910:2950])  # Interesting point
    # matcher.match(route[2910:2929])  # Interesting point
    # matcher.match(route[6000:])
    path_pred = matcher.path_pred_onlynodes

    if directory:
        matcher.print_lattice_stats()
        logger.debug("Plotting post map ...")
        fig = plt.figure(figsize=(200, 200))
        ax = fig.get_axes()
        mm_viz.plot_map(map_con,
                        matcher=matcher,
                        use_osm=True,
                        ax=ax,
                        show_lattice=False,
                        show_labels=True,
                        zoom_path=zoom_path,
                        show_matching=True,
                        show_graph=False)
        plt.savefig(str(directory / "test_newson_route_matched.png"))
        plt.close(fig)
        logger.debug("... done")
        logger.debug("Best path:")
        for m in matcher.lattice_best:
            logger.debug(m)

    print(path_pred)