3

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.

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'
  • 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. – belit1mw Nov 15 '16 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? – jbchurchill Nov 15 '16 at 17:37
  • Yes it is. I added arcpy.env.overwriteOutput=True and your response worked! Thanks! – belit1mw Nov 15 '16 at 17:42
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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