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

Shortest path returns None when loading network from .gpkg #1053

Closed adamamer20 closed 1 year ago

adamamer20 commented 1 year ago

Contributing guidelines

Documentation

Existing issues

What operating system and Python version are you using?

Windows 10/Conda 23.3.1/Python 3.10.12.final.0

What OSMnx version are you using?

1.6.0

Environment packages and versions

# Name                    Version                   Build  Channel
affine                    2.4.0              pyhd8ed1ab_0    conda-forge
arrow                     1.2.3              pyhd8ed1ab_0    conda-forge
attrs                     23.1.0             pyh71513ae_1    conda-forge
beautifulsoup4            4.12.2             pyha770c72_0    conda-forge
binaryornot               0.4.4                      py_1    conda-forge
blosc                     1.21.4               hdccc3a2_0    conda-forge
boost-cpp                 1.78.0               h9f4b32c_3    conda-forge
branca                    0.6.0              pyhd8ed1ab_0    conda-forge
brotli                    1.0.9                hcfcfb64_9    conda-forge
brotli-bin                1.0.9                hcfcfb64_9    conda-forge
brotli-python             1.0.9           py311h12c1d0e_9    conda-forge
bzip2                     1.0.8                h8ffe710_4    conda-forge
c-ares                    1.19.1               hcfcfb64_0    conda-forge
ca-certificates           2023.7.22            h56e8100_0    conda-forge
cairo                     1.16.0            hdecc03f_1016    conda-forge
certifi                   2023.7.22          pyhd8ed1ab_0    conda-forge
cfitsio                   4.3.0                h9b0cee5_0    conda-forge
chardet                   5.2.0           py311h1ea47a8_0    conda-forge
charset-normalizer        3.2.0              pyhd8ed1ab_0    conda-forge
click                     8.1.7           win_pyh7428d3b_0    conda-forge
click-plugins             1.1.1                      py_0    conda-forge
cligj                     0.7.2              pyhd8ed1ab_1    conda-forge
colorama                  0.4.6              pyhd8ed1ab_0    conda-forge
contourpy                 1.1.0           py311h005e61a_0    conda-forge
cookiecutter              2.3.0              pyh1a96a4e_0    conda-forge
cycler                    0.11.0             pyhd8ed1ab_0    conda-forge
dbfread                   2.0.7                      py_0    conda-forge
expat                     2.5.0                h63175ca_1    conda-forge
fiona                     1.9.4           py311h4e4dc46_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               hbde0cde_0    conda-forge
fonts-conda-ecosystem     1                             0    conda-forge
fonts-conda-forge         1                             0    conda-forge
fonttools                 4.42.1          py311ha68e1ae_0    conda-forge
freetype                  2.12.1               h546665d_1    conda-forge
freexl                    1.0.6                h67ca5e6_1    conda-forge
gdal                      3.7.1           py311h68c9619_6    conda-forge
geopandas                 0.13.2             pyhd8ed1ab_1    conda-forge
geopandas-base            0.13.2             pyha770c72_1    conda-forge
geos                      3.12.0               h1537add_0    conda-forge
geotiff                   1.7.1               h4e61e90_11    conda-forge
gettext                   0.21.1               h5728263_0    conda-forge
glib                      2.76.4               h12be248_0    conda-forge
glib-tools                2.76.4               h12be248_0    conda-forge
gst-plugins-base          1.22.5               h001b923_0    conda-forge
gstreamer                 1.22.5               hb4038d2_0    conda-forge
hdf4                      4.2.15               h1334946_6    conda-forge
hdf5                      1.14.1          nompi_h73e8ff5_100    conda-forge
icu                       72.1                 h63175ca_0    conda-forge
idna                      3.4                pyhd8ed1ab_0    conda-forge
importlib-metadata        6.8.0              pyha770c72_0    conda-forge
intel-openmp              2023.2.0         h57928b3_49496    conda-forge
jinja2                    3.1.2              pyhd8ed1ab_1    conda-forge
joblib                    1.3.2              pyhd8ed1ab_0    conda-forge
kealib                    1.5.1                h96def13_4    conda-forge
kiwisolver                1.4.4           py311h005e61a_1    conda-forge
krb5                      1.21.2               heb0366b_0    conda-forge
lcms2                     2.15                 h3e3b177_1    conda-forge
lerc                      4.0.0                h63175ca_0    conda-forge
libabseil                 20230125.3      cxx17_h63175ca_0    conda-forge
libaec                    1.0.6                h63175ca_1    conda-forge
libarchive                3.6.2                h6f8411a_1    conda-forge
libblas                   3.9.0              17_win64_mkl    conda-forge
libbrotlicommon           1.0.9                hcfcfb64_9    conda-forge
libbrotlidec              1.0.9                hcfcfb64_9    conda-forge
libbrotlienc              1.0.9                hcfcfb64_9    conda-forge
libcblas                  3.9.0              17_win64_mkl    conda-forge
libclang                  15.0.7          default_h77d9078_3    conda-forge
libclang13                15.0.7          default_h77d9078_3    conda-forge
libcrc32c                 1.1.2                h0e60522_0    conda-forge
libcurl                   8.2.1                hd5e4a3a_0    conda-forge
libdeflate                1.18                 hcfcfb64_0    conda-forge
libexpat                  2.5.0                h63175ca_1    conda-forge
libffi                    3.4.2                h8ffe710_5    conda-forge
libgdal                   3.7.1                h6393f06_6    conda-forge
libglib                   2.76.4               he8f3873_0    conda-forge
libgoogle-cloud           2.12.0               hbc1b25b_1    conda-forge
libgrpc                   1.56.2               hea2d5f7_1    conda-forge
libhwloc                  2.9.2           nocuda_h15da153_1008    conda-forge
libiconv                  1.17                 h8ffe710_0    conda-forge
libjpeg-turbo             2.1.5.1              hcfcfb64_0    conda-forge
libkml                    1.3.0             hf2ab4e4_1015    conda-forge
liblapack                 3.9.0              17_win64_mkl    conda-forge
libnetcdf                 4.9.2           nompi_h5902ca5_107    conda-forge
libogg                    1.3.4                h8ffe710_1    conda-forge
libpng                    1.6.39               h19919ed_0    conda-forge
libpq                     15.4                 h43585b0_0    conda-forge
libprotobuf               4.23.3               h1975477_0    conda-forge
libpysal                  4.7.0              pyhd8ed1ab_0    conda-forge
librttopo                 1.1.0               h92c5fdb_14    conda-forge
libspatialindex           1.9.3                h39d44d4_4    conda-forge
libspatialite             5.0.1               hc49ff46_28    conda-forge
libsqlite                 3.42.0               hcfcfb64_0    conda-forge
libssh2                   1.11.0               h7dfc565_0    conda-forge
libtiff                   4.5.1                h6c8260b_0    conda-forge
libvorbis                 1.3.7                h0e60522_0    conda-forge
libwebp-base              1.3.1                hcfcfb64_0    conda-forge
libxcb                    1.15                 hcd874cb_0    conda-forge
libxml2                   2.11.5               hc3477c8_0    conda-forge
libzip                    1.9.2                h519de47_1    conda-forge
libzlib                   1.2.13               hcfcfb64_5    conda-forge
lz4-c                     1.9.4                hcfcfb64_0    conda-forge
lzo                       2.10              he774522_1000    conda-forge
m2w64-gcc-libgfortran     5.3.0                         6    conda-forge
m2w64-gcc-libs            5.3.0                         7    conda-forge
m2w64-gcc-libs-core       5.3.0                         7    conda-forge
m2w64-gmp                 6.1.0                         2    conda-forge
m2w64-libwinpthread-git   5.0.0.4634.697f757               2    conda-forge
mapclassify               2.5.0              pyhd8ed1ab_1    conda-forge
markdown-it-py            3.0.0              pyhd8ed1ab_0    conda-forge
markupsafe                2.1.3           py311ha68e1ae_0    conda-forge
matplotlib                3.7.2           py311h1ea47a8_0    conda-forge
matplotlib-base           3.7.2           py311h6e989c2_0    conda-forge
mdurl                     0.1.0              pyhd8ed1ab_0    conda-forge
mesa                      1.1.1                    pypi_0    pypi
mkl                       2022.1.0           h6a75c08_874    conda-forge
momepy                    0.6.0              pyhd8ed1ab_1    conda-forge
msys2-conda-epoch         20160418                      1    conda-forge
munch                     4.0.0              pyhd8ed1ab_0    conda-forge
munkres                   1.1.4              pyh9f0ad1d_0    conda-forge
networkx                  3.1                pyhd8ed1ab_0    conda-forge
numpy                     1.25.2          py311h0b4df5a_0    conda-forge
openjpeg                  2.5.0                ha2aaf27_2    conda-forge
openssl                   3.1.2                hcfcfb64_0    conda-forge
osmnx                     1.6.0              pyhd8ed1ab_0    conda-forge
packaging                 23.1               pyhd8ed1ab_0    conda-forge
pandas                    2.0.3           py311hf63dbb6_1    conda-forge
patsy                     0.5.3              pyhd8ed1ab_0    conda-forge
pcre2                     10.40                h17e33f8_0    conda-forge
pillow                    10.0.0          py311hde623f7_0    conda-forge
pip                       23.2.1             pyhd8ed1ab_0    conda-forge
pixman                    0.40.0               h8ffe710_0    conda-forge
platformdirs              3.10.0             pyhd8ed1ab_0    conda-forge
ply                       3.11                       py_1    conda-forge
pooch                     1.7.0              pyha770c72_3    conda-forge
poppler                   23.08.0              h45d20d0_0    conda-forge
poppler-data              0.4.12               hd8ed1ab_0    conda-forge
postgresql                15.4                 hc80876b_0    conda-forge
proj                      9.2.1                h660b3b0_0    conda-forge
pthread-stubs             0.4               hcd874cb_1001    conda-forge
pthreads-win32            2.9.1                hfa6e2cd_3    conda-forge
pygments                  2.16.1             pyhd8ed1ab_0    conda-forge
pyparsing                 3.0.9              pyhd8ed1ab_0    conda-forge
pyproj                    3.6.0           py311hcff2a09_1    conda-forge
pyqt                      5.15.9          py311h125bc19_4    conda-forge
pyqt5-sip                 12.12.2         py311h12c1d0e_4    conda-forge
pysocks                   1.7.1              pyh0701188_6    conda-forge
python                    3.11.4          h2628c8c_0_cpython    conda-forge
python-dateutil           2.8.2              pyhd8ed1ab_0    conda-forge
python-slugify            8.0.1              pyhd8ed1ab_1    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
pyyaml                    6.0             py311ha68e1ae_5    conda-forge
qt-main                   5.15.8              h063a7da_15    conda-forge
rasterio                  1.3.8           py311h58bba51_0    conda-forge
re2                       2023.03.02           hd4eee63_0    conda-forge
requests                  2.31.0             pyhd8ed1ab_0    conda-forge
rich                      13.5.1             pyhd8ed1ab_0    conda-forge
rtree                     1.0.1           py311hcacb13a_2    conda-forge
scikit-learn              1.3.0           py311h142b183_0    conda-forge
scipy                     1.11.2          py311h37ff6ca_0    conda-forge
seaborn                   0.12.2               hd8ed1ab_0    conda-forge
seaborn-base              0.12.2             pyhd8ed1ab_0    conda-forge
setuptools                68.1.2             pyhd8ed1ab_0    conda-forge
shapely                   2.0.1           py311h72efec2_2    conda-forge
sip                       6.7.11          py311h12c1d0e_0    conda-forge
six                       1.16.0             pyh6c4a22f_0    conda-forge
snappy                    1.1.10               hfb803bf_0    conda-forge
snuggs                    1.4.7                      py_0    conda-forge
soupsieve                 2.3.2.post1        pyhd8ed1ab_0    conda-forge
sqlite                    3.42.0               hcfcfb64_0    conda-forge
src                       1.0                       dev_0    <develop>
statsmodels               0.14.0          py311h59ca53f_1    conda-forge
tbb                       2021.10.0            h91493d7_0    conda-forge
text-unidecode            1.3                        py_0    conda-forge
threadpoolctl             3.2.0              pyha21a80b_0    conda-forge
tiledb                    2.16.3               hd58e885_1    conda-forge
tk                        8.6.12               h8ffe710_0    conda-forge
toml                      0.10.2             pyhd8ed1ab_0    conda-forge
tomli                     2.0.1              pyhd8ed1ab_0    conda-forge
tornado                   6.3.3           py311ha68e1ae_0    conda-forge
tqdm                      4.66.1             pyhd8ed1ab_0    conda-forge
typing-extensions         4.7.1                hd8ed1ab_0    conda-forge
typing_extensions         4.7.1              pyha770c72_0    conda-forge
tzdata                    2023c                h71feb2d_0    conda-forge
ucrt                      10.0.22621.0         h57928b3_0    conda-forge
unidecode                 1.3.6              pyhd8ed1ab_0    conda-forge
urllib3                   2.0.4              pyhd8ed1ab_0    conda-forge
vc                        14.3                h64f974e_17    conda-forge
vc14_runtime              14.36.32532         hfdfe4a8_17    conda-forge
vs2015_runtime            14.36.32532         h05e6639_17    conda-forge
wheel                     0.41.1             pyhd8ed1ab_0    conda-forge
win_inet_pton             1.1.0              pyhd8ed1ab_6    conda-forge
xerces-c                  3.2.4                h63175ca_2    conda-forge
xorg-libxau               1.0.11               hcd874cb_0    conda-forge
xorg-libxdmcp             1.1.3                hcd874cb_0    conda-forge
xyzservices               2023.7.0           pyhd8ed1ab_0    conda-forge
xz                        5.2.6                h8d14728_0    conda-forge
yaml                      0.2.5                h8ffe710_2    conda-forge
zipp                      3.16.2             pyhd8ed1ab_0    conda-forge
zlib                      1.2.13               hcfcfb64_5    conda-forge
zstd                      1.5.2                h12be248_7    conda-forge

How did you install OSMnx?

Conda and conda-forge

Problem description

I have downloaded a graph using osmnx.graph.graph_from_place and saved to a geopackage file using osmnx.io.save_graph_geopackage. When loading the geopackage using geopandas, osmnx.distance.shortest_path cannot find a path (Returns None). The same issue does not occur when saving and loading from a graphml file (as recommended in the User Reference "To save/load graphs to/from disk for later use in OSMnx, use the io.save_graphml and io.load_graphml functions instead.").

Complete minimal reproducible example

import osmnx as ox
import os.path
import geopandas as gpd
import random

current_directory = os.path.dirname(__file__)

roads = ox.graph_from_place('University of Toronto, Toronto, Ontario, Canada')

#Saving to geopackage
ox.io.save_graph_geopackage(roads, current_directory+'/roads.gpkg')

#Saving to ml
ox.io.save_graphml(roads, current_directory+'/roads.graphml')

#Loading from geopackage
roads_nodes = gpd.read_file(current_directory+'/roads.gpkg', layer = 'nodes').set_index('osmid') 
roads_edges = gpd.read_file(current_directory+'/roads.gpkg', layer = 'edges').set_index(['u', 'v', 'key']) 
roads_gpkg = ox.utils_graph.graph_from_gdfs(roads_nodes, roads_edges)

#Loading from graphml
roads_graphml = ox.io.load_graphml(current_directory+'/roads.graphml')

#Computing shortest path
origin = random.choice(list(roads.nodes))
destination = random.choice(list(roads.nodes))
path_gpkg = ox.distance.shortest_path(roads_gpkg,
                                      origin,
                                      destination)
print(path_gpkg) #None 
path_graphml = ox.distance.shortest_path(roads_graphml,
                                         origin,
                                         destination)
print(path_graphml) #[389678210, 24959550, 389678187, 389678188, 774054381, 389678189, 2428750571, 389678190, 389678191, 8604490311, 262732431, 389678027, 389678028, 389678029, 3342358877, 779168879, 389677893, 4295105603, 389678009, 389678008, 2078205535, 390545070, 24959546, 306725181, 771950969]
gboeing commented 1 year ago

Not a bug. You saved the graph with:

ox.io.save_graph_geopackage(roads, './roads.gpkg')

but it's a directed graph (which you wanted to reload again later as a directed graph), so you should use:

ox.io.save_graph_geopackage(roads, './roads.gpkg', directed=True)

From the docs:

directed (bool) – if False, save one edge for each undirected edge in the graph but retain original oneway and to/from information as edge attributes; if True, save one edge for each directed edge in the graph

GeoPackages serialize geometrical data, not topological data, so it makes sense to have just one "linestring" for each graph "edge". But the network itself is topological, and as a directed graph, you need one edge from node u to node v and another back from v to u.

adamamer20 commented 1 year ago

Ah thank you! It's my first time working with osmnx and directed graphs.