Saturday, 14 April 2018

arcpy - Multiple conditional statements within a Con Tool


Well I want to apply multiple conditional statements within a Conditional tool. My aim is to loop through a list of rasters and on the basis of a specified range assign each range with a constant 1, 2, 3, 4 or 5 value so that all the final rasters have pixels having a DN of 1,2,3,4, or 5. I copied the template from the following Desktop Help website and adjusted to my requirements.


http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#/Conditional_evaluation_with_Con/009z00000008000000/


My code is as follows:


import arcpy
from arcpy import env
# Set the current workspace
env.workspace = r"C:\Experiments\Test_Jan03-11_A\Masked"
# Get a list of Rasters

rasters = arcpy.ListRasters("*", "ALL")
for raster in rasters:
output = arcpy.sa.Con(raster <= -10, 1, Con(raster <= 73, 2, Con(raster <= 196, 3, Con(raster <= 403, 4, Con(raster <= 600, 5, 0)))))
output.save("C:/zE/ResearchWork/UnderWorks/Experiments/Test_Jan03-11_A/Masked/Classify")
Print = "Done"
Runtime error : name 'Con' is not defined

The error indicates that I am making a mistake with the conditional statement that I can't identify.




The following code has worked. But with some issues stated below



import arcpy
from arcpy import env
arcpy.env.extent = "MAXOF"
# check out spatial analyst extension
if arcpy.CheckExtension('Spatial') == 'Available':
arcpy.CheckOutExtension('Spatial')
else:
arcpy.AddMessage("Error: Couldn't get Spatial Analyst extenstion, exiting")
sys.exit(1)
from arcpy.sa import *

# Set the current workspace
env.workspace = r"C:\zE\ResearchWork\UnderWorks\Experiments\Test_Jan03-11_A\Masked\CMasked"
# set cell size
arcpy.env.cellSize = r"C:\zE\ResearchWork\UnderWorks\Experiments\Test_Jan03-11_A\Masked\CMasked\GW1AM2_20130100_01M_EQMA_L3SGSMCLA1110100"
# Get a list of Rasters
rasters = arcpy.ListRasters("*", "ALL")
for raster in rasters:
inRas = Raster(raster)
for raster in rasters:
output = arcpy.sa.Con(inRas <= -10,1,Con(inRas <= 73,2,Con(inRas <= 196,3,Con(inRas <= 403,4,Con(inRas <= 600,5,0)))))

output.save("C:/zE/ResearchWork/UnderWorks/Experiments/Test_Jan03-11_A/Masked/CMasked/MaskedClassify")
Print = "Done"

The problem is that the result is only one raster. I was expecting each of the input rasters to be classified and saved separately. Also it is giving me the following error for saving data.


Runtime error : ERROR 010240: Could not save raster dataset to C:/zE/ResearchWork/UnderWorks/Experiments/Test_Jan03-11_A/Masked/CMasked/MaskedClassify with output format GRID.

Answer



You'll laugh.


Reference http://resources.arcgis.com/en/help/main/10.1/index.html#//009z00000005000000


In order to use con directly in that statement one must from arcpy.sa import * or arcpy.sa.Con(raster <= -10, 1, arcpy.sa.Con(raster <= 73, 2, arcpy.sa.Con(raster <= 196, 3, arcpy.sa.Con(raster <= 403, 4, arcpy.sa.Con(raster <= 600, 5, 0)))))


I've fiddled with your code and fixed a few little problems, please note the comments as there's still a few traps there...



for raster in rasters:
inRas = Raster(raster)
for raster in rasters:
output = arcpy.sa.Con(inRas <= -10,1,Con(inRas <= 73,2,Con(inRas <= 196,3,Con(inRas <= 403,4,Con(inRas <= 600,5,0)))))

Is incorrect logic so I have removed one of the loops: if you loop through creating your rasters and then loop again creating your outputs you are essentially creating the last raster as output, one for each input raster and they will all be the same! The value of inRas coming into the second loop is the last raster and is not changed in the second loop.


This is the amended code:


import arcpy, os
from arcpy import env
arcpy.env.extent = "MAXOF"

# check out spatial analyst extension
if arcpy.CheckExtension('Spatial') == 'Available':
arcpy.CheckOutExtension('Spatial')
else:
arcpy.AddMessage("Error: Couldn't get Spatial Analyst extenstion, exiting")
sys.exit(1)

from arcpy.sa import *
# Set the current workspace
env.workspace = r"C:\zE\ResearchWork\UnderWorks\Experiments\Test_Jan03-11_A\Masked\CMasked"

# set cell size
arcpy.env.cellSize = r"C:\zE\ResearchWork\UnderWorks\Experiments\Test_Jan03-11_A\Masked\CMasked\GW1AM2_20130100_01M_EQMA_L3SGSMCLA1110100"
# Get a list of Rasters
rasters = arcpy.ListRasters("*", "ALL")
for raster in rasters:
# get the name and extention from the filename
# so abc.tif becomes RasName "abc" and RasExt ".tif"
RasName, RasExt = os.path.splitext(raster)
inRas = Raster(raster)
output = arcpy.sa.Con(inRas <= -10,1,Con(inRas <= 73,2,Con(inRas <= 196,3,Con(inRas <= 403,4,Con(inRas <= 600,5,0)))))

# note: you're putting your outputs in the same folder as your inputs
# so next time you run it the process will pick these up too
output.save(env.workspace + "/MaskedClassify" + RasName + ".tif")
Print = "Done"

# let it go until next time
arcpy.CheckInExtension("Spatial")

The function os.splitext has a great reference https://stackoverflow.com/questions/541390/extracting-extension-from-filename-in-python it separates the base name and extension of the file, this is not ArcPY just python. The base path of your output.save is the same as your env.workspace so instead of putting it in full I changed it to concatenate (join) the strings using '+', there are other ways to do this but I felt that this way is the most basic.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...