cosbidev / PyTrack

a Map-Matching-based Python Toolbox for Vehicle Trajectory Reconstruction
https://pytrack-lib.readthedocs.io/en/latest/#
BSD 3-Clause Clear License
63 stars 10 forks source link

KeyError: 'geometry' #5

Closed raul-parada closed 2 years ago

raul-parada commented 2 years ago

PyTrack version checks

Issue Description

I'm using Pycharm 2022.2. (Community Edition) under Ubuntu. The latest Pytrack-lib version 2.0.4.

By running:

G_interp, candidates = candidate.get_candidates(G, points, interp_dist=5, closest=True, radius=5)

I got the following error:

Traceback (most recent call last): File "/home/PycharmProjects/pythonProject/20221011_AMT_MAP.py", line 196, in G_interp, candidates = candidate.get_candidates(G, points, interp_dist=5, closest=True, radius=5)
File "/home/pww3/lib/python3.8/site-packages/pytrack/matching/candidate.py", line 66, in get_candidates G = distance.interpolate_graph(G, dist=interp_dist) File "/home/pww3/lib/python3.8/site-packages/pytrack/graph/distance.py", line 155, in interpolate_graph geom = data["geometry"] KeyError: 'geometry'

The values of G and points are correct: G: MultiDiGraph with 195 nodes and 300 edges

points: [(41.391026049285344, 2.163678830146503), (41.391070215189984, 2.163620451465465), (41.39111036090477, 2.1635620742324218), (41.39115406618348, 2.16350370222313), (41.39119705576544, 2.1634453332919166), (41.39123736983398, 2.163386966093803), (41.39127868897586, 2.163328602793066), (41.39132019968665, 2.163270241336454), (41.391358679200195, 2.1632118830298586), (41.39140113607066, 2.16315352787142)]

Mention that in Colab, it worked correctly.

Reproducible Example

G_interp, candidates = candidate.get_candidates(G, points, interp_dist=5, closest=True, radius=5) 

G:
MultiDiGraph with 195 nodes and 300 edges

points:
[(41.391026049285344, 2.163678830146503), (41.391070215189984, 2.163620451465465), (41.39111036090477, 2.1635620742324218), (41.39115406618348, 2.16350370222313), (41.39119705576544, 2.1634453332919166), (41.39123736983398, 2.163386966093803), (41.39127868897586, 2.163328602793066), (41.39132019968665, 2.163270241336454), (41.391358679200195, 2.1632118830298586), (41.39140113607066, 2.16315352787142)]

Error Message

KeyError: 'geometry'

PyTrack/Python version information

2.0.4 / 3.8

Additional Context

No response

matteotortora commented 2 years ago

It appears that the geometries of the road network have not been generated. Before using the candidate.get_candidates function, you must add a graph to the map using the add_graph method. Below is an example:

from pytrack.analytics import visualization
from pytrack.matching import candidate

maps = visualization.Map(location=(lat, long), zoom_start=15)
maps.add_graph(G, plot_nodes=True)
G_interp, candidates = candidate.get_candidates(G,  points,  interp_dist=5,  closest=True,  radius=5)

Let me know if this resolves the error.

raul-parada commented 2 years ago

I get the same error adding that line. Find more details below. As said, I've done the same on Colab and it worked

loc = (np.mean(v1['predicted1_lat'][:10]), np.mean(v1['predicted1_lon'][:10]))
print(loc)
(41.39121538210964, 2.1634161612484037)

maps = visualization.Map(location=loc, zoom_start=15)
maps.add_graph(G, plot_nodes=True)
print(maps)
<pytrack.analytics.visualization.Map object at 0x7fe03c0b4a00>

The only difference is that by printing (points) I get the second line (A total...):

[(41.39098823608519, 2.163730737124399), (41.39103031801428, 2.163670610671824), (41.39107674965668, 2.163604269370366), (41.39111895487103, 2.1635439667684153), (41.39116490225711, 2.163478317369984), (41.39121009723076, 2.163413743014728), (41.39125247943552, 2.163353187529769), (41.391295918274494, 2.163291122331044), (41.39134179728059, 2.163225570633605), (41.39138432610756, 2.163164805655552)]
A total of 6 points has no candidates: (0, 2, 4, 6, 8, 9)

Thanks

matteotortora commented 2 years ago

It sounds very bizarre since Colab also uses a Linux-based operating system. However, now the problem seems different, in fact, if you get the following line printed:

A total of 6 points has no candidates: (0, 2, 4, 6, 8, 9)

it means that the following line of code:

G_interp, candidates = candidate.get_candidates(G, points, interp_dist=5, closest=True, radius=5) 

was executed successfully and therefore the following error was not encountered:

File '/home/pww3/lib/python3.8/site-packages/pytrack/graph/distance.py', line 155, in interpolate_graph
geom = data["geometry"]
KeyError: 'geometry'.

So now another error does not complete the execution of the code. Unfortunately I cannot replicate your error, so if you attach the complete script you are trying to run on your machine I can try to repeat it. Please also attach the version of your environment's libraries, such as networkx, pandas, numpy, etc.

raul-parada commented 2 years ago

Hi,

Well, the line of no candidates was from Colab (successful) and from the system I'm trying to run.

On Wed, 12 Oct 2022, 11:30 Matteo Tortora, @.***> wrote:

It sounds very bizarre since Colab also uses a Linux-based operating system. However, now the problem seems different, in fact, if you get the following line printed:

A total of 6 points has no candidates: (0, 2, 4, 6, 8, 9)

it means that the following line of code:

G_interp, candidates = candidate.get_candidates(G, points, interp_dist=5, closest=True, radius=5)

was executed successfully and therefore the following error was not encountered:

File '/home/pww3/lib/python3.8/site-packages/pytrack/graph/distance.py', line 155, in interpolate_graph geom = data["geometry"] KeyError: 'geometry'.

So now another error does not complete the execution of the code. Unfortunately I cannot replicate your error, so if you attach the complete script you are trying to run on your machine I can try to repeat it. Please also attach the version of your environment's libraries, such as networkx, pandas, numpy, etc.

— Reply to this email directly, view it on GitHub https://github.com/cosbidev/PyTrack/issues/5#issuecomment-1275867267, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACAMJCFJ7THMAZOPXEKGIX3WC2AK5ANCNFSM6AAAAAARCIV4II . You are receiving this because you authored the thread.Message ID: @.***>

matteotortora commented 2 years ago

To remove the no candidate row, it is possible to enlarge the graph area using distance.enlarge_bbox function:

G = graph.graph_from_bbox(*distance.enlarge_bbox(north, south, west, east, 500), simplify=True, network_type='drive')

and increasing the radius value in:

G_interp, candidates = candidate.get_candidates(G, points, interp_dist=5, closest=True, radius=radius)
raul-parada commented 2 years ago

It is curious how the same code worked in one environment (Colab) but another (pycharm).

On Thu, 13 Oct 2022, 09:27 Matteo Tortora, @.***> wrote:

To remove the no candidate row, it is possible to enlarge the graph area using distance.enlarge_bbox function:

G = graph.graph_from_bbox(*distance.enlarge_bbox(north, south, west, east, 500), simplify=True, network_type='drive')

and increasing the radius value in:

G_interp, candidates = candidate.get_candidates(G, points, interp_dist=5, closest=True, radius=radius)

— Reply to this email directly, view it on GitHub https://github.com/cosbidev/PyTrack/issues/5#issuecomment-1277152397, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACAMJCDHRE5AMQSZR6URFO3WC62XHANCNFSM6AAAAAARCIV4II . You are receiving this because you authored the thread.Message ID: @.***>

raul-parada commented 2 years ago

I add here a reproducible code:

import numpy as np import pandas as pd

from pytrack.graph import graph, distance from pytrack.analytics import visualization from pytrack.matching import candidate, mpmatching_utils, mpmatching v1= pd.DataFrame(columns=["predicted1_lat", "predicted1_lon"], data=[[41.391026049285344, 2.163678830146503], [41.391070215189984, 2.163620451465465], [41.39111036090477, 2.1635620742324218], [41.39115406618348, 2.16350370222313], [41.39119705576544, 2.1634453332919166], [41.39123736983398, 2.163386966093803], [41.39127868897586, 2.163328602793066], [41.39132019968665, 2.163270241336454], [41.391358679200195, 2.1632118830298586], [41.39140113607066, 2.16315352787142]]) points = [(lat, lon) for lat, lon in zip(v1['predicted1_lat'][:10], v1['predicted1_lon'][:10])]

north, east = np.max(np.array([points]), 0) south, west = np.min(np.array([points]), 0)

Extract road graph

G = graph.graph_from_bbox(*distance.enlarge_bbox(north, south, west, east, 500), simplify=True, network_type='drive')

loc = (np.mean(v1['predicted1_lat'][:10]), np.mean(v1['predicted1_lon'][:10])) maps = visualization.Map(location=loc, zoom_start=15)

maps.add_graph(G, plot_nodes=True)

G_interp, candidates = candidate.get_candidates(G, points, interp_dist=5, closest=True, radius=5) libraries_map.txt

Find the list of libraries attached.

raul-parada commented 2 years ago

Solved. The problem was the double use of the same variable name.