GeoPandas to_crs方法报错ProjError:x,y,z,time维度不一致求助
解决GeoPandas to_crs转换时的ProjError问题
问题概述
使用GeoPandas的to_crs方法转换坐标参考系时触发错误:ProjError: x, y, z, and time must be same size,已执行conda update --all并在多环境测试,问题未解决。
可复现代码
### to crs method check import geopandas as gpd import pandas as pd from shapely.geometry import Point df = pd.DataFrame( {'City': ['Buenos Aires', 'Brasilia', 'Santiago', 'Bogota', 'Caracas'], 'Country': ['Argentina', 'Brazil', 'Chile', 'Colombia', 'Venezuela'], 'Latitude': [-34.58, -15.78, -33.45, 4.60, 10.48], 'Longitude': [-58.66, -47.91, -70.66, -74.08, -66.86]}) gdf = gpd.GeoDataFrame(df, geometry=gpd.points_from_xy(df.Longitude, df.Latitude)) gdf.crs = "EPSG:4326" # original crs gdf = gdf.to_crs("EPSG:6668") # convert to new crs print(gdf)
报错栈
ProjError Traceback (most recent call last) Cell In[18], line 14 12 gdf = gpd.GeoDataFrame(df, geometry=gpd.points_from_xy(df.Longitude, df.Latitude)) 13 gdf.crs = "EPSG:4326" # original crs ---> 14 gdf = gdf.to_crs("EPSG:6668") # convert to new crs 15 print(gdf) File ~\anaconda3\lib\site-packages\geopandas\geodataframe.py:1364, in GeoDataFrame.to_crs(self, crs, epsg, inplace) 1362 else: 1363 df = self.copy() -> 1364 geom = df.geometry.to_crs(crs=crs, epsg=epsg) 1365 df.geometry = geom 1366 if not inplace: File ~\anaconda3\lib\site-packages\geopandas\geoseries.py:1124, in GeoSeries.to_crs(self, crs, epsg) 1047 def to_crs(self, crs=None, epsg=None): 1048 """Returns a ``GeoSeries`` with all geometries transformed to a new 1049 coordinate reference system. 1050 (...) 1121 1122 """ 1123 return GeoSeries( -> 1124 self.values.to_crs(crs=crs, epsg=epsg), index=self.index, name=self.name 1125 ) File ~\anaconda3\lib\site-packages\geopandas\array.py:779, in GeometryArray.to_crs(self, crs, epsg) 775 return self 777 transformer = Transformer.from_crs(self.crs, crs, always_xy=True) -> 779 new_data = vectorized.transform(self.data, transformer.transform) 780 return GeometryArray(new_data, crs=crs) File ~\anaconda3\lib\site-packages\geopandas\_vectorized.py:1114, in transform(data, func) 1111 result[~has_z] = set_coordinates(data[~has_z].copy(), np.array(new_coords_z).T) 1113 coords_z = get_coordinates(data[has_z], include_z=True) -> 1114 new_coords_z = func(coords_z[:, 0], coords_z[:, 1], coords_z[:, 2]) 1115 result[has_z] = set_coordinates(data[has_z].copy(), np.array(new_coords_z).T) 1117 return result File ~\anaconda3\lib\site-packages\pyproj\transformer.py:430, in Transformer.transform(self, xx, yy, zz, tt, radians, errcheck, direction) 428 intime = None 429 # call pj_transform. inx,iny,inz buffers modified in place. -> 430 self._transformer._transform( 431 inx, 432 iny, 433 inz=inz, 434 intime=intime, 435 direction=direction, 436 radians=radians, 437 errcheck=errcheck, 438 ) 439 # if inputs were lists, tuples or floats, convert back. 440 outx = _convertback(xisfloat, xislist, xistuple, inx) File pyproj/_transformer.pyx:459, in pyproj._transformer._Transformer._transform() ProjError: x, y, z, and time must be same size
环境信息
- 系统:Windows 10 10.0.22621-SP0,Python 3.9.16
- 核心库版本:
- GEOS: 3.8.0
- GDAL: 3.6.2
- PROJ: 6.2.1
- geopandas: 0.12.2
- pyproj: 2.6.1.post1
- shapely: 2.0.1
解决思路与方案
1. 版本兼容性修复(优先推荐)
错误根源是Shapely 2.0.x与旧版pyproj/PROJ的兼容性冲突:Shapely 2.0对几何坐标的Z值处理逻辑更新,而pyproj 2.6.1、PROJ 6.2.1不支持新逻辑,导致转换时Z坐标维度匹配失败。
执行以下conda命令升级到兼容版本:
conda install -c conda-forge pyproj>=3.0 proj>=7.0 geopandas shapely
该命令会安装与Shapely 2.0匹配的pyproj/PROJ版本,同时确保GeoPandas依赖兼容。
2. 强制转换为2D几何
如果暂时无法升级库,可以强制将所有几何对象转为2D,避免Z坐标干扰转换流程:
修改代码,在设置CRS后添加以下行:
# 转换所有几何为2D gdf['geometry'] = gdf['geometry'].apply(lambda geom: geom.to_2d() if hasattr(geom, 'to_2d') else geom)
之后再执行to_crs转换即可。
3. 验证坐标系定义
若上述方案无效,可先尝试转换到常见坐标系(如EPSG:3857):
gdf = gdf.to_crs("EPSG:3857")
如果转换成功,说明问题出在EPSG:6668的坐标系定义——旧版PROJ的坐标参考系数据可能缺失或不兼容该EPSG代码,此时需升级PROJ到7.0+版本(方案1)。
内容的提问来源于stack exchange,提问作者wayway0914
相关产品推荐
相关产品推荐

