1

I have a problem when using the con() argument.

What I want to do is to remap all raster of a raster file ("Red_SL") cells with values larger than 40 to the new value 5. All other values should be replaced by another raster file called "remap2". "remap" 2 contains only 4 definite values (1,2,3,4), and "Red SL" has continuous values between 0 and 90.

My syntax is the following:

import arcpy
from arcpy.sa import *

arcpy.env.overwriteOutput = True
arcpy.env.workspace = r"D:\\Projekt\\Testprojekt\\Daten\\Ergebnisse\\"
arcpy.CheckOutExtension("Spatial")
remaped2 = "remaped2.img"
slope = "Red_SL"

CF= Con((slope < 40), 5, remaped2)
CF.save("Red_SL_Neigung_int.tif")
arcpy.CheckInExtension("Spatial")

now the problem is: I do not get an error message but the produced file is nonsense! Python is somehow not able to recognize the slope value in: CF= Con((slope < 40), 5, total) This means it doesent matter what value I give python (here I set it to 40, but I will get the exact same result if I give values of 1 or 3000), the result will always be the a new file which is exactly the same as "remaped2". Python therefore thinks all values are below 40 (or whatever number I give), which is not true.

The exact opposite happens if I change the "<" to ">" (CF= Con((slope > 40), 5, total)). Now I will get i new file, which has only 5 in every raster cell. Here again the value whch I type in (instead of 40) does not matter, any value between 1 and 3000 will produce the exact same result. I double checked, but the file "Red_SL" has all values between 0 and 90, therfore the result I get from python is nonsense.

I already tried (without sucess): -Change "Red_SL" from floating to integer -try differend formats (tif, grid, img) for "Red_SL" - replace comma in floating raster with points (german vs. englisch delimiters)

Can anyone see what is wrong with my use of the Con arguement?

3
  • As you have put an r in front of the string for the workspace you do not need to use \\ use a single \.
    – Hornbydd
    Jul 30, 2014 at 13:40
  • Thank you. I removed the doubleslashes but it has no effect on the the problem. Anyway, good to know! Jul 30, 2014 at 13:42
  • Yes you are right. I am sorry but this was just o fault of myself which happened wehn I copied the syntax into the forum (I have tried so many ways that I had to rechange some stuff). However, I changed it in the posted syntax as well as on my computer but still the same results Jul 30, 2014 at 13:50

1 Answer 1

3

You need to create raster objects when working with many of the spatial analyst tools, including Con (Spatial Analyst). Try the following changes:

import arcpy
from arcpy.sa import *

arcpy.env.overwriteOutput = True
arcpy.env.workspace = r"D:\Projekt\Testprojekt\Daten\Ergebnisse"
arcpy.CheckOutExtension("Spatial")
remaped2 = Raster("remaped2.img")
slope = Raster("Red_SL")

CF= Con((slope < 40), 5, remaped2)
CF.save("Red_SL_Neigung_int.tif")
arcpy.CheckInExtension("Spatial")
2
  • Thats the solution! Thanks a lot for your help everything works fine now!! Jul 30, 2014 at 13:58
  • @Christophmang if this is a solution for you, you should tick it as correct.
    – Hornbydd
    Jul 30, 2014 at 15:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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