Jen*_*nny 5 python polygon geospatial bokeh geopandas
我是 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')
Run Code Online (Sandbox Code Playgroud)
我已经更新了您的函数,getPolyCoords()以支持处理其他几何类型,即MultiPolygon、Point和LineString。希望它适用于您的项目。
def getPolyCoords(row, geom, coord_type):
"""
Returns the coordinates ('x|y') of edges/vertices of a Polygon/others
Args:
- row: the row object from a geodataframe; i.e. df.loc[1]
- geom: the name of "geometry" column, usually "geometry"
- coord_type: 'x', or 'y'
...
Valid geometrie types 'Polygon', 'Point', 'LineString', 'MultiLineString', 'MultiPolygon'
"""
# 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 = []
# updated code, using .geoms
for ea in geometry.geoms:
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.geoms:
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 []
Run Code Online (Sandbox Code Playgroud)
处理几何图形的代码部分MultiPolygon有一个循环,该循环迭代所有成员Polygon并处理每个成员。处理代码Polygon在那里被重用。
使用该函数的示例代码
import geopandas as gpd
from shapely.geometry import Polygon, LineString, MultiLineString
# Create a sample geodataframe
a = LineString([(0, 0), (0, 0.4), (1, 0.4), (1, 0)])
b = MultiLineString([[(0, 1.6), (0.5, 2.4), (1.2, 2.6), (1.3, 1.7)], [(0, 1.5), (1, 1.3), (1.5, 1.6)]])
c = Polygon([(1.4, 0), (1.5, .75), (2, 1), (2.1, 0)])
df = gpd.GeoDataFrame({"ID": ["a", "b", "c"], "geometry": [a, b, c]})
Run Code Online (Sandbox Code Playgroud)
使用该函数的示例代码:-
r0 = df.loc[0] # lower-left LineString
r1 = df.loc[1] # upper-left MultiLineString
r2 = df.loc[2] # lower-right Polygon
print( getPolyCoords(r0, "geometry", "x")) #[0.0, 0.0, 1.0, 1.0]
print( getPolyCoords(r1, "geometry", "y")) #[[1.6, 2.4, 2.6, 1.7], [1.5, 1.3, 1.6]]
print( getPolyCoords(r2, "geometry", "x")) #[1.4, 1.5, 2.0, 2.1, 1.4]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5091 次 |
| 最近记录: |