Select to view content in your preferred language

Create a raster from a raster function:"Raster_calculator_function"

1068
10
08-28-2023 12:36 AM
Labels (2)
AlexanderZhang
New Contributor III

Hi All,

  Recently i develop a gis app with Navtive Maps SDK 200.1, i want to apply a raster function(Raster_calculator_function) on a raster file, but it doesn't work, so i wander whether i can use  the function:"Con()" in the expression statement as following:

string theJSON_String =
@"{
""raster_function_arguments"":
{
""expression"":{ ""string"":""Con(r1>r2,0,1)"",""name"":""expression"",""type"":""Raster_function_variable""},
""raster_names"":{ ""name"":""raster_names"",""type"":""Raster_function_variable""},
""raster_names"":{ ""name"":""raster_names"",""string_array"":[""r1"",""r2""],""type"":""Raster_function_variable""},
""r1"":{ ""name"":""r1"",""is_raster"":true,""type"":""Raster_function_variable""},
""r2"":{ ""name"":""r2"",""is_raster"":true,""type"":""Raster_function_variable""},
""type"":""Raster_function_arguments""
},
""raster_function"":{ ""type"":""Raster_calculator_function""},
""type"":""Raster_function_template""
}";

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

 Esri.ArcGISRuntime.Rasters.RasterFunction(path as String)

// Get the raster function arguments
RasterFunctionArguments myRasterFunctionArguments = myRasterFunction.Arguments;

// Get the list of names from the raster function arguments
IReadOnlyList<string> myRasterNames = myRasterFunctionArguments.GetRasterNames();

// Apply the first raster name and image service raster in the raster function arguments
myRasterFunctionArguments.SetRaster(myRasterNames[0], raster1);
myRasterFunctionArguments.SetRaster(myRasterNames[1], raster2);

var layer = new RasterLayer(new Raster(myRasterFunction));

Tags (1)
0 Kudos
10 Replies
williambohrmann3
Esri Contributor

@AlexanderZhang thank you for your question. It seems like you've viewed our RasterLayerRasterFunction WPF sample. Since your code follows that logic, this should work! The comparison expression makes sense in your JSON. But you said it isn't working, so a few things to consider:

  • Verify that your raster1 or raster2 is instantiated correctly. Ensure the link to the raster service, or the file path to the offline raster is correct.
  • Is your code not compiling? I am guessing this is just a copy-paste error, but "Esri.ArcGISRuntime.Rasters.RasterFunction(path as String)" won't compile as is...

Let me check to see that the Con() expression works with Raster_calculator per your request. Sorry for the correction, hoping it's an easy fix!

0 Kudos
AlexanderZhang
New Contributor III

@williambohrmann3  thank you for your help, following your suggestion, I recoded my program. I fixed the compilation error you pointed out,  then i modified the JSON string as following:

string theJSON_String =
@"{
""raster_function_arguments"":
{
""expression"":{ ""string"":""r1+r2"",""name"":""expression"",""type"":""Raster_function_variable""},
""raster_names"":{ ""name"":""raster_names"",""string_array"":[""r1"",""r2""],""type"":""Raster_function_variable""},
""r1"":{ ""name"":""r1"",""is_raster"":true,""type"":""Raster_function_variable""},
""r2"":{ ""name"":""r2"",""is_raster"":true,""type"":""Raster_function_variable""},
""type"":""Raster_function_arguments""
},
""raster_function"":{ ""type"":""Raster_calculator_function""},
""type"":""Raster_function_template""
}";

It works well! However, When I modify the JSON string to the following case, it doesn't work anymore(The app automatically exited without any error message.):

string theJSON_String =
@"{
""raster_function_arguments"":
{
""expression"":{ ""string"":""Con(r1>r2,0,1)"",""name"":""expression"",""type"":""Raster_function_variable""},
""raster_names"":{ ""name"":""raster_names"",""string_array"":[""r1"",""r2""],""type"":""Raster_function_variable""},
""r1"":{ ""name"":""r1"",""is_raster"":true,""type"":""Raster_function_variable""},
""r2"":{ ""name"":""r2"",""is_raster"":true,""type"":""Raster_function_variable""},
""type"":""Raster_function_arguments""
},
""raster_function"":{ ""type"":""Raster_calculator_function""},
""type"":""Raster_function_template""
}";

The raster1 and raster2  are  offline rasters with a single band. So, i would like to know if the raster function(Raster_calculator_function) supports the following expression: "Con(r1>r2,0,1)".

Thanks.

0 Kudos
williambohrmann3
Esri Contributor

Hey @AlexanderZhang , your original JSON was correct. My mistake. Glad you fixed the compilation error. Waiting to hear from our raster experts on if Con() expression will work here.

0 Kudos
williambohrmann3
Esri Contributor

Your Con() expression works in ArcGIS Pro. However, with your app, I am also getting same behavior: silent failure. I checked the output in Visual Studio: The program has exited with code 3221225477 (0xc0000005) 'Access violation'. When debugging, I notice the RasterLayer.Raster.Path was empty. However, I changed your expression to "r1 > r2" and the code compiled. Could you try changing the expression and let me know if that resolves your issue?

If that doesn't work, I'd like to know... Are you working with a raster service or a local file? If a local file, what file extension? And did you ever see the same access violation in output?

0 Kudos
AlexanderZhang
New Contributor III

Thanks for your information, i'm afraid it can't resolves my issue by changing the expression, because I will use Con expression like "Con(r1>2000,r1,r2)" later!

In my app, the path of the r1 and r2 point to two local raster files(.tif).

0 Kudos
williambohrmann3
Esri Contributor

I wonder if the issue is the Con() function isn't syntactically correct. I think r1 and r2 need to be surrounded by quotations, since they're names of raster function variables. This is how the raster calculator works in ArcGIS Pro. Can you change your expression to: ""Con(\""r1\"">\""r2\"",0,1)""? 

0 Kudos
AlexanderZhang
New Contributor III

Hi,@williambohrmann3, I've tried your idea but it still doesn't work,Thanks for your advice.

0 Kudos
williambohrmann3
Esri Contributor

Hey @AlexanderZhang 

If you're able to send me a reproducer project with your two raster files, I'd be happy to take a look.

0 Kudos
AlexanderZhang
New Contributor III

Thank you @williambohrmann3, the raster files are too big to send you, i'm currently trying another solution to implement the function: create a geoprocessing package (.gpkx) in ArcGIS Pro, and then execute the geoprocessing package with ArcGIS Runtime Local Server.

Unfortunately, I encountered another problem , would you like give me some advice?

 

0 Kudos