7

I have a raster file containing some integer values.

I need to find all pixels whose values are not defined in list like this one:

l = [1,2,4,8,16,32,64,128].

Is there any easy way to solve that problem without converting raster to points and editing attribute table?

7

Extract by attributes using "VALUE NOT IN (1,2,4,8,16,32,64,128)" (Thanks to @afalciano) will give you a new raster with only the cells where the value is different from your list.

  • ...or using "VALUE NOT IN (1,2,4,8,16,32,64,128)" – Antonio Falciano Jan 20 '14 at 14:28
  • That would be more convenient of course.. Thanks, I'll update the answer – Martin Jan 20 '14 at 14:32
3

One option would be to use a SQL statement within a Con function. The following example performs the following actions:

  1. Generate a SQL expression from a list of values. Doing this in an automated fashion is important for long lists of values.
  2. Use Con to perform an if/else evaluation on a raster. In this case, all values in the list will be assigned "1" and all others "0".

import arcpy
from arcpy import env
from arcpy.sa import *
arcpy.CheckOutExtension = "Spatial"

vals = [1,2,4,8,16,32,64,128]
inRaster = r'C:\temp\example.tif'
new = []

# Generate a SQL expression from the "vals" list
for i in vals:
    new.append("value " + "= " + str(i) + " OR ")
    expression = ''.join(new)[:-4]

# Use Con to convert all raster values from the "vals" list to 1 and all others to 0
outCon = Con(inRaster, 1, 0, expression)
outCon.save("C:/temp/revisedRaster.tif")

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.