Select to view content in your preferred language

Arcade Code for find and return values?

1236
7
Jump to solution
06-16-2022 06:42 AM
anonymous55
Frequent Contributor

Hello,

I need arcade code to return borough name for each community boards. My table don't have borough name field and I can't add one. I have only community board code. Each community board start with specific number which represent each borough. 

I have this code so far 

 

var portal = Portal("https://www.arcgis.com")
var CD = FeatureSetByPortalItem(Portal,
"itemID",0,['BoroCD'], true);

if (Find("1",['BoroCD'], 0)>0) {
   return "Manhattan";
} else if (Find("2", ['BoroCD'], 0)>0) {
   return "Bronx";
} else if(Find("3", ['BoroCD'], 0)>0) {
   return "Brooklyn";
} else if(Find("4", ['BoroCD'], 0)>0) {
   return "Queens";
} else if(Find("5", ['BoroCD'], 0)>0) {
   return "Staten Island"
}

return CD

 

 

Tags (3)
0 Kudos
1 Solution

Accepted Solutions
KenBuja
MVP Esteemed Contributor

Try this:

var portal = Portal("https://www.arcgis.com")
var CD = FeatureSetByPortalItem(Portal,
"ea60eba0707d40a1be2f43696e2cca9f",0,['BoroCD'], true);
var CD1 = First(Intersects($feature, CD));
var CDCode = CD1.BoroCD
var boro = when (CDCode < 200, "Manhattan",
      CDCode < 300, "Bronx",
      CDCode < 400, "Brooklyn",
      CDCode < 500, "Queens", "Staten Island");

var board = Mid(CDCode, 1, 3);      
return Concatenate([boro, board], " ");

View solution in original post

7 Replies
KenBuja
MVP Esteemed Contributor

Are all codes three digit numbers? If so, you could do something like this  

var feature = $feature.BoroCD;

var boro = when (feature < 200, "Manhattan",
      feature < 300, "Bronx",
      feature < 400, "Brooklyn",
      feature < 500, "Queens",
      feature < 600, "Staten Island",
      "Other");

var board = Number(Mid(feature, 1,3));      
return Concatenate([boro, board], " ");

 

0 Kudos
anonymous55
Frequent Contributor

Thanks for replay 
I don't have $feature,BoroCD.

0 Kudos
KenBuja
MVP Esteemed Contributor

What you're getting from FeatureSetByPortalItem is a FeatureSet, or a collection of features. You will have to cycle through them using a For loop.

var portal = Portal("https://www.arcgis.com")
var CD = FeatureSetByPortalItem(Portal,
"ea60eba0707d40a1be2f43696e2cca9f",0,['BoroCD'], true);

for (var feature in CD) {
  //code
}
0 Kudos
jcarlson
MVP Esteemed Contributor

Can you confirm what kind of field BoroCD is? @KenBuja provided a really good solution with the When function. Perhaps you could convert the field to a number first, then put it through that?

Alternate approach: Distinct. It's not really what the function is meant for, but it allows us to use SQL to generate the new field, and is usually a lot faster than a for loop, especially for something simple like this.

Try something like this:

 

var portal = Portal("https://www.arcgis.com")

var fs = FeatureSetByPortalItem(
    portal,
    "ea60eba0707d40a1be2f43696e2cca9f",
    0,
    ['BoroCD'],
    false // unless you *really* need the geometry, setting this to 'true' usually impacts performance more than it's worth
);

var sql = `CASE
WHEN BoroCD LIKE '2%' THEN 'Manhattan'
WHEN BoroCD LIKE '3%' THEN 'Bronx'
WHEN BoroCD LIKE '4%' THEN 'Brooklyn'
WHEN BoroCD LIKE '5%' THEN 'Queens'
WHEN BoroCD LIKE '6%' THEN 'Staten Island'
ELSE 'Other'
END`

return Distinct(
    fs,
    [
        {name: 'objectid', expression: 'objectid'},
        {name: 'BoroCD', expression: 'BoroCD'},
        {name: 'borough_name', expression: sql}
    ]
)

 

  I tested this with a fake FeatureSet, and got these results:

jcarlson_0-1655397945245.png

 

- Josh Carlson
Kendall County GIS
anonymous55
Frequent Contributor

@jcarlson This is working but how can I get return in popup I am getting [object Object] 

0 Kudos
jcarlson
MVP Esteemed Contributor

More details about what you're doing would help. If you're using this for a popup, then it's running on a per-feature basis, and using a big FeatureSet function like this doesn't really make sense.

What feature is this originating from? What fields does it have?

- Josh Carlson
Kendall County GIS
0 Kudos
KenBuja
MVP Esteemed Contributor

Try this:

var portal = Portal("https://www.arcgis.com")
var CD = FeatureSetByPortalItem(Portal,
"ea60eba0707d40a1be2f43696e2cca9f",0,['BoroCD'], true);
var CD1 = First(Intersects($feature, CD));
var CDCode = CD1.BoroCD
var boro = when (CDCode < 200, "Manhattan",
      CDCode < 300, "Bronx",
      CDCode < 400, "Brooklyn",
      CDCode < 500, "Queens", "Staten Island");

var board = Mid(CDCode, 1, 3);      
return Concatenate([boro, board], " ");