Iteration with "For" does not works for Raster Object in Arcgis Pro 2.5

906
7
Jump to solution
03-27-2020 04:35 PM
Jhon_EricAunta
New Contributor

Hello,


I was trying to iterate through folder with rasters for some calculations between the bands of each one.

I have a script that is stored in a toolbox and ask for couple of parameters. when I run the script, it makes the process for the first image but after that it does not works for the next one and the following error appears:

I should mention that I tried the same but running the script as stand-alone using the python command promt and it works

Then I tried something easy in the python window in ArcgisPRO: 

import arcpy
from arcpy.sa import *
from arcpy import env
import os
arcpy.env.workspace = 'C:\\Imagenes_Satelitales\\2018\\Junio\\Poligono3'
arcpy.env.overwriteOutput = True
LR = arcpy.ListSpatialReferences('*SR_clip*')
LR
['20180607_145218_0f4e_3B_AnalyticMS_SR_clip.tif', '20180607_145219_0f4e_3B_AnalyticMS_SR_clip.tif', '20180607_145220_0f4e_3B_AnalyticMS_SR_clip.tif']
for raster in LR:
   #getting bands
   Image = Raster(raster)
   b3 = Image.getRasterBands()[2]
   b4 = Image.getRasterBands()[3]
   result = arcpy.sa.Float(b4 - b3)

   name = os.path.splitext(raster)[0]
   result.save(os.path.join('C:\\Imagenes_Satelitales\\2018\\Junio\\Poligono3', name + 'result.tif')

Traceback (most recent call last):
File "<string>", line 3, in <module>
File "c:\program files\arcgis\pro\Resources\arcpy\arcpy\sa\Raster.py", line 79, in __new__
return super().__new__(cls, in_raster, is_multidimensional)
RuntimeError: ERROR 000732: Input Raster: Dataset 20180607_145218_0f4e_3B_AnalyticMS_SR_clip.tif does not exist or is not supported

That type of error is the same that I got when I run the complete script.

For now, I am going to continue using the python command promt for running the script but I would like to do it from the toolbox that I have for that purpose. 

Thanks,

Jhon Eric

0 Kudos
1 Solution

Accepted Solutions
JoshuaBixby
MVP Esteemed Contributor

Try passing the full path of the rasters and not relying on behind-the-scenes path concatenation.

View solution in original post

7 Replies
DanPatterson_Retired
MVP Emeritus

is this line in error?

LR = arcpy.ListSpatialReferences('*SR_clip*')

yet the *.tif files are listed and not the spatial references?

/blogs/dan_patterson/2016/08/14/script-formatting 

will provide line numbers.

0 Kudos
Jhon_EricAunta
New Contributor

Sorry Dan,

yes, it was an error when I pasted

The line was:

LR = arcpy.ListRasters(*SR_clip*)

so, it was the reason just rasters names  were listed.

But the error occurs when I try to do any math operation using Raster object, script works for the first image, after that the error occurs.

Thanks 

0 Kudos
DanPatterson_Retired
MVP Emeritus

If you follow the path to the Raster.py script in the C:\...your installation folder... \Resources\ArcPy\arcpy\sa

I am wondering if it doesn't like what it is finding in the files in terms of the number of bands, the band numbers/names.  Of course, I am assuming that it isn't a multidimensional raster not to be confused with a multiband raster.

I would suggest that you throw in a few lines to print out some of the raster properties to confirm what is actually in there.  There is nothing in the code that would fail unless it was getting something it wasn't expecting (at least at first glance).

matthewsharr
New Contributor

Hi everyone,

Thank you for bringing this up Jhon Eric. I'm having this same issue, trying to iterate through rasters in a geodatabase, in Arc Pro 2.5. I am able to successfully output the first raster in my list, but it fails on the second time around.

# text version

def convert_depth_raster(self, parameters):
   arcpy.env.workspace = self.gdb_out
   db_rasters = arcpy.ListRasters("*")
   print("Converting depth bands: ", db_rasters)

   

   for r in db_rasters:
      convert = ((1.4661 * 182329)/(2 * arcpy.sa.Raster(r) * 1.732)) / 1852
      lnm_name = r.replace('DepthBand', 'LNM')
      lnm_out = self.gdb_out + "\\" + lnm_name
      convert.save(lnm_path)
      print("Created: ", lnm_out)


Traceback (most recent call last):   File "<string>", line 257, in execute   File "<string>", line 234, in convert_depth_raster   File "c:\users\matt sharr\appdata\local\programs\arcgis\pro\Resources\arcpy\arcpy\sa\Raster.py", line 79, in __new__     return super().__new__(cls, in_raster, is_multidimensional) RuntimeError: ERROR 000732: Input Raster: Dataset DepthBand_21_40 does not exist or is not supported

Are there any known solutions at this time? Are there other workarounds besides model builder and manually iterating?

Thank you,
-Matt
0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Try passing the full path of the rasters and not relying on behind-the-scenes path concatenation.

matthewsharr
New Contributor

That did it. Thanks, Joshua.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Should the approach you originally took work?  Probably, but I consider it a best practice to always use full paths with geoprocessing tools.  Not only does doing so reduce the risk of running into odd defects like this, it simplifies troubleshooting because there is no guessing what was passed to the geoprocessing tools.

0 Kudos