2

I have an original .tif file which has values ranging from 2 to 28. From that original .tif file, I am extracting cells having a value of >=10 using the following code:

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/2-25-16/HSA"

outws="C:/Subhasis/Test/2-25-16/HSA/HSA_10"

#checkout ArcGIS spatial analyst extension license
arcpy.CheckOutExtension("Spatial")

inraster = arcpy.ListRasters("*", "TIF")


for i in inraster:
    flds = ("VALUE", "COUNT")  
    dct = {row[0]:row[1] for row in arcpy.da.SearchCursor(i, flds)}  
    sumcnt = sum(dct.values())
    dct1 = {k:v for (k,v) in dct.items() if k >= 10}
    sumcnt1 = sum(dct1.values())
    percentage=(float(sumcnt1)/float(sumcnt))
    print i,percentage
    newraster = ExtractByAttributes(str(i), "VALUE>=10")
    outname=os.path.join(outws,str(i))
    newraster.save(outname)

The code is working. However, when I open the new extracted tif file in GIS, I found that the cell values range from 10 to 30 instead of 10 to 28.

  • 1
    Have you tried using newraster = ExtractByAttributes(str(i), "VALUE>=10 AND VALUE<=28")? – Midavalo Feb 26 '16 at 19:08
  • Would suggest that 30 is a value in the raster. If the attribute table is out of date then this could happen. Suggest you try and rebuild the attribute table for a raster that is returning 30? – Hornbydd Nov 2 '17 at 8:45
-1

I would highly recommend using the numpy library. you can convert a raster to a matrix, then you can extract all values >10 in literally 1 line, then convert it back into a raster file.

use the RasterToNumPyArray and NumPyArrayToRaster functions: http://pro.arcgis.com/en/pro-app/arcpy/functions/rastertonumpyarray-function.htm

numpy library: http://www.numpy.org

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.