Select to view content in your preferred language

Arcade - how calculate Field in FeatureSet

5268
7
Jump to solution
11-26-2021 11:20 AM
VHolubec
Esri Regular Contributor

Hello,

I am trying to get work an Arcade expression to create a FeatureSet, which I want to use then for Serial chart.
In simplification, I am taking an attribute ZONE from the layer:


var fs = FeatureSetByPortalItem(Portal('https://arcgis.com'),'e7ad315d17a742fda44fb63cd311152a',0,['ZONE'], false)

The ZONE attribute is a string which looks like P6-0176. The goal is to create a field in the FeatureSet which recalculate the ZONE filed but keeps the first two chars only - so the result for this example would be P6.

So I am creating a new FeatureSet definition, and the attribute ZONE I would like to use in LEFT function.
But if I use the code like this:

var novy = {
'fields': [{'name':'Zona', 'type':'esriFieldTypeString'}],
'geometryType': '',
'features':
[{'attributes':
{'Zona': Left((fs,'ZONE'), 2)
}}]};
return FeatureSet(Text(novy))

The result is the error for the LEFT function.

 

What is the correct syntax to call the column ZONE from the original FeatureSet FS in the LEFT function? Or is there any other way (and this is not the correct one) how to recalculate the ZONE attribute in original FeatureSet FS.

Thank you for any ideas

 

Vladimir
2 Solutions

Accepted Solutions
JohannesLindner
MVP Frequent Contributor

To insert code:

JohannesLindner_0-1638260465949.png

JohannesLindner_1-1638260485929.png

 

You are trying to input a tuple of (FeatureSet, Text) into the Left function, which is not a valid argument.

According to the docs, the first argument can be Text, Array, Number, or Boolean.

You have to fill your feature set sequencially:

var fs = FeatureSetByPortalItem(Portal('https://arcgis.com'),'e7ad315d17a742fda44fb63cd311152a', 0, ['ZONE'], false)

var novy = {
  'fields': [{'name':'Zona', 'type':'esriFieldTypeString'}],
  'geometryType': '',
  'features': []
}
for(var f in fs) {
  var new_f = {'attributes': {'Zona': Left(f.ZONE, 2)}}
  Push(novy.features, new_f)
}
return FeatureSet(Text(novy))

 


Have a great day!
Johannes

View solution in original post

JohannesLindner
MVP Frequent Contributor

Your field declaration and the fields of your features have to match.

  • name
    • STMP2022F1Sp, SumF1Sp, F1Sp_Per
    • SPECIES, SPArea, SPPer
  • type
    • according to the docs, there is no BigInteger, only Integer
    • SPArea and SPPer are probably Double, so either use esriFieldTypeDouble or use Round(x, 0)

Have a great day!
Johannes

View solution in original post

7 Replies
JohannesLindner
MVP Frequent Contributor

To insert code:

JohannesLindner_0-1638260465949.png

JohannesLindner_1-1638260485929.png

 

You are trying to input a tuple of (FeatureSet, Text) into the Left function, which is not a valid argument.

According to the docs, the first argument can be Text, Array, Number, or Boolean.

You have to fill your feature set sequencially:

var fs = FeatureSetByPortalItem(Portal('https://arcgis.com'),'e7ad315d17a742fda44fb63cd311152a', 0, ['ZONE'], false)

var novy = {
  'fields': [{'name':'Zona', 'type':'esriFieldTypeString'}],
  'geometryType': '',
  'features': []
}
for(var f in fs) {
  var new_f = {'attributes': {'Zona': Left(f.ZONE, 2)}}
  Push(novy.features, new_f)
}
return FeatureSet(Text(novy))

 


Have a great day!
Johannes
PeterMilenkovic
Frequent Contributor

Hello,

Thought this would be a suitable thread, I have a very similar requirement. Generating a featureset to use as the data source for a dashboard graph.

var fs = FeatureSetByPortalItem(Portal('https://cityofsydney.maps.arcgis.com'), '103a3484d254440caede4c2a28c46c5b', 9,['StreetSectionArea','StreetFinalForCityReview','STMP2022F1Sp'],false);

var fsfilter = Filter(fs, 'StreetFinalForCityReview = 5')

var F1Sp_GB = Top(OrderBy(GroupBy(fsfilter, ['STMP2022F1Sp'], [ { name: 'SumF1SP', expression: 'StreetSectionArea', statistic: 'SUM' } ]),'SumF1SP DESC'),10)

var TotAreaFinalised = SUM(F1Sp_GB,'SumF1SP')

var joinedDict = {
    
    fields:[
        {'name':'STMP2022F1Sp','type': 'esriFieldTypesString'},
        {'name':'SumF1Sp','type': 'esriFieldTypeBigInteger'},
        {'name':'F1Sp_Per','type': 'esriFieldTypeBigInteger'}],
        'geometryType': '',
        'features':[]
        }
    
for (var f in F1Sp_GB){
    var new_f = {'attributes': {'SPECIES': DomainName(f,"STMP2022F1Sp"),
                                'SPArea': f["SumF1SP"],
                                'SPPer': (f["SumF1SP"]/TotAreaFinalised)*100}}
    Push(joinedDict.features, new_f)
}


return  FeatureSet(Text(joinedDict))

 When i return the raw dictionary i can see the correct values, but the featureset does not show when i have the dictionary wrapped inside FeatureSet(Text()).

Can't figure out what i have done wrong.

 

Thanks

Peter

 

0 Kudos
JohannesLindner
MVP Frequent Contributor

Your field declaration and the fields of your features have to match.

  • name
    • STMP2022F1Sp, SumF1Sp, F1Sp_Per
    • SPECIES, SPArea, SPPer
  • type
    • according to the docs, there is no BigInteger, only Integer
    • SPArea and SPPer are probably Double, so either use esriFieldTypeDouble or use Round(x, 0)

Have a great day!
Johannes
JohannesLindner
MVP Frequent Contributor

Also, it should be "esriFieldTypeString", not "esriFieldTypesString".


Have a great day!
Johannes
0 Kudos
PeterMilenkovic
Frequent Contributor

Thanks so much for your help, it's working now!

Have another question now, the domain name does not get returned, only the code.. 

I assume the f in my loop is the feature in the featureset. 

DomainName($feature, 'fieldName')

0 Kudos
PeterMilenkovic
Frequent Contributor

Figured it out now and i think i understand what was happening...

When i created the variable F1Sp_GB (group by table), it removed the domain... i was just left with the code, to bring back the domain name i had to use the original featureset and insert the code from group by table.

var fs = FeatureSetByPortalItem(Portal('https://cityofsydney.maps.arcgis.com'), '103a3484d254440caede4c2a28c46c5b', 9,['StreetSectionArea','StreetFinalForCityReview','STMP2022F1Sp'],false);

var fsfilter = Filter(fs, 'StreetFinalForCityReview = 5')

var F1Sp_GB = Top(OrderBy(GroupBy(fsfilter, ['STMP2022F1Sp'], [ { name: 'SumF1SP', expression: 'StreetSectionArea', statistic: 'SUM' } ]),'SumF1SP DESC'),10)

var TotAreaFinalised = SUM(F1Sp_GB,'SumF1SP')

var joinedDict = {
    
    fields:[
        {'name':'STMP2022F1Sp','type':'esriFieldTypeString'},
        {'name':'SumF1Sp','type': 'esriFieldTypeDouble'},
        {'name':'F1Sp_Per','type': 'esriFieldTypeDouble'}],
        'geometryType': '',
        'features':[]
        }
    
for (var f in F1Sp_GB){
    var new_f = {'attributes': {'STMP2022F1Sp': DomainName(fs,'STMP2022F1Sp',f['STMP2022F1Sp']),
                                'SumF1Sp': round(f['SumF1SP'],0),
                                'F1Sp_Per': round((f['SumF1SP']/TotAreaFinalised)*100,2)}}
    Push(joinedDict.features, new_f)
}


return  FeatureSet(Text(joinedDict))

 

VHolubec
Esri Regular Contributor

Thank you very much @JohannesLindner, it works great!

Vladimir
0 Kudos