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.88k stars 827 forks source link

ox.graph.graph_from_polygon gives only one graph instead of several if multipolygons are used #860

Closed al-yakubovich closed 2 years ago

al-yakubovich commented 2 years ago

Hi. If I create shapely.geometry.MultiPolygon with Shapely and try to convert it into graph with ox.graph.graph_from_polygon module I will get only first polygon as graph and all others are missing.

from shapely.geometry import Polygon, MultiPolygon
import osmnx as ox
import networkx as nx
p1_long_lat = [(-86.840049, 33.538406), (-86.782240, 33.534072), (-86.773982, 33.465972), (-86.876753, 33.464951)]
p1 = Polygon(p1_long_lat)
p2_long_lat = [(-86.883929, 33.463853), (-86.866684, 33.458227), (-86.876318, 33.440622), (-86.908593, 33.438854)]
p2 = Polygon(p2_long_lat)
mp = MultiPolygon([p1, p2]) # type: shapely.geometry.multipolygon.MultiPolygon
G = ox.graph.graph_from_polygon(mp) # type: networkx.classes.multidigraph.MultiDiGraph
orig = list(G)[0]
dest = list(G)[-1]
route = nx.shortest_path(G, orig, dest, weight='length')
ox.plot_graph_route(G, route, route_linewidth=6, show=False, close=False, edge_color='black', bgcolor='w', edge_alpha=0.5, node_color='black')  

mp represents two polygons: 2022-08-24 16_08_18-Untitled3 - Jupyter Notebook but I can see only one graph when use ox.plot_graph_route (there should be two graphs on image): 2022-08-24 16_09_19-Untitled3 - Jupyter Notebook

# Python packages: absl-py 0.12.0 pypi_0 pypi affine 2.3.0 py_0 conda-forge aiofile 3.7.1 pypi_0 pypi anyio 3.3.1 pypi_0 pypi argon2-cffi 20.1.0 py39hb82d6ee_2 conda-forge asgiref 3.4.1 pypi_0 pypi astunparse 1.6.3 pypi_0 pypi async_generator 1.10 py_0 conda-forge attrs 21.2.0 pyhd8ed1ab_0 conda-forge backcall 0.2.0 pyh9f0ad1d_0 conda-forge backports 1.0 py_2 conda-forge backports-entry-points-selectable 1.1.0 pypi_0 pypi backports.functools_lru_cache 1.6.4 pyhd8ed1ab_0 conda-forge bleach 3.3.0 pyh44b312d_0 conda-forge blosc 1.10.2 pypi_0 pypi boost-cpp 1.74.0 h5b4e17d_4 conda-forge branca 0.4.2 pyhd8ed1ab_0 conda-forge brotlipy 0.7.0 py39hb82d6ee_1001 conda-forge bzip2 1.0.8 h8ffe710_4 conda-forge ca-certificates 2021.10.8 h5b45459_0 conda-forge cachetools 4.2.2 pypi_0 pypi caio 0.9.3 pypi_0 pypi cairo 1.16.0 hb19e0ff_1008 conda-forge certifi 2021.10.8 py39hcbf5309_1 conda-forge cffi 1.14.5 py39h0878f49_0 conda-forge cfitsio 3.470 h0af3d06_7 conda-forge chardet 4.0.0 py39hcbf5309_1 conda-forge charset-normalizer 2.0.0 pyhd8ed1ab_0 conda-forge clang 5.0 pypi_0 pypi click 8.0.1 pypi_0 pypi click-plugins 1.1.1 py_0 conda-forge cligj 0.7.2 pyhd8ed1ab_1 conda-forge cloudpickle 1.6.0 pypi_0 pypi colorama 0.4.4 pyh9f0ad1d_0 conda-forge contextily 1.2.0 pyhd8ed1ab_0 conda-forge coverage 5.5 pypi_0 pypi cramjam 2.3.1 py39h94cc256_1 conda-forge cryptography 35.0.0 py39hd8d06c1_0 conda-forge curl 7.79.1 h789b8ee_1 conda-forge cycler 0.10.0 py_2 conda-forge decorator 4.4.2 py_0 conda-forge defusedxml 0.7.1 pyhd8ed1ab_0 conda-forge descartes 1.1.0 py_4 conda-forge descarteslabs 1.10.0 pypi_0 pypi distlib 0.3.2 pypi_0 pypi efficientnet 1.1.1 pypi_0 pypi entrypoints 0.3 pyhd8ed1ab_1003 conda-forge et_xmlfile 1.0.1 py_1001 conda-forge expat 2.4.1 h39d44d4_0 conda-forge fastapi 0.68.2 pypi_0 pypi fastparquet 0.7.1 py39h5d4886f_0 conda-forge filelock 3.0.12 pypi_0 pypi fiona 1.8.20 py39hea8b339_2 conda-forge flask 2.0.1 pypi_0 pypi flatbuffers 1.12 pypi_0 pypi folium 0.12.0 pyhd8ed1ab_1 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.13.1 h1989441_1005 conda-forge fonts-conda-ecosystem 1 0 conda-forge fonts-conda-forge 1 0 conda-forge freetype 2.10.4 h546665d_1 conda-forge freexl 1.0.6 ha8e266a_0 conda-forge fsspec 2021.10.0 pypi_0 pypi gast 0.4.0 pypi_0 pypi gdal 3.3.2 py39h7c9a9b1_4 conda-forge geographiclib 1.52 pyhd8ed1ab_0 conda-forge geojson 2.5.0 pypi_0 pypi geopandas 0.10.2 pyhd8ed1ab_0 conda-forge geopandas-base 0.10.2 pyha770c72_0 conda-forge geopy 2.2.0 pyhd8ed1ab_0 conda-forge geos 3.9.1 h39d44d4_2 conda-forge geotiff 1.7.0 h350af67_3 conda-forge gettext 0.19.8.1 ha2e2712_1008 conda-forge google-auth 1.30.1 pypi_0 pypi google-auth-oauthlib 0.4.4 pypi_0 pypi google-pasta 0.2.0 pypi_0 pypi grpcio 1.39.0 pypi_0 pypi h11 0.12.0 pypi_0 pypi h5py 3.1.0 pypi_0 pypi hdf4 4.2.15 h0e5069d_3 conda-forge hdf5 1.12.1 nompi_h2a0e4a3_101 conda-forge ibis-framework 1.4.0 pypi_0 pypi icu 68.1 h0e60522_0 conda-forge idna 2.10 pypi_0 pypi imagecodecs 2022.2.22 pypi_0 pypi imageio 2.9.0 pypi_0 pypi importlib-metadata 4.0.1 py39hcbf5309_0 conda-forge imutils 0.5.4 pypi_0 pypi intel-openmp 2021.2.0 h57928b3_616 conda-forge ipykernel 5.5.5 py39h832f523_0 conda-forge ipyleaflet 0.15.0 pypi_0 pypi ipython 7.23.1 py39h832f523_0 conda-forge ipython_genutils 0.2.0 py_1 conda-forge ipywidgets 7.7.0 pypi_0 pypi itsdangerous 2.0.1 pypi_0 pypi jbig 2.1 h8d14728_2003 conda-forge jdcal 1.4.1 py_0 conda-forge jedi 0.18.0 py39hcbf5309_2 conda-forge jinja2 3.0.1 pyhd8ed1ab_0 conda-forge joblib 1.0.1 pyhd8ed1ab_0 conda-forge jpeg 9d h8ffe710_0 conda-forge jsonpath-rw 1.4.0 pypi_0 pypi jsonpath-rw-ext 1.2.2 pypi_0 pypi jsonpickle 2.0.0 pypi_0 pypi jsonschema 3.2.0 pyhd8ed1ab_3 conda-forge jupyter_client 6.1.12 pyhd8ed1ab_0 conda-forge jupyter_contrib_core 0.3.3 py_2 conda-forge jupyter_contrib_nbextensions 0.5.1 pyhd8ed1ab_2 conda-forge jupyter_core 4.7.1 py39hcbf5309_0 conda-forge jupyter_highlight_selected_word 0.2.0 py39hcbf5309_1002 conda-forge jupyter_latex_envs 1.4.6 pyhd8ed1ab_1002 conda-forge jupyter_nbextensions_configurator 0.4.1 py39hcbf5309_2 conda-forge jupyterlab-widgets 1.1.0 pypi_0 pypi jupyterlab_pygments 0.1.2 pyh9f0ad1d_0 conda-forge kealib 1.4.14 h8995ca9_3 conda-forge keras 2.6.0 pypi_0 pypi keras-applications 1.0.8 pypi_0 pypi keras-nightly 2.5.0.dev2021032900 pypi_0 pypi keras-preprocessing 1.1.2 pypi_0 pypi kiwisolver 1.3.1 py39h2e07f2f_1 conda-forge krb5 1.19.2 hbae68bd_0 conda-forge lazy-object-proxy 1.7.1 pypi_0 pypi lcms2 2.12 h2a16943_0 conda-forge lerc 2.2.1 h0e60522_0 conda-forge libblas 3.9.0 9_mkl conda-forge libcblas 3.9.0 9_mkl conda-forge libclang 11.1.0 default_h5c34c98_1 conda-forge libcurl 7.79.1 h789b8ee_1 conda-forge libdeflate 1.7 h8ffe710_5 conda-forge libffi 3.4.2 h0e60522_4 conda-forge libgdal 3.3.2 h08edb8f_4 conda-forge libglib 2.70.0 h3be07f2_0 conda-forge libiconv 1.16 he774522_0 conda-forge libkml 1.3.0 h9859afa_1014 conda-forge liblapack 3.9.0 9_mkl conda-forge libnetcdf 4.8.1 nompi_h1cc8e9d_101 conda-forge libpng 1.6.37 h1d00b33_2 conda-forge libpq 13.3 hfcc5ef8_1 conda-forge librttopo 1.1.0 hb340de5_6 conda-forge libsodium 1.0.18 h8d14728_1 conda-forge libspatialindex 1.9.3 h39d44d4_4 conda-forge libspatialite 5.0.1 h6a90f17_9 conda-forge libssh2 1.10.0 h680486a_2 conda-forge libtiff 4.3.0 h0c97f57_1 conda-forge libwebp-base 1.2.1 h8ffe710_0 conda-forge libxml2 2.9.12 hf5bbc77_0 conda-forge libxslt 1.1.33 h65864e5_2 conda-forge libzip 1.8.0 hfed4ece_1 conda-forge libzlib 1.2.11 h8ffe710_1013 conda-forge lxml 4.6.3 py39h4fd7cdf_0 conda-forge lz4-c 1.9.3 h8ffe710_0 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.4.3 pyhd8ed1ab_0 conda-forge markdown 3.3.4 pypi_0 pypi markdown2 2.4.2 pypi_0 pypi markupsafe 2.0.1 py39hb82d6ee_0 conda-forge matplotlib 3.4.2 py39hcbf5309_0 conda-forge matplotlib-base 3.4.2 py39h581301d_0 conda-forge matplotlib-inline 0.1.2 pyhd8ed1ab_2 conda-forge mercantile 1.2.1 pyhd8ed1ab_0 conda-forge mistune 0.8.4 py39hb82d6ee_1003 conda-forge mkl 2021.2.0 hb70f87d_389 conda-forge msys2-conda-epoch 20160418 1 conda-forge multipledispatch 0.6.0 pypi_0 pypi munch 2.5.0 py_0 conda-forge nbclient 0.5.3 pyhd8ed1ab_0 conda-forge nbconvert 6.0.7 py39hcbf5309_3 conda-forge nbformat 5.1.3 pyhd8ed1ab_0 conda-forge nest-asyncio 1.5.1 pyhd8ed1ab_0 conda-forge networkx 2.6.2 pypi_0 pypi notebook 6.4.0 pyha770c72_0 conda-forge numpy 1.22.3 pypi_0 pypi oauthlib 3.1.0 pypi_0 pypi olefile 0.46 pyh9f0ad1d_1 conda-forge opencv-python 4.5.3.56 pypi_0 pypi openjpeg 2.4.0 hb211442_1 conda-forge openpyxl 3.0.7 pyhd8ed1ab_0 conda-forge openssl 1.1.1l h8ffe710_0 conda-forge opt-einsum 3.3.0 pypi_0 pypi orjson 3.6.3 pypi_0 pypi osmnx 1.1.2 pypi_0 pypi packaging 20.9 pyh44b312d_0 conda-forge pandas 1.3.3 pypi_0 pypi pandoc 2.13 h8ffe710_0 conda-forge pandocfilters 1.4.2 py_1 conda-forge parso 0.8.2 pyhd8ed1ab_0 conda-forge patsy 0.5.1 py_0 conda-forge pbr 5.6.0 pypi_0 pypi pcre 8.45 h0e60522_0 conda-forge pickleshare 0.7.5 py_1003 conda-forge pillow 8.2.0 py39h1a9d4f7_1 conda-forge pip 21.1.2 pyhd8ed1ab_0 conda-forge pixman 0.40.0 h8ffe710_0 conda-forge platformdirs 2.3.0 pypi_0 pypi plotly 5.6.0 py_0 plotly pluggy 0.13.1 pypi_0 pypi ply 3.11 pypi_0 pypi poppler 21.09.0 h24fffdf_3 conda-forge poppler-data 0.4.11 hd8ed1ab_0 conda-forge postgresql 13.3 h1c22c4f_1 conda-forge proj 8.1.1 h1cfcee9_2 conda-forge prometheus_client 0.10.1 pyhd8ed1ab_0 conda-forge prompt-toolkit 3.0.18 pyha770c72_0 conda-forge protobuf 3.17.1 pypi_0 pypi py 1.10.0 pypi_0 pypi pyarrow 6.0.0 pypi_0 pypi pyasn1 0.4.8 pypi_0 pypi pyasn1-modules 0.2.8 pypi_0 pypi pycparser 2.20 pyh9f0ad1d_2 conda-forge pydantic 1.8.2 pypi_0 pypi pygments 2.9.0 pyhd8ed1ab_0 conda-forge pyopenssl 21.0.0 pyhd8ed1ab_0 conda-forge pyparsing 2.4.7 pyh9f0ad1d_0 conda-forge pyproj 3.2.1 py39h39b2389_2 conda-forge pyqt 5.12.3 py39hcbf5309_7 conda-forge pyqt-impl 5.12.3 py39h415ef7b_7 conda-forge pyqt5-sip 4.19.18 py39h415ef7b_7 conda-forge pyqtchart 5.12 py39h415ef7b_7 conda-forge pyqtwebengine 5.12.1 py39h415ef7b_7 conda-forge pyrsistent 0.17.3 py39hb82d6ee_2 conda-forge pysocks 1.7.1 py39hcbf5309_3 conda-forge python 3.9.4 h7840368_0_cpython conda-forge python-dateutil 2.8.1 py_0 conda-forge python-multipart 0.0.5 pypi_0 pypi python-snappy 0.6.0 py39h1d87f24_1 conda-forge python_abi 3.9 1_cp39 conda-forge pythonping 1.1.0 pypi_0 pypi pytz 2021.1 pyhd8ed1ab_0 conda-forge pywavelets 1.1.1 pypi_0 pypi pywin32 300 py39hb82d6ee_0 conda-forge pywinpty 1.1.1 py39h99910a6_0 conda-forge pyxlsb 1.0.8 pypi_0 pypi pyyaml 5.4.1 py39hb82d6ee_0 conda-forge pyzmq 22.1.0 py39he46f08e_0 conda-forge qt 5.12.9 h5909a2a_4 conda-forge rasterio 1.2.10 py39h20dd13d_0 conda-forge regex 2022.3.15 pypi_0 pypi requests 2.25.1 pypi_0 pypi requests-oauthlib 1.3.0 pypi_0 pypi robotframework 4.1 pypi_0 pypi robotframework-archivelibrary 0.4.1 pypi_0 pypi robotframework-dependencylibrary 1.0.0.post1 pypi_0 pypi robotframework-jsonlibrary 0.3.1 pypi_0 pypi robotframework-pythonlibcore 3.0.0 pypi_0 pypi robotframework-seleniumlibrary 5.1.3 pypi_0 pypi rsa 4.7.2 pypi_0 pypi rtree 0.9.7 py39h09fdee3_2 conda-forge scikit-image 0.18.3 pypi_0 pypi scikit-learn 0.24.2 py39he931e04_0 conda-forge scipy 1.6.3 py39hc0c34ad_0 conda-forge seaborn 0.11.1 hd8ed1ab_1 conda-forge seaborn-base 0.11.1 pyhd8ed1ab_1 conda-forge selenium 3.141.0 pypi_0 pypi send2trash 1.5.0 py_0 conda-forge setuptools 49.6.0 py39hcbf5309_3 conda-forge shapely 1.7.1 py39haadaec5_5 conda-forge six 1.15.0 pypi_0 pypi snappy 1.1.8 ha925a31_3 conda-forge sniffio 1.2.0 pypi_0 pypi snuggs 1.4.7 py_0 conda-forge sqlite 3.36.0 h8ffe710_2 conda-forge starlette 0.14.2 pypi_0 pypi statsmodels 0.12.2 py39h5d4886f_0 conda-forge tbb 2021.2.0 h2d74725_0 conda-forge tenacity 8.0.1 pyhd8ed1ab_0 conda-forge tensorboard 2.6.0 pypi_0 pypi tensorboard-data-server 0.6.1 pypi_0 pypi tensorboard-plugin-wit 1.8.0 pypi_0 pypi tensorflow 2.6.0 pypi_0 pypi tensorflow-estimator 2.6.0 pypi_0 pypi termcolor 1.1.0 pypi_0 pypi terminado 0.10.0 py39hcbf5309_0 conda-forge testpath 0.5.0 pyhd8ed1ab_0 conda-forge threadpoolctl 2.1.0 pyh5ca1d4c_0 conda-forge thrift 0.15.0 py39h415ef7b_1 conda-forge tifffile 2021.4.8 pypi_0 pypi tiledb 2.3.4 h78dabda_0 conda-forge tk 8.6.10 h8ffe710_1 conda-forge toolz 0.11.2 pypi_0 pypi tornado 6.1 py39hb82d6ee_1 conda-forge tox 3.0.0 pypi_0 pypi tqdm 4.63.0 pypi_0 pypi traitlets 5.0.5 py_0 conda-forge traittypes 0.2.1 pypi_0 pypi typing-extensions 3.7.4.3 pypi_0 pypi tzdata 2021a he74cb21_0 conda-forge urllib3 1.26.5 pypi_0 pypi utm 0.7.0 pypi_0 pypi uvicorn 0.15.0 pypi_0 pypi vc 14.2 hb210afc_4 conda-forge virtualenv 20.7.2 pypi_0 pypi vs2015_runtime 14.28.29325 h5e1d092_4 conda-forge wcwidth 0.2.5 pyh9f0ad1d_2 conda-forge webencodings 0.5.1 py_1 conda-forge werkzeug 2.0.1 pypi_0 pypi wheel 0.36.2 pyhd3deb0d_0 conda-forge widgetsnbextension 3.6.0 pypi_0 pypi win_inet_pton 1.1.0 py39hcbf5309_2 conda-forge wincertstore 0.2 py39hcbf5309_1006 conda-forge winpty 0.4.3 4 conda-forge wrapt 1.12.1 pypi_0 pypi xerces-c 3.2.3 h0e60522_3 conda-forge xyzservices 2021.10.0 pyhd8ed1ab_0 conda-forge xz 5.2.5 h62dcd97_1 conda-forge yaml 0.2.5 he774522_0 conda-forge zeromq 4.3.4 h0e60522_0 conda-forge zipp 3.4.1 pyhd8ed1ab_0 conda-forge zlib 1.2.11 h8ffe710_1013 conda-forge zstd 1.5.0 h6255e5f_0 conda-forge ```
gboeing commented 2 years ago

Per the docs, use the retain_all parameter if you wish to retain more than just the largest weakly connected component.