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

Convert nested list of coordinates to Shapely polygons

我有这样的数据集:

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: 

如果单元格是列表的列表

,你可以简单地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,...