python - for loops, indexing, taking the min value of a list -
i have 2 shapefiles: of lakes , of cities. need find closest city each lake , add name of city lake shapefile. have:
for lake in lake_cursor: lake_geom = lake.shape city_dist_list = [] #create city dis list = list of dist 1 lake each city cityid in range(0, city_length-1): #obtaining x , y both cities , lakes cityx = citylist_x_coor[cityid] cityy = citylist_y_coor[cityid] lakex = lake_geom.centroid.x lakey = lake_geom.centroid.y #calculate distance dist = math.sqrt((cityx-lakex)**2 + (cityy-lakey)**2) #add dist city dist list city_dist_list.append(dist) closest = min(city_dist_list) closestid = city_dist_list.index(closest) lake.city_name = citylist_city_name[closestid] lake.x_coor = citylist_x_coor[closestid] lake.y_coor = citylist_y_coor[closestid] print closest
but keep getting error message starting @ lake.city_name
. python shell isn't telling me wrong - ideas? , how fix well? thanks!
the traceback shows:
traceback (most recent call last): file "c:\users\xxx\xxx\xxx.py", line 71, in <module> lake.city_name = citylist_city_name[closestid] file "c:\program files\arcgis\desktop10.1\arcpy\arcpy\arcobjects\_base.py", line 35, in __setattr__ return setattr(self._arc_object, attr, ao) runtimeerror: error 999999: error executing function.
without seeing rest of code, it's hard tell certain, sure looks traceback trying update in searchcursor. searchcursor's rows don't have setters. make sure using updatecursor, , don't forget lake_cursor.updaterow(lake)
each lake update. below simple example of how use updatecursor.
import arcpy fc = 'c:/temp/temp.shp' rows = arcpy.updatecursor(fc) row in rows: print row.mapname row.mapname = 'some value' rows.updaterow(row) del row, rows
this example uses regular arcpy.updatecursor
. if have arcgis 10.1, can use arcpy.da.updatecursor
, run lot faster. read these updatecursors here , here.
Comments
Post a Comment