Select to view content in your preferred language

RasterLayer Alpha Channel?

1223
4
Jump to solution
10-20-2020 07:50 AM
artifact
Occasional Contributor

Is there anyway to set an alpha channel for a rasterlayer renderer?

0 Kudos
1 Solution

Accepted Solutions
MichaelBranscomb
Esri Frequent Contributor

Hi,

You could also try applying a mask e.g.

async Task AddRasterAndSetBackgroundColorValue() 
{
    try
    {
        // Create a raster dataset from a local file raster.
        Raster raster = new Raster(@"..\FileRaster.tif");

        // Await the Load to catch any errors.
        await raster.LoadAsync();

        // Create a raster function from the JSON string using the static/Shared method called: RasterFunction.FromJson(json as String)
        RasterFunction rasterFunction = RasterFunction.FromJson(jsonFunction);

        // Get the raster function arguments
        RasterFunctionArguments rasterFunctionArguments = rasterFunction.Arguments;
                
        // Get the list of raster names from the raster function arguments.
        IReadOnlyList<string> rasterNames = rasterFunctionArguments.GetRasterNames();

        // Set the local file raster as a raster name argument.
        rasterFunctionArguments.SetRaster(rasterNames[0], raster);

        // Create a new raster based on the raster function.
        Raster rasterWithFunction = new Raster(rasterFunction);

        // Create a new raster layer from the raster with function applied.
        RasterLayer rasterLayer = new RasterLayer(rasterWithFunction);

        // Add RasterLayer with function to Map OperationalLayers collection.
        Map.OperationalLayers.Add(rasterLayer);
    }
    catch (Exception ex)
    {

    }
}

string jsonFunction =
@"{
    ""raster_function"":{""type"":""Mask_function""},
    ""raster_function_arguments"":
    {
        ""nodata_values"":{""double_array"":[0],""type"":""Raster_function_variable""},
        ""nodata_interpretation"":{""nodata_interpretation"":""all"",""type"":""Raster_function_variable""},
        ""raster"":{""name"":""raster"",""is_raster"":true,""type"":""Raster_function_variable""},
        ""type"":""Raster_function_arguments""
   },
    ""type"":""Raster_function_template""
}";

Mike

View solution in original post

4 Replies
artifact
Occasional Contributor

or set a transparent background value?

0 Kudos
artifact
Occasional Contributor
0 Kudos
MichaelBranscomb
Esri Frequent Contributor

Hi,

You could also try applying a mask e.g.

async Task AddRasterAndSetBackgroundColorValue() 
{
    try
    {
        // Create a raster dataset from a local file raster.
        Raster raster = new Raster(@"..\FileRaster.tif");

        // Await the Load to catch any errors.
        await raster.LoadAsync();

        // Create a raster function from the JSON string using the static/Shared method called: RasterFunction.FromJson(json as String)
        RasterFunction rasterFunction = RasterFunction.FromJson(jsonFunction);

        // Get the raster function arguments
        RasterFunctionArguments rasterFunctionArguments = rasterFunction.Arguments;
                
        // Get the list of raster names from the raster function arguments.
        IReadOnlyList<string> rasterNames = rasterFunctionArguments.GetRasterNames();

        // Set the local file raster as a raster name argument.
        rasterFunctionArguments.SetRaster(rasterNames[0], raster);

        // Create a new raster based on the raster function.
        Raster rasterWithFunction = new Raster(rasterFunction);

        // Create a new raster layer from the raster with function applied.
        RasterLayer rasterLayer = new RasterLayer(rasterWithFunction);

        // Add RasterLayer with function to Map OperationalLayers collection.
        Map.OperationalLayers.Add(rasterLayer);
    }
    catch (Exception ex)
    {

    }
}

string jsonFunction =
@"{
    ""raster_function"":{""type"":""Mask_function""},
    ""raster_function_arguments"":
    {
        ""nodata_values"":{""double_array"":[0],""type"":""Raster_function_variable""},
        ""nodata_interpretation"":{""nodata_interpretation"":""all"",""type"":""Raster_function_variable""},
        ""raster"":{""name"":""raster"",""is_raster"":true,""type"":""Raster_function_variable""},
        ""type"":""Raster_function_arguments""
   },
    ""type"":""Raster_function_template""
}";

Mike

artifact
Occasional Contributor

Hey Michael, 

This is exactly what I ended up doing based on that link to a post you made back in 2019.

Cheers!

0 Kudos