Monday 27 March 2017

How to create a loop for multiple points with cost distance / cost path in ArcGIS 10.2 in python


I would like to know how to simplify the following code in order to execute it more efficiently than copy paste 82 processes (with a loop I guess?) but I don't know how to do it.


There are 4 main processes : Select analysis, cost distance, cost path, and raster to polylines. The study region is Uganda and there are 82 points and therefore 82 (or 81) routes to be founded. In the future, I would like to execute this code for other countries as well with more points. Many thanks in advance for your help :-)!





# Import arcpy module
import arcpy
arcpy.env.workspace = "E:\HEC\strategicmap\strategicmap.gdb"
arcpy.env.overwriteOutput = True

# Check out any necessary licenses
arcpy.CheckOutExtension("spatial")

# Set Geoprocessing environments

arcpy.env.scratchWorkspace = "E:\\HEC\\strategicmap\\scratch.gdb"
arcpy.env.snapRaster = ""
arcpy.env.extent = "E:\\HEC\\strategicmap\\strategicmap.gdb\\Uganda"
arcpy.env.cellSize = "MAXOF"
arcpy.env.mask = ""

# Local variables:
WorldWGS84WM = "E:\\HEC\\strategicmap\\strategicmap.gdb\\WorldWGS84WM"
PRIOcAfricaWGS84 = "E:\\HEC\\strategicmap\\strategicmap.gdb\\PRIOcAfricaWGS84"
costsurface = "E:\\HEC\\strategicmap\\strategicmap.gdb\\costsurface"

Uganda = "E:\\HEC\\strategicmap\\strategicmap.gdb\\Uganda"
outputcostdist = "E:\\HEC\\strategicmap\\strategicmap.gdb\\outputcostdist"
outputcostbacklink = "E:\\HEC\\strategicmap\\strategicmap.gdb\\outputcostbacklink"
PRIOUganda = "E:\\HEC\\strategicmap\\strategicmap.gdb\\PRIOUganda"
outputcostpath = "E:\\HEC\\strategicmap\\strategicmap.gdb\\outputcostpath"

#all files for Uganda (82 points)
route1= "E:\\HEC\\strategicmap\\strategicmap.gdb\\route1"
route2= "E:\\HEC\\strategicmap\\strategicmap.gdb\\route2"
route3= "E:\\HEC\\strategicmap\\strategicmap.gdb\\route3"

route4= "E:\\HEC\\strategicmap\\strategicmap.gdb\\route4"
route5= "E:\\HEC\\strategicmap\\strategicmap.gdb\\route5"
route6= "E:\\HEC\\strategicmap\\strategicmap.gdb\\route6"
route7= "E:\\HEC\\strategicmap\\strategicmap.gdb\\route7"
route8= "E:\\HEC\\strategicmap\\strategicmap.gdb\\route8"
route9= "E:\\HEC\\strategicmap\\strategicmap.gdb\\route9"
route10= "E:\\HEC\\strategicmap\\strategicmap.gdb\\route10"
route11= "E:\\HEC\\strategicmap\\strategicmap.gdb\\route11"
route12= "E:\\HEC\\strategicmap\\strategicmap.gdb\\route12"
route13= "E:\\HEC\\strategicmap\\strategicmap.gdb\\route13"

route14= "E:\\HEC\\strategicmap\\strategicmap.gdb\\route14"
route15= "E:\\HEC\\strategicmap\\strategicmap.gdb\\route15"
route16= "E:\\HEC\\strategicmap\\strategicmap.gdb\\route16"
route17= "E:\\HEC\\strategicmap\\strategicmap.gdb\\route17"
route18= "E:\\HEC\\strategicmap\\strategicmap.gdb\\route18"
route19= "E:\\HEC\\strategicmap\\strategicmap.gdb\\route19"
route20= "E:\\HEC\\strategicmap\\strategicmap.gdb\\route20"
##etc...until route82

Uganda1= "E:\\HEC\\strategicmap\\strategicmap.gdb\\Uganda1"

Uganda2= "E:\\HEC\\strategicmap\\strategicmap.gdb\\Uganda2"
Uganda3= "E:\\HEC\\strategicmap\\strategicmap.gdb\\Uganda3"
Uganda4= "E:\\HEC\\strategicmap\\strategicmap.gdb\\Uganda4"
Uganda5= "E:\\HEC\\strategicmap\\strategicmap.gdb\\Uganda5"
Uganda6= "E:\\HEC\\strategicmap\\strategicmap.gdb\\Uganda6"
Uganda7= "E:\\HEC\\strategicmap\\strategicmap.gdb\\Uganda7"
Uganda8= "E:\\HEC\\strategicmap\\strategicmap.gdb\\Uganda8"
Uganda9= "E:\\HEC\\strategicmap\\strategicmap.gdb\\Uganda9"
Uganda10= "E:\\HEC\\strategicmap\\strategicmap.gdb\\Uganda10"
Uganda11= "E:\\HEC\\strategicmap\\strategicmap.gdb\\Uganda11"

Uganda12= "E:\\HEC\\strategicmap\\strategicmap.gdb\\Uganda12"
Uganda13= "E:\\HEC\\strategicmap\\strategicmap.gdb\\Uganda13"
Uganda14= "E:\\HEC\\strategicmap\\strategicmap.gdb\\Uganda14"
Uganda15= "E:\\HEC\\strategicmap\\strategicmap.gdb\\Uganda15"
Uganda16= "E:\\HEC\\strategicmap\\strategicmap.gdb\\Uganda16"
Uganda17= "E:\\HEC\\strategicmap\\strategicmap.gdb\\Uganda17"
Uganda18= "E:\\HEC\\strategicmap\\strategicmap.gdb\\Uganda18"
Uganda19= "E:\\HEC\\strategicmap\\strategicmap.gdb\\Uganda19"
Uganda20= "E:\\HEC\\strategicmap\\strategicmap.gdb\\Uganda20"
##etc... until Uganda82


arcpy.Select_analysis(WorldWGS84WM, Uganda, "\"FIPS10_4\"='UG'")
arcpy.Clip_analysis(PRIOcAfricaWGS84, Uganda, PRIOUganda, "")

arcpy.Select_analysis(PRIOUganda,Uganda1, "\"OBJECTID\" =1")
arcpy.gp.CostPath_sa(PRIOUganda, outputcostdist, outputcostbacklink, outputcostpath, "EACH_CELL", "OBJECTID")
arcpy.RasterToPolyline_conversion(outputcostpath,route1, "ZERO", "0", "SIMPLIFY", "VALUE")
arcpy.gp.CostDistance_sa(Uganda1, costsurface, outputcostdist, "", outputcostbacklink)
arcpy.gp.CostPath_sa(PRIOUganda, outputcostdist, outputcostbacklink, outputcostpath,"EACH_CELL","OBJECTID")
arcpy.RasterToPolyline_conversion(outputcostpath,route1, "ZERO", "0", "SIMPLIFY", "VALUE")


arcpy.Select_analysis(PRIOUganda,Uganda2, "\"OBJECTID\" =2")
arcpy.gp.CostPath_sa(PRIOUganda, outputcostdist, outputcostbacklink, outputcostpath, "EACH_CELL", "OBJECTID")
arcpy.RasterToPolyline_conversion(outputcostpath,route2, "ZERO", "0", "SIMPLIFY", "VALUE")
arcpy.gp.CostDistance_sa(Uganda2, costsurface, outputcostdist, "", outputcostbacklink)
arcpy.gp.CostPath_sa(PRIOUganda, outputcostdist, outputcostbacklink, outputcostpath,"EACH_CELL","OBJECTID")
arcpy.RasterToPolyline_conversion(outputcostpath,route2, "ZERO", "0", "SIMPLIFY", "VALUE")

arcpy.Select_analysis(PRIOUganda,Uganda3, "\"OBJECTID\" =3")
arcpy.gp.CostPath_sa(PRIOUganda, outputcostdist, outputcostbacklink, outputcostpath, "EACH_CELL", "OBJECTID")

arcpy.RasterToPolyline_conversion(outputcostpath,route3, "ZERO", "0", "SIMPLIFY", "VALUE")
arcpy.gp.CostDistance_sa(Uganda3, costsurface, outputcostdist, "", outputcostbacklink)
arcpy.gp.CostPath_sa(PRIOUganda, outputcostdist, outputcostbacklink, outputcostpath,"EACH_CELL","OBJECTID")
arcpy.RasterToPolyline_conversion(outputcostpath,route3, "ZERO", "0", "SIMPLIFY", "VALUE")

##etc... until 82


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...