Select to view content in your preferred language

Using arcade to rank polygons

2104
6
Jump to solution
02-20-2023 09:19 AM
junocs
by
Emerging Contributor

I have a layer of national parks polygons with differing numbers of train stations near each one. I'm trying to rank them in my pop-ups according to which is the most accessible (Count of Points / Shape_Area), but I don't know the correct functions in arcade. Could someone please advise me on how to proceed?

 

(picture for context)Using arcade to rank polygons.png

Tags (3)
0 Kudos
1 Solution

Accepted Solutions
JohannesLindner
MVP Frequent Contributor
// order the layer
var ordered = OrderBy($layer, "(PointCount/Shape_Area) DESC")

var rank = 0
// iterate over the ordered layer
for(var f in ordered) {
    // increase the rank
    rank++
    // if the feature we're looking at is the feature we clicked on, return the current rank
    if(f.OBJECTID == $feature.OBJECTID) {
        return rank
    }
}

 

If you use this expression in the popup, it will recalculate the rank each time you click on a feature. This is good for dynamic data, but it will slow down the popup somewhat.

If you have static data (train stations and national parks sound pretty static), it might be better to use the expression to calculate a new field and show that in the popup.


Have a great day!
Johannes

View solution in original post

0 Kudos
6 Replies
DanPatterson
MVP Esteemed Contributor

maybe orderby

order by in Arcade expressions


... sort of retired...
0 Kudos
junocs
by
Emerging Contributor

I know absolutely nothing about SQL lol but I gave it a try and searched the internet about it a bit. I came up with only this from the esri website:

OrderBy($layer, 'Rank ASC')

However, it doesn't return anything, because I think it doesn't know what to rank? How do I tell it to rank a specific expression or field?

0 Kudos
JohannesLindner
MVP Frequent Contributor
// order the layer
var ordered = OrderBy($layer, "(PointCount/Shape_Area) DESC")

var rank = 0
// iterate over the ordered layer
for(var f in ordered) {
    // increase the rank
    rank++
    // if the feature we're looking at is the feature we clicked on, return the current rank
    if(f.OBJECTID == $feature.OBJECTID) {
        return rank
    }
}

 

If you use this expression in the popup, it will recalculate the rank each time you click on a feature. This is good for dynamic data, but it will slow down the popup somewhat.

If you have static data (train stations and national parks sound pretty static), it might be better to use the expression to calculate a new field and show that in the popup.


Have a great day!
Johannes
0 Kudos
junocs
by
Emerging Contributor

Thank you so much! This code has helped me a lot, but I've run into one problem while using it. I've tried OBJECTID and almost every field (alias and name) that my layer has that could match, but I keep getting the error message that the field is not found.

It's this line:

if(f.OBJECTID == $feature.OBJECTID)

I've found that I can double click the field for the $feature.OBJECTID part and the code fills in with my field, but the f. part just won't work. How do I fix this?

Error message.png

btw layer information is from here: https://naturalengland-defra.opendata.arcgis.com/datasets/national-parks-england/explore?location=52...

I've joined mine with another layer so it's not exactly the same

0 Kudos
JohannesLindner
MVP Frequent Contributor

Ah, joins.

When you add a join to a table, the full name of the field is used: Tablename.Fieldname (you can see that in your screenshot, too).

 

Clean solution: Instead of adding a join, use Join Field to permanently join the point count field to the polygons. This will change the polygon feature class, so it might not be possible for you.

Messy solution: use the full field names.

I downloaded the polygons and replicated your workflow: run the Summarize Nearby tool on the polygons and a point fc, join the output to the National Parks.

My feature class names are "National_Parks__England____Natural_England" for the original polygons and "parks_near" for the parks with summarized points.

This expression works for me:

// order the layer
var ordered = OrderBy($layer, "(parks_near.Point_Count / National_Parks__England____Natural_England.Shape_Area) DESC")

var rank = 0
// iterate over the ordered layer
for(var f in ordered) {
    // increase the rank
    rank++
    // if the feature we're looking at is the feature we clicked on, return the current rank
    if(f['National_Parks__England____Natural_England.OBJECTID'] == $feature['National_Parks__England____Natural_England.OBJECTID']) {
        return rank
    }
}

 


Have a great day!
Johannes
0 Kudos
junocs
by
Emerging Contributor

Oh, I had no idea that join field was so different. I tried it the clean way and it worked! Thanks so much for your help

0 Kudos