Попытка разделить данные многоугольника на координаты x и y, но получить ошибку: «У объекта« MultiPolygon »нет атрибута« внешний »» - PullRequest
1 голос
/ 13 апреля 2019

Я новичок в Python и пытаюсь разделить данные многоугольника на координаты x и y.Я продолжаю получать сообщение об ошибке: «AttributeError: (« У объекта «MultiPolygon» нет атрибута «внешний», «произошел с индексом 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')

Ответы [ 2 ]

2 голосов
/ 13 апреля 2019

Я обновил вашу функцию getPolyCoords(), чтобы включить обработку других типов геометрии, а именно, MultiPolygon, Point и LineString.Надеюсь, что это работает для вашего проекта.

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 геометрии, имеет цикл, который выполняет итерацию для всех элементов Polygon s и обрабатывает каждый из них.Код для обработки Polygon используется повторно.

0 голосов
/ 13 апреля 2019

См. Подробные документы о мультиполигонах

Мультиполигон - это последовательность полигонов, и это объект многоугольника, который имеет внешний атрибут.Вам нужно перебрать полигоны мультиполигона и получить exterior.coords каждого полигона.

На практике вы, вероятно, хотите, чтобы ваши геометрии в GeoDataFrame были полигонами, а не мультиполигонами, но это не так.,Возможно, вы захотите разбить строки, в которых есть мультиполигоны, на несколько строк, каждая с одним полигоном (или нет, в зависимости от вашего варианта использования)

...