bw4sz

Nested Loop - Site Comparison

Discussion created by bw4sz on Dec 21, 2010
Latest reply on Jan 10, 2011 by LokionNitrox
Hi!

I'm new to python, but i believe i've exhausted my tools in modelbuilder. I have a cost path model that creates the best single path from every site, to every other site and then measures the cost path length. 64 sites, so a total of 2016 possible paths.

My sites are all in one table.

The model holds the first row as a source, and labels all others as destinations. So the first iteration the FID = 0 is the source and FID's 1-63 are the destinations. 2nd iteration FID = 1 is the source and FID's 2-63 are the destinations....

All i need is the model to iteratively (cursor?) go through and select each row from the "Possible destinations" (1,2,..,63) and perform the downstream tasks. I've called this a nested loop because i still need the entire model to iterate 63 times to grab the 63 sources and there corresponding possible destinations.

Below is my script, exported from modelbuilder

# Import system modules
import sys, string, os, arcgisscripting

# Create the Geoprocessor object
gp = arcgisscripting.create()

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

# Load required toolboxes...
gp.AddToolbox("C:/Program Files (x86)/ArcGIS/ArcToolbox/Toolboxes/Spatial Analyst Tools.tbx")
gp.AddToolbox("C:/Program Files (x86)/ArcGIS/ArcToolbox/Toolboxes/Data Management Tools.tbx")
gp.AddToolbox("C:/Program Files (x86)/ET SpatialTechniques/ET GeoWizards 10.0 for ArcGIS 9.2 and 9.3/ET GeoWizards.tbx")
gp.AddToolbox("C:/Program Files (x86)/ArcGIS/ArcToolbox/Toolboxes/Analysis Tools.tbx")
gp.AddToolbox("C:/Program Files (x86)/ArcGIS/ArcToolbox/Toolboxes/Conversion Tools.tbx")

# Script arguments...
Cost_Path_Raster_from_Source_to_Destination_ = sys.argv[1]
if Cost_Path_Raster_from_Source_to_Destination_ == '#':
Cost_Path_Raster_from_Source_to_Destination_ = "H:\\BenModel\\CostPat_Jord1" # provide a default value if unspecified

v_FID_____n_ = sys.argv[2]
if v_FID_____n_ == '#':
v_FID_____n_ = "\"FID\" = %n%" # provide a default value if unspecified

Input_Sites = sys.argv[3]
if Input_Sites == '#':
Input_Sites = "Jordani1kmSites" # provide a default value if unspecified

# Local variables...
CostPath_Output = "H:\\BenModel\\PathoutputError.shp"
Vector_CostPath = "H:\\BenModel\\RasterT_CostPat1.shp"
v___ = "H:\\BenModel\\CostPat_Jord1"
v__ = "H:\\BenModel\\RasterT_CostPat1.shp"
Destination_Site = "H:\\BenModel\\Jordani1kmSites_Select1_Sele.shp"
Row_Count = "63"
= "H:\\BenModel\\Jordani1kmSites_Select1_Sele.shp"
Path_Output = "H:\\BenModel\\PathoutputError.shp"
Extent = "227845 3922854 315204.999984 3963250.127696"
Possible_Destinations_for_Selected_Site_shp = "H:\\BenModel\\Jordani1kmSites_Select1.shp"
Final_Output = "H:\\BenModel\\PathoutputError.shp"
Source = "Jordani1kmSites"
Cost_Distance_n_ = "C:\\Users\\Ari\\Desktop\\BackupModel\\test\\Cdist%n%"
Backlink_n_ = "C:\\Users\\Ari\\Desktop\\BackupModel\\test\\blink%n%"
Extent__2_ = "227845 3922854 315204.999984 3963250.127696"
Energy_Friction_Surface = "H:\\BenModel\\finaltran_eb"
Jordani1kmSites = "Jordani1kmSites"

# Process: Select Site to act as source...
gp.SelectLayerByAttribute_management(Input_Sites, "NEW_SELECTION", v_FID_n_)

# Process: Cost Distance...
tempEnvironment0 = gp.extent
gp.extent = "227845 3922854 315204.999984 3963250.127696"
gp.CostDistance_sa(Source, Energy_Friction_Surface, Cost_Distance_n_, "", Backlink_n_)
gp.extent = tempEnvironment0

# Process: Select Individual Site...
gp.Select_analysis(Jordani1kmSites, Possible_Destinations_for_Selected_Site_shp, "\"FID\" > %n%")

# Process: Get Count...
gp.GetCount_management(Possible_Destinations_for_Selected_Site_shp)

#HERE -> i need everything downstream of here to be iterated (a for loop based on the row count above) for each row in the above file!


# Process: Selects Row in Possible Destinations...
gp.Select_analysis(Possible_Destinations_for_Selected_Site_shp, Destination_Site, "\"FID\" = %n%")

# Process: Cost Path...
tempEnvironment0 = gp.extent
gp.extent = "227845 3922854 315204.999984 3963250.127696"
gp.CostPath_sa(Destination_Site, Cost_Distance_n_, Backlink_n_, Cost_Path_Raster_from_Source_to_Destination_, "BEST_SINGLE", "FID")
gp.extent = tempEnvironment0

# Process: Raster to Polyline...
gp.RasterToPolyline_conversion(Cost_Path_Raster_from_Source_to_Destination_, Vector_CostPath, "ZERO", "0", "NO_SIMPLIFY", "VALUE")

# Process: Delete...
gp.Delete_management(Cost_Path_Raster_from_Source_to_Destination_, "")

# Process: Append...
gp.Append_management("H:\\BenModel\\RasterT_CostPat1.shp", Path_Output, "NO_TEST", "ARCID 'ARCID' true false false 6 Long 0 6 ,First,#,H:\\BenModel\\RasterT_CostPat1.shp,ARCID,-1,-1;GRID_CODE 'GRID_CODE' true false false 6 Long 0 6 ,First,#,H:\\BenModel\\RasterT_CostPat1.shp,GRID_CODE,-1,-1;FROM_NODE 'FROM_NODE' true false false 6 Long 0 6 ,First,#;TO_NODE 'TO_NODE' true false false 6 Long 0 6 ,First,#;Length 'Length' true false false 13 Float 0 0 ,First,#;ID 'ID' true false false 4 Short 0 4 ,First,#", "")

# Process: Delete (2)...
gp.Delete_management(Vector_CostPath, "")

# Process: Delete (3)...
gp.Delete_management(Destination_Site, "")

# Process: Calculate Length...
gp.toolbox = "C:/Program Files (x86)/ET SpatialTechniques/ET GeoWizards 10.0 for ArcGIS 9.2 and 9.3/ET GeoWizards.tbx";
gp.ET_GPCalculateLength(Path_Output, "", "", "")

Thanks for any help

Ben Weinstein
University of Minnesota

Outcomes