Python double loop

775
1
06-25-2010 09:57 AM
AlexanderHohl
New Contributor II
Hello people,
I have to run the process "extract by mask" 10 times. The issue here is that I don't have the classic case where I have an input raster of which I want to extract multipe parts by looping through a folder of masks which are named with three digit numbers (eg. wsh105). I also have 10 different input files, which are also named with three digit numbers (eg. sec105). The task is to extract cells from each input raster by using the corresponding mask raster (same number in the filename). Unfortunately, if I execute the code, the two files do not correspond to each other, meaning that eg. I extract wsh105 from sec106, instead of sec 105. For testing purposes, I just list the rasters in the code below:

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

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

# Set the necessary product code...
gp.SetProduct("ArcInfo")

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

# Allow output to overwrite...
gp.OverwriteOutput = 1

# Load required toolboxes...
gp.AddToolbox(r"C:\Program Files\ArcGIS\ArcToolbox\Toolboxes\Spatial Analyst Tools.tbx")

# Set the Geoprocessing environment...
gp.snapRaster = r"C:\CPA\BaseData.gdb\fric"
gp.cellSize = 30

# Set the workspace...
workspace = r"C:\CPA\Test.gdb"
OutputSpace = r"C:\CPA\SingleCostDistances\EucDist.gdb"

# Get a list of grids in the workspace.
try:

gp.workspace = workspace
Rasters = gp.ListRasters("wsh*","GRID")
Masks = gp.ListRasters("sec*","GRID")
Rasters.Reset()
Masks.Reset()
Raster = Rasters.Next()
Mask = Masks.Next()
Rasters.Sort()
Masks.Sort()



except:
print "listrasters1"
print gp.GetMessages()


try:
while Mask:
print Raster
print Mask
Raster = Rasters.Next()
Mask = Masks.Next()


As you can see, both input rasters and mask rasters are in the same geodatabase. I get the result below:

listrasters1

wsh107
sec101
wsh106
sec100
wsh105
sec102
wsh103
sec001
wsh102
sec103
wsh101
sec105
wsh100
sec106
wsh108
sec107
wsh001
sec108
wsh109
sec109

But my goal is to have this:

listrasters1

wsh001
sec001
wsh100
sec100
wsh101
sec101
wsh102
sec102
wsh103
sec103
wsh105
sec105
wsh106
sec106
wsh107
sec107
wsh108
sec108
wsh109
sec109

Can anyone help me with this? Don't worry about the indents, they got lost while posting this.

Alexander Hohl
University of North Carolina at Charlotte
0 Kudos
1 Reply
ChrisMathers
Occasional Contributor III
Take the filenames of the rasters and then slice out the number and then iterate over the list of masks and select the one you want by that number.
for raster in Rasters:
 number=raster[3:6]
 for mask in Masks:
  if number==mask[3:6]:
   DO YOUR STUFF HERE
  else:
   print ('No matching mask'); break
 

The reason you arent getting a good sort is the same reason sorting by a text field thats full of numbers doesnt put the numbers in the right order. Text doesnt sort like numbers and your list is all strings. This method doesnt need to be sorted to work. You just match the raster to its mask and go.
0 Kudos