你如何重新投影 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
这让我的应用程序有点复杂,因为我希望编写一个将 GeoDataframes
和 GeoSeries
作为 *args
并对它们中的每一个进行重新投影,而无需 return 并将对象重新分配给它们的变量。
没什么大不了的。我主要只是想知道,为什么 是这种情况,因为许多其他方法(如 .dropna()
)do 允许 inplace
参数在 GeoDataFrame
和 GeoSeries
对象中。那么为什么不用这个特定的方法呢?这是疏忽吗?还是有我不知道的充分理由?还是我用错了?
非常感谢!
PS:这超出了这个问题的范围,对于那些想知道用例的人:当有多个变量指向给定对象时,拥有方法的就地版本特别有价值,因此,其中一些指向对象的 'old'(即未重新投影)版本存在危险,从而导致错误。这是一个场景:
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
编辑:回答:你没有。
原问题:
我刚刚注意到,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
并对它们中的每一个进行重新投影,而无需 return 并将对象重新分配给它们的变量。
没什么大不了的。我主要只是想知道,为什么 是这种情况,因为许多其他方法(如 .dropna()
)do 允许 inplace
参数在 GeoDataFrame
和 GeoSeries
对象中。那么为什么不用这个特定的方法呢?这是疏忽吗?还是有我不知道的充分理由?还是我用错了?
非常感谢!
PS:这超出了这个问题的范围,对于那些想知道用例的人:当有多个变量指向给定对象时,拥有方法的就地版本特别有价值,因此,其中一些指向对象的 'old'(即未重新投影)版本存在危险,从而导致错误。这是一个场景:
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