为何Geopandas的.to_crs()首次返回(inf, inf),二次调用结果正确?
Geopandas连续调用
.to_crs()返回不同结果(第一次为POINT (inf inf)) 问题背景
在Mac M1设备上使用Intel版本Anaconda创建的环境中,连续两次对相同输入调用Geopandas的.to_crs()函数,第一次返回无效的POINT (inf inf),第二次返回正确坐标值。
环境配置
name: geotest channels: - defaults - conda-forge dependencies: - python=3.11 - geopandas
最小复现代码
import geopandas as gpd import shapely print( gpd.GeoDataFrame( geometry=[shapely.geometry.Point(-100,40)], crs='EPSG:4326') .to_crs('ESRI:102008') ) # 完全相同的命令 print( gpd.GeoDataFrame( geometry=[shapely.geometry.Point(-100,40)], crs='EPSG:4326') .to_crs('ESRI:102008') )
运行输出
$ python minimal.py Intel MKL WARNING: Support of Intel(R) Streaming SIMD Extensions 4.2 (Intel(R) SSE4.2) enabled only processors has been deprecated. Intel oneAPI Math Kernel Library 2025.0 will require Intel(R) Advanced Vector Extensions (Intel(R) AVX) instructions. Intel MKL WARNING: Support of Intel(R) Streaming SIMD Extensions 4.2 (Intel(R) SSE4.2) enabled only processors has been deprecated. Intel oneAPI Math Kernel Library 2025.0 will require Intel(R) Advanced Vector Extensions (Intel(R) AVX) instructions. geometry 0 POINT (inf inf) geometry 0 POINT (-321422.376 6782.160)
相关依赖版本
python 3.11.7 hf27a42d_0 gdal 3.6.2 py311he4f215e_4 geopandas 0.14.2 py311hecd8cb5_0 geopandas-base 0.14.2 py311hecd8cb5_0 geos 3.8.0 hb1e8313_0 pyproj 3.6.1 py311h717f92e_0 shapely 2.0.1 py311ha6175ea_0
解决方案
1. 提前初始化pyproj坐标转换
首次调用to_crs时,pyproj可能未完成底层转换逻辑的初始化,导致计算出错。可以提前触发初始化:
import geopandas as gpd import shapely from pyproj import CRS # 提前初始化目标CRS并触发一次转换 crs_src = CRS('EPSG:4326') crs_dst = CRS('ESRI:102008') gpd.GeoSeries([shapely.geometry.Point(0,0)], crs=crs_src).to_crs(crs_dst) # 执行实际转换 print( gpd.GeoDataFrame( geometry=[shapely.geometry.Point(-100,40)], crs=crs_src) .to_crs(crs_dst) ) print( gpd.GeoDataFrame( geometry=[shapely.geometry.Point(-100,40)], crs=crs_src) .to_crs(crs_dst) )
2. 修复M1设备上的Intel环境兼容性
Intel版本Anaconda在M1芯片上运行时,底层计算库(如MKL、GDAL)可能存在兼容性问题:
- 修改环境配置,将
conda-forge设为优先通道,安装适配M1的原生包:
重新创建环境:name: geotest channels: - conda-forge - defaults dependencies: - python=3.11 - geopandas - pyproj>=3.7.0 - gdal>=3.8.0conda env create -f environment.yaml - 确保终端以Rosetta 2模式运行(右键终端→显示简介→勾选“使用Rosetta打开”)
3. 升级依赖版本
当前使用的geopandas、pyproj版本较旧,新版本可能已修复此类初始化问题:
conda update geopandas pyproj gdal shapely
内容的提问来源于stack exchange,提问作者patman
相关产品推荐
相关产品推荐

