2

I am new to Python and am having difficulties trying to loop multiple rasters into a conditional statement. I want to extract all land-uses that have the value 21 from a folder with three different land-use layers.

When I run my code, I get an output that shows the land-use that equals value 21 for the first raster file in my folder, but it does not calculate an output for my other two raster files in my folder. My code is:

 import arcpy
 from arcpy.sa import *
 arpy.CheckOutExtension('Spatial')
 arcpy.env.workspace=r'U:data\landuses'
 arcpy.env.scratchWorkspace='U\data\work.gdb'
 rasters=arcpy.ListRasters("*")
 for raster in rasters:
 ... ras=Raster(raster)
 ... outraster=Con(ras==21,21)
 ... outraster.save('U:\data\work.gdb' + raster)

When I run this, I get:

ERROR 010093: Output raster format UNKNOWN is unsupported.

My raster files are all "GRID" format.

PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Nov 15, 2016 at 17:07

2 Answers 2

2

You must put a slash after the "work.gdb" on that last line. It is assuming you are saving it as a work.gdbraster file name (e.g. it is appending whatever string "raster" represents to "work.gdb".

Change that last line to ...

outraster.save(r'U:\data\work.gdb' + "\\" + raster

and I think that will work for you.

Note that I'm using the "r" in that line (as you did above). That will make sure the slashes in your path are respected. Python would try to read that backslash as an escape character (test this at the command line). It may be reading your paths incorrectly.

I also noticed that on this line (line 5 of what you put in the question), you also left out the colon.

arcpy.env.scratchWorkspace='U\data\work.gdb'

should be ...

arcpy.env.scratchWorkspace=r'U:\data\work.gdb'
answered Nov 15, 2016 at 17:16
3
  • When I changed the last line to what you suggested, I am now getting an output that displays when land-use is equal to 21 for the last raster file, but it does not calculate an output for the first two now. Commented Nov 15, 2016 at 17:34
  • Is it naming it the same name as the input raster? or what exactly is the name? Could it be overwriting the previous output? Commented Nov 15, 2016 at 17:37
  • Yes it is. I added arcpy.env.overwriteOutput=True and your response worked! Thanks! Commented Nov 15, 2016 at 17:42
1

Using Error Handlers will give you lots of information about how and where your code is failing. Here is a link to error handling in ArcGIS: http://pro.arcgis.com/en/pro-app/arcpy/get-started/error-handling-with-python.htm But that is not your problem here.

The paths to your workspaces are not valid.

arpy.CheckOutExtension('Spatial')
arcpy.env.workspace=r'U:data\landuses'
arcpy.env.scratchWorkspace='U\data\work.gdb'
#shoud be
arcpy.CheckOutExtension('Spatial')
arcpy.env.workspace=r'U:\data\landuses'
arcpy.env.scratchWorkspace='U:\data\work.gdb'

But that is not your problem either...your code is likely failing because the output raster is using some unsupported file format like JPG. See this link:arcpy.Raster.save(*.jpg) - format unknown

answered Nov 15, 2016 at 17:24

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.