Skip to content

bad mapmatching results when oneway=false and reversed=true #1346

@wharstr0927

Description

@wharstr0927

Contributing guidelines

  • I understand the contributing guidelines

Documentation

  • My problem is not addressed by the documentation or examples

Existing issues

  • My problem does not appear in an existing issue

What operating system and Python version are you using?

linux,Python 3.8.10

What OSMnx version are you using?

2.0.6

Environment packages and versions

root@0660f4a5c0dc:/mnt/workplace/online_mapmatch# pip list
Package             Version             
------------------- --------------------
asttokens           3.0.0               
attrs               25.1.0              
backcall            0.2.0               
certifi             2019.11.28          
chardet             3.0.4               
click               8.1.8               
click-plugins       1.1.1               
cligj               0.7.2               
comm                0.2.2               
contourpy           1.1.1               
cycler              0.12.1              
cykhash             2.0.1               
Cython              3.0.12              
dbus-python         1.2.16              
debugpy             1.8.12              
decorator           5.1.1               
distro-info         0.23+ubuntu1.1      
executing           2.2.0               
fiona               1.9.6               
fonttools           4.56.0              
GDAL                3.3.2               
geopandas           0.10.2              
idna                2.8                 
importlib-metadata  8.5.0               
importlib-resources 6.4.5               
ipykernel           6.29.5              
ipython             8.12.3              
jedi                0.19.2              
jinja2              3.1.5               
joblib              1.4.2               
jupyter-client      8.6.3               
jupyter-core        5.7.2               
kiwisolver          1.4.7               
lxml                5.3.1               
MarkupSafe          2.1.5               
matplotlib          3.7.5               
matplotlib-inline   0.1.7               
nest-asyncio        1.6.0               
numpy               1.24.4              
osmium              4.0.2               
packaging           24.2                
pandas              2.0.3               
parso               0.8.4               
pexpect             4.9.0               
pickleshare         0.7.5               
pillow              10.4.0              
pip                 20.0.2              
platformdirs        4.3.6               
prompt-toolkit      3.0.50              
psutil              7.0.0               
ptyprocess          0.7.0               
pure-eval           0.2.3               
pygments            2.19.1              
PyGObject           3.36.0              
pyparsing           3.1.4               
pyproj              3.5.0               
pyrobuf             0.9.3               
pyrosm              0.6.1               
python-apt          2.0.1+ubuntu0.20.4.1
python-dateutil     2.9.0.post0         
python-rapidjson    1.20                
pytz                2025.1              
pyzmq               26.2.1              
requests            2.22.0              
requests-unixsocket 0.2.0               
scikit-learn        1.3.2               
scipy               1.10.1              
setuptools          45.2.0              
shapely             2.0.3               
six                 1.14.0              
stack-data          0.6.3               
threadpoolctl       3.5.0               
tornado             6.4.2               
tqdm                4.67.1              
traitlets           5.14.3              
typing-extensions   4.12.2              
tzdata              2025.1              
unattended-upgrades 0.1                 
unzip               1.0.0               
urllib3             1.25.8              
wcwidth             0.2.13              
wheel               0.34.2              
zipp                3.20.2

How did you install OSMnx?

OSMnx Docker image

Problem description

I keep trying many trials about the mapmatching process, and per step operation, but so far so bad, I am exhausted, so I have to ask for help, thank you so much if I can get any response.
As you see I have four <u,v> pairs, but no u in the ubodt file as source id, I am so confused.
any comments I will appreciate.
Image

Image

Minimal standalone reproducible example

#!/usr/bin/env python
# coding: utf-8

# encoding = "utf-8"
import os
import osmnx as ox
import numpy as np
import pandas as pd
from shapely.geometry import LineString
encoding = "utf-8"


city_names=['北京市']
for city in city_names:
    cmd = f"osmium cat beijing-latest.osm.pbf --overwrite -o ./{city}-latest.osm"
    print(f"执行:{cmd}")
    get_ipython().system('{cmd}')


#debug
city_name = "beijingshi"
# encoding = "utf-8"

print(f"==> 正在处理:{city_name}")
osm_path = f"./北京市-latest.osm"
output_dir = f"./{city_name}"
os.makedirs(output_dir, exist_ok=True)


G = ox.graph_from_xml(osm_path, bidirectional=False, simplify=False, retain_all=True)
# G = ox.graph_from_xml(osm_path, bidirectional=False, simplify=True, retain_all=True)

gdf_edges, gdf_edges = ox.graph_to_gdfs(G)
print(f"原始道路条数:{len(gdf_edges)}")


gdf_nodes = gdf_nodes.reset_index()    # 如果你也想把节点的 osmid 变成一列
gdf_edges = gdf_edges.reset_index()    # u, v, key → 普通列
# gdf_edges.head()
# ——— 3. 非数值列字符串化,避免写 Shapefile 时出错 ———
gdf_edges = ox.io._stringify_nonnumeric_cols(gdf_edges)
len(gdf_edges)
# gdf_edges

gdf_nodes[gdf_nodes['osmid'] == 1072576082]

mask_highway = gdf_edges['highway'].notnull() & (gdf_edges['highway'] != '')
gdf_edges = gdf_edges[mask_highway].copy()

# 6. 重建 fid
gdf_edges['fid'] = np.arange(len(gdf_edges), dtype=int)


gdf_edges[gdf_edges['osmid']==201724128][['u','v','osmid','oneway','reversed', 'fid', 'geometry']]

gdf_edges[gdf_edges['osmid']=='1072576086'][['u','v','osmid','oneway','reversed', 'fid', 'geometry']]


gdf_edges[gdf_edges['osmid']=='1072576082'][['u','v','osmid','oneway','reversed', 'fid', 'geometry']]


# 7. 输出
edges_path = os.path.join(output_dir, "edges.shp")
gdf_edges.to_file(edges_path, encoding=encoding)
print(f"生成{city_name}shp文件")
print(f"→ 已保存边文件(含过滤+双向+fid),共 {len(gdf_edges)} 条:{edges_path}")

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions