3

I am looking to see if anyone has a more efficient method of setting bit depth for my output rasters than what I am currently doing. I am using ArcPy in ArcGIS Pro's notebook to create rasters from features and then computing path distance on them. My primary issue is that some of my input datasets are gigantic and can't handle the automatic 32 bit that is being assigned. I am trying to find a way to reduce my storage during the process and thought that maybe changing the bit depth would help. The only method I can figure out to accomplish this is using the 'copy raster' function which is working. The main issue is that it is taking forever because it has to create double the rasters essentially.

Do you have any tidbits on both reducing the storage during processing (particularly during the path distance function as this is where my code runs out of storage) or how to set bit depth?

Here is my current code:

# Convert Features to Rasters

# Temporarily set workspace to the feature dataset to list its feature classes
arcpy.env.workspace = feature_dataset
feature_classes = arcpy.ListFeatureClasses()
arcpy.env.workspace = working_gdb  # Reset workspace to working_gdb
arcpy.env.overwriteOutput = True

# Step 3: Convert feature classes to raster datasets
for input_fc in feature_classes:
    if 'AOI' in input_fc:
        continue

    input_fc_path = os.path.join(feature_dataset, input_fc)
    # Construct the output raster name by appending '_R' to the feature class name
    temp_raster = os.path.join(working_gdb, f"temp_{input_fc}_R")
    output_raster = os.path.join(working_gdb, f"{input_fc}_R")
    
    # Set the processing extent
    arcpy.env.extent = reference_fc
    arcpy.env.nodata = 65535

    # Convert feature class to raster
    arcpy.conversion.FeatureToRaster(
        in_features=input_fc_path,
        field="OBJECTID",
        out_raster=temp_raster,
        cell_size=cell_size
    )

    # Convert the temporary raster to a compressed format with a consistent bit depth
    arcpy.management.CopyRaster(
        in_raster=temp_raster,
        out_rasterdataset=output_raster,
        pixel_type="16_BIT_UNSIGNED",
        format="TIFF",
        nodata_value="65535"
    )

    # Delete the temporary raster to free up space
    arcpy.management.Delete(temp_raster)

print("Step 3 completed successfully.")

# Path Distance 

# Dictionary to store maximum distance values
max_distance_dict = {
    "C1_Wet_pnt": 47.854,
    "C1_HydroEdge": 47.854,
    "C1_Wet_Area": 45.72,
    "C2_1_Wet_pnt": 22.86,
    "C2_1_HydroEdge": 22.86,
    "C2_1_Wet_Area": 22.86,
    "C2_2_Wet_pnt": 31.09,
    "C2_2_HydroEdge": 31.09,
    "C2_2_Wet_Area": 30.48
}

# List all raster datasets in the working geodatabase
arcpy.env.workspace = working_gdb
raster_datasets = arcpy.ListRasters("*_R")

# Step 4: Compute Path Distance
for input_raster in raster_datasets:
    
    # Set the processing extent 
    arcpy.env.extent = reference_fc
    
    output_path_distance = os.path.join(working_gdb, f"{input_raster}_PD")
    
    # Determine the max distance based on the raster name
    max_distance = None
    for key, distance in max_distance_dict.items():
        if key in input_raster:
            max_distance = distance
            break
    
    if max_distance is None:
        continue

    try:
        # Compute Path Distance
        path_distance_result = arcpy.sa.PathDistance(
            in_source_data=input_raster,
            in_surface_raster=dtm_layer,
            maximum_distance=max_distance
        )
        path_distance_result.save(output_path_distance)

        # Delete the input raster to save storage space
        arcpy.management.Delete(input_raster)
    except arcpy.ExecuteError:
        print(f"Failed to compute Path Distance for {input_raster}. {arcpy.GetMessages()}")

print("Step 4 completed successfully.")

2 Answers 2

2

Short answer is no, you are doing what the rest of us are doing, using the Copy raster tool to reduce bit depth. Having examined your code for the copy you don't appear to be applying a compression and not taking advantage of parallel processing, these are environment settings of the tool.

2

The FeatureToRaster function is documented in https://pro.arcgis.com/en/pro-app/latest/tool-reference/conversion/feature-to-raster.htm and there is no possibility to select the datatype of the output raster.

If you do not need to use just arcpy, GDAL has similar rasterizing capabilities either with Python or with the binary utility gdal_rasterize https://gdal.org/programs/gdal_rasterize.html. GDAL allows user to select the datatype of the output raster.

-ot <type>

Force the output bands to be of the indicated data type. Defaults to Float64, unless the attribute field to burn is of type Int64, in which case Int64 is used for the output raster data type if the output driver supports it.

The datatype can be one of these:

 {Byte/Int8/Int16/UInt16/UInt32/Int32/UInt64/Int64/Float32/Float64/CInt16/CInt32/CFloat32/CFloat64}
2
  • 1
    And ArcGIS Pro ships with the GDAL python bindings so osgeo.gdal.Rasterize is available in the default arcgispro-py3 conda environment.
    – user2856
    Commented Aug 6 at 11:13
  • 1
    I ended up using GDAL and was able to get the rasters output as 16 bit, thanks for the suggestion!
    – greg684
    Commented Aug 6 at 15:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.