Hi everyone!
I have a weird problem with the CopyRaster tool from arcpy. Let me describe here shortly my approach:
I want a python script that would automatically copy and process a set of rasters but I don t want to use the batch version of the tool (copy raster). So, I ran the tool once, succesfully, and downloaded the script locally (from "History - Download as python script"). Here is the script:
import arcpy
arcpy.ImportToolbox(r"@\Data Management Tools.tbx")
with arcpy.EnvManager(compression="'LZW' 75", tileSize="256 256", pyramid="PYRAMIDS 10 NEAREST NONE 75 NO_SKIP NO_SIPS", nodata="NONE"):
arcpy.management.CopyRaster(
in_raster=r"R:\rasterTest.JP2",
out_rasterdataset=r"R:\ArcGIS\scripttest\delete.tif",
config_keyword="",
background_value=None,
nodata_value="",
onebit_to_eightbit="NONE",
colormap_to_RGB="NONE",
pixel_type="8_BIT_UNSIGNED",
scale_pixel_value="ScalePixelValue",
RGB_to_Colormap="NONE",
format="TIFF",
transform="NONE",
process_as_multidimensional="CURRENT_SLICE",
build_multidimensional_transpose="NO_TRANSPOSE"
)
Note that after running this tool once, from ArcGIS, the resulting raster has No Data set to "". In other words, the field of No Data is empty when I check the raster properties.
Ok, so far so good. Next, I inserted this code in my own script in order to apply this process to a set of rasters. Please notice that the parameters are exactly the same, I did not change anything. Here is my code:
import os, arcpy
StartDir = arcpy.GetParameterAsText(0)
rstList=[]
for root, dirs, files in os.walk(StartDir):
for file in files:
if file.endswith(("tif","JP2")):
rstList.append(os.path.join(root, file))
arcpy.AddMessage("Found "+str((len(rstList)))+" rasters")
for myRaster in rstList:
outRaster=myRaster[:-4]+"_output.tif"
with arcpy.EnvManager(compression="'LZW' 75", tileSize="256 256", pyramid="PYRAMIDS 10 NEAREST NONE 75 NO_SKIP NO_SIPS", nodata="NONE"):
arcpy.AddMessage("Processing {0}...".format(myRaster))
arcpy.management.CopyRaster(
myRaster,
outRaster,
config_keyword="",
background_value=None,
nodata_value="",
onebit_to_eightbit="NONE",
colormap_to_RGB="NONE",
pixel_type="8_BIT_UNSIGNED",
scale_pixel_value="ScalePixelValue",
RGB_to_Colormap="NONE",
format="TIFF",
transform="NONE",
process_as_multidimensional="CURRENT_SLICE",
build_multidimensional_transpose="NO_TRANSPOSE"
)
Next, I made a custom tool based on this script and added it to a toolbox. When I execute this tool, I see that the No Data value is set to "0,0,0". Why this difference? I would like my tool to keep the "" value (empty) for the No Data field.
Thank you!