【问题标题】:geopandas assign to geometrygeopandas 分配给几何
【发布时间】:2019-10-08 05:04:42
【问题描述】:

我有一个代表美国大陆各州的 geopandas GeoDataframe。我想通过非平凡的映射将所有坐标转换为不同的坐标系。

我的计划是做类似的事情(这只是一个简单的测试,而不是真正的转换):

from shapely.ops import transform
def id_func(x, y):
    return x-10, y

alabama = conus.iloc[0]
alabama.geometry = transform (id_func, alabama.geometry)

这不起作用,conus.geometry 的值似乎没有改变。

有什么提示吗?

【问题讨论】:

  • 我猜你的 GeoDataFrame 没有单一的数据类型,所以阿拉巴马州是一个副本,而不是一个视图:stackoverflow.com/questions/47972633/…
  • 我回答了关于转换几何的实际主题,但关于“分配不起作用”:如果您要分配几何列表/数组,则设置几何属性应该正常工作。在上面的代码中,您分配了一个值,这通常会引发错误。您没有收到错误消息?

标签: python pandas geopandas shapely


【解决方案1】:

shapely.ops.transform 函数适用于单个 几何体。例如(使用您的 id_func 函数):

In [15]: from shapely.geometry import LineString

In [16]: from shapely.ops import transform

In [17]: l = LineString([(0, 0), (1, 1)])

In [18]: def id_func(x, y): 
    ...:     return x-10, y

In [20]: print(transform(id_func, l))
LINESTRING (-10 0, -9 1)

如果您想将此应用到 GeoSeries / GeoDataFrame 中的每个几何体,您将需要遍历它们或应用函数:

new_geometries = [transform(id_func, geom) for geom in alabama.geometry]

顺便说一句,如果您正在寻找使用自定义函数转换几何的方法,仿射变换可能会很有趣:https://shapely.readthedocs.io/en/stable/manual.html#affine-transformations(其中大部分直接暴露在 GeoDataFrame / GeoSeries 上)

【讨论】:

    【解决方案2】:

    此代码有效:

    from shapely.ops import transform
    def touv (x, y):
        if type(x) is not float:
            raise TypeError
        return ll2UV (satlat, satlon, BSlat, BSlon, y, x)
    
    for row in conus.index:
        conus.at[row,'geometry'] = transform (touv, conus.loc[row,'geometry'])
    

    看来,关键是我需要使用 '.at' 来执行分配。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-08-28
      • 1970-01-01
      • 1970-01-01
      • 2021-11-11
      • 2021-06-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多