简体   繁体   English

您如何重新投影 geopandas GeoSeries?

[英]How do you inplace reproject a geopandas GeoSeries?

EDIT: Answer: you don't.编辑:回答:你没有。

Original question:原问题:

I just noticed, that geopandas GeoDataFrame allows for inplace reprojection:我刚刚注意到, 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)

...but GeoSeries does not: ...但 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

It is complicating things a bit in my app, as I was hoping to write a function that takes GeoDataframes and GeoSeries as *args and do a reprojection on each of them, without needing to return and re-assign the objects to their variables.这使我的应用程序中的事情有点复杂,因为我希望编写一个函数,将GeoDataframesGeoSeries作为*args ,并对它们中的每一个进行重新投影,而无需返回并将对象重新分配给它们的变量。

It is not a huge deal.这不是什么大不了的事。 I was mainly just wondering, why this is the case, as many other methods (like .dropna() ) do allow an inplace argument in both the GeoDataFrame and the GeoSeries objects.我主要只是想知道为什么会这样,因为许多其他方法(如.dropna()确实允许在GeoDataFrameGeoSeries对象中使用inplace参数。 So why not this specific method?那么为什么不使用这个特定的方法呢? Is it an oversight?是疏忽吗? Or is there a good reason for it that I'm unaware of?还是有一个我不知道的充分理由? Or am I just using it wrong?还是我只是用错了?

Many thanks!非常感谢!


PS: it's beyond the scope of this question, for those wondering about the use case: having an in-place version of a method is especially valuable when there are multiple variables pointing to a given object, and so there is a danger of some of these pointing to an 'old' (ie, not reprojected) version of the object, leading to errors down the line. PS:对于那些想知道用例的人来说,这超出了这个问题的范围:当有多个变量指向给定对象时,拥有一个方法的就地版本特别有价值,因此存在一些危险这些指向对象的“旧”(即,未重新投影)版本,从而导致错误。 Here is a scenario:这是一个场景:

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 is using GeoSeries to_crs to do the transformation, while GeoSeries.to_crs() is reprojecting geometries using apply . GeoDataFrame to_crs使用 GeoSeries to_crs进行转换,而GeoSeries.to_crs()使用apply重新投影几何。 Apply does not allow in place transformation and no one actually tried to implement in place option for that manually. Apply 不允许就地转换,也没有人真正尝试过手动实现就地选项。

This is the part of the code responsible for transformation:这是负责转换的代码部分:

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

I believe that there is no reason for not supporting it, but I might be as well wrong.我相信没有理由不支持它,但我也可能错了。 No one probably thought of implementing it :).可能没有人想到实施它:)。 Feel free to open issue or make a PR on GitHub .随意在GitHub 上打开问题或进行 PR。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM