I have to add two rasters, one bigger than the other, but before adding I have to set the value of NODATA of smaller raster to the value of bigger raster.

So i used the following code to do it:

import arcpy
from arcpy.sa import *


arcpy.env.extent = "MAXOF"
arcpy.env.overwriteOutput = True

smallerRaster = arcpy.Raster(r"C:\Temp\smallraster.asc")
biggerRaster = arcpy.Raster(r"C:\Temp\bigraster.asc")

OutputRaster = Con(IsNull(smallerRaster), biggerRaster, smallerRaster + biggerRaster)

resultRaster = r"C:\Temp\resultRaster"
OutputRaster.save(resultRaster + "_con.tif")
arcpy.RasterToASCII_conversion(resultRaster + "_con.tif", resultRaster + "_con.asc")

arcpy.gp.Con_sa(IsNull(smallerRaster), 0, resultRaster + "_con_sa.tif", smallerRaster + biggerRaster)
arcpy.RasterToASCII_conversion(resultRaster + "_con_sa.tif", resultRaster + "_con_sa.asc")

For Con_sa, smaller raster cell value (those that are NODATA) is assigned to NODATA value (in my sample it is -9999) instead of assign with bigger raster cell value. (I tried with constant value of 0, it still assigned them with NODATA)

I need to use Con_sa because the script can be executed by more than one instance of application. While Con result is what I needed, the output temporary result will be stored in "\AppData\Local\ESRI\Desktop10.3\SpatialAnalyst\", when the script executed in paralel, the CON will fail due to racing condition in code execution (either throw FATAL error) or (Raster cannot be found).

So is there away to set the NODATA value to constant value when I'm using Con_sa for my scenario above? I will attach the raster ASCII that I used in this example.

Small Raster

Small Raster

Big Raster

Big Raster

Raster CON Result

Raster CON result

Raster CON_SA Result

Raster CON_SA result

  • 3
    Just use Con and use a different arcpy.env.scratchWorkspace (i.e with tempdir.mkdtemp() ) for each process. – user2856 Jun 3 '16 at 13:10
  • Thanks Luke Never know the purpose of Scratch Workspace, now the result can be generated on separate folder when run the script concurrently. – Henri Lim Jun 10 '16 at 9:28

As mentioned by @Luke, set arcpy.env.scratchWorkspace before running Spatial Analysis tool will redirect that result to scratch workspace.

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.