尝试将多边形数据分成x和y坐标,但得到错误“'MultiPolygon'对象没有属性'外部'”

问题描述 投票:1回答:2

我是Python的新手,我正在尝试将多边形数据分成x和y坐标。我一直收到错误:“AttributeError :(”'MultiPolygon'对象没有属性'exterior'“,'发生在索引1')”

据我所知,Python对象MultiPolygon不包含数据外观。但是如何解决这个问题以使功能发挥作用呢?

def getPolyCoords(row, geom, coord_type):
    """Returns the coordinates ('x' or 'y') of edges of a Polygon exterior"""

    # Parse the exterior of the coordinate
    geometry = row[geom]

    if coord_type == 'x':
        # Get the x coordinates of the exterior
        return list( geometry.exterior.coords.xy[0] )
    elif coord_type == 'y':
        # Get the y coordinates of the exterior
        return list( geometry.exterior.coords.xy[1] )


# Get the Polygon x and y coordinates
grid['x'] = grid.apply(getPolyCoords, geom='geometry', coord_type='x', axis=1)
grid['y'] = grid.apply(getPolyCoords, geom='geometry', coord_type='y', axis=1)

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-8-73511dbae283> in <module>
      1 # Get the Polygon x and y coordinates
----> 2 grid['x'] = grid.apply(getPolyCoords, geom='geometry', coord_type='x', axis=1)
      3 grid['y'] = grid.apply(getPolyCoords, geom='geometry', coord_type='y', axis=1)

~\Anaconda3\lib\site-packages\pandas\core\frame.py in apply(self, func, axis, broadcast, raw, reduce, result_type, args, **kwds)
   6012                          args=args,
   6013                          kwds=kwds)
-> 6014         return op.get_result()
   6015 
   6016     def applymap(self, func):

~\Anaconda3\lib\site-packages\pandas\core\apply.py in get_result(self)
    140             return self.apply_raw()
    141 
--> 142         return self.apply_standard()
    143 
    144     def apply_empty_result(self):

~\Anaconda3\lib\site-packages\pandas\core\apply.py in apply_standard(self)
    246 
    247         # compute the result using the series generator
--> 248         self.apply_series_generator()
    249 
    250         # wrap results

~\Anaconda3\lib\site-packages\pandas\core\apply.py in apply_series_generator(self)
    275             try:
    276                 for i, v in enumerate(series_gen):
--> 277                     results[i] = self.f(v)
    278                     keys.append(v.name)
    279             except Exception as e:

~\Anaconda3\lib\site-packages\pandas\core\apply.py in f(x)
     72         if kwds or args and not isinstance(func, np.ufunc):
     73             def f(x):
---> 74                 return func(x, *args, **kwds)
     75         else:
     76             f = func

<ipython-input-4-8c3864d38986> in getPolyCoords(row, geom, coord_type)
      7     if coord_type == 'x':
      8         # Get the x coordinates of the exterior
----> 9         return list( geometry.exterior.coords.xy[0] )
     10     elif coord_type == 'y':
     11         # Get the y coordinates of the exterior

AttributeError: ("'MultiPolygon' object has no attribute 'exterior'", 'occurred at index 1')

python geospatial polygon bokeh geopandas
2个回答
2
投票

我更新了你的函数getPolyCoords(),以便能够处理其他几何类型,即MultiPolygonPointLineString。希望它适用于您的项目。

def getPolyCoords(row, geom, coord_type):
    """Returns the coordinates ('x|y') of edges/vertices of a Polygon/others"""

    # Parse the geometries and grab the coordinate
    geometry = row[geom]
    #print(geometry.type)

    if geometry.type=='Polygon':
        if coord_type == 'x':
            # Get the x coordinates of the exterior
            # Interior is more complex: xxx.interiors[0].coords.xy[0]
            return list( geometry.exterior.coords.xy[0] )
        elif coord_type == 'y':
            # Get the y coordinates of the exterior
            return list( geometry.exterior.coords.xy[1] )

    if geometry.type in ['Point', 'LineString']:
        if coord_type == 'x':
            return list( geometry.xy[0] )
        elif coord_type == 'y':
            return list( geometry.xy[1] )

    if geometry.type=='MultiLineString':
        all_xy = []
        for ea in geometry:
            if coord_type == 'x':
                all_xy.append(list( ea.xy[0] ))
            elif coord_type == 'y':
                all_xy.append(list( ea.xy[1] ))
        return all_xy

    if geometry.type=='MultiPolygon':
        all_xy = []
        for ea in geometry:
            if coord_type == 'x':
                all_xy.append(list( ea.exterior.coords.xy[0] ))
            elif coord_type == 'y':
                all_xy.append(list( ea.exterior.coords.xy[1] ))
        return all_xy

    else:
        # Finally, return empty list for unknown geometries
        return []

处理MultiPolygon几何的代码部分有一个循环,迭代所有成员Polygons,并处理它们中的每一个。 Polygon处理的代码在那里重用。


0
投票

请参阅有关multipolygons的匀称文档

多面体是一系列多边形,它是具有外部属性的多边形对象。您需要迭代多面的多边形,并获得每个多边形的exterior.coords

实际上,您可能希望GeoDataFrame中的几何图形是多边形,而不是多边形,但它们不是。您可能希望将具有多个多边形的行拆分为多个行,每个行都有一个多边形(或不,具体取决于您的用例)

© www.soinside.com 2019 - 2024. All rights reserved.