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
Solved! Go to Solution.
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], " ");
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], " ");
Thanks for replay
I don't have $feature,BoroCD.
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
}
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 This is working but how can I get return in popup I am getting [object Object]
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?
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], " ");