Exception with Color_ramp Raster Function (WPF/v100.4)

981
3
08-16-2019 08:36 AM
KeithGemeinhart1
New Contributor III

I'm trying to implement a raster function, starting with the code in the Raster Layer Raster Function sample. The sample uses Hillshade. I can get that to work, but I'm getting an exception with the Color_ramp function. I have not investigated this with v100.5, but I did check the release notes and don't see anything related.

I'm using the json for Color_ramp function listed on Add raster data—ArcGIS Runtime SDK for .NET | ArcGIS for Developers . I'm getting an exception in the RasterFunction.FromJson() method. The exception is

"invalid start of field name - expecting \"": } : 1158

If I add some random spaces in the json string, the 1158 number in the exception changes, so I'm thinking that is possibly the location in the string where the problem is.

Stack trace is:

   at Esri.ArcGISRuntime.ArcGISException.HandleCoreError(CoreError error, Boolean throwException)
   at RuntimeCoreNet.GeneratedWrappers.Interop.CheckError(IntPtr errorHandle, Boolean throwOnFailure, GCHandle wrapperHandle)
   at RuntimeCoreNet.GeneratedWrappers.CoreRasterFunction.FromJSON(String jSON)
   at Esri.ArcGISRuntime.Rasters.RasterFunction.FromJson(String json)

Here is my code. I added a line to parse the json to make sure it was valid json syntax, and that works OK.

string colorRamp =
@"{
  ""raster_function"":{""type"":""Color_ramp_function""},
  ""raster_function_arguments"":
  {
    ""resizable"":{""bool"":false,""type"":""Raster_function_variable""},
    ""color_ramp"":
    {
      ""color_ramp"":
      {
        ""ramps"":
        [
         {""to_color"":[0,255,0],""from_color"":[0,191,191],""num_colors"":3932,""type"":""Algorithmic_color_ramp"",""algorithmic_type"":""hsv""},
         {""to_color"":[255,255,0],""from_color"":[0,255,0],""num_colors"":3932,""type"":""Algorithmic_color_ramp"",""algorithmic_type"":""hsv""},
         {""to_color"":[255,127,0],""from_color"":[255,255,0],""num_colors"":3932,""type"":""Algorithmic_color_ramp"",""algorithmic_type"":""hsv""},
         {""to_color"":[191,127,63],""from_color"":[255,127,0],""num_colors"":3932,""type"":""Algorithmic_color_ramp"",""algorithmic_type"":""hsv""},
         {""to_color"":[20,20,20],""from_color"":[191,127,63],""num_colors"":3935,""type"":""Algorithmic_color_ramp"",""algorithmic_type"":""hsv""}
        ],
        ""type"":""Multipart_color_ramp""
      },
      ""type"":""Raster_function_variable""
    },
    ""raster"":{""name"":""raster"",""is_raster"":true,""type"":""Raster_function_variable""},
    ""type"":""Raster_function_arguments"",
  },
  ""type"":""Raster_function_template"",
}";

var json = colorRamp;
try
{
    JObject rss = JObject.Parse(json);
    RasterFunction myRasterFunction = RasterFunction.FromJson(json);

}
catch (Exception e)
{
    Console.WriteLine(e);
}‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos
3 Replies
KeithGemeinhart1
New Contributor III

After more debugging, I figured it out!

The json for Color_ramp function on the page Add raster data—ArcGIS Runtime SDK for .NET | ArcGIS for Developers  is incorrect. It has 2 extraneous commas. In the json below, the extra commas were at the end of lines 23 and 24. Michael Branscomb‌, someone should correct this on the developers site.

Follow-up question for anyone who is reading this. Is there a syntax spec for the color ramp raster function? I would like to be able to use either actual values or % values to set my colors. This sample doesn't have either of those parameters. But other pages indicate maybe there is a way to do that. See:

Color ramp objects—Common Data Types | ArcGIS for Developers 

{
  "raster_function":{"type":"Color_ramp_function"},
  "raster_function_arguments":
  {
    "resizable":{"bool":false,"type":"Raster_function_variable"},
    "color_ramp":
    {
      "color_ramp":
      {
        "ramps":
        [
         {"to_color":[0,255,0],"from_color":[0,191,191],"num_colors":3932,"type":"Algorithmic_color_ramp","algorithmic_type":"hsv"},
         {"to_color":[255,255,0],"from_color":[0,255,0],"num_colors":3932,"type":"Algorithmic_color_ramp","algorithmic_type":"hsv"},
         {"to_color":[255,127,0],"from_color":[255,255,0],"num_colors":3932,"type":"Algorithmic_color_ramp","algorithmic_type":"hsv"},
         {"to_color":[191,127,63],"from_color":[255,127,0],"num_colors":3932,"type":"Algorithmic_color_ramp","algorithmic_type":"hsv"},
         {"to_color":[20,20,20],"from_color":[191,127,63],"num_colors":3935,"type":"Algorithmic_color_ramp","algorithmic_type":"hsv"}
        ],
        "type":"Multipart_color_ramp"
      },
      "type":"Raster_function_variable"
    },
    "raster":{"name":"raster","is_raster":true,"type":"Raster_function_variable"},
    "type":"Raster_function_arguments"
  },
  "type":"Raster_function_template"
}
MichaelBranscomb
Esri Frequent Contributor

Hi Keith,

Sorry you ran into this - I've logged an issue for our doc team to follow up and fix up.

Regarding a spec for the functions - I'll check with one of the raster folk and let you know.

Cheers

Mike

KeithGemeinhart1
New Contributor III

Thanks Mike!

0 Kudos