Select to view content in your preferred language

Return value gets automatically cast to output field type?

449
1
03-08-2022 05:23 PM
Bud
by
Esteemed Contributor


The Arcade developer Attribute Rule Calculation docs say:

When the attribute rule is evaluated for a dataset, the return value for the expression is cast to the field type of the output value. It is best practice to handle casting within the script for full control of casting behavior to NumberDate, or Text return types.

It sounds like that second sentence is implying that we only need to cast datatypes to the output type when the datatype is Number, Date, or Text.

Is that correct?

For example, for geometries, would we return Polyline(my_dictionary) -- and then rely on Arcade to automatically cast that polyline to a geometry?

Or is the fact that Polyline is a subtype of the Geometry type mean that Polyline is technically already a geometry?

 

0 Kudos
1 Reply
JohannesLindner
MVP Alum

Is that correct?

No.

What it means is:

  • If you don't do it yourself, the Attribute Rule will take care of the conversion between the data types.
  • To be sure about the results (which should be best practice), you should do it yourself.

 

Take this rule:

// converts the value from the text field to integer and double
var txt = $feature.TextField
if(IsEmpty(txt)) { return }
return {"result": {"attributes": {"IntegerField": txt, "DoubleField": txt}}}

I'm not doing any type casting here. I return text values for numeric fields, ArcGIS automatically takes care of the conversion. Great, less work for me! Let's see how that turns out:

JohannesLindner_0-1646824249719.png

Uh oh, something is wrong here...

  • I'm German, and we (and also the German ArcGIS locationing) use comma as decimal separator and dot as thousands separator. If I input numbers in American format as text (stupid, but just as an example), I get unexpected results.
  • I expected int(1.5) to be floor(1.5) = 1, but ArcGIS uses Round(1.5) = 2

So the Attribute Rule automatically casted between the data types, but to be sure about the results I get (again. that should be best practice), I should do it myself:

var txt = $feature.TextField
if(IsEmpty(txt)) { return }

txt = Replace(txt, ",", "")  // remove American thousands separator
txt = Replace(txt, ".", ",") // replace decimal separator
var dbl = Number(txt)
var int = Floor(dbl)
return {"result": {"attributes": {"IntegerField": int, "DoubleField": dbl}}}

JohannesLindner_1-1646825223713.png

 

Same concept applies to conversion from and to date. Probably more so, seeing how many different date formats there are...

As for geometries: Yes, you just return the Polyline, which is already a Geometry.


Have a great day!
Johannes