1

I am trying to figure out a way to input corresponding raster datasets from two separate folders into an equation using arcpy.

I think it requires some sort of nested loop but I can't figure out how to do this so that only corresponding rasters are inputted into the equation.

For the sake of this example lets say I have two folders with rasters listed in them as follows:

Folder 1        Folder 2
WC_top_2008     WC_int_2008
WC_top_2009     WC_int_2009
WC_top_2010     WC_int_2010
WC_top_2011     WC_int_2011
WC_top_2012     WC_int_2012

If I wanted to multiply the corresponding years in each dataset by one another (i.e. WC_top_2008 * WC_int_2008, WC_top_2009 * WC_int_2009 etc.).

How would I do this in arcpy?

I am using ArcGIS 10 with spatial analyst.

closed as off-topic by PolyGeo Feb 25 '18 at 21:39

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions seeking help to debug/write/improve code must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Providing a clear problem statement and evidence of a code attempt will help others to help you. See: How to create a Minimal, Complete, and Verifiable example." – PolyGeo
If this question can be reworded to fit the rules in the help center, please edit the question.

  • will the file structure always look like this? will there always be a corresponding raster dataset? will numbers being equally matched? if so why not just square grab the string and square it. – Moggy May 19 '14 at 18:08
  • There will always be matching years in the names of the raster datasets. I'm not sure what you mean by "square grab the string and square it"? The equations will be more complex than the simple multiplication I have given as well, this was just a simplified example. Thank you. – Catchment_Jack May 20 '14 at 8:06
  • use os.walk to make a list of the file names and then use a numpy array to multiply them. let me know if you need code – Moggy May 20 '14 at 20:52
  • Code would be useful thank you Moggy if you have the time spare? Still struggling to get my head around it. Thank you. – Catchment_Jack May 21 '14 at 17:58
1

I would use zip to group your rasters by year, iterate over those, and then do the raster algebra. This assumes you have a corresponding years for all of the datasets and they are in the correct order.

import arcpy, os

arcpy.env.workspace = r'C:\path\to\your\ws'
a = arcpy.ListRasters(WC_top*)
b = arcpy.ListRasters(WC_int*)
outws = r'C:\temp'

c = zip(sorted(a),sorted(b))

for d in c:
    name = os.path.join(outws, 'outRaster' + d[-4:] + ".img") # e.g. C:\temp\outRaster2008.img
    output = Raster(d[0]) * Raster(d[1])
    output.save(name)
  • I tried this but when I got the error message "Runtime error <type 'exceptions.TypeError'>: expected a raster or layer name". When I did print c[0] it gave [u'WC_Top_2008', 'WC_int_2008']. So it seems that it calls on the group rather than the individual raster in the group? – Catchment_Jack May 22 '14 at 18:36
  • Ah I see the issue, it should be output = Raster(d[0]) etc... I've edited your answer and given it the tick as I prefer this method to the one I came up with. – Catchment_Jack May 24 '14 at 14:29
  • @Catchment_Jack Regarding the edit, as long as you have corresponding years in the folder, they do not have to be in the correct order, that is why I added the sorted() function to c = zip(sorted(a),sorted(b)). – Aaron May 24 '14 at 16:05
0

I think I've figured out a way if the rasters are placed into the same input folder by using calling the rasters according to a range.

To get the desired results from the above example the following code works:

env.workspace = # folder with both sets of input files
WC_top_list = arcpy.ListRasters(WC_top*)
WC_int_list = arcpy.ListRasters(WC_int*)

for k in range (5):
  output = Raster(WC_top_list[k]) * Raster(WC_int_list[k])
  output.save("output%s" % k) # saves each raster with the name output followed by the number in the range

The only issue with this is keeping the years in the output file name.

The answer is by no means perfect but it has got me to where I need to be.

Not the answer you're looking for? Browse other questions tagged or ask your own question.