将嵌套列表的坐标转换为Shapely多边形。

3

我有一个类似这样的数据集:

Area  Co-ordinates
01    [[-3.70497,40.59092],[-3.71467,40.59672],[-3.71977,40.6058]]
02    [[-3.67677,40.3948],[-3.67611,40.39428],[-3.67448,40.39541],[-3.67647,40.39786],[-3.67757,40.39613],[-3.67677,40.3948]]
03    [[-3.71417,40.60214],[-3.71754,40.60096],[-3.71977,40.6058],[-3.71643,40.60685],[-3.71417,40.60214]]

我希望将“坐标”列转换为多边形,如下所示:
Area  Co-ordinates
01    POLYGON((-3.70497,40.59092,-3.71467,40.59672,-3.71977,40.6058))
02    POLYGON((-3.67677,40.3948,-3.67611,40.39428,-3.67448,40.39541,-3.67647,40.39786,-3.67757,40.39613,-3.67677,40.3948))
03    POLYGON((-3.71417,40.60214,-3.71754,40.60096,-3.71977,40.6058,-3.71643,40.60685,-3.71417,40.60214))

这是我尝试过的:

df['Co-ordinates'] = df['Co-ordinates'].apply(Polygon)

这是我收到的错误信息:
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
/usr/local/lib/python3.6/dist-packages/shapely/speedups/_speedups.pyx in shapely.speedups._speedups.geos_linearring_from_py()

AttributeError: 'str' object has no attribute '__array_interface__'

During handling of the above exception, another exception occurred:

AssertionError                            Traceback (most recent call last)
<ipython-input-36-de451520e9b6> in <module>
----> 1 df['Co-ordinates'] = df['Co-ordinates'].apply(Polygon)

/usr/local/lib/python3.6/dist-packages/pandas/core/series.py in apply(self, func, convert_dtype, args, **kwds)
   4211             else:
   4212                 values = self.astype(object)._values
-> 4213                 mapped = lib.map_infer(values, f, convert=convert_dtype)
   4214 
   4215         if len(mapped) and isinstance(mapped[0], Series):

pandas/_libs/lib.pyx in pandas._libs.lib.map_infer()

/usr/local/lib/python3.6/dist-packages/shapely/geometry/polygon.py in __init__(self, shell, holes)
    241 
    242         if shell is not None:
--> 243             ret = geos_polygon_from_py(shell, holes)
    244             if ret is not None:
    245                 self._geom, self._ndim = ret

/usr/local/lib/python3.6/dist-packages/shapely/geometry/polygon.py in geos_polygon_from_py(shell, holes)
    507 
    508     if shell is not None:
--> 509         ret = geos_linearring_from_py(shell)
    510         if ret is None:
    511             return None

/usr/local/lib/python3.6/dist-packages/shapely/speedups/_speedups.pyx in shapely.speedups._speedups.geos_linearring_from_py()

AssertionError: 

POLYGON是什么,它属于哪个库?你尝试过什么? - ifly6
POLYGON 来自 shapely 或 geopandas 等库。它是地图上的点列表,用于创建多边形。 - star_it8293
1个回答

2

如果单元格是列表的列表,您可以简单地使用apply函数应用Polygon

from shapely.geometry import Polygon

df['Co-ordinates'] = df['Co-ordinates'].apply(Polygon)

print(df)
  Area                                       Co-ordinates
0   01  POLYGON ((-3.70497 40.59092, -3.71467 40.59672...
1   02  POLYGON ((-3.67677 40.3948, -3.67611 40.39428,...

出于某种原因,我得到了一个属性错误? - star_it8293
1
@user341383你能具体说明一下吗?这太模糊了,但这是在构建多边形时没有至少3个点时出现的错误类型。例如,尝试执行df.loc[df['Co-ordinates'].str.len()>=3, 'Co-ordinates'].apply(Polygon)。如果你没有收到错误提示,那么问题就在于你的数据。 - Ben.T
4
@user341383,我看到你的错误了,原因是你的单元格是字符串而不是列表嵌套列表,请尝试使用df['Co-ordinates'].apply(lambda x: Polygon(eval(x))) - Ben.T

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接