Stack Exchange Network
Stack Exchange network consists of 183 Q&A communities including
Stack Overflow
, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.
Visit Stack Exchange
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It only takes a minute to sign up.
Sign up to join this community
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
Want to improve this question?
Update the question so it focuses on one problem only by
editing this post
.
Closed
9 years ago
.
Here is my code (It selects each polygon contained in the shapefile "selected_polygons.shp" and builds the least-cost path between the previously selected polygon and each polygon contained in the same shapefile)
import arcpy
from arcpy import env
from arcpy.sa import *
arcpy.CheckOutExtension("Spatial")
arcpy.env.overwriteOutput = True
arcpy.env.workspace = "C:\Users\LCP"
arcpy.env.extent = "costsn.tif"
with arcpy.da.SearchCursor("selected_polygons.shp",("*")) as rowsInPatches_start:
for rowStart in rowsInPatches_start:
ID_patch_start = int(rowStart[3])
## Define SQL expression for the fonction Select Layer By Attribute
expressionForSelectInPatches_start = "GRIDCODE=%s" % (ID_patch_start)
## Process: Select Layer By Attribute
arcpy.MakeFeatureLayer_management("selected_polygons.shp", "Selected_patch_start", expressionForSelectInPatches_start)
## Process: Cost Distance
outCostDist=CostDistance("Selected_patch_start", "costsn.tif", "", "outCostLink.tif")
## Save the output
outCostDist.save("outCostDist.tif")
with arcpy.da.SearchCursor("selected_polygons.shp",("*")) as rowsInSelectedPatches_end:
for rowEnd in rowsInSelectedPatches_end:
ID_patch_end = int(rowEnd[3])
## Define SQL expression for the fonction Select Layer By Attribute
expressionForSelectInPatches_end = "GRIDCODE=%s" % (ID_patch_end)
## Process: Select Layer By Attribute in Patches_end
arcpy.MakeFeatureLayer_management("selected_polygons.shp", "Selected_patch_end", expressionForSelectInPatches_end)
## Process: Cost Path
outCostPath = CostPath("Selected_patch_end", "outCostDist.tif", "outCostLink.tif", "EACH_ZONE","FID")
## Save the output
outCostPath.save('P_' + str(int(ID_patch_start)) + '_' + str(int(ID_patch_end)) + ".tif")
## Write in a file .txt
outfile=open('P_' + str(int(ID_patch_start)) + '_' + str(int(ID_patch_end)) + ".txt", "w")
rowsTxt = arcpy.SearchCursor('P_' + str(int(ID_patch_start)) + '_' + str(int(ID_patch_end)) + ".tif")
for rowTxt in rowsTxt:
value = rowTxt.getValue("Value")
count = rowTxt.getValue("Count")
pathcost = rowTxt.getValue("PATHCOST")
startrow = rowTxt.getValue("STARTROW")
startcol = rowTxt.getValue("STARTCOL")
print value, count, pathcost, startrow, startcol
outfile.write(str(value) + " " + str(count) + " " + str(pathcost) + " " + str(startrow) + " " + str(startcol) + "\n")
outfile.close()
–
–
–
–
–
The reason you are getting this particular error is that you are closing your outfile after the first iteration through the following loop:
for rowTxt in rowsTxt:
value = rowTxt.getValue("Value")
count = rowTxt.getValue("Count")
pathcost = rowTxt.getValue("PATHCOST")
startrow = rowTxt.getValue("STARTROW")
startcol = rowTxt.getValue("STARTCOL")
print value, count, pathcost, startrow, startcol
outfile.write(str(value) + " " + str(count) + " " + str(pathcost) + " " + str(startrow) + " " + str(startcol) + "\n")
outfile.close()
On the next iteration, you are calling the write method on a file that you just closed.
You need to move your outfile.close() statement outside of that loop:
for rowTxt in rowsTxt:
value = rowTxt.getValue("Value")
count = rowTxt.getValue("Count")
pathcost = rowTxt.getValue("PATHCOST")
startrow = rowTxt.getValue("STARTROW")
startcol = rowTxt.getValue("STARTCOL")
print value, count, pathcost, startrow, startcol
outfile.write(str(value) + " " + str(count) + " " + str(pathcost) + " " + str(startrow) + " " + str(startcol) + "\n")
outfile.close()
arcpy.CheckOutExtension("Spatial")
arcpy.env.overwriteOutput = True
arcpy.env.workspace = r"C:\Users\LCP"
arcpy.env.extent = "costsn.tif"
with arcpy.da.SearchCursor("selected_polygons.shp",("*")) as rowsInPatches_start:
for rowStart in rowsInPatches_start: