Issue using comparison in expression for popup in MapViewer

164
2
Jump to solution
a week ago
KateHowlett
Emerging Contributor

Hello,

I'm quite new to Arcade so please bear with me if I'm being completely daft about this.

I am writing an expression in a pop-up that I want to compare two numeric values and, if the same, do one action, else do a different one. However the comparison isn't working and I'm really not sure why.

Some proper context: I have been asked to present school catchments as one element if they are infant/junior/primary schools. Current infant and primary schools are on one layer in my map and junior are another layer, as such, when a user clicks on the map, we show a pop up for infant/primary AND a pop up for junior schools. My goal would be to have a pop up that, when the map was clicked, would say "This is the school catchment for X infant school and Y junior school" - if the school catchments overlap of course which is the case about 9 times out of 10.

To that end, I wrote an expression, that got the feature (a junior school), then the feature set from another layer within the map (primary schools), iterated through that feature set and, for each primary school checked it the area was the same as the featured junior school's area. If so, it would then print into the pop up "This is the school catchment for X infant school and Y junior school". 

Below is the code I have so far. As you can see the column name I'm using is a bit weird so I decided to set it as a variable and then access it that way. Later I realised I could put the column name within square brackets and quotes but that didn't help so.

var popupTitle = 'blah';
var juniorSchool = $feature;
var primarySchools = FeatureSetByName($map, 'Primary School Catchments - Present', ['*'], true);

Console(popupTitle)

for(var primarySchool in primarySchools){

    var areaColumnPrimary = 'Shape.STArea()'
    var areaColumnJunior = 'Shape.STArea()'
    var juniorSchoolArea = juniorSchool[areaColumnJunior]
    var primarySchoolArea = primarySchool[areaColumnPrimary]

    Console("Junior area for " + juniorSchool.Name1 + " is: " + juniorSchoolArea)
    Console("Primary area for " + primarySchool.Name1 + " is: " + primarySchoolArea)

    var type = TypeOf(juniorSchoolArea)
    var type2 = TypeOf(primarySchoolArea)
    Console(type)
    Console(type2)

    if(primarySchoolArea == juniorSchoolArea){
      popupTitle = `Catchment area for ${primarySchool.Name1} and ${juniorSchool.Name1}`
    } else {
      popupTitle = `Catchment area for ${juniorSchool.Name1}`
    }
}

return popupTitle

 It's getting the area for each fine, they're both numbers but the comparison is failing. Below is an example of the data, these are two schools, one is an infant school, one is a junior school and they have the same area.

KateHowlett_1-1779196167557.pngKateHowlett_2-1779196211893.png

(Aside: I appreciate that there is a non-zero chance that two catchment areas that do not overlap would have the same area and so this is possibly not the best choice but as you can see the Centroids do not overlap and the data set is sufficiently small that I am not considering that concern for the time being)

IF the comparison were working correctly, it would see those two area numbers and judge that they were the same and so the pop up would say "This is the catchment area for F Infant & Nursery School and F Junior School". But it doesn't, and I don't know why.

Let me know if you need any further information, I'm going a bit cross-eyed looking at my code but I've had a colleague look over it with me and looked at a few different other posts, plus the arcade page on operators and I'm 99% sure it's correct - except it's not working.

Any help would be deeply appreciated.

Kind regards,

Kate

0 Kudos
1 Solution

Accepted Solutions
Andy_CS
Regular Contributor

you're executing the comparison for each feature in the featureset (primary schools), and returning the result for the last feature in the featureset. each iteration is overwriting the last until you get to the final one.

I think your approach might be off to - are you wanting the popup to be returned when users click on a feature or anywhere on the map?

View solution in original post

2 Replies
Andy_CS
Regular Contributor

you're executing the comparison for each feature in the featureset (primary schools), and returning the result for the last feature in the featureset. each iteration is overwriting the last until you get to the final one.

I think your approach might be off to - are you wanting the popup to be returned when users click on a feature or anywhere on the map?

KateHowlett
Emerging Contributor

Thank you so much! Completely correct, I'd just gone a bit code-blind! I've added a break statement to stop the loop if we have a match and now it works. You're an absolute legend.

To answer your question, people will click on the map and be shown which catchment area(s) they have clicked on.

0 Kudos