TypeError Expecting a raster, but it IS a raster?

1250
5
Jump to solution
02-16-2012 12:58 PM
RichardThurau
Occasional Contributor
Hi,
I am trying to use python to run a few consecutive reclassifications, but the script crashes between processes.


import arcpy #import os from arcpy.sa import * from arcpy import env arcpy.CheckOutExtension("Spatial") arcpy.env.overwriteOutput = True  EOSD = "X:\\EOSD_for100" MunRas2 = "X:\\MunRas_East_100" WScom1_rc = "X:\\area_e4buf_comb1_RC.img"#WScom1[0:-4] + "_RC.img" WScom2 = WScom1_rc[0:-4] + "_mun_com.img" WScom2_rc = WScom2[0:-4] + "_RC.img" WScom3 = WScom2_rc[0:-4] + "_eosd_com.img" WScom3_rc = WScom3[0:-4] + "_RC.img"  WScom2 = (Raster(WScom1_rc) + Raster(MunRas2)) WScom2_rc = Reclassify(WScom2, "Value", RemapValue([[0, "NODATA"], [1, 1], [2, 2], [3, 3], [4, 4], [5, 5], [6, 6], [9, 9], [100, "NODATA"], [101, 1], [102, 2], [103, 7], [104, 4], [105, 5], [106, 6], [109, 9]]))  WScom3 = (Raster(WScom2_rc) + Raster(EOSD)) WScom3_rc = Reclassify(WScom3, "Value", RemapValue([[0, "NODATA"], [1, 1], [2, 2], [3,3], [4, 4], [5,5],[6, 6], [7, 7], [9, 9], [101, 11], [102, 12], [103, 13], [104, 14], [105, 15], [106, 16], [107, 17], [109, 19]]))


So, between WScom2_rc and WScom3, the code crashes with the following error:

    WScom3 = (Raster(WScom2_rc) + Raster(EOSD))
TypeError: expected a raster or layer name

If I comment out the first set of code and run the second, it processes without issue.

Can anyone see something I'm doing wrong?

Thanks

Rich
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
StacyRendall1
Occasional Contributor III
Richard,

the output of Reclassify() is a raster object. The Raster() function converts a path or layer name into a raster object, but you already have raster objects.

The raster addition will accept either a path or a raster object, so where you currently have this:
WScom3 = (Raster(WScom2_rc) + Raster(EOSD))

it fails as WScom2_rc is already a raster object - you cannot put it into Raster().. However, this should work:
WScom3 = WScom2_rc + Raster(EOSD)

and so should this (although in my opinion it should not):
WScom3 = WScom2_rc + EOSD


When you commented out part of your code, you were just adding the rasters by path so it worked.

Also, you shouldn't put unnecessary brackets around things (i.e. when just adding two things together) as brackets can have a special meaning - I took them out for the bottom two snippets above.

Cheers,
Stacy

View solution in original post

0 Kudos
5 Replies
StacyRendall1
Occasional Contributor III
Richard,

Is that the code you are actually using?

In the lines:
WScom2 = (Raster(WScom1_rc) + Raster(MunRas2))

and
WScom3 = (Raster(WScom2_rc) + Raster(EOSD))

MunRas2 and EOSD have never been defined, but that should cause a NameError...
0 Kudos
RichardThurau
Occasional Contributor
Stacy,
They are defined. This is condensed. I'm recovering from a cold, so not as crafty as I think I am right now.

I'll try to edit my post.

Also, I originally tried running this while saving all my intermediate raster layers. The same error occurs. Of course without the intermediate rasters saved, I can't just comment out and continue.
Working with rasters in Arcpy seems to be rought with frustration.

Thanks for looking.

RT
0 Kudos
StacyRendall1
Occasional Contributor III
Richard,

the output of Reclassify() is a raster object. The Raster() function converts a path or layer name into a raster object, but you already have raster objects.

The raster addition will accept either a path or a raster object, so where you currently have this:
WScom3 = (Raster(WScom2_rc) + Raster(EOSD))

it fails as WScom2_rc is already a raster object - you cannot put it into Raster().. However, this should work:
WScom3 = WScom2_rc + Raster(EOSD)

and so should this (although in my opinion it should not):
WScom3 = WScom2_rc + EOSD


When you commented out part of your code, you were just adding the rasters by path so it worked.

Also, you shouldn't put unnecessary brackets around things (i.e. when just adding two things together) as brackets can have a special meaning - I took them out for the bottom two snippets above.

Cheers,
Stacy
0 Kudos
RichardThurau
Occasional Contributor
Stacy,
I see, that makes sense. Look forward to trying it in the morning.

Thanks for taking the time to respond.

Rich
0 Kudos
RichardThurau
Occasional Contributor
As I suspected, code works very well now. I'm unable to find the example again, but I'm pretty sure the esri example has the double parentheses. Unfortunately, or for whatever it's worth, those examples and these forums have provided the bulk of my python knowledge.

Thanks again.

Rich
0 Kudos