我是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')
答案 0 :(得分:2)
我已经更新了您的函数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
进行迭代,并处理每个成员。 Polygon
的代码
处理在那里重复使用。
答案 1 :(得分:0)
查看有关multipolygons的精美文档
一个多面体是一系列面,它是具有外部属性的面对象。您需要遍历多多边形的多边形,并获取每个多边形的exterior.coords
。
在实践中,您可能希望GeoDataFrame中的几何为多边形,而不是多多边形,但不是。您可能需要将包含多面体的行拆分为多行,每行各包含一个多边形(或不,取决于您的使用情况)