Select to view content in your preferred language

Loop not working in business analyst drive time - python

1380
7
06-21-2010 03:54 PM
SabitaTripathi
Emerging Contributor
I have 3 shp files stored in a folder (Retailers_0, Retailers_1 and Retailers_2). I want to create a drive time by looping into each file. Below is the code. The problem I'm having is that it only reads the last file Retailers_2. Can anyone point out what's wrong in the code below?



# ---------------------------------------------------------------------------
# TestDT.py
# Created on: Mon Jun 21 2010 01:31:26 PM
# (generated by ArcGIS/ModelBuilder)
# Usage: TestDT <Input_Store_SHP_File> <Enter_Drive_Times> <Select_Drive_Time_Polygon_Output_Path>
# ---------------------------------------------------------------------------

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

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


# Load required toolboxes...
gp.CheckOutExtension("Business")
gp.CheckOutExtension("Network")

gp.AddToolbox("C:/Program Files/ArcGIS/ArcToolbox/Toolboxes/Business Analyst Tools.tbx")

for i in range(0,3):
s = str(i)
print s
Input_Store_SHP_File = "H:\\FINANCE\\Analysis\\ESRI\\Sabita\\NewModel\\Test\\Retailers_"+s+".shp"
print Input_Store_SHP_File
# Script arguments...
Enter_Drive_Times = "3" # provide a default value if unspecified
Select_Drive_Time_Polygon_Output_Path = "H:\\FINANCE\\Analysis\\ESRI\\Sabita\\NewModel\\Test\\Result\\Retailers_"+s+".shp"

# Local variables...
Output_Traversed_Streets_Feature_Class = ""

# Process: 1 to 20 min Drive Time...
#tempEnvironment0 = gp.scratchWorkspace
#gp.scratchWorkspace = "H:\\FINANCE\\Analysis\\ESRI\\Sabita\\NewModel\\Test\\Result\\"
gp.DriveTime_ba(Input_Store_SHP_File, "COMPID", "ALL", Enter_Drive_Times, "Minutes", Select_Drive_Time_Polygon_Output_Path, "", "", "false", "false", "false", "true", "false", "false", Output_Traversed_Streets_Feature_Class)
#gp.scratchWorkspace = tempEnvironment0
0 Kudos
7 Replies
CedricWannaz
Emerging Contributor
Hello,

     Is your code displaying 0, 1, 2 and outputting only the last file or is there even no output? Could you use CODE tags so we can see how your code is indented? You can do it by clicking the sharp (#) button in the editor.

Cedric
0 Kudos
SabitaTripathi
Emerging Contributor
It displays 0,1 and 2 but just reads the last file. I edit code with IDLE not pythonwin so sharp button is not there. Let me know if I can do any other way.



See below:

0
1
2
H:\FINANCE\Analysis\ESRI\Sabita\NewModel\Test\Retailers_2.shp
0 Kudos
CedricWannaz
Emerging Contributor
Sorry, I meant in the forum, as screenshot-ed below.
0 Kudos
SabitaTripathi
Emerging Contributor
Here it is:


# ---------------------------------------------------------------------------
# TestDT.py
# Created on: Mon Jun 21 2010 01:31:26 PM
#   (generated by ArcGIS/ModelBuilder)
# Usage: TestDT <Input_Store_SHP_File> <Enter_Drive_Times> <Select_Drive_Time_Polygon_Output_Path> 
# ---------------------------------------------------------------------------

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

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


# Load required toolboxes...
gp.CheckOutExtension("Business")

gp.AddToolbox("C:/Program Files/ArcGIS/ArcToolbox/Toolboxes/Business Analyst Tools.tbx")
        
# Script arguments...
for i in range(0,3):
    print i
    s = str(i)
Input_Store_SHP_File = "H:\\FINANCE\\Analysis\\ESRI\\Sabita\\NewModel\\Test\\Retailers_"+s+".shp" # provide a default value if unspecified
print Input_Store_SHP_File
Enter_Drive_Times = "3" # provide a default value if unspecified
Select_Drive_Time_Polygon_Output_Path = "H:\\FINANCE\\Analysis\\ESRI\\Sabita\\NewModel\\Test\\Result\\Retailers_"+s+".shp" # provide a default value if unspecified

# Local variables...
Output_Traversed_Streets_Feature_Class = ""

# Process: 1 to 20 min Drive Time...
#tempEnvironment0 = gp.scratchWorkspace
gp.scratchWorkspace = "H:\\FINANCE\\Analysis\\ESRI\\Sabita\\NewModel\\Test\\"
gp.DriveTime_ba(Input_Store_SHP_File, "COMPID", "ALL", Enter_Drive_Times, "Minutes", Select_Drive_Time_Polygon_Output_Path, "", "", "false", "false", "false", "true", "false", "false", Output_Traversed_Streets_Feature_Class)
#gp.scratchWorkspace = tempEnvironment0
0 Kudos
CedricWannaz
Emerging Contributor
Then try with this indentation:

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

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


# Load required toolboxes...
gp.CheckOutExtension("Business")

gp.AddToolbox("C:/Program Files/ArcGIS/ArcToolbox/Toolboxes/Business Analyst Tools.tbx")
        
# Script arguments...
for i in range(0,3):
 print i
 s = str(i)
 Input_Store_SHP_File = "H:\\FINANCE\\Analysis\\ESRI\\Sabita\\NewModel\\Test\\Retailers_"+s+".shp" # provide a default value if unspecified
 print Input_Store_SHP_File
 Enter_Drive_Times = "3" # provide a default value if unspecified
 Select_Drive_Time_Polygon_Output_Path = "H:\\FINANCE\\Analysis\\ESRI\\Sabita\\NewModel\\Test\\Result\\Retailers_"+s+".shp" # provide a default value if unspecified

 # Local variables...
 Output_Traversed_Streets_Feature_Class = ""

 # Process: 1 to 20 min Drive Time...
 #tempEnvironment0 = gp.scratchWorkspace
 gp.scratchWorkspace = "H:\\FINANCE\\Analysis\\ESRI\\Sabita\\NewModel\\Test\\"
 gp.DriveTime_ba(Input_Store_SHP_File, "COMPID", "ALL", Enter_Drive_Times, "Minutes", Select_Drive_Time_Polygon_Output_Path, "", "", "false", "false", "false", "true", "false", "false", Output_Traversed_Streets_Feature_Class)
 #gp.scratchWorkspace = tempEnvironment0


Your problem was that only the first two lines following the "for .. " were taken in the loop because of the indentation.

Best regards,

Cedric
0 Kudos
SabitaTripathi
Emerging Contributor
It worked. Thanks a lot!
0 Kudos
CedricWannaz
Emerging Contributor
My pleasure 🙂

Cedric
0 Kudos