簡體   English   中英

您如何重新投影 geopandas GeoSeries?

[英]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

這使我的應用程序中的事情有點復雜,因為我希望編寫一個函數,將GeoDataframesGeoSeries作為*args ,並對它們中的每一個進行重新投影,而無需返回並將對象重新分配給它們的變量。

這不是什么大不了的事。 我主要只是想知道為什么會這樣,因為許多其他方法(如.dropna()確實允許在GeoDataFrameGeoSeries對象中使用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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM