[英]How do you inplace reproject a geopandas GeoSeries?
编辑:回答:你没有。
原问题:
我刚刚注意到, 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()
)确实允许在GeoDataFrame
和GeoSeries
对象中使用inplace
参数。 那么为什么不使用这个特定的方法呢? 是疏忽吗? 还是有一个我不知道的充分理由? 还是我只是用错了?
非常感谢!
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
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 上打开问题或进行 PR。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.