spatial analysis tool: Combine with list in python

284
4
10-10-2017 03:04 PM
DanielaMazo
New Contributor

hi all, 

I need to combine several rasters but they have to fulfill a determinate condition (year), at the moment I´ve been able to extract the info that I need in to a list,  but when I try to run the tool: Combine it seems it doesn't accept the list as a parameter, this is the code that I´m using:

import arcpy
from arcpy.sa import *

arcpy.env.workspace = "D:\Baseline_2\Eliana\Biomass.gdb"
rasters = arcpy.ListRasters()

year="_0"

for rc in rasters:
if rc.endswith(year):
   listtemp.append(arcpy.ListRasters(rc))

   Year_0 = arcpy.sa.Combine([listtemp]); year_0.save(r"D:\Baseline_2\Eliana\Eliana.gdb\Year_0")
print("end script")
print(listtemp)

if you could help me that will be wonderfull 

thanks!!

0 Kudos
4 Replies
XanderBakker
Esri Esteemed Contributor

Try this:

import arcpy
from arcpy.sa import *
arcpy.env.workspace = "D:\Baseline_2\Eliana\Biomass.gdb"
rasters = arcpy.ListRasters()

year="_0"
for rc in rasters:
    if rc.endswith(year):
       listtemp.append(rc)
year_0 = arcpy.sa.Combine(listtemp)
year_0.save(r"D:\Baseline_2\Eliana\Eliana.gdb\Year_0")
print("end script")
print(listtemp)‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

The listtemp variable is already a list, don't use additional square brackets

Watch out with upper and lower case; year_0 is not the same as Year_0.

When you insert code, please use these instructions: Posting code with Syntax Highlighting on GeoNet 

Edit: corrected an error with indentation in the code and the error detected by mbabinski1988 

DanielaMazo
New Contributor

thanks xander, sorry I didn't know that part of the page is the first time that i used this forum.

It was very usefull,  

0 Kudos
XanderBakker
Esri Esteemed Contributor

In that case a welcome is in place and don't worry, we are here to help.

Did you try to run the code? I think that maybe you should include a validation to check on the number of items in the list. If there is only 1 or 0, Combine will not make much sense (or fail). Furthermore, it might be wise to check out a spatial analyst license to be sure:

import arcpy
arcpy.env.workspace = "D:\Baseline_2\Eliana\Biomass.gdb"
rasters = arcpy.ListRasters()

arcpy.CheckExtension("Spatial")

year = "_0"
for rc in rasters:
    if rc.endswith(year):
       listtemp.append(rc)

if len(listtemp) > 1:
    year_0 = arcpy.sa.Combine(listtemp)
    year_0.save(r"D:\Baseline_2\Eliana\Eliana.gdb\Year_0")
else:
    print("Not enough rasters to Combine...")

print("end script")
print(listtemp)
0 Kudos
MicahBabinski
Regular Contributor

Hi Daniela,

What is the error you are getting? I think you might be building the list of rasters incorrectly. When you do this:

for rc in rasters:
    if rc.endswith(year):
         listtemp.append(arcpy.ListRasters(rc))

...you seem to be checking the name of the raster dataset and then if it matches your format you are again using arcpy.ListRasters() and adding the resulting list to your listtemp list. Adding a list to an existing list will give a list of lists, which won't work with the Combine tool.

Micah