Exemple #1
0
def create_map(fn=osm_fn, include_footways=False, include_parking=False):
    map_con = InMemMap("map", use_latlon=True)
    cnt = 0
    ways_filter = ['bridleway', 'bus_guideway', 'track']
    if not include_footways:
        ways_filter += ['footway', 'cycleway', 'path']
    parking_filter = ['driveway']
    if not include_parking:
        parking_filter += ['parking_aisle']
    for entity in osmread.parse_file(str(fn)):
        if isinstance(entity, osmread.Way):
            tags = entity.tags
            if 'highway' in tags \
                and not (tags['highway'] in ways_filter) \
                and not ('access' in tags and tags['access'] == 'private') \
                and not ('landuse' in tags and tags['landuse'] == 'square') \
                and not ('amenity' in tags and tags['amenity'] == 'parking') \
                and not ('service' in tags and tags['service'] in parking_filter) \
                and not ('area' in tags and tags['area'] == 'yes'):
                for node_a, node_b in zip(entity.nodes, entity.nodes[1:]):
                    map_con.add_edge(node_a, node_b)
                    # Some roads are one-way. We'll add both directions.
                    map_con.add_edge(node_b, node_a)
        if isinstance(entity, osmread.Node):
            map_con.add_node(entity.id, (entity.lat, entity.lon))
    map_con.purge()
    return map_con
Exemple #2
0
def create_map_osmread() -> None:
    xml_file = Path("./tests") / "osm.xml"
    map_con = InMemMap("myosm",
                       use_latlon=True,
                       use_rtree=True,
                       index_edges=True)
    for entity in osmread.parse_file(str(xml_file)):
        if isinstance(entity, osmread.Way) and 'highway' in entity.tags:
            for node_a, node_b in zip(entity.nodes, entity.nodes[1:]):
                map_con.add_edge(node_a, node_b)
                # Some roads are one-way. We'll add both directions.
                map_con.add_edge(node_b, node_a)
        if isinstance(entity, osmread.Node):
            map_con.add_node(entity.id, (entity.lat, entity.lon))
    map_con.purge()
Exemple #3
0
def test_path3_few_obs_e():
    path = [(1, 0), (7.5, 0.65), (10.1, 1.9)]
    path_sol = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I']
    mapdb = InMemMap("map", 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 = SimpleMatcher(mapdb, max_dist_init=0.2, obs_noise=1, obs_noise_ne=10,
                                  non_emitting_states=True, only_edges=True)
    matcher.match(path)
    path_pred = matcher.path_pred_onlynodes
    if directory:
        matcher.print_lattice_stats()
        matcher.print_lattice()
        from leuvenmapmatching import visualization as mmviz
        mmviz.plot_map(mapdb, matcher=matcher, show_labels=True, show_matching=True, linewidth=10,
                       filename=str(directory / "test_test_path_e_3_fo.png"))
    assert path_pred == path_sol, f"Nodes not equal:\n{path_pred}\n{path_sol}"
Exemple #4
0
def test_path3_dist():
    path = [(0, 1), (0.65, 7.5), (1.9, 10.1)]
    path_sol = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I']
    mapdb = InMemMap("map", graph={
        "A": ((0.00, 1), ["B"]),
        "B": ((0.00, 3), ["A", "C"]),
        "C": ((0.70, 3), ["B", "D"]),
        "D": ((1.00, 5), ["C", "E"]),
        "E": ((1.00, 6), ["D", "F"]),
        "F": ((0.70, 7), ["E", "G"]),
        "G": ((0.00, 8), ["F", "H"]),
        "H": ((0.0, 10), ["G", "I"]),
        "I": ((2.0, 10), ["H"])
    }, use_latlon=False)
    matcher = DistanceMatcher(mapdb, max_dist_init=0.2,
                              obs_noise=0.5, obs_noise_ne=2, dist_noise=0.5,
                              non_emitting_states=True)
    states, lastidx = matcher.match(path)
    path_pred = matcher.path_pred_onlynodes
    if directory:
        from leuvenmapmatching import visualization as mmviz
        mmviz.plot_map(mapdb, matcher=matcher, show_labels=True, show_matching=True, linewidth=2,
                       filename=str(directory / "test_path_3_dist.png"))
    assert path_pred == path_sol, f"Nodes not equal:\n{path_pred}\n{path_sol}"

    for obs_idx, m in enumerate(matcher.lattice_best):  # type: Tuple[int, DistanceMatching]
        state = m.shortkey  # tuple indicating edge
        ne_str = "e" if m.is_emitting() else "ne"  # state is emitting or not
        p1_str = "{:>5.2f}-{:<5.2f}".format(*m.edge_m.pi)  # best matching location on graph
        p2_str = "{:>5.2f}-{:<5.2f}".format(*m.edge_o.pi)  # best matching location on track
        print(f"{obs_idx:<2} | {state} | {ne_str:<2} | {p1_str} | {p2_str}")
Exemple #5
0
def test_path_duplicate():
    from datetime import datetime
    # A path with two identical points
    path = [(0.8, 0.7), (0.9, 0.7), (1.1, 1.0), (1.2, 1.5), (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.1, 3.3), (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)]

    mapdb = InMemMap("map", graph={
        "A": ((1, 1), ["B", "C"]),
        "B": ((1, 3), ["A", "C", "D"]),
        "C": ((2, 2), ["A", "B", "D", "E"]),
        "D": ((2, 4), ["B", "C", "D", "E"]),
        "E": ((3, 3), ["C", "D", "F"]),
        "F": ((3, 5), ["D", "E"])
    }, use_latlon=False)

    matcher = SimpleMatcher(mapdb, max_dist=None, min_prob_norm=None,
                                  non_emitting_states = True, only_edges=False)

    #Matching with and without timestamps signed to the points
    path_pred = matcher.match(path, unique=False)

    path = [(p1, p2, datetime.fromtimestamp(i)) for i, (p1, p2) in enumerate(path)]
    path_pred_time = matcher.match(path, unique=False)

    if directory:
        from leuvenmapmatching import visualization as mmviz
        matcher.print_lattice_stats()
        matcher.print_lattice()
        mmviz.plot_map(mapdb, matcher=matcher, show_labels=True, show_matching=True,
                       filename=str(directory / "test_nonemitting_test_path_duplicate.png"))

    # The path should be identical regardless of the timestamps
    assert path_pred == path_pred_time, f"Nodes not equal:\n{path_pred}\n{path_pred_time}"
def test_path2():
    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.1, 3.3), (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)]
    # path_sol = ['A', ('A', 'B'), 'B', ('B', 'D'), 'D', ('D', 'E'), 'E', ('E', 'F')]
    path_sol_nodes = ['A', 'B', 'D', 'E', 'F']
    mapdb = InMemMap("map", 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)

    matcher = SimpleMatcher(mapdb, max_dist=None, min_prob_norm=0.001,
                            non_emitting_states=False, only_edges=False)
    path_pred, _ = matcher.match(path, unique=True)
    if directory:
        matcher.print_lattice_stats()
        matcher.print_lattice()
        from leuvenmapmatching import visualization as mmviz
        mmviz.plot_map(mapdb, matcher=matcher, show_labels=True, show_matching=True,
                       filename=str(directory / "test_path2.png"))
    # assert path_pred == path_sol, "Nodes not equal:\n{}\n{}".format(path_pred, path_sol)
    nodes_pred = matcher.path_pred_onlynodes
    assert nodes_pred == path_sol_nodes, f"Nodes not equal:\n{nodes_pred}\n{path_sol_nodes}"
def test_path_outlier():
    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.1, 3.3), (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)]
    path_sol = ['A', 'B', 'D', 'C', 'D', 'E', 'F']
    path.insert(13, (2.3, 1.8))
    mapdb = InMemMap("map", 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)

    matcher = SimpleMatcher(mapdb, max_dist=None, min_prob_norm=0.0001,
                            max_dist_init=1, obs_noise=0.5, obs_noise_ne=10,
                            non_emitting_states=True)
    matcher.match(path, unique=True)
    path_pred = matcher.path_pred_onlynodes
    if directory:
        matcher.print_lattice_stats()
        matcher.print_lattice()
        from leuvenmapmatching import visualization as mmviz
        with (directory / 'lattice.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_path_outlier.png"))
        print("Path through lattice:\n" + "\n".join(m.label for m in matcher.lattice_best))
    assert path_pred == path_sol, "Nodes not equal:\n{}\n{}".format(path_pred, path_sol)
def test_path_outlier_dist():
    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.1, 3.3), (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)]
    path_sol = ['A', 'B', 'D', 'C', 'E', 'F']
    path.insert(13, (2.3, 1.8))
    mapdb = InMemMap("map", 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)

    matcher = DistanceMatcher(mapdb, max_dist=None, min_prob_norm=0.0001,
                              max_dist_init=1, obs_noise=0.5, obs_noise_ne=10,
                              non_emitting_states=True)
    matcher.match(path)
    path_pred = matcher.path_pred_onlynodes
    if directory:
        from leuvenmapmatching import visualization as mmviz
        mmviz.plot_map(mapdb, matcher=matcher,
                       show_labels=True, show_matching=True, show_graph=True,
                       filename=str(directory / "test_path_outlier_dist.png"))
    # TODO: Smoothing the observation distances could eliminate the outlier
    assert path_pred == path_sol, "Nodes not equal:\n{}\n{}".format(path_pred, path_sol)
def test_path_outlier2():
    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.1, 3.3), (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)]
    path.insert(13, (2.3, -3.0))
    mapdb = InMemMap("map", 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)

    matcher = DistanceMatcher(mapdb, max_dist=None, min_prob_norm=0.1,
                            max_dist_init=1, obs_noise=0.25, obs_noise_ne=1,
                            non_emitting_states=True)
    _, last_idx = matcher.match(path, unique=True)
    if directory:
        # matcher.print_lattice_stats()
        # matcher.print_lattice()
        from leuvenmapmatching import visualization as mmviz
        # with (directory / 'lattice.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_path_outlier2.png"))
    assert last_idx == 12
def test_path3_dist():
    path = [(3.0, 3.2), (3.1, 3.8), (3.0, 4.0), (3.1, 4.3), (3.1, 4.6),
            (3.0, 4.9)]
    path_sol = ['E', 'F']
    mapdb = InMemMap("map",
                     graph={
                         "E": ((3, 3), ["F"]),
                         "F": ((3, 5), ["E"]),
                     },
                     use_latlon=False)

    matcher = DistanceMatcher(mapdb,
                              max_dist=None,
                              min_prob_norm=0.0001,
                              max_dist_init=1,
                              obs_noise=0.25,
                              obs_noise_ne=10,
                              non_emitting_states=True)
    matcher.match(path, unique=True)
    path_pred = matcher.path_pred_onlynodes
    if directory:
        from leuvenmapmatching import visualization as mmviz
        mmviz.plot_map(mapdb,
                       matcher=matcher,
                       show_labels=True,
                       show_matching=True,
                       filename=str(directory / "test_path3_dist.png"))
        print("Path through lattice:\n" +
              "\n".join(m.label for m in matcher.lattice_best))
    assert path_pred == path_sol, "Nodes not equal:\n{}\n{}".format(
        path_pred, path_sol)
def test_path1_dist():
    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.1, 3.3), (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)]
    # path_sol = ['A', ('A', 'B'), 'B', ('B', 'D'), 'D', ('D', 'E'), 'E', ('E', 'F')]
    path_sol_nodes = ['A', 'B', 'D', 'E', 'F']
    mapdb = InMemMap("map",
                     graph={
                         "A": ((1, 1), ["B", "C"]),
                         "B": ((1, 3), ["A", "C", "D"]),
                         "C": ((2, 2), ["A", "B", "D", "E"]),
                         "D": ((2, 4), ["B", "C", "D", "E"]),
                         "E": ((3, 3), ["C", "D", "F"]),
                         "F": ((3, 5), ["D", "E"])
                     },
                     use_latlon=False)

    matcher = DistanceMatcher(mapdb,
                              max_dist=None,
                              min_prob_norm=None,
                              obs_noise=0.5,
                              non_emitting_states=False)
    matcher.match(path)
    if directory:
        from leuvenmapmatching import visualization as mmviz
        mmviz.plot_map(mapdb,
                       matcher=matcher,
                       show_labels=True,
                       show_matching=True,
                       filename=str(directory / "test_path1_dist.png"))
    nodes_pred = matcher.path_pred_onlynodes
    assert nodes_pred == path_sol_nodes, f"Nodes not equal:\n{nodes_pred}\n{path_sol_nodes}"
Exemple #12
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()
 def make_map(self, index_edges=True, rtree=True):
     map_con = InMemMap(
         "athensmap",
         use_latlon=self.match_latlon,
         index_edges=index_edges,
         use_rtree=rtree)  # Deprecated crs style in algorithm
     if self.match_latlon:
         for nid, rown in self.network_nodes.iterrows():
             map_con.add_node(int(rown.n1), (rown.lat1, rown.lon1))
         for eid, rowe in self.network_edges.iterrows():
             map_con.add_edge(rowe.n1, rowe.n2)
     else:
         for nid, rown in self.network_nodes.iterrows():
             map_con.add_node(int(rown.n1), (rown.y1, rown.x1))
         for eid, rowe in self.network_edges.iterrows():
             map_con.add_edge(rowe.n1, rowe.n2)
     return map_con
Exemple #14
0
    def build_map_matching_graph(cls, road_network):
        """Return leuvenmapmatching.BaseMap used for map matching.

        """
        # Create the Leuven.MapMatching graph
        G = road_network.network
        map_con = InMemMap('road_network',
                           use_latlon=False,
                           use_rtree=True,
                           index_edges=True,
                           crs_lonlat=None,
                           crs_xy=None,
                           graph=None)
        for key in G.nodes:
            node = G.nodes[key]
            try:
                map_con.add_node(key, (node['easting'], node['northing']))
            except KeyError:
                raise Exception('Must call '
                                '\'populate_meters_from_lon_lat\' first '
                                'to define the Cartesian coordinates '
                                'associated with the latitude and '
                                'longitude coordinates.')

        for edge in G.edges:
            map_con.add_edge(*edge)

        return map_con
Exemple #15
0
def get_InMemMap(graph):
    '''
        This function takes the OSMNX road network graph and returns a leuvenmapmatching InMem map

        Parameters:
        ___________
            graph: OSMNX road network
        Returns:
        __________
            leuvenmapmatching InMem map   
    '''
    # Leuven Map Matching is using a different internal graph structure for the street data.
    # Therefore, the OSMnx graph needs to be transformed to the InMemMap
    streetmap = InMemMap("enviroCar",
                         use_latlon=True,
                         use_rtree=True,
                         index_edges=True)
    # add nodes
    nodes = list(graph.nodes)
    for node in nodes:
        lng = graph.nodes[node]['x']
        lat = graph.nodes[node]['y']
        streetmap.add_node(node, (lat, lng))

    # add edges
    edges = list(graph.edges)
    for edge in edges:
        node_a, node_b = edge[0], edge[1]
        streetmap.add_edge(node_a, node_b)

        # exclude bi-directional edges when street is oneway
        if not graph.edges[edge]['oneway']:
            streetmap.add_edge(node_b, node_a)

    streetmap.purge()

    # returns streetmap
    return streetmap
def test_path4_dist_inc():
    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", "D", "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)
    matcher.match(path[:5])
    if directory:
        from leuvenmapmatching import visualization as mmviz
        mmviz.plot_map(map_con,
                       matcher=matcher,
                       show_labels=True,
                       show_matching=True,
                       show_graph=True,
                       filename=str(directory / "test_path4_dist_inc_1.png"))

    matcher.match(path, expand=True)
    nodes = matcher.path_pred_onlynodes
    if directory:
        from leuvenmapmatching import visualization as mmviz
        mmviz.plot_map(map_con,
                       matcher=matcher,
                       show_labels=True,
                       show_matching=True,
                       show_graph=True,
                       filename=str(directory / "test_path4_dist_inc_2.png"))
    nodes_sol = ['X', 'A', 'B', 'D', 'E', 'F']
    assert nodes == nodes_sol, "Nodes not equal:\n{}\n{}".format(
        nodes, nodes_sol)
Exemple #17
0
def setup_map():
    path1 = [(1.8, 0.1), (1.8, 3.5), (3.0, 4.9)]  # More nodes than observations
    path2 = [(1.8, 0.1), (1.8, 2.0), (1.8, 3.5), (3.0, 4.9)]
    path_sol = ['X', 'C', 'D', 'F']
    mapdb = InMemMap("map", 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", "E", "K", "L", "F"]),
        "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)
    return mapdb, path1, path2, path_sol
Exemple #18
0
def make_map(graph):
    graph_proj = ox.project_graph(graph)

    map_con = InMemMap("myosm",
                       use_latlon=True,
                       use_rtree=True,
                       index_edges=True)

    nodes, _ = ox.graph_to_gdfs(graph_proj, nodes=True, edges=True)

    nodes_proj = nodes.to_crs("EPSG:3395")

    for nid, row in nodes_proj.iterrows():
        map_con.add_node(nid, (row['lat'], row['lon']))

    # adding edges using networkx graph
    for nid1, nid2, _ in graph.edges:
        map_con.add_edge(nid1, nid2)

    map_con.purge()
    return map_con
Exemple #19
0
 def make_map(graph, latlon=True, index_edges=True):
     map_con = InMemMap(
         "athensmap",
         use_latlon=latlon,
         index_edges=index_edges,
         use_rtree=True)  # Deprecated crs style in algorithm
     nodes_proj, edges_proj = ox.graph_to_gdfs(graph,
                                               nodes=True,
                                               edges=True)
     for nid, row in nodes_proj[['y', 'x']].iterrows():
         map_con.add_node(nid, (row['y'], row['x']))
     for nid, row in edges_proj[['u', 'v']].iterrows():
         map_con.add_edge(row['u'], row['v'])
     return map_con
Exemple #20
0
def test_bug1():
    dist = 10
    nb_steps = 20

    map_con = InMemMap("map", graph={
        "A":  ((1, dist), ["B"]),
        "B":  ((2, dist), ["A", "C", "CC"]),
        "C":  ((3, 0), ["B", "D"]),
        "D":  ((4 + dist, 0), ["C", "E"]),
        "CC": ((3, 2 * dist), ["B", "DD"]),
        "DD": ((4 + dist, 2 * dist), ["CC", "E"]),
        "E":  ((5 + dist, dist), ["F", "D", "DD"]),
        "F":  ((6 + dist, dist), ["E", ]),

    }, use_latlon=False)

    i = 10
    path = [(1.1,      2*dist*i/nb_steps),
            (2.1,      2*dist*i/nb_steps),
            (5.1+dist, 2*dist*i/nb_steps),
            (6.1+dist, 2*dist*i/nb_steps)
            # (1, len*i/nb_steps),
            # (2, len*i/nb_steps),
            # (3, len*i/nb_steps)
            ]

    matcher = SimpleMatcher(map_con, max_dist=dist + 1, obs_noise=dist + 1, min_prob_norm=None,
                                  non_emitting_states=True)

    nodes = matcher.match(path, unique=False)
    print("Solution: ", nodes)
    if directory:
        import leuvenmapmatching.visualization as mm_vis
        matcher.print_lattice()
        matcher.print_lattice_stats()
        mm_vis.plot_map(map_con, path=path, nodes=nodes, counts=matcher.node_counts(),
                        show_labels=True, filename=str(directory / "test_bugs_1.png"))
Exemple #21
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")
def mk_map(graph_proj):
    map_con = InMemMap("myosm",
                       use_latlon=True,
                       use_rtree=True,
                       index_edges=True)

    #Create GeoDataFrames
    nodes_proj, edges_proj = ox.graph_to_gdfs(graph_proj,
                                              nodes=True,
                                              edges=True)

    #좌표계 변경
    nodes_proj = nodes_proj.to_crs({'init': 'epsg:4326'})
    edges_proj = edges_proj.to_crs({'init': 'epsg:4326'})

    for nid, row in nodes_proj[['lon', 'lat']].iterrows():
        map_con.add_node(nid, (row['lat'], row['lon']))
    for nid, row in edges_proj[['u', 'v']].iterrows():
        map_con.add_edge(row['u'], row['v'])

    return map_con
Exemple #23
0
# graph = ox.graph_from_point((40.00792367,116.29994668),distance=3000,network_type='drive_service')
# graph_proj = ox.project_graph(graph)
# G_project_file=open('G_project.pickle','wb')
# pickle.dump(graph,G_project_file)
# G_project_file.close()


# Create GeoDataFrames
file=open('G_project.pickle','rb')
graph_proj = pickle.load(file)
nodes_proj, edges_proj = ox.graph_to_gdfs(graph_proj, nodes=True, edges=True)



map_con = InMemMap("myosm", use_rtree=True, index_edges=True)

for nid, row in nodes_proj[['x', 'y']].iterrows():
    map_con.add_node(int(nid), (row['x'], row['y']))
for nid, row in edges_proj[['u', 'v']].iterrows():
    map_con.add_edge(row['u'], row['v'])
map_con.purge()

# from leuvenmapmatching.util.gpx import gpx_to_path

# #track = gpx_to_path("mytrack.gpx")
# matcher = DistanceMatcher(map_con,
#                          max_dist=0.8,
#                          max_dist_init=25,  # meter
#                          min_prob_norm=0.01,
#                          #non_emitting_length_factor=0.75,
                    trj_ids[i], map_con.graph[nodes[j]][0][0],
                    map_con.graph[nodes[j]][0][1]
                ]
                output.write('{}\n'.format(str(trj_data)))
        print(i)


# def plot_route(map_con, matcher, filename="my_plot.png", use_osm=True):
#     mmviz.plot_map(map_con, matcher=matcher[1],
#                 use_osm=use_osm, zoom_path=True,
#                 show_labels=False, show_matching=True, show_graph=False,
#                 filename=filename)

if __name__ == "__main__":
    map_con = InMemMap("osmmap",
                       use_latlon=True,
                       use_rtree=True,
                       index_edges=True)
    graph = ox.load_graphml("mynetwork.graphml")
    graph_proj = ox.project_graph(graph)

    # Create GeoDataFrames
    # Approach 2
    print("start1")
    nodes, edges = ox.graph_to_gdfs(graph_proj, nodes=True, edges=True)

    nodes_proj = nodes.to_crs("EPSG:3414")
    edges_proj = edges.to_crs("EPSG:3414")

    for nid, row in nodes_proj.iterrows():
        map_con.add_node(nid, (row['lat'], row['lon']))
Exemple #25
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_path2_inc():
    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.1, 3.3), (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)]
    # path_sol = ['A', ('A', 'B'), 'B', ('B', 'D'), 'D', ('D', 'E'), 'E', ('E', 'F')]
    path_sol_nodes = ['A', 'B', 'D', 'E', 'F']
    mapdb = InMemMap("map",
                     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)

    ## Phase 1
    print('=== PHASE 1 ===')
    matcher = SimpleMatcher(mapdb,
                            max_dist=None,
                            min_prob_norm=0.001,
                            non_emitting_states=False,
                            only_edges=False,
                            max_lattice_width=1)
    path_pred, _ = matcher.match(path, unique=True)
    if directory:
        matcher.print_lattice_stats()
        matcher.print_lattice()
        from leuvenmapmatching import visualization as mmviz
        with (directory / 'test_path2_inc_1.gv').open('w') as ofile:
            matcher.lattice_dot(file=ofile, precision=2, render=True)
        mmviz.plot_map(mapdb,
                       matcher=matcher,
                       show_labels=True,
                       show_matching=True,
                       show_lattice=True,
                       show_graph=True,
                       filename=str(directory / "test_path2_inc_1.png"))

    ## Next phases
    for phase_nb, phase_width in enumerate([2, 3]):
        print(f'=== PHASE {phase_nb + 2} ===')
        path_pred, _ = matcher.increase_max_lattice_width(phase_width,
                                                          unique=True)
        if directory:
            matcher.print_lattice_stats()
            matcher.print_lattice()
            from leuvenmapmatching import visualization as mmviz
            with (directory /
                  f'test_path2_inc_{phase_nb + 2}.gv').open('w') as ofile:
                matcher.lattice_dot(file=ofile, precision=2, render=True)
            mmviz.plot_map(mapdb,
                           matcher=matcher,
                           show_labels=True,
                           show_matching=True,
                           show_lattice=True,
                           show_graph=True,
                           filename=str(directory /
                                        f"test_path2_inc_{phase_nb + 2}.png"))

    # assert path_pred == path_sol, "Nodes not equal:\n{}\n{}".format(path_pred, path_sol)
    nodes_pred = matcher.path_pred_onlynodes
    assert nodes_pred == path_sol_nodes, f"Nodes not equal:\n{nodes_pred}\n{path_sol_nodes}"