我正在尝试执行此代码,
import osgeo.ogr
def findPoints(geometry, results):
for i in range(geometry.GetPointCount()):
x,y,z = geometry.GetPoint(i)
if results['north'] == None or results['north'][1] < y:
results['north'] = (x,y)
if results['south'] == None or results['south'][1] > y:
results['south'] = (x,y)
for i in range(geometry.GetGeometryCount()):
findPoints(geometry.GetGeometryRef(i), results)
shapefile = osgeo.ogr.Open("../../Data/tl_2009_us_state/tl_2009_us_state.shp")
layer = shapefile.GetLayer(0)
feature = layer.GetFeature(53)
geometry = feature.GetGeometryRef()
results = {'north' : None,
'south' : None}
findPoints(geometry, results)
我不断收到这个错误,
Traceback (most recent call last):
File "identify_northsouth_point.py", line 22, in <module>
findPoints(geometry, results)
File "identify_northsouth_point.py", line 8, in findPoints
results['north'] = (x,y)
UnboundLocalError: local variable 'x' referenced before assignment
我尝试过全局和非本地,但它不起作用。由于我没有从函数外部获得任何输入,所以无论如何我都不需要全局或非本地的。