添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
热心肠的茶叶  ·  Appending Features | ...·  2 天前    · 
机灵的炒面  ·  spring ...·  1 年前    · 
爱喝酒的橙子  ·  input ...·  1 年前    · 
没有腹肌的松鼠  ·  java ...·  1 年前    · 

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()
                Could you please supply some context?  For example, what does this script do?  What are your thoughts on why this may be happening?  What have you tried to troubleshoot this issue?  Thanks.
– Aaron
                Aug 15, 2014 at 16:02
                first arcpy.env.workspace = "C:\Users\LCP" should have an r in front of "C:.... ie r"C:\.... second, why is your outfile.close(0 within the loop? it should be outside and only written to when you are done
– user681
                Aug 15, 2014 at 16:03
                I posted a solution below to help with your first problem, which seems to be indentation. However there are other inefficiencies in your code that I'm seeing as well
– GISHuman
                Aug 15, 2014 at 16:05
                Thanks very much Aaron, Dan, and GISKid for your answer. I added an r in front of "C:, I placed outfile.close() at the same level than for rowTxt in rowsTxt, and I corrected the indentation. Now It works ! However, strange files (e.g. t_t763.aux.xml, t_t764.aux.xml,...) and folders (e.g. t_t763, t_t764, ...) are created every time that a file. txt is created. I think that this slows my code. Should I post a new question ?
– Marine
                Aug 15, 2014 at 16:49
                I would create a smaller subset of your data to test this on until you perfect your code. As for the file names, any idea where this is coming from? You should update your question with the current code you are using (under the former code as an edit) and perhaps add print statements to see where those names could be coming from
– GISHuman
                Aug 15, 2014 at 18:08

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: