What is the fastest way to automatically generate least-cost paths (LCPs) between several polygons with ArcGIS ?
I created a python program to do this by using the functions "CostDistance" and "CostPath" of ArcGIS but my program is much too slow.
I need to rapidly build 275493 LCPs.
Here is my code:
# Import system modules import arcpy from arcpy import env from arcpy.sa import * arcpy.CheckOutExtension("Spatial") # Overwrite outputs arcpy.env.overwriteOutput = True # Set the workspace arcpy.env.workspace = "C:\Users\LCP" # Set the extent environment arcpy.env.extent = "costs.tif"rowsInPatches_start = arcpy.SearchCursor("selected_patches.shp") for rowStart in rowsInPatches_start: ID_patch_start = rowStart.getValue("GRIDCODE") ## Define SQL expression for the fonction Select Layer By AttributeexpressionForSelectInPatches_start = "GRIDCODE=%s" % (ID_patch_start) # Process: Select Layer By Attribute in Patches_startarcpy.MakeFeatureLayer_management("selected_patches.shp", "Selected_patch_start", expressionForSelectInPatches_start)# Process: Cost DistanceoutCostDist=CostDistance("Selected_patch_start", "costs.tif", "", "outCostLink.tif")# Save the output outCostDist.save("outCostDist.tif")rowsInSelectedPatches_end = arcpy.SearchCursor("selected_patches.shp") for rowEnd in rowsInSelectedPatches_end:ID_patch_end = rowEnd.getValue("GRIDCODE") ## Define SQL expression for the fonction Select Layer By AttributeexpressionForSelectInPatches_end = "GRIDCODE=%s" % (ID_patch_end) # Process: Select Layer By Attribute in Patches_endarcpy.MakeFeatureLayer_management("selected_patches.shp", "Selected_patch_end", expressionForSelectInPatches_end)# Process: Cost PathoutCostPath = CostPath("Selected_patch_end", "outCostDist.tif", "outCostLink.tif", "EACH_ZONE","FID")# Save the outputoutCostPath.save('P_' + str(int(ID_patch_start)) + '_' + str(int(ID_patch_end)) + ".tif")# Writing in file .txtoutfile=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()
أكثر...
I created a python program to do this by using the functions "CostDistance" and "CostPath" of ArcGIS but my program is much too slow.
I need to rapidly build 275493 LCPs.
Here is my code:
# Import system modules import arcpy from arcpy import env from arcpy.sa import * arcpy.CheckOutExtension("Spatial") # Overwrite outputs arcpy.env.overwriteOutput = True # Set the workspace arcpy.env.workspace = "C:\Users\LCP" # Set the extent environment arcpy.env.extent = "costs.tif"rowsInPatches_start = arcpy.SearchCursor("selected_patches.shp") for rowStart in rowsInPatches_start: ID_patch_start = rowStart.getValue("GRIDCODE") ## Define SQL expression for the fonction Select Layer By AttributeexpressionForSelectInPatches_start = "GRIDCODE=%s" % (ID_patch_start) # Process: Select Layer By Attribute in Patches_startarcpy.MakeFeatureLayer_management("selected_patches.shp", "Selected_patch_start", expressionForSelectInPatches_start)# Process: Cost DistanceoutCostDist=CostDistance("Selected_patch_start", "costs.tif", "", "outCostLink.tif")# Save the output outCostDist.save("outCostDist.tif")rowsInSelectedPatches_end = arcpy.SearchCursor("selected_patches.shp") for rowEnd in rowsInSelectedPatches_end:ID_patch_end = rowEnd.getValue("GRIDCODE") ## Define SQL expression for the fonction Select Layer By AttributeexpressionForSelectInPatches_end = "GRIDCODE=%s" % (ID_patch_end) # Process: Select Layer By Attribute in Patches_endarcpy.MakeFeatureLayer_management("selected_patches.shp", "Selected_patch_end", expressionForSelectInPatches_end)# Process: Cost PathoutCostPath = CostPath("Selected_patch_end", "outCostDist.tif", "outCostLink.tif", "EACH_ZONE","FID")# Save the outputoutCostPath.save('P_' + str(int(ID_patch_start)) + '_' + str(int(ID_patch_end)) + ".tif")# Writing in file .txtoutfile=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()
أكثر...