gboeing / osmnx

OSMnx is a Python package to easily download, model, analyze, and visualize street networks and other geospatial features from OpenStreetMap.
https://osmnx.readthedocs.io
MIT License
4.85k stars 824 forks source link

Mismatched numbers of nodes and edges using save_graph_xml() function #1023

Closed heykuldip closed 1 year ago

heykuldip commented 1 year ago

Contributing guidelines

Documentation

Existing issues

What operating system and Python version are you using?

MacOS/Python 3.11

What OSMnx version are you using?

1.5.0

Environment packages and versions

# packages in environment at ~/opt/anaconda3/envs/ox:
#
# Name                    Version                   Build  Channel
affine                    2.4.0              pyhd8ed1ab_0    conda-forge
appnope                   0.1.3              pyhd8ed1ab_0    conda-forge
asttokens                 2.2.1              pyhd8ed1ab_0    conda-forge
attrs                     23.1.0             pyh71513ae_1    conda-forge
backcall                  0.2.0              pyh9f0ad1d_0    conda-forge
backports                 1.0                pyhd8ed1ab_3    conda-forge
backports.functools_lru_cache 1.6.5              pyhd8ed1ab_0    conda-forge
blosc                     1.21.4               heccf04b_0    conda-forge
boost-cpp                 1.78.0               hf5ba120_3    conda-forge
branca                    0.6.0              pyhd8ed1ab_0    conda-forge
brotli                    1.0.9                hb7f2c08_9    conda-forge
brotli-bin                1.0.9                hb7f2c08_9    conda-forge
bzip2                     1.0.8                h0d85af4_4    conda-forge
c-ares                    1.19.1               h0dc2134_0    conda-forge
ca-certificates           2023.5.7             h8857fd0_0    conda-forge
cairo                     1.16.0            h09dd18c_1016    conda-forge
certifi                   2023.5.7           pyhd8ed1ab_0    conda-forge
cfitsio                   4.2.0                hd56cc12_0    conda-forge
charset-normalizer        3.1.0              pyhd8ed1ab_0    conda-forge
click                     8.1.3           unix_pyhd8ed1ab_2    conda-forge
click-plugins             1.1.1                      py_0    conda-forge
cligj                     0.7.2              pyhd8ed1ab_1    conda-forge
comm                      0.1.3              pyhd8ed1ab_0    conda-forge
contourpy                 1.1.0           py311h5fe6e05_0    conda-forge
curl                      8.1.2                hbee3ae8_0    conda-forge
cycler                    0.11.0             pyhd8ed1ab_0    conda-forge
debugpy                   1.6.7           py311h814d153_0    conda-forge
decorator                 5.1.1              pyhd8ed1ab_0    conda-forge
executing                 1.2.0              pyhd8ed1ab_0    conda-forge
expat                     2.5.0                hf0c8a7f_1    conda-forge
fiona                     1.9.4           py311h4c3e48b_0    conda-forge
folium                    0.14.0             pyhd8ed1ab_0    conda-forge
font-ttf-dejavu-sans-mono 2.37                 hab24e00_0    conda-forge
font-ttf-inconsolata      3.000                h77eed37_0    conda-forge
font-ttf-source-code-pro  2.038                h77eed37_0    conda-forge
font-ttf-ubuntu           0.83                 hab24e00_0    conda-forge
fontconfig                2.14.2               h5bb23bf_0    conda-forge
fonts-conda-ecosystem     1                             0    conda-forge
fonts-conda-forge         1                             0    conda-forge
fonttools                 4.40.0          py311h2725bcf_0    conda-forge
freetype                  2.12.1               h3f81eb7_1    conda-forge
freexl                    1.0.6                hb7f2c08_1    conda-forge
gdal                      3.7.0           py311hfbd0d3d_3    conda-forge
geopandas                 0.13.2             pyhd8ed1ab_1    conda-forge
geopandas-base            0.13.2             pyha770c72_1    conda-forge
geos                      3.11.2               hf0c8a7f_0    conda-forge
geotiff                   1.7.1                h5cf5d3c_9    conda-forge
gettext                   0.21.1               h8a4c099_0    conda-forge
giflib                    5.2.1                hb7f2c08_3    conda-forge
hdf4                      4.2.15               h9804679_6    conda-forge
hdf5                      1.14.1          nompi_hedada53_100    conda-forge
icu                       72.1                 h7336db1_0    conda-forge
idna                      3.4                pyhd8ed1ab_0    conda-forge
importlib-metadata        6.7.0              pyha770c72_0    conda-forge
importlib_metadata        6.7.0                hd8ed1ab_0    conda-forge
ipykernel                 6.23.3             pyh5fb750a_0    conda-forge
ipython                   8.14.0             pyhd1c38e8_0    conda-forge
jedi                      0.18.2             pyhd8ed1ab_0    conda-forge
jinja2                    3.1.2              pyhd8ed1ab_1    conda-forge
joblib                    1.3.0              pyhd8ed1ab_0    conda-forge
json-c                    0.16                 h01d06f9_0    conda-forge
jupyter_client            8.3.0              pyhd8ed1ab_0    conda-forge
jupyter_core              5.3.1           py311h6eed73b_0    conda-forge
kealib                    1.5.1                h7014c1b_4    conda-forge
kiwisolver                1.4.4           py311hd2070f0_1    conda-forge
krb5                      1.20.1               h049b76e_0    conda-forge
lcms2                     2.15                 h2dcdeff_1    conda-forge
lerc                      4.0.0                hb486fe8_0    conda-forge
libaec                    1.0.6                hf0c8a7f_1    conda-forge
libarchive                3.6.2                h0b5dc4a_1    conda-forge
libblas                   3.9.0           17_osx64_openblas    conda-forge
libbrotlicommon           1.0.9                hb7f2c08_9    conda-forge
libbrotlidec              1.0.9                hb7f2c08_9    conda-forge
libbrotlienc              1.0.9                hb7f2c08_9    conda-forge
libcblas                  3.9.0           17_osx64_openblas    conda-forge
libcurl                   8.1.2                hbee3ae8_0    conda-forge
libcxx                    16.0.6               hd57cbcb_0    conda-forge
libdeflate                1.18                 hac1461d_0    conda-forge
libedit                   3.1.20191231         h0678c8f_2    conda-forge
libev                     4.33                 haf1e3a3_1    conda-forge
libexpat                  2.5.0                hf0c8a7f_1    conda-forge
libffi                    3.4.2                h0d85af4_5    conda-forge
libgdal                   3.7.0                h1aad74f_3    conda-forge
libgfortran               5.0.0           11_3_0_h97931a8_31    conda-forge
libgfortran5              12.2.0              he409387_31    conda-forge
libglib                   2.76.3               hc62aa5d_0    conda-forge
libiconv                  1.17                 hac89ed1_0    conda-forge
libjpeg-turbo             2.1.5.1              hb7f2c08_0    conda-forge
libkml                    1.3.0             haeb80ef_1015    conda-forge
liblapack                 3.9.0           17_osx64_openblas    conda-forge
libnetcdf                 4.9.2           nompi_hfeda9e8_106    conda-forge
libnghttp2                1.52.0               he2ab024_0    conda-forge
libopenblas               0.3.23          openmp_h429af6e_0    conda-forge
libpng                    1.6.39               ha978bb4_0    conda-forge
libpq                     15.3                 h9dc22bb_1    conda-forge
librttopo                 1.1.0               h5c328d2_13    conda-forge
libsodium                 1.0.18               hbcb3906_1    conda-forge
libspatialindex           1.9.3                he49afe7_4    conda-forge
libspatialite             5.0.1               h54d511b_27    conda-forge
libsqlite                 3.42.0               h58db7d2_0    conda-forge
libssh2                   1.11.0               hd019ec5_0    conda-forge
libtiff                   4.5.1                hf955e92_0    conda-forge
libwebp-base              1.3.0                hb7f2c08_0    conda-forge
libxcb                    1.15                 hb7f2c08_0    conda-forge
libxml2                   2.11.4               hd95e348_0    conda-forge
libzip                    1.9.2                h6db710c_1    conda-forge
libzlib                   1.2.13               h8a1eda9_5    conda-forge
llvm-openmp               16.0.6               hff08bdf_0    conda-forge
lz4-c                     1.9.4                hf0c8a7f_0    conda-forge
lzo                       2.10              haf1e3a3_1000    conda-forge
mapclassify               2.5.0              pyhd8ed1ab_1    conda-forge
markupsafe                2.1.3           py311h2725bcf_0    conda-forge
matplotlib-base           3.7.1           py311h2bf763f_0    conda-forge
matplotlib-inline         0.1.6              pyhd8ed1ab_0    conda-forge
munch                     3.0.0              pyhd8ed1ab_0    conda-forge
munkres                   1.1.4              pyh9f0ad1d_0    conda-forge
ncurses                   6.4                  hf0c8a7f_0    conda-forge
nest-asyncio              1.5.6              pyhd8ed1ab_0    conda-forge
networkx                  3.1                pyhd8ed1ab_0    conda-forge
nspr                      4.35                 hea0b92c_0    conda-forge
nss                       3.89                 h78b00b3_0    conda-forge
numpy                     1.25.0          py311hc44ba51_0    conda-forge
openjpeg                  2.5.0                h13ac156_2    conda-forge
openssl                   3.1.1                h8a1eda9_1    conda-forge
osmnx                     1.5.0              pyhd8ed1ab_0    conda-forge
packaging                 23.1               pyhd8ed1ab_0    conda-forge
pandas                    2.0.2           py311hab14417_0    conda-forge
parso                     0.8.3              pyhd8ed1ab_0    conda-forge
pcre2                     10.40                h1c4e4bc_0    conda-forge
pexpect                   4.8.0              pyh1a96a4e_2    conda-forge
pickleshare               0.7.5                   py_1003    conda-forge
pillow                    9.5.0           py311h7cb0e2d_1    conda-forge
pip                       23.1.2             pyhd8ed1ab_0    conda-forge
pixman                    0.40.0               hbcb3906_0    conda-forge
platformdirs              3.8.0              pyhd8ed1ab_0    conda-forge
pooch                     1.7.0              pyha770c72_3    conda-forge
poppler                   23.05.0              he041c3a_1    conda-forge
poppler-data              0.4.12               hd8ed1ab_0    conda-forge
postgresql                15.3                 h325e403_1    conda-forge
proj                      9.2.1                hc8d59c9_0    conda-forge
prompt-toolkit            3.0.38             pyha770c72_0    conda-forge
prompt_toolkit            3.0.38               hd8ed1ab_0    conda-forge
psutil                    5.9.5           py311h5547dcb_0    conda-forge
pthread-stubs             0.4               hc929b4f_1001    conda-forge
ptyprocess                0.7.0              pyhd3deb0d_0    conda-forge
pure_eval                 0.2.2              pyhd8ed1ab_0    conda-forge
pygments                  2.15.1             pyhd8ed1ab_0    conda-forge
pyparsing                 3.1.0              pyhd8ed1ab_0    conda-forge
pyproj                    3.6.0           py311h854a015_1    conda-forge
pysocks                   1.7.1              pyha2e5f31_6    conda-forge
python                    3.11.4          h30d4d87_0_cpython    conda-forge
python-dateutil           2.8.2              pyhd8ed1ab_0    conda-forge
python-tzdata             2023.3             pyhd8ed1ab_0    conda-forge
python_abi                3.11                    3_cp311    conda-forge
pytz                      2023.3             pyhd8ed1ab_0    conda-forge
pyzmq                     25.1.0          py311h5dacc12_0    conda-forge
rasterio                  1.3.7           py311h5f90dc6_2    conda-forge
readline                  8.2                  h9e318b2_1    conda-forge
requests                  2.31.0             pyhd8ed1ab_0    conda-forge
rtree                     1.0.1           py311hbc1f44b_1    conda-forge
scikit-learn              1.2.2           py311h83feae1_2    conda-forge
scipy                     1.11.0          py311h16c3c4d_0    conda-forge
setuptools                68.0.0             pyhd8ed1ab_0    conda-forge
shapely                   2.0.1           py311heb7bb94_1    conda-forge
six                       1.16.0             pyh6c4a22f_0    conda-forge
snappy                    1.1.10               h225ccf5_0    conda-forge
snuggs                    1.4.7                      py_0    conda-forge
sqlite                    3.42.0               h2b0dec6_0    conda-forge
stack_data                0.6.2              pyhd8ed1ab_0    conda-forge
threadpoolctl             3.1.0              pyh8a188c0_0    conda-forge
tiledb                    2.13.2               h8b9cbf0_0    conda-forge
tk                        8.6.12               h5dbffcc_0    conda-forge
tornado                   6.3.2           py311h2725bcf_0    conda-forge
traitlets                 5.9.0              pyhd8ed1ab_0    conda-forge
typing-extensions         4.6.3                hd8ed1ab_0    conda-forge
typing_extensions         4.6.3              pyha770c72_0    conda-forge
tzcode                    2023c                hb7f2c08_0    conda-forge
tzdata                    2023c                h71feb2d_0    conda-forge
urllib3                   2.0.3              pyhd8ed1ab_0    conda-forge
wcwidth                   0.2.6              pyhd8ed1ab_0    conda-forge
wheel                     0.40.0             pyhd8ed1ab_0    conda-forge
xerces-c                  3.2.4                h90c7484_2    conda-forge
xorg-libxau               1.0.11               h0dc2134_0    conda-forge
xorg-libxdmcp             1.1.3                h35c211d_0    conda-forge
xyzservices               2023.5.0           pyhd8ed1ab_1    conda-forge
xz                        5.2.6                h775f41a_0    conda-forge
zeromq                    4.3.4                he49afe7_1    conda-forge
zipp                      3.15.0             pyhd8ed1ab_0    conda-forge
zlib                      1.2.13               h8a1eda9_5    conda-forge
zstd                      1.5.2                hbc0c0cd_6    conda-forge

How did you install OSMnx?

Conda and conda-forge

Problem description

Hi,

I'm downloading a road network using OSMnx for the San Francisco region. Compared to directly converting the downloaded graph to nodes and edges using graph_to_gdfs() without saving it, I get a different number of nodes and edges of the graph by saving it using osmnx.io.save_graph_xml(), re-creating the graph using osmnx.graph_from_xml(), and finally converting it using the graph_to_gdfs(). By directly converting the graph, I get 9671 nodes and 16030 edges. But, by first saving and then re-creating the same graph, I get 9649 nodes and 15975 edges. Please see the minimal reproducible example below. Thank you so much for your help and cooperation!

Complete minimal reproducible example

import osmnx as ox

utn = ox.settings.useful_tags_node
oxna = ox.settings.osm_xml_node_attrs
oxnt = ox.settings.osm_xml_node_tags
utw = ox.settings.useful_tags_way
oxwa = ox.settings.osm_xml_way_attrs
oxwt = ox.settings.osm_xml_way_tags
utn = list(set(utn + oxna + oxnt))
utw = list(set(utw + oxwa + oxwt))
ox.settings.all_oneway = True
ox.settings.useful_tags_node = utn
ox.settings.useful_tags_way = utw
G = ox.graph_from_bbox(37.708300, 37.813300, -122.517100, -122.355100, network_type='drive')

nodes, edges = ox.utils_graph.graph_to_gdfs(G)
print(nodes.shape)
print(edges.shape)

ox.io.save_graph_xml(G, filepath="./data/SF_Road_Network.osm")

Recreated_G = ox.graph_from_xml('./data/SF_Road_Network.osm')
recreated_nodes, recreated_edges = ox.utils_graph.graph_to_gdfs(Recreated_G)
print(recreated_nodes.shape)
print(recreated_edges.shape)
gboeing commented 1 year ago

Not a bug. Cross-posted from StackOverflow and answered there: https://stackoverflow.com/a/76583192/7321942