如何重新投影Geopandas GeoSeries?

问题描述 投票:0回答:1

我刚刚注意到,geopandas GeoDataFrame允许inplace重投影:

In [1]: import geopandas as gpd
In [2]: import shapely.geometry as sg
In [3]: data = {'geometry': [sg.Point(9,53), sg.Point(10,53.5)]}

In [4]: gdf = gpd.GeoDataFrame(data, crs='epsg:4326')
In [5]: gdf
Out[5]: 
                    geometry
0   POINT (9.00000 53.00000)
1  POINT (10.00000 53.50000)

In [6]: gdf.to_crs('epsg:3395', inplace=True) #No problem
In [7]: gdf
Out[7]: 
                          geometry
0  POINT (1001875.417 6948849.385)
1  POINT (1113194.908 7041652.839)

...但是GeoSeries不:

In [8]: gs = gpd.GeoSeries(data['geometry'], crs='epsg:4326')
In [9]: gs
Out[9]: 
0     POINT (9.00000 53.00000)
1    POINT (10.00000 53.50000)
dtype: geometry

In [10]: gs.to_crs('epsg:3395', inplace=True) #Problem
TypeError: to_crs() got an unexpected keyword argument 'inplace'

In [11]: gs.to_crs('epsg:3395')
Out[11]: 
0    POINT (1001875.417 6948849.385)
1    POINT (1113194.908 7041652.839)
dtype: geometry

这会使我的应用程序变得有些复杂,因为我希望编写一个将GeoDataframes GeoSeries当作*args的函数,并对每个函数进行重新投影,而无需返回并将对象重新分配给它们的变量。

这没什么大不了的。我主要是想知道,为什么是这种情况,因为许多其他方法(例如.dropna()doinplaceGeoDataFrame对象中都允许GeoSeries参数。那么为什么不使用这种特定方法呢?是疏忽吗?还是有一个我不知道的充分理由?还是我使用错了?

非常感谢!


PS:对于超出用例范围的那些人来说,这超出了此问题的范围:当存在多个指向给定对象的变量时,就地使用方法的版本特别有价值,因此存在着其中一些指向对象的“旧”(即未重新投影)版本,从而导致错误。这是一个场景:

gdf = self._geodataframe = gpd.GeoDataFrame(...) #saving dataframe as class variable
gdf.to_crs(..., inplace=True) # self._geodataframe is also reprojected

gs = self._geoseries = gpd.GeoSeries(...) #saving series as class variable
gs = gs.to_crs(...) #self._geoseries still has the original crs
python geopandas
1个回答
1
投票

GeoDataFrame to_crs正在使用GeoSeries to_crs进行转换,而GeoSeries.to_crs()正在使用apply重新投影几何。 Apply不允许就地转换,也没有人实际尝试为此手动实现就地选项。

这是代码中负责转换的部分:

transformer = Transformer.from_crs(self.crs, crs, always_xy=True)
result = self.apply(lambda geom: transform(transformer.transform, geom))
result.__class__ = GeoSeries
result.crs = crs
result._invalidate_sindex()
return result

我相信没有理由不支持它,但是我可能也是错的。没有人可能想到实现它:)。随时提出问题或对GitHub进行公关。

© www.soinside.com 2019 - 2024. All rights reserved.