使用多个几何重命名 GeoDataFrame 中的几何列

Renaming geometry column in GeoDataFrame with more than one geometry

我对具有多个类型 geometry 的列的数据框有这个问题。我的数据框如下所示:

New_zone_short_edges = 

       id  vertex_id                    point1  \
1   A1                2  POINT (119.79008 28.35047)   
3   A1                4  POINT (122.85067 44.85106)   
5   A2                1  POINT (138.79141 26.48802)   
7   A2                3  POINT (141.73386 44.89716)   
13   A3               5   POINT (68.47770 44.07370)   
11   A3               3   POINT (46.63571 51.16575)   

                      point2    length                    midpoint  
1   POINT (122.85067 28.08433)    3.072140  POINT (121.32038 28.21740)  
3   POINT (119.92314 44.71798)    2.930553  POINT (121.38690 44.78452)  
5   POINT (141.92247 26.26168)    3.139230  POINT (140.35694 26.37485)  
7   POINT (138.79141 44.89716)    2.942450  POINT (140.26263 44.89716)  
13   POINT (65.42208 48.14785)    5.092692   POINT (66.94989 46.11078)  
11   POINT (46.59798 47.65744)    3.508504   POINT (46.61685 49.41159) 

具有以下信息:

<class 'geopandas.geodataframe.GeoDataFrame'>
Int64Index: 206 entries, 1 to 425
Data columns (total 6 columns):
 #   Column      Non-Null Count  Dtype   
---  ------      --------------  -----   
 0   id          206 non-null    object  
 1   vertex_id   206 non-null    int64   
 2     point1    206 non-null    geometry
 3     point2    206 non-null    geometry
 4     length    206 non-null    float64 
 5   midpoint    206 non-null    geometry
dtypes: float64(1), geometry(3), int64(1), object(1)
memory usage: 11.3+ KB
None

现在,如果我的数据框是 pandas 数据框,我可以重命名单个列,例如:

New_zone_short_edges = New_zone_short_edges.rename(columns ={'point1':'new_point'})

但是因为我想重命名一个几何体,我需要做这样的事情:

New_zone_short_edges.rename_geometry('<the new name>', inplace=True)

这是有问题的,因为我无法选择要重命名的几何图形。是否有与 pandas 类似的操作?

将我的数据框一分为三,更改几何名称然后与另一个合并似乎是一件乏味的事情,如果可以避免的话。我到处寻找替代方案,但没有任何运气。我在某处读到,在一个数据集中包含多个几何图形是不好的做法,但出于我的目的,至少对于计算而言,必须采用这种方式。

欢迎任何想法。谢谢!

第一次重命名时效果很好:

>>> df.rename(columns ={'point1':'new_point'})
    id  vertex_id                   new_point                      point2    length                    midpoint
1   A1          2  POINT (119.79008 28.35047)  POINT (122.85067 28.08433)  3.072140  POINT (121.32038 28.21740)
3   A1          4  POINT (122.85067 44.85106)  POINT (119.92314 44.71798)  2.930553  POINT (121.38690 44.78452)
5   A2          1  POINT (138.79141 26.48802)  POINT (141.92247 26.26168)  3.139230  POINT (140.35694 26.37485)
7   A2          3  POINT (141.73386 44.89716)  POINT (138.79141 44.89716)  2.942450  POINT (140.26263 44.89716)
13  A3          5   POINT (68.47770 44.07370)   POINT (65.42208 48.14785)  5.092692   POINT (66.94989 46.11078)
11  A3          3   POINT (46.63571 51.16575)   POINT (46.59798 47.65744)  3.508504   POINT (46.61685 49.41159)
>>> df.rename(columns ={'point1':'new_point'}).info()
<class 'geopandas.geodataframe.GeoDataFrame'>
Int64Index: 6 entries, 1 to 11
Data columns (total 6 columns):
 #   Column     Non-Null Count  Dtype   
---  ------     --------------  -----   
 0   id         6 non-null      object  
 1   vertex_id  6 non-null      int64   
 2   new_point  6 non-null      geometry
 3   point2     6 non-null      geometry
 4   length     6 non-null      float64 
 5   midpoint   6 non-null      geometry
dtypes: float64(1), geometry(3), int64(1), object(1)
memory usage: 508.0+ bytes

似乎您可能对以下事实感到困惑 rename_geometry 不需要您指定要重命名的几何列 - 这是因为它设置了 活动几何 列.

A GeoDataFrame may also contain other columns with geometrical (shapely) objects, but only one column can be the active geometry at a time. To change which column is the active geometry column, use the GeoDataFrame.set_geometry() method.

它基本上是由 df.geometry 访问器返回的列,以及您作为 geometry= 参数传递给 GeoDataFrame 构造函数的列。

例如,这样设置 point1 列:

>>> df.set_geometry('point1').rename_geometry('new point').info()
<class 'geopandas.geodataframe.GeoDataFrame'>
Int64Index: 6 entries, 1 to 11
Data columns (total 6 columns):
 #   Column     Non-Null Count  Dtype   
---  ------     --------------  -----   
 0   id         6 non-null      object  
 1   vertex_id  6 non-null      int64   
 2   new point  6 non-null      geometry
 3   point2     6 non-null      geometry
 4   length     6 non-null      float64 
 5   midpoint   6 non-null      geometry
dtypes: float64(1), geometry(3), int64(1), object(1)
memory usage: 508.0+ bytes

例如,这对于使用 GeoDataFrame.plot 绘图很有用。您指定的参数决定绘图颜色,形状由几何列决定。

如果您在没有 rename_geometry 的情况下重命名,您 运行 遇到的问题是您的 GeoDataFrame 然后会失去对其活动几何列的跟踪:

>>> df.set_geometry('point1').rename(columns={'point1': 'new point'}).plot()
[...]
AttributeError: No geometry data set yet (expected in column 'point1'.)

所以你需要

  • 将您的活动几何列重命名为 .rename_geometry(),将其他列重命名为 .rename()
  • 使用 .rename() 重命名所有列,然后使用 .set_geometry(new_column_name)
  • 再次设置活动几何