Hello, I am trying to display the closest 3 points (staff locations) to a given feature in a popup. I would like them arranged in order of distance. I have a working expression to show me ALL points within a buffer of the feature, but I just can't figure out how to arrange and truncate them without messing up my resulting string.
Any tips? TYIA
Solved! Go to Solution.
Sorry I miss read your question. I would probably create an empty featureset and populate it in your loop, then you can sort it and get the top 3.
var New_FS = {fields:[
{'name':'StaffLocation','type': 'esriFieldTypeString'},
{'name':'SLDistance','type': 'esriFieldTypeDouble'}],
'geometryType': '',
'features':[]
}
for (var s in staff){
var dist = Distance(s,$feature,'miles')
var new_f = {'attributes': {'StaffLocation': s['Location'] ,
'SLDistance': dist}
}
Push(New_FS.features, new_f)}
You would then have to loop through this new fs to construct the string for the popup
Sorry, forgot to mention you need to convert the populated dictionary/featureset to read like one.
Try
var sorted = Top(OrderBy(FeatureSet(Text(New_FS)), 'SLDistance ASC'),3)
Cheers
Peter
I don't know about truncating the first three, but OrderBy should allow you to at least list them in descending order. I think you would do:
var dist = Top(OrderBy(Distance(s, $feature, 'miles'), 'miles DESC'),3)
But, I'm not an Arcade expert. 😅 Maybe that goes around Push(Top(Orderby(stafflist....) instead of the Distance function.
Solved: Re: How to Use Arcade GroupBy with Top and OrderBy... - Esri Community
Still erroring saying invalid parameter 😕
Maybe because 'miles' isn't a field already? idk
Thanks for the reply though!
I would use a top and order by on the intersects
https://developers.arcgis.com/arcade/function-reference/featureset_functions/#top
Peter
That didn't work either. My problem is that the feature set I'm wanting to sort only has a name field, and I can't get OrderBy to recognize conditional data from an arcade expression.
Thanks for the suggestion!
Sorry I miss read your question. I would probably create an empty featureset and populate it in your loop, then you can sort it and get the top 3.
var New_FS = {fields:[
{'name':'StaffLocation','type': 'esriFieldTypeString'},
{'name':'SLDistance','type': 'esriFieldTypeDouble'}],
'geometryType': '',
'features':[]
}
for (var s in staff){
var dist = Distance(s,$feature,'miles')
var new_f = {'attributes': {'StaffLocation': s['Location'] ,
'SLDistance': dist}
}
Push(New_FS.features, new_f)}
You would then have to loop through this new fs to construct the string for the popup
This feels closer to what I'm trying to do, but now I can't get the sort to work. Am I doing something wrong?
Thank you
// create 250mi buffer on selection
var buff = Buffer($feature, 250, 'miles')
// get intersecting staff locations
var staff = Intersects(buff, FeatureSetByName($map, "CS Staff Locations"))
// create empty featureset to hold results
var New_FS = {fields:[
{'name':'StaffLocation','type': 'esriFieldTypeString'},
{'name':'SLDistance','type': 'esriFieldTypeDouble'}],
'geometryType': '',
'features':[]
}
for (var s in staff){
var dist = Distance(s,$feature,'miles')
var new_f = {'attributes': {'StaffLocation': s['Location'] ,
'SLDistance': dist}
}
Push(New_FS.features, new_f)}
var sorted= Top(OrderBy(New_FS, 'SLDistance ASC'),3)
return sorted
Sorry, forgot to mention you need to convert the populated dictionary/featureset to read like one.
Try
var sorted = Top(OrderBy(FeatureSet(Text(New_FS)), 'SLDistance ASC'),3)
Cheers
Peter
This is fantastic, thank you so much!