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.92k stars 828 forks source link

Warning when using graph_to_gdfs with fill_edge_geometry=False #1006

Closed pmlpm1986 closed 1 year ago

pmlpm1986 commented 1 year ago

Completely fill out the following template.

Problem description

I built a package that relies on osmnx (1.4.0) and got the following warning:

 /.../lib/python3.11/site-packages/osmnx/utils_graph.py:85: FutureWarning: You are adding a column named 'geometry' to a GeoDataFrame constructed without an active geometry column. Currently, this automatically sets the active geometry column to 'geometry' but in the future that will no longer happen. Instead, either provide geometry to the GeoDataFrame constructor (GeoDataFrame(... geometry=GeoSeries()) or use `set_geometry('geometry')` to explicitly set the active geometry column.
    gdf_edges["geometry"] = np.nan

-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html

I just wanted to signal this.

Environment information

affine 2.4.0 alabaster 0.7.13 arrow 1.2.3 astroid 2.15.4 asttokens 2.2.1 atomicwrites 1.4.1 attrs 23.1.0 autopep8 2.0.2 Babel 2.12.1 backcall 0.2.0 beautifulsoup4 4.12.2 binaryornot 0.4.4 black 23.3.0 bleach 6.0.0 build 0.10.0 certifi 2022.12.7 cffi 1.15.1 chardet 5.1.0 charset-normalizer 3.1.0 click 8.1.3 click-plugins 1.1.1 cligj 0.7.2 cloudpickle 2.2.1 comm 0.1.3 ConfigArgParse 1.5.3 contextily 1.3.0 contourpy 1.0.7 cookiecutter 2.1.1 coverage 7.2.5 cryptography 40.0.2 cycler 0.11.0 debugpy 1.6.7 decorator 5.1.1 defusedxml 0.7.1 diff-match-patch 20230430 dill 0.3.6 docstring-to-markdown 0.12 docutils 0.18.1 et-xmlfile 1.1.0 executing 1.2.0 fastjsonschema 2.16.3 Fiona 1.9.4.post1 flake8 6.0.0 fluids 1.0.23 fonttools 4.39.3 geographiclib 2.0 geopandas 0.13.2 geopy 2.3.0 iapws 1.5.3 idna 3.4 imagesize 1.4.1 importlib-metadata 6.6.0 inflection 0.5.1 iniconfig 2.0.0 intervaltree 3.1.0 ipykernel 6.22.0 ipython 8.13.1 ipython-genutils 0.2.0 isort 5.12.0 jaraco.classes 3.2.3 jedi 0.18.2 jeepney 0.8.0 jellyfish 0.11.2 Jinja2 3.1.2 jinja2-time 0.2.0 joblib 1.2.0 jsonschema 4.17.3 jupyter_client 8.2.0 jupyter_core 5.3.0 jupyterlab-pygments 0.2.2 keyring 23.13.1 kiwisolver 1.4.4 lazy-object-proxy 1.9.0 MarkupSafe 2.1.2 matplotlib 3.7.1 matplotlib-inline 0.1.6 mccabe 0.7.0 mercantile 1.2.1 mistune 2.0.5 more-itertools 9.1.0 munch 2.5.0 mypy-extensions 1.0.0 nbclient 0.7.4 nbconvert 7.3.1 nbformat 5.8.0 nest-asyncio 1.5.6 networkx 3.1 numpy 1.24.3 numpydoc 1.5.0 openpyxl 3.1.2 osmnx 1.4.0 packaging 23.1 pandas 2.0.2 pandocfilters 1.5.0 parso 0.8.3 pathspec 0.11.1 pexpect 4.8.0 pickleshare 0.7.5 Pillow 9.5.0 pip 23.0.1 platformdirs 3.5.0 pluggy 1.0.0 ply 3.11 prompt-toolkit 3.0.38 psutil 5.9.5 ptyprocess 0.7.0 pure-eval 0.2.2 pycodestyle 2.10.0 pycparser 2.21 pydocstyle 6.3.0 pyflakes 3.0.1 Pygments 2.15.1 pylint 2.17.3 pylint-venv 3.0.1 pyls-spyder 0.4.0 pymhlib 0.1.5 pyogrio 0.6.0 Pyomo 6.6.1 pyparsing 3.0.9 pyproj 3.5.0 pyproject_hooks 1.0.0 PyQt5 5.15.9 PyQt5-Qt5 5.15.2 PyQt5-sip 12.12.1 PyQtWebEngine 5.15.6 PyQtWebEngine-Qt5 5.15.2 pyrsistent 0.19.3 pytest 7.3.1 pytest-cov 4.0.0 python-dateutil 2.8.2 python-lsp-black 1.2.1 python-lsp-jsonrpc 1.0.0 python-lsp-server 1.7.2 python-slugify 8.0.1 pytoolconfig 1.2.5 pytz 2023.3 pyxdg 0.28 PyYAML 6.0 pyzmq 25.0.2 QDarkStyle 3.1 qstylizer 0.2.2 QtAwesome 1.2.3 qtconsole 5.4.2 QtPy 2.3.1 rasterio 1.3.6 requests 2.29.0 rope 1.7.0 Rtree 1.0.1 scikit-learn 1.2.2 scipy 1.10.1 seaborn 0.12.2 SecretStorage 3.3.3 setuptools 66.1.1 shapely 2.0.1 six 1.16.0 snowballstemmer 2.2.0 snuggs 1.4.7 sortedcontainers 2.4.0 soupsieve 2.4.1 Sphinx 6.2.1 sphinx-copybutton 0.5.2 sphinx-rtd-theme 1.2.2 sphinxcontrib-applehelp 1.0.4 sphinxcontrib-devhelp 1.0.2 sphinxcontrib-htmlhelp 2.0.1 sphinxcontrib-jquery 4.1 sphinxcontrib-jsmath 1.0.1 sphinxcontrib-qthelp 1.0.3 sphinxcontrib-serializinghtml 1.1.5 spyder 5.4.3 spyder-kernels 2.4.3 stack-data 0.6.2 text-unidecode 1.3 textdistance 4.5.0 threadpoolctl 3.1.0 three-merge 0.1.1 tinycss2 1.2.1 toml 0.10.2 tomlkit 0.11.8 tornado 6.3.1 traitlets 5.9.0 tzdata 2023.3 ujson 5.7.0 urllib3 1.26.15 watchdog 3.0.0 wcwidth 0.2.6 webencodings 0.5.1 whatthepatch 1.0.4 wrapt 1.15.0 wurlitzer 3.0.3 xyzservices 2023.2.0 yapf 0.32.0 zipp 3.15.0

Provide a complete minimal reproducible example

import osmnx as ox
G = ox.graph_from_place('Spoy, France')
G2 = G.copy()
for edge_key in G.edges(keys=True):
    if 'geometry' in G2.edges[edge_key]:
        G2.edges[edge_key].pop('geometry')
gdf = ox.utils_graph.graph_to_gdfs(G2,fill_edge_geometry=False)
gboeing commented 1 year ago

Thanks @pmlpm1986, I noticed this GeoPandas warning recently too and I think we're in the clear and our code is behaving properly for the future. The relevant lines are here. Our code takes a GeoDataFrame that may or may not already have a "geometry" column, adds a null "geometry" column to it if it's lacking one, then sets the geometry. However, the GeoPandas warning states:

Currently, this automatically sets the active geometry column to 'geometry' but in the future that will no longer happen. Instead, either provide geometry to the GeoDataFrame constructor (GeoDataFrame(... geometry=GeoSeries()) or use set_geometry('geometry') to explicitly set the active geometry column.

We're already doing the latter---using set_geometry('geometry') to explicitly set the active geometry column---so I'm not totally clear why the warning is happening anyway. Maybe a GeoPandas expert can confirm if we're handling this properly for future-proofing if they have any time and inclination @martinfleis @jorisvandenbossche.

gboeing commented 1 year ago

FWIW the relevant bit in the GeoPandas code base is here.

martinfleis commented 1 year ago

we're in the clear and our code is behaving properly for the future

I don't think that is correct but not because of the assignment on l.85 but the line 86.

https://github.com/gboeing/osmnx/blob/e4e2b6c515f166da5ccf9b7ccc31ea8040b040a2/osmnx/utils_graph.py#L85-L86

set_geometry does not work in place by default so the line does nothing. Once the behaviour of default active geometry columns changes, this GeoDataFrame will result in a one with no active geometry.

I will make a PR to fix this.