简体   繁体   English

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

[英]Convert nested list of coordinates to Shapely polygons

I have a dataset like this:我有一个这样的数据集:

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]]

I want to convert the "Co-ordinates" column into POLYGONS, like this:我想将“坐标”列转换为多边形,如下所示:

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))

This is what I have tried:这是我尝试过的:

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

This is the error I get:这是我得到的错误:

---------------------------------------------------------------------------
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: 

you can do simply apply the function Polygon if the cells are list of lists如果单元格是列表列表,您可以简单地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,...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM