automate raster extraction and saving data based on input raster

2941
3
Jump to solution
07-21-2014 11:44 AM
InceptionWoznicki
New Contributor

Hi ,

I have 70 raster files and I want to make 3 new raster from each of the 70 raster. For that, I want to automate the process using python script and store the output new raster based on the raster file name plus value field. For example, My input raster is  01367620-r-r and 3 value fields are 9,11, and 13. So the output folder should be 01367620-r-r-9. 01367620-r-r-11, and 01367620-r-r-13. When I ran my code, I got the below error message. I think, I am doing mistake at "outname=os.path.join(outws,str(i), str(j))" in my code. I would appreciate your suggestion/input.

 

Error Message:

Traceback (most recent call last):

  File "C:/Subhasis/Test/Neshanic_Python/Neshanic-test.py", line 23, in <module>

    attExtract.save(outname)

RuntimeError: ERROR 000875: Output raster: C:\Subhasis\Test\Neshanic_Python\extract\01367620-r-r\9's workspace is an invalid output workspace.

 

I am providing my code below.

import arcpy, os

from arcpy import env

from arcpy.sa import *

 

#To overwrite output

arcpy.env.overwriteOutput = True

 

#Set environment settings

env.workspace = "C:/Subhasis/Test/Neshanic_Python"

 

outws="C:/Subhasis/Test/Neshanic_Python/extract"

 

#checkout ArcGIS spatial analyst extension license

arcpy.CheckOutExtension("Spatial")

 

# set local variable

inraster = ["01367620-r-r"]

for i in inraster:

    STI_list=[9,11,13]

    for j in STI_list:

        attExtract = ExtractByAttributes(str(i), "VALUE>=" + str(j))

        outname=os.path.join(outws,str(i), str(j))

        attExtract.save(outname)

 

Thanks!

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
XanderBakker
Esri Esteemed Contributor

If this question is related to the rasters created in your thread Re: loop for value and counts in raster attribute‌, then Dan Patterson‌ is absolutely right. The problem occurs in the line:

outname=os.path.join(outws,str(i), str(j))

This should be:

outname=os.path.join(outws,"{0}{1}".format(i,j))

The first creates an outname of: C:/Subhasis/Test/Neshanic_Python/extract\01367620-r-r\9

The second creates an outname of: C:/Subhasis/Test/Neshanic_Python/extract\01367620-r-r9

The os,path.join will place a slash between each parameter. This causes the value "j" to be the raster name and the value "i" is interpreted as folder.

BTW: if the thread Re: loop for value and counts in raster attribute is solved, you should mark it as answered!

Kind regards, Xander

View solution in original post

0 Kudos
3 Replies
DanPatterson_Retired
MVP Emeritus

The gist I get from that error message is that this folder ... C:\Subhasis\Test\Neshanic_Python\extract\01367620-r-r\9 ... doesn't exist and I think you need to create a raster object before using save

outname = Raster(os.path.join(outws,str(i), str(j)))


but I can't check... see ArcGIS Help (10.2, 10.2.1, and 10.2.2)

0 Kudos
XanderBakker
Esri Esteemed Contributor

If this question is related to the rasters created in your thread Re: loop for value and counts in raster attribute‌, then Dan Patterson‌ is absolutely right. The problem occurs in the line:

outname=os.path.join(outws,str(i), str(j))

This should be:

outname=os.path.join(outws,"{0}{1}".format(i,j))

The first creates an outname of: C:/Subhasis/Test/Neshanic_Python/extract\01367620-r-r\9

The second creates an outname of: C:/Subhasis/Test/Neshanic_Python/extract\01367620-r-r9

The os,path.join will place a slash between each parameter. This causes the value "j" to be the raster name and the value "i" is interpreted as folder.

BTW: if the thread Re: loop for value and counts in raster attribute is solved, you should mark it as answered!

Kind regards, Xander

0 Kudos
InceptionWoznicki
New Contributor

Hi Xander,

Thank you very much for your reply. I marked it as answer.

Regards,

Subhais

0 Kudos