Select to view content in your preferred language

Arcade Expression to summarize values in text string

958
8
Jump to solution
05-03-2023 09:59 AM
Labels (1)
AustinCanty1
New Contributor III

I have an arcade expression for a popup that returns a text string comprised of a land use description and a length. How would I go about summarizing the total length by the land use description? 

 

var buffer = BufferGeodetic($feature, 50, "feet")
var int_ma = Intersects(FeatureSetByName($map, "AllParcels"), BufferGeodetic($feature, 50, "feet"))
var leng = LengthGeodetic($feature, 'feet')

var int_dict = {}
var xs = ""
for (var i in int_ma){
    var xs = AreaGeodetic(Intersection(buffer, i), 'square-feet')
    if(HasKey(int_dict,i.USE_CODE)){
        int_dict[i.USE_CODE] += xs
    } else {
        int_dict[i.USE_CODE] = xs
    }
var total = 0
var out_str = 0
for (var t in int_dict){
    total += sum(number(int_dict[t]))
}

for (var d in int_dict){
  var Ucode = decode(d,
  "0101", "Multiple-Use, Primarily Residential",
"031", "Multiple-Use, Primarily Commercial",
"101", "Residential, Single Family",
"102", "Residential, Multi-family",
"1320", "Residential, Vacant Land",
"140", "Residential, Other",
"300", "Commercial",
"3000", "Commercial",
"400", "Industrial",
"901", "Exempt Property",
'other');
  out_str += '\n' + Ucode + ' - ' +round(number(int_dict[d])/(total)*leng,2)+" Ft"
}}
return out_str
 
 

Output.png

Tags (3)
0 Kudos
1 Solution

Accepted Solutions
JoshuaSharp-Heward
Occasional Contributor III

I think you need to change the decode to be decoding i.USE_CODE, as currently it's decoding the feature object itself? And you need to pass that decoded value into your dictionary not the i.USE_CODE, so something like this:

var buffer = BufferGeodetic($feature, 50, "feet")
var int_ma = Intersects(FeatureSetByName($map, "AllParcels"), BufferGeodetic($feature, 50, "feet"))
var leng = LengthGeodetic($feature, 'feet')

var int_dict = {}
var xs = ""
for (var i in int_ma){
  var Ucode = decode(i.USE_CODE,
  "0101", "Multiple-Use, Primarily Residential",
  "0102", "Multiple-Use, Primarily Residential",
  "0104", "Multiple-Use, Primarily Residential",
  "031", "Multiple-Use, Primarily Commercial",
  "0316", "Multiple-Use, Primarily Commercial",
  "0321", "Multiple-Use, Primarily Commercial",
  "101", "Residential, Single Family",
  "1010", "Residential, Single Family",
  "1014", "Residential, Multi-family",
  "102", "Residential, Multi-family",
  "130", "Residential, Vacant Land",
  "300", "Commercial",
  "3000", "Commercial",
  "302", "Commercial",
  "400", "Industrial",
  "4000", "Industrial",
  "901", "Exempt Property",
  "902", "Exempt Property",
  'other');
    var xs = AreaGeodetic(Intersection(buffer, i), 'square-feet')
    if(HasKey(int_dict,UCode)){
        int_dict[UCode] += xs
    } else {
        int_dict[UCode] = xs
    }
var total = 0
var out_str = ''
for (var t in int_dict){
    total += sum(number(int_dict[t]))
}

for (var d in int_dict){
  out_str += '\n' +  Ucode + ' - ' +round(number(int_dict[d])/(total)*leng,2)+" Ft"
}}

return out_str

View solution in original post

0 Kudos
8 Replies
JoshuaSharp-Heward
Occasional Contributor III

Hi,

I'm a little confused because the screenshot and your code above don't match - I'm guessing the problem is because there are multiple codes linked to one land use description (e.g. '0101' and '0102' for "Multiple-Use, Primarily Residential") so they're not being aggregated? If you use your "decode" before you write the key to the int_dict variable that should group it all correctly (i.e. using "Multiple-Use, Primarily Residental" use the key instead of the code). Otherwise it all looks good to me.

JohannesLindner
MVP Frequent Contributor

To post code:

JohannesLindner_0-1677736512957.png
JohannesLindner_1-1677736529803.png

 

Wow, I searched and searched for an error, because your expression returned the correct result (except for that "0" at the start). But you actually have multiple codes for the same land use...

So you need to summarize by the actual land use, not the code. Do the decoding in the loop where you fill int_dict.


Have a great day!
Johannes
AustinCanty1
New Contributor III

Thanks! That makes sense, but I'm having trouble getting that to work. I moved the decode up into the first loop but now everything is returning as "Other". Do I have to change something with the HasKey?

 

var buffer = BufferGeodetic($feature, 50, "feet")
var int_ma = Intersects(FeatureSetByName($map, "AllParcels"), BufferGeodetic($feature, 50, "feet"))
var leng = LengthGeodetic($feature, 'feet')

var int_dict = {}
var xs = ""
for (var i in int_ma){
  var Ucode = decode(i,
  "0101", "Multiple-Use, Primarily Residential",
  "0102", "Multiple-Use, Primarily Residential",
  "0104", "Multiple-Use, Primarily Residential",
  "031", "Multiple-Use, Primarily Commercial",
  "0316", "Multiple-Use, Primarily Commercial",
  "0321", "Multiple-Use, Primarily Commercial",
  "101", "Residential, Single Family",
  "1010", "Residential, Single Family",
  "1014", "Residential, Multi-family",
  "102", "Residential, Multi-family",
  "130", "Residential, Vacant Land",
  "300", "Commercial",
  "3000", "Commercial",
  "302", "Commercial",
  "400", "Industrial",
  "4000", "Industrial",
  "901", "Exempt Property",
  "902", "Exempt Property",
  'other');
    var xs = AreaGeodetic(Intersection(buffer, i), 'square-feet')
    if(HasKey(int_dict,i.USE_CODE)){
        int_dict[i.USE_CODE] += xs
    } else {
        int_dict[i.USE_CODE] = xs
    }
var total = 0
var out_str = ''
for (var t in int_dict){
    total += sum(number(int_dict[t]))
}

for (var d in int_dict){
  out_str += '\n' +  Ucode + ' - ' +round(number(int_dict[d])/(total)*leng,2)+" Ft"
}}

return out_str

 

 

0 Kudos
JoshuaSharp-Heward
Occasional Contributor III

I think you need to change the decode to be decoding i.USE_CODE, as currently it's decoding the feature object itself? And you need to pass that decoded value into your dictionary not the i.USE_CODE, so something like this:

var buffer = BufferGeodetic($feature, 50, "feet")
var int_ma = Intersects(FeatureSetByName($map, "AllParcels"), BufferGeodetic($feature, 50, "feet"))
var leng = LengthGeodetic($feature, 'feet')

var int_dict = {}
var xs = ""
for (var i in int_ma){
  var Ucode = decode(i.USE_CODE,
  "0101", "Multiple-Use, Primarily Residential",
  "0102", "Multiple-Use, Primarily Residential",
  "0104", "Multiple-Use, Primarily Residential",
  "031", "Multiple-Use, Primarily Commercial",
  "0316", "Multiple-Use, Primarily Commercial",
  "0321", "Multiple-Use, Primarily Commercial",
  "101", "Residential, Single Family",
  "1010", "Residential, Single Family",
  "1014", "Residential, Multi-family",
  "102", "Residential, Multi-family",
  "130", "Residential, Vacant Land",
  "300", "Commercial",
  "3000", "Commercial",
  "302", "Commercial",
  "400", "Industrial",
  "4000", "Industrial",
  "901", "Exempt Property",
  "902", "Exempt Property",
  'other');
    var xs = AreaGeodetic(Intersection(buffer, i), 'square-feet')
    if(HasKey(int_dict,UCode)){
        int_dict[UCode] += xs
    } else {
        int_dict[UCode] = xs
    }
var total = 0
var out_str = ''
for (var t in int_dict){
    total += sum(number(int_dict[t]))
}

for (var d in int_dict){
  out_str += '\n' +  Ucode + ' - ' +round(number(int_dict[d])/(total)*leng,2)+" Ft"
}}

return out_str
0 Kudos
AustinCanty1
New Contributor III

I've tried that and it seems like it's grouping the use types together properly but not applying the use code descriptions correctly. Any ideas?

Output.png

0 Kudos
JoshuaSharp-Heward
Occasional Contributor III

You're printing out Ucode which is just the most recent decoded value, replace "Ucode" with "d" in the out_str line to print out the relevant dictionary key.

0 Kudos
AustinCanty1
New Contributor III

Ah that's it! Thank you so much for your help.

0 Kudos
JoshuaSharp-Heward
Occasional Contributor III

No worries, glad I could help out!

0 Kudos