geopandas指定给几何体

2024-03-29 11:17:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个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)

这是行不通的圆锥几何似乎没有改变。在

有什么提示吗?在


Tags: fromidtransform代表事情ops计划func
2条回答

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中的每个几何体,则需要通过它们进行迭代或应用函数:

^{pr2}$

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

此代码起作用:

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”来执行任务。在

相关问题 更多 >