No data in CopyRaster python script

342
1
Jump to solution
01-04-2024 06:53 AM
bogdanpalade2
New Contributor III

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!

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
bogdanpalade2
New Contributor III

Just to reply my own question here, I have just been informed by ESRI that this is due to an existing bug. So, no solution after all. 

View solution in original post

0 Kudos
1 Reply
bogdanpalade2
New Contributor III

Just to reply my own question here, I have just been informed by ESRI that this is due to an existing bug. So, no solution after all. 

0 Kudos