0

I try to change a particular value of raster dataset to zero using python script. When I deal with one raster, it works. Python scripts as follow:

import arcpy
from arcpy import env
from arcpy.sa import *
arcpy.CheckOutExtension("Spatial")
# Set environment settings
env.workspace = r'D:\MATLAB\TEST'
# Set local variables
a="states1166.tif"
inRaster = Raster(a)
outCon = Con(inRaster == 1.000000020040877e+20, 0, inRaster)
# Save the outputs
outCon.save("D:\MATLAB\TEST\prif11166.tif")

But when I deal with multiple raster, it failed. Python scripts as follow:

# Import system modules
  import arcpy
  from arcpy import env
  from arcpy.sa import *
  import sys
  import os
  arcpy.CheckOutExtension("Spatial")

  env.workspace = r'D:\MATLAB\PriForest'

  outdir = os.path.join(os.path.dirname(env.workspace),"Replace")
  if not os.path.exists(outdir) : os.mkdir(outdir)
  inRasters = arcpy.ListRasters("*")

  print "Begin Replace"

  for inRaster in inRasters:

      outCon = Con(inRaster == 1.000000020040877e+20, 0, inRaster)

      filename = 'PriForest' + os.path.basename(inRaster)
      outfile = os.path.join(outdir,filename)

      outCon.save(outfile)

   print('finished')[enter link description here][1]

Does anyone know the reason?

  • Show us the error message. – Mr. Che Apr 12 at 8:14
  • well, the raster was a global map of forest area and the missing value was filled with 1.000000020040877e+20 . When I deal with multiple raster, the value was only replaced in the region without land. While the land area still remained the original value. – Jianxing_Zhu Apr 12 at 8:24
  • So the script does not fail to work? Try to print output parameters (outfile) and check that files. – Mr. Che Apr 12 at 8:32
  • The script run fine. I just did not get the ideal results from the script and confused about the results. The results were totally different with the result obtained form the former script and the results I got from raster calculator in ArcMap. – Jianxing_Zhu Apr 12 at 8:45
  • arcpy.Raster(inRaster) might help. – FelixIP Apr 12 at 9:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.