使用OSMNX的ox.graph_from_place函数时触发OSError异常求助
OSError: access violation writing 0x0000000000001101 when using osmnx.graph_from_place()
问题描述
用pip重装OSMNX后,执行代码G = ox.graph_from_place('Berlin', network_type = 'drive', simplify=False)时触发OSError,报错回溯如下:
OSError Traceback (most recent call last) ~\AppData\Local\Temp\ipykernel_18096\802543517.py in <module> 6 7 # download the street network ----> 8 G = ox.graph_from_place('Berlin', network_type = 'drive', simplify=False) ~\anaconda3\lib\site-packages\osmnx\graph.py in graph_from_place(query, network_type, simplify, retain_all, truncate_by_edge, which_result, buffer_dist, clean_periphery, custom_filter) 345 346 # extract the geometry from the GeoDataFrame to use in API query --> 347 polygon = gdf_place["geometry"].unary_union 348 utils.log("Constructed place geometry polygon(s) to query API") 349 ~\anaconda3\lib\site-packages\geopandas\base.py in unary_union(self) 726 POLYGON ((0 1, 0 2, 2 2, 2 0, 1 0, 0 0, 0 1)) 727 --> 728 return self.geometry.values.unary_union() 729 730 # ~\anaconda3\lib\site-packages\geopandas\array.py in unary_union(self) 636 637 def unary_union(self): --> 638 return vectorized.unary_union(self.data) 639 640 # ~\anaconda3\lib\site-packages\geopandas\_vectorized.py in unary_union(data) 909 data = [g for g in data if g is not None] 910 if data: --> 911 return shapely.ops.unary_union(data) 912 else: 913 return None ~\anaconda3\lib\site-packages\shapely\ops.py in unary_union(self, geoms) 159 subs[i] = g._geom 160 collection = lgeos.GEOSGeom_createCollection(6, subs, L) --> 161 return geom_factory(lgeos.methods['unary_union'](collection)) 162 163 operator = CollectionOperator() OSError: exception: access violation writing 0x0000000000001101
已尝试重启环境、更换虚拟环境,问题依旧。推测异常与geopandas或shapely有关,使用Anaconda、Win10系统(拥有管理员权限),此前无类似问题。
解决方案
彻底卸载并重新安装相关依赖(优先用conda而非pip,避免跨包管理器冲突):
conda remove shapely geopandas osmnx --force conda install -c conda-forge osmnx geopandas shapely注:pip和conda混合安装容易导致底层GEOS库版本不兼容,Access Violation多是C库调用时的内存错误,conda-forge的预编译包更适配Windows环境。
强制指定GEOS库版本,确保与shapely兼容:
conda install -c conda-forge geos=3.11.1可根据shapely版本调整,shapely 2.x系列通常需要GEOS 3.10及以上版本。
禁用shapely多线程(Windows环境下部分版本的并行处理会触发内存访问错误):
在代码开头添加以下设置:import os os.environ['SHAPELY_DISABLE_PARALLEL'] = '1'降级shapely到稳定版本:
如果最新版shapely存在兼容性问题,尝试降级到经过验证的稳定版本:conda install -c conda-forge shapely=2.0.1
内容的提问来源于stack exchange,提问作者Skerre
相关产品推荐
相关产品推荐

