python-3.x GeoPandas的ProjError(x,y,z和时间大小必须相同)

lf5gs5x2  于 2023-05-19  发布在  Python
关注(0)|答案(1)|浏览(476)
问题描述

不知何故,geopandas to_crs方法不起作用。我有`conda update --all``,并尝试在几个环境中运行下面的代码(主环境,仅安装了geopandas的最小环境),但它返回了相同的错误(ProjError:x、y、z和时间必须是相同的大小)。如果有人有办法解决这个问题,我很感激你的好心帮助。

代码示例,可复制粘贴的示例

### 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
geopandas.show_versions()输出

系统信息

python:3.9.16(main,Mar 8 2023,10:39:24)[MSC v.1916 64 bit(AMD 64)]可执行文件:C:\Users\xxx\anaconda3\python.exe计算机:Windows-10-10.0.22621-SP0

GEOS、GDAL、项目信息

GEOS:3.8.0 GEOS lib:无GDAL:3.6.2 GDAL数据目录:无项目:6.2.1 PROJ数据目录:C:\Users\xxx\anaconda3\Library\share\proj

Python依赖

geopandas:0.12.2 numpy:1.24.3Pandas:1.5.3 pyroj:2.6.1.post1匀称:2.0.1菲奥娜:1.9.1地质化学2:无地理位置:2.2.0 matplotlib:3.7.1Map分类:2.5.0 pygeos:无脓杆菌:无psycopg 2:无pyarrow:无rtree:1.0.1

pgvzfuti

pgvzfuti1#

您需要转换为有效的CRS:

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"  
gdf = gdf.to_crs("EPSG:3857")  
print(gdf)

它给出了

City    Country  Latitude  Longitude  \
0  Buenos Aires  Argentina    -34.58     -58.66   
1      Brasilia     Brazil    -15.78     -47.91   
2      Santiago      Chile    -33.45     -70.66   
3        Bogota   Colombia      4.60     -74.08   
4       Caracas  Venezuela     10.48     -66.86   

                            geometry  
0  POINT (-6530001.330 -4106950.295)  
1  POINT (-5333316.804 -1779259.486)  
2  POINT (-7865835.219 -3955187.399)  
3    POINT (-8246547.878 512620.654)  
4   POINT (-7442821.154 1173188.376)  
​

相关问题