So I'm coding in arcpy and trying to process all rasters in a geodatabase and output to a new geodatabase.
The steps go Minus(constant - raster), 1 divided by output, times 100. Then Reclassify results. Reclassify tool did not work. it would output a raster identical to the input.
Tried as a series of Con and Plus statements. Seemed to work. However I am running into an issue where a Con statement will ignore part of the expression for the raster and give the opposite of the desired results. This occurs when run from the native toolbox as well as from python. It has also done this whether using arcpy.gp.Con_sa or arcpy.sa.Con
ex. arcpy.gp.Con_sa(inraster,"1",savepath, 1,0,""""VALUE" > 0 AND "VALUE" <= 25"""). The input raster is a 32-bit floating point raster.
In some cases this will work correctly if the raster has values that exist between 0 and 25. However, if the raster has values that aren't between 0 and 25, it won't work correctly. The function will return a raster that has the true value for the entire raster when it should have a false value for the entire raster. It only seems to do that if the input raster values are relatively close to the expression values. I'm not really sure what to do.
Reminder input is a geodatabase containing 32-bit floating point rasters. "c" is a number input
#IMPORT
import arcpy
import os
from arcpy.sa import *
#-------------------------------------------------------------------------------------------
#ENVIRONMENT SETTINGS
#Prevents outputs from being added to map
arcpy.env.addOutputsToMap = False
#Allows intermediate layer to be overwriten
arcpy.env.overwriteOutput = True
#-------------------------------------------------------------------------------------------
#PARAMETERS
#Sets workspace as input parameter and as variable
arcpy.env.workspace = z = arcpy.GetParameterAsText(0)
#sets output workspace
destPath = arcpy.GetParameterAsText(1)
#Variables to do with naming and processing.
a = arcpy.GetParameterAsText(2)
b = arcpy.GetParameterAsText(3)
c = arcpy.GetParameter(4)
#-------------------------------------------------------------------------------------------
#OUTPUT SAVING
#splits destination path, and gets geodatabse name and path in separate variables
x = destPath.split("\\")
for destGDB in x:
print destGDB
destPath = destPath.strip(destGDB)
#puts the split variables back together in a new variable
finalGDB = str(destPath) + str("\\") + str(destGDB)
#-------------------------------------------------------------------------------------------
#GEODATABASE DESTINATION
#if destination geodatabase exists, do nothing, if it doesn't, make it.
if arcpy.Exists(finalGDB):
finalGDB = finalGDB
else:
arcpy.CreateFileGDB_management(destPath, destGDB, "CURRENT")
#-------------------------------------------------------------------------------------------
#MAIN CODE FOR LOOP
#lists rasters in input workspace
Listrasters = arcpy.ListRasters()
#Loops through rasters in input Geodatabase
for raster in Listrasters:
#gets the layer's name in a variable
rastname = str(raster)
#sets final layer name
finalname = str("Quart_") + a + str("_") + b + str("_") + rastname
#sets the full path of the final layer
finalpath = finalGDB + str("//") + finalname
arcpy.AddMessage("Minusing...")
tempras = Minus(c,raster)
arcpy.AddMessage("Dividing...")
tempras1 = Divide(1, tempras)
arcpy.AddMessage("Multiplying...")
tempras2 = Times(tempras1, 100)
#|------------------------------------------------------------------------------------
#Conditional statements to reclassify input
arcpy.AddMessage("Con1...")
workpath1 = z + str("//") + str("con1")
con1 = arcpy.gp.Con_sa(tempras2,"1", workpath1, "0", """"VALUE" >= 0 AND "VALUE" <= 25""")
arcpy.AddMessage("Con2...")
workpath2 = z + str("//") + str("con2")
con2 = arcpy.gp.Con_sa(tempras2,"2", workpath2, "0", """"VALUE" > 25 AND "VALUE" <= 75""")
arcpy.AddMessage("Con3...")
workpath3 = z + str("//") + str("con3")
con3 = arcpy.gp.Con_sa(tempras2,"3", workpath3, "0", """"VALUE" > 75 AND "VALUE" < 90""")
arcpy.AddMessage("Con4...")
workpath4 = z + str("//") + str("con4")
con4 = arcpy.gp.Con_sa(tempras2,"4", workpath4, "0", """"VALUE" >= 90""")
arcpy.AddMessage("Con5...")
workpath5 = z + str("//") + str("con5")
con5 = arcpy.gp.Con_sa(tempras2,"5", workpath5, "0", """"VALUE" < 0""")
#|-----------------------------------------------------------------------------------
#Addition statments to add con rasters for full reclass
arcpy.AddMessage("Add1...")
add1 = arcpy.sa.Plus(con1,con2)
arcpy.AddMessage("Add2...")
add2 = arcpy.sa.Plus(con3,con4)
arcpy.AddMessage("Add3...")
add3 = arcpy.sa.Plus(con5,add1)
arcpy.AddMessage("Add4...")
outras = arcpy.sa.Plus(add2,add3)
arcpy.AddMessage("Saving...")
outras.save(finalpath)
#|------------------------------------------------------------------------------------
lastmsg = str("SUCCESS! You have processed ") + rastname + str("!")
arcpy.AddMessage(lastmsg)
Can anyone advise on what's going wrong? I suspect that it may be a version issue, or an issue with the input data. But at this point I have no idea.
This issue also appears when the con tool is run on its own from its native toolbox.