HTenkanen/pyrosm

ImportError: cannot import name 'TopologicalError' from 'shapely.geos'

Picard2200 opened this issue · 3 comments

Hi,

I tried to run one of your example code and got the following error:
_import os
os.environ['USE_PYGEOS'] = '0'
import geopandas

In a future release, GeoPandas will switch to using Shapely by default. If you are using PyGEOS directly (calling PyGEOS functions on geometries from GeoPandas), this will then stop working and you are encouraged to migrate from PyGEOS to Shapely 2.0 (https://shapely.readthedocs.io/en/latest/migration_pygeos.html).
import geopandas as gpd
Traceback (most recent call last):
File "E:\Dev\Scripts\RoutePlanner\test_osmnx.py", line 1, in
import osmnx as ox
File "C:\Users\needi\miniconda3\envs\ox\lib\site-packages\osmnx_init_.py", line 3, in
from ._api import *
File "C:\Users\needi\miniconda3\envs\ox\lib\site-packages\osmnx_api.py", line 22, in
from .geometries import geometries_from_address
File "C:\Users\needi\miniconda3\envs\ox\lib\site-packages\osmnx\geometries.py", line 21, in
from shapely.geos import TopologicalError
ImportError: cannot import name 'TopologicalError' from 'shapely.geos' (C:\Users\needi\miniconda3\envs\ox\lib\site-packages\shapely\geos.py)

Process finished with exit code 1_

I am running the script that you gave as example (extract below):
**import osmnx as ox
from shapely.geometry import box
from pyrosm import OSM, get_data
import time

Parse buildings with Pyrosm and time it

start_t = time.time()

A PBF data for Helsinki Region (~34 MB)

will be downloaded automatically to TEMP with Pyrosm

pbf_path = get_data("helsinki_region_pbf")**

My environment is:

  • Windows 10
  • miniconda python 3.8
  • environment ox just for installing osmnx and pyrosm
  • geopandas is installed when I install osmnx so as you recommended, pyrosm has been installed after using pip

Environment.yml:
name: ox
channels:

  • conda-forge
  • defaults
    dependencies:
  • affine=2.4.0=pyhd8ed1ab_0
  • appdirs=1.4.4=pyh9f0ad1d_0
  • attrs=22.2.0=pyh71513ae_0
  • blosc=1.21.3=hdccc3a2_0
  • boost-cpp=1.78.0=h9f4b32c_1
  • branca=0.6.0=pyhd8ed1ab_0
  • brotli=1.0.9=hcfcfb64_8
  • brotli-bin=1.0.9=hcfcfb64_8
  • brotlipy=0.7.0=py38h91455d4_1005
  • bzip2=1.0.8=h8ffe710_4
  • ca-certificates=2022.12.7=h5b45459_0
  • cairo=1.16.0=hd694305_1014
  • certifi=2022.12.7=pyhd8ed1ab_0
  • cffi=1.15.1=py38h57701bc_3
  • cfitsio=4.2.0=h9ebe7e4_0
  • charset-normalizer=2.1.1=pyhd8ed1ab_0
  • click=8.1.3=win_pyhd8ed1ab_2
  • click-plugins=1.1.1=py_0
  • cligj=0.7.2=pyhd8ed1ab_1
  • colorama=0.4.6=pyhd8ed1ab_0
  • contourpy=1.0.7=py38hb1fd069_0
  • cryptography=39.0.0=py38h95f5157_0
  • curl=7.87.0=h68f0423_0
  • cycler=0.11.0=pyhd8ed1ab_0
  • expat=2.5.0=h1537add_0
  • fiona=1.9.0=py38hc97cbf3_0
  • folium=0.14.0=pyhd8ed1ab_0
  • font-ttf-dejavu-sans-mono=2.37=hab24e00_0
  • font-ttf-inconsolata=3.000=h77eed37_0
  • font-ttf-source-code-pro=2.038=h77eed37_0
  • font-ttf-ubuntu=0.83=hab24e00_0
  • fontconfig=2.14.2=hbde0cde_0
  • fonts-conda-ecosystem=1=0
  • fonts-conda-forge=1=0
  • fonttools=4.38.0=py38h91455d4_1
  • freetype=2.12.1=h546665d_1
  • freexl=1.0.6=h67ca5e6_1
  • gdal=3.6.2=py38h5a6f081_3
  • geopandas=0.12.2=pyhd8ed1ab_0
  • geopandas-base=0.12.2=pyha770c72_0
  • geos=3.11.1=h1537add_0
  • geotiff=1.7.1=h720ab47_5
  • gettext=0.21.1=h5728263_0
  • hdf4=4.2.15=h1b1b6ef_5
  • hdf5=1.12.2=nompi_h57737ce_101
  • icu=70.1=h0e60522_0
  • idna=3.4=pyhd8ed1ab_0
  • intel-openmp=2023.0.0=h57928b3_25922
  • jinja2=3.1.2=pyhd8ed1ab_1
  • joblib=1.2.0=pyhd8ed1ab_0
  • jpeg=9e=h8ffe710_2
  • kealib=1.5.0=h61be68b_0
  • kiwisolver=1.4.4=py38hb1fd069_1
  • krb5=1.20.1=heb0366b_0
  • lcms2=2.14=ha5c8aab_1
  • lerc=4.0.0=h63175ca_0
  • libaec=1.0.6=h63175ca_1
  • libblas=3.9.0=16_win64_mkl
  • libbrotlicommon=1.0.9=hcfcfb64_8
  • libbrotlidec=1.0.9=hcfcfb64_8
  • libbrotlienc=1.0.9=hcfcfb64_8
  • libcblas=3.9.0=16_win64_mkl
  • libcurl=7.87.0=h68f0423_0
  • libdeflate=1.17=hcfcfb64_0
  • libffi=3.4.2=h8ffe710_5
  • libgdal=3.6.2=h060c9ed_3
  • libglib=2.74.1=he8f3873_1
  • libhwloc=2.8.0=h039e092_1
  • libiconv=1.17=h8ffe710_0
  • libkml=1.3.0=hf2ab4e4_1015
  • liblapack=3.9.0=16_win64_mkl
  • libnetcdf=4.8.1=nompi_h8c042bf_106
  • libpng=1.6.39=h19919ed_0
  • libpq=15.1=ha9684e8_3
  • librttopo=1.1.0=he22b5cd_12
  • libspatialindex=1.9.3=h39d44d4_4
  • libspatialite=5.0.1=h07bf483_22
  • libsqlite=3.40.0=hcfcfb64_0
  • libssh2=1.10.0=h9a1e1f7_3
  • libtiff=4.5.0=hf8721a0_2
  • libwebp-base=1.2.4=h8ffe710_0
  • libxcb=1.13=hcd874cb_1004
  • libxml2=2.10.3=hc3477c8_0
  • libzip=1.9.2=h519de47_1
  • libzlib=1.2.13=hcfcfb64_4
  • lz4-c=1.9.4=hcfcfb64_0
  • m2w64-gcc-libgfortran=5.3.0=6
  • m2w64-gcc-libs=5.3.0=7
  • m2w64-gcc-libs-core=5.3.0=7
  • m2w64-gmp=6.1.0=2
  • m2w64-libwinpthread-git=5.0.0.4634.697f757=2
  • mapclassify=2.5.0=pyhd8ed1ab_1
  • markupsafe=2.1.2=py38h91455d4_0
  • matplotlib-base=3.6.3=py38h528a6c7_0
  • mkl=2022.1.0=h6a75c08_874
  • msys2-conda-epoch=20160418=1
  • munch=2.5.0=py_0
  • munkres=1.1.4=pyh9f0ad1d_0
  • networkx=3.0=pyhd8ed1ab_0
  • numpy=1.24.1=py38h7ec9225_0
  • openjpeg=2.5.0=ha2aaf27_2
  • openssl=3.0.7=hcfcfb64_2
  • osmnx=1.1.1=pyhd8ed1ab_0
  • packaging=23.0=pyhd8ed1ab_0
  • pandas=1.5.3=py38h5846ac1_0
  • pcre2=10.40=h17e33f8_0
  • pillow=9.4.0=py38h087119c_1
  • pip=23.0=pyhd8ed1ab_0
  • pixman=0.40.0=h8ffe710_0
  • pooch=1.6.0=pyhd8ed1ab_0
  • poppler=22.12.0=h183ae7b_1
  • poppler-data=0.4.12=hd8ed1ab_0
  • postgresql=15.1=hd87cd2b_3
  • proj=9.1.0=heca977f_1
  • pthread-stubs=0.4=hcd874cb_1001
  • pthreads-win32=2.9.1=hfa6e2cd_3
  • pycparser=2.21=pyhd8ed1ab_0
  • pyopenssl=23.0.0=pyhd8ed1ab_0
  • pyparsing=3.0.9=pyhd8ed1ab_0
  • pyproj=3.4.1=py38hac5b721_0
  • pysocks=1.7.1=pyh0701188_6
  • python=3.8.16=h4de0772_1_cpython
  • python-dateutil=2.8.2=pyhd8ed1ab_0
  • python_abi=3.8=3_cp38
  • pytz=2022.7.1=pyhd8ed1ab_0
  • rasterio=1.3.4=py38h533082a_0
  • requests=2.28.2=pyhd8ed1ab_0
  • rtree=1.0.1=py38h8b54edf_1
  • scikit-learn=1.2.1=py38h69724d7_0
  • scipy=1.10.0=py38h0f6ee2a_0
  • setuptools=67.1.0=pyhd8ed1ab_0
  • shapely=2.0.1=py38h9c0aba1_0
  • six=1.16.0=pyh6c4a22f_0
  • snappy=1.1.9=hfb803bf_2
  • snuggs=1.4.7=py_0
  • sqlite=3.40.0=hcfcfb64_0
  • tbb=2021.7.0=h91493d7_1
  • threadpoolctl=3.1.0=pyh8a188c0_0
  • tiledb=2.13.2=h3132609_0
  • tk=8.6.12=h8ffe710_0
  • ucrt=10.0.22621.0=h57928b3_0
  • unicodedata2=15.0.0=py38h91455d4_0
  • urllib3=1.26.14=pyhd8ed1ab_0
  • vc=14.3=hb6edc58_10
  • vs2015_runtime=14.34.31931=h4c5c07a_10
  • wheel=0.38.4=pyhd8ed1ab_0
  • win_inet_pton=1.1.0=pyhd8ed1ab_6
  • xerces-c=3.2.4=h63175ca_1
  • xorg-libxau=1.0.9=hcd874cb_0
  • xorg-libxdmcp=1.1.3=hcd874cb_0
  • xyzservices=2022.9.0=pyhd8ed1ab_0
  • xz=5.2.6=h8d14728_0
  • zlib=1.2.13=hcfcfb64_4
  • zstd=1.5.2=h12be248_6
  • pip:
    • cykhash==2.0.0
    • cython==0.29.33
    • igraph==0.10.4
    • pygeos==0.14
    • pyrobuf==0.9.3
    • pyrosm==0.6.1
    • python-rapidjson==1.9
    • texttable==1.6.7

Your help will be appreciated

Revert Shapely to 1.8.5 and Numpy to 1.23.4
Should work.

Thank you very much for your help!
It solved this issue but I have now the following error:

....\ox\python.exe test_osmnx.py
....\ox\lib\site-packages\geopandas_compat.py:123: UserWarning: The Shapely GEOS version (3.11.1-CAPI-1.17.1) is incompatible with the GEOS version PyGEOS was compiled with (3.10.4-CAPI-1.16.2). Conversions between both will be slow.
warnings.warn(
Downloaded Protobuf data 'Helsinki_region.osm.pbf' (34.99 MB) to:
'C:\Users....\AppData\Local\Temp\pyrosm\Helsinki_region.osm.pbf'
Traceback (most recent call last):
File "....\test_osmnx.py", line 14, in
buildings_A = osm.get_buildings()
File "....\ox\lib\site-packages\pyrosm\pyrosm.py", line 265, in get_buildings
gdf = get_building_data(
File "....\ox\lib\site-packages\pyrosm\buildings.py", line 47, in get_building_data
gdf = prepare_geodataframe(
File "pyrosm/frames.pyx", line 134, in pyrosm.frames.prepare_geodataframe
cpdef prepare_geodataframe(nodes, node_coordinates, ways,
File "pyrosm/frames.pyx", line 147, in pyrosm.frames.prepare_geodataframe
relation_gdf = prepare_relation_gdf(node_coordinates, relations, relation_ways, tags_as_columns)
File "pyrosm/frames.pyx", line 123, in pyrosm.frames.prepare_relation_gdf
relations = prepare_relations(relations, relation_ways,
File "pyrosm/relations.pyx", line 175, in pyrosm.relations.prepare_relations
cpdef prepare_relations(relations, relation_ways, node_coordinates, tags_to_keep):
File "pyrosm/relations.pyx", line 176, in pyrosm.relations.prepare_relations
return _prepare_relations(relations, relation_ways, node_coordinates, tags_to_keep)
File "pyrosm/relations.pyx", line 165, in pyrosm.relations._prepare_relations
relation_records = get_relations(relations, relation_ways, node_coordinates)
File "pyrosm/relations.pyx", line 142, in pyrosm.relations.get_relations
geometry = fix_geometry(geometry)
File "pyrosm/geometry.pyx", line 25, in pyrosm.geometry.fix_geometry
fix_candidate = geometry.buffer(0)
File "....\ox\lib\site-packages\shapely\geometry\base.py", line 636, in buffer
return geom_factory(self.impl['buffer_with_params'](self, params, distance))
File "....\ox\lib\site-packages\shapely\topology.py", line 81, in call
return self.fn(this._geom, *args)
OSError: exception: access violation reading 0x0000000000055C98

The source code is what I found as example but I put here so that you check that I made no modification:

import osmnx as ox
from shapely.geometry import box
from pyrosm import OSM, get_data
import time

Parse buildings with Pyrosm and time it

start_t = time.time()

A PBF data for Helsinki Region (~34 MB)

will be downloaded automatically to TEMP with Pyrosm

pbf_path = get_data("helsinki_region_pbf")

osm = OSM(pbf_path)
buildings_A = osm.get_buildings()
print("=====================================\nPYROSM\n=====================================")
print(f"Parsing buildings with Pyrosm lasted {round(time.time() - start_t, 1)} seconds.")
print(f"Number of buildings in the Pyrosm dataset: {len(buildings_A)}")
print("\n=====================================\n")

Parse buildings from the same region using OSMnx and time it

start_t = time.time()
data_extent = box(*buildings_A.total_bounds)
buildings_B = ox.footprints_from_polygon(polygon=data_extent,
footprint_type="building",
retain_invalid=False,
)
print("=====================================\nOSMNX\n=====================================")
print(f"Parsing buildings with OSMnx lasted {round(time.time() - start_t, 1)} seconds.")
print(f"Number of buildings in the OSMnx dataset: {len(buildings_B)}")
print("\n=====================================\n")

This is now fixed in the new release pyrosm v0.6.2 which uses by default Shapely 2.0. See #214 . Try updating pyrosm with pip (installing from conda-forge is soon available as well).