【问题标题】:Geopandas Rotate functionGeopandas 旋转功能
【发布时间】:2019-01-18 04:24:11
【问题描述】:

我想使用geopandas.rotate() 函数根据列中的属性旋转geodataframe 中的所有特征。如果我只选择一个功能,我就成功了。但是对于多行,我相信我需要创建一个 for 循环。

new_gdf = gpd.GeoDataFrame(columns=foo.columns)
for index, row in foo.iterrows():
    bar = foo[foo[row]].rotate(foo[rotation_col], origin='center')
    new_gdf=new_gdf.append(bar)

当我尝试编写 for 循环时,我得到一个类型错误 unhashable type:'Polygon'。我还尝试编写.apply() 函数。我不确定我做对了。但这就是我所拥有的:

foo_new = foo.apply(foo.rotate(foo.rotation_col, origin='center'))

我可以根据需要提供更多信息。

谢谢

【问题讨论】:

    标签: python pandas gis geopandas


    【解决方案1】:

    您正在尝试旋转 shapely.Polygon,而不是 GeoPandas.GeoSeries,因此您需要使用 shapely 来执行此操作。

    import geopandas as gpd
    import shapely
    
    p1 = shapely.geometry.Polygon([(0, 0), (1, 0), (1, 1), (0, 1)])
    p2 = shapely.geometry.Polygon([(2, 0), (3, 0), (3, 1), (2, 1)])
    
    g = gpd.GeoSeries([p1, p2])
    
    gdf = gpd.GeoDataFrame(geometry=g)
    gdf['angle'] = [25, 45]
    
    for index, row in gdf.iterrows():
        rotated = shapely.affinity.rotate(row['geometry'], row['angle'])
        gdf.loc[index, 'geometry'] = rotated
    

    【讨论】:

    • 非常感谢。 geopandas 中有一种旋转方法。我不确定为什么它不能循环执行。只有当它是一项功能时才有效。
    • 因为 GeoPandas rotate 正在将 GeoSeries 中的所有元素旋转相同的角度。如果您执行循环,则不再通过 GeoSeries,但在您的情况下,shapely.Polygon 和 GeoPandas rotate 需要 GeoSeries。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-23
    相关资源
    最近更新 更多