How do I check an Arcade variable to see if it is numeric?

6162
15
Jump to solution
02-23-2018 03:03 PM
Syvertson
Occasional Contributor III

I am working on a complex label in ArcGIS Pro, and trying to learn arcade in the process.  The process breaks aparttwo address fields and extracts the first part so I can use this as the "House Number."   Then I make a label using the two "house numbers."  In some cases, they put something other than a standard address in the field, so after parsing, a few would give me a word instead of a number. In other programing languages the "IsNumeric" (or similar) function will give me a boolean result so then I can test it.  How do I do that in Arcade?  I want all non-numeric to be converted to an empty string.  

I will post the code here, but please be kind because I am no professional coder by any stretch of the imagination, especially not in Arcade.

var x = $feature['RC_GIS.dbo.ALL_PARCEL_DATA.Street_Address'];
var y = $feature['RC_GIS.dbo.ALL_PARCEL_DATA.paddress2'];
var x1 = split(x,' ')[0];
var y1 = split(y,' ')[0];
var label;


if(x1 == '0')
{x1 = '';}

if(y1 == '0')
{y1 == '';}

if (x1 == y1)
{
label = x1;
}
else
{
if (x1 == '')
{
label = y1;
}
else
{
if (y1 == '')
{
label = x1;
}
else
{
label = concatenate([x1,y1],"-");
}
}
}

return label;

Thanks in advance for any advise you might have.

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
Syvertson
Occasional Contributor III

Solved this!  Just so others know if the have same problem, I had to change back to text, and at that point the NaN became a string 'NaN' that I could test for.  See below:

var x1 = Number(split(x,' ')[0]);
var y1 = Number(split(y,' ')[0]);

x1 = Text(x1);
if(x1 == '0' || x1 == 'NaN')
{x1 = NULL;}

y1 = Text(y1);
if(y1 == '0' || y1 == 'NaN')
{y1 = NULL;}

View solution in original post

15 Replies
Syvertson
Occasional Contributor III

I think I may have asked the problem wrong, because what you gave me is a very good answer for what I asked, but not what I needed.  

My first step parses the addresses into an array of the "words" in the string.  I take the first item in the array and assign it to a "house number" variable (x1).  (of course it is still a string, but hopefully a numeric string)  I tried using if(typeof(x1) == 'String') to test for non-numeric and replace with an empty string, and it actually eliminated all the results.  

My next idea was to use Numeric(x1), which did actually convert all of the appropriate numeric responses to a number, but the non-numeric ones have a "NaN" value for "not a number."  Since I don't want NaN on the label, andI need to find a way to test for "NaN" and replace with the empty string.  So far I have not found that one test.

Latest version of this code:

var x = $feature['RC_GIS.dbo.ALL_PARCEL_DATA.Street_Address'];
var y = $feature['RC_GIS.dbo.ALL_PARCEL_DATA.paddress2'];
var x1 = Number(split(x,' ')[0]);
var y1 = Number(split(y,' ')[0]);
var label;


if(ABS(x1) == 0)
   {x1 = NULL;}

if(ABS(y1) == 0)
   {y1 = NULL;}

if (x1 == y1)
{
   label = x1;
}
else
{
   if (x1 == NULL)
      {
         label = y1;
      }
   else
   {
      if (y1 == NULL)
      {
         label = x1;
      }
      else
      {
         label = concatenate([x1,y1],"-");
      }
   }
}

return label;

Result is below:  Everything looks fine, except the "NaN".

0 Kudos
Syvertson
Occasional Contributor III

Solved this!  Just so others know if the have same problem, I had to change back to text, and at that point the NaN became a string 'NaN' that I could test for.  See below:

var x1 = Number(split(x,' ')[0]);
var y1 = Number(split(y,' ')[0]);

x1 = Text(x1);
if(x1 == '0' || x1 == 'NaN')
{x1 = NULL;}

y1 = Text(y1);
if(y1 == '0' || y1 == 'NaN')
{y1 = NULL;}

Syvertson
Occasional Contributor III

Now that this is solved and I have learned a little bit about "Arcade" and how to use it in labeling, I need to ask the follow on question.  What are the reasons someone should use Arcade rather than VB Script, Javascript or Python?  There must have been a gap that needed to be filled in order for this newer language to be applied here...???? 

0 Kudos
KoryKramer
Esri Community Moderator

Arcade works across the ArcGIS platform.  So if you're using it in a labeling expression or to control rendering (or in popups, etc. etc.) whatever you see in ArcGIS Pro will be rendered and displayed the same through services in a web browser or app.  That's the underlying idea.

Syvertson
Occasional Contributor III

Thanks.  It seems funny you say this, because I read this right after trying to find a way to use this arcade code in ArcGIS Desktop.  And there appears to be no way.  But if had been written in javascript I could have ported it over.  They seem to be already abandoning that platform, without having the Pro platform ready for "primetime."

0 Kudos
XanderBakker
Esri Esteemed Contributor

The Arcade page ArcGIS Arcade | ArcGIS for Developers  mentions this text, like Kory Kramer already mentioned:

Arcade is an expression language that can be used across the ArcGIS Platform. Whether writing simple scripts to control how features are rendered, or expressions to control label text, Arcade provides a simple scripting syntax to deliver these capabilities.

Arcade scripts are portable. This means that scripts you author in ArcGIS Pro can be read and executed in a WebMap through a browser, or in a Mobile Application

Every change can be difficult, and with the Web GIS pattern there is the need to adapt the platform to enable it to grow and expand its capabilities. None of the existing scripting languages was able to grow like that. In the mean time we will be in a transition phase where Arcade will grow and will be able to do more each time and where other scripting languages would not be able to grow at the same rate due to restrictions they have.

The same is happening with Desktop. Many innovations will not be implemented in ArcMap and will only be available in ArcGIS Pro, because ArcGIS Pro is the feature. A huge example is the Utility Network Management extension. The capabilities are not available in ArcMap, but they are in Web, Pro and mobile. A new feature introduced are the new attribute rules. Those will be configured using Arcade expressions. Many enhancements will come to Arcade and Arcade serves a different purpose then for instance Python. Give it some time and i will prove its benefits over other language.

KoryKramer
Esri Community Moderator

Thoughtful response, Xander!  Matthew Syvertson   In a nutshell, the transition from ArcMap to ArcGIS Pro is a dial, not a switch.  While ArcMap will continue to be supported for many years, new functionality is being developed in ArcGIS Pro.  Arcade is new functionality.  Depending on specific workflows, some customers will be on the ArcMap side of the dial longer than other customers.  Some may be at level 5 (of 10), doing certain things in ArcMap and carrying out different, complete workflows using ArcGIS Pro.  And then there are others who are already clamoring for the dial to go to 11, or 12...

Syvertson
Occasional Contributor III

I understand all that, but there is KEY functionality that is left out that I keep running into.  For instance.  The attribute assistant.  It worked fantastic in ArcGIS Desktop.  Now I have to figure out how to use the new Attribute Rules (which has only a limited subset of capabilities) and when I attempt to make some of the simple rules, it errors. See the screenshot below:

Notice that the green "expression is valid" displays after I validate.  But also notice the red failure notification.  The reason for the failure is an invalid expression.  HUH?

Oh, and also if I add these rules to a feature class they become no longer usable in ArcGIS Desktop.  That is just nuts...  Why not find a way for "Arcade" to use the dynamic value table that was already created in our databases since they are building the language???

I keep trying to make Pro work, but keep running into road blocks.  And I am a very computer savvy person, I can't imagine being the casual GIS user and having to try to navigate these obstacles.

0 Kudos