【问题标题】:Renaming geometry column in GeoDataFrame with more than one geometry使用多个几何重命名 GeoDataFrame 中的几何列
【发布时间】:2021-08-29 05:48:24
【问题描述】:

我对具有多个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 类似的东西?

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

欢迎任何想法。谢谢!

【问题讨论】:

    标签: python-3.x pandas geopandas


    【解决方案1】:

    第一次尝试重命名效果很好:

    >>> 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 不需要您指定要重命名的几何列这一事实感到困惑 - 这是因为它设置了 活动几何 列。

    GeoDataFrame 还可以包含其他具有几何(形状)对象的列,但一次只能有一列是活动几何。要更改哪一列是活动几何列,请使用 GeoDataFrame.set_geometry() 方法。

    它基本上是由 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)再次设置活动几何

    【讨论】:

    • 谢谢!正如你所说,我在那里感到困惑。
    猜你喜欢
    • 2023-03-09
    • 1970-01-01
    • 2017-01-26
    • 2018-08-25
    • 2011-01-25
    • 2021-12-28
    • 2020-02-05
    • 2017-04-21
    • 1970-01-01
    相关资源
    最近更新 更多