I wish to create an expression for a pop-up for my polygon feature layer (counties) that interrogates an overlapping point feature layer (samples).
I will relate and filter the values using a field titled 'county_name'.
The attribute table for 'samples' consists of:
county_name | surveyed | a | b |
A | 2005 | High | Low |
A | 2006 | High | Low |
A | 2006 | Low | Medium |
B | 2007 | Low | High |
B | 2008 | Medium | Low |
B | 2008 | Medium | Medium |
C | 2009 | High | Low |
When a user clicks in a relevant county in my map, I wish the pop-up to do the following:
Issue I have run into is how to get the range of surveyed years and access sampling categories. My current code will filter by 'county_name' and give a total count of samples within county but breaking down further into more detail has me stumped.
What I think will need to happen is create one expression and possibly repeat it several times for either each year or each sampling category and build responses into some HTML code for a custom table in the pop-up.
Any advice appreciated.
Regards,
Craig
If you can get the relevant samples returned as its own FeatureSet, you can do more with it. However, a single expression is only going to output one thing. You can get the different values into a big output string, but you can't color code them using HTML unless you're willing to write a separate expression for every single potential line in the output string, and unless you know the upper limit of potential values per county, this doesn't seem easy or worthwhile.
Still, getting it into a big string is possible:
// Get samples layer
var samples = FeatureSetByName($map, 'samples')
// Clicked county name
var county = $feature.county_name
// Filter samples by county
var county_samples = Filter(samples, "county_name = @county")
// Group filtered samples by year, count per category
var grouped_a = GroupBy(
county_samples,
['surveyed', 'a'],
{name: 'count_a', expression: 'a', statistic: 'COUNT'}
)
var grouped_b = GroupBy(
county_samples,
['surveyed', 'b'],
{name: 'count_b', expression: 'b', statistic: 'COUNT'}
)
We group a and b separately so that we get the true count of each, rather than the count of unique a/b combinations.
Using these grouped FeatureSets, we can then populate our string using a few loops.
var years = Distinct(count_samples, 'surveyed')
var out_str = ''
for(var y in years){
var the_year = y['surveyed']
out_str += `${the_year}, a: `
var filt_a = Filter(grouped_a, 'surveyed = @the_year')
for(var a in filt_a){
if(a['count'] > 0){
out_str += `${a['count']} ${a['a']}, `
}
}
out_str += 'b: '
var filt_b = Filter(grouped_b, 'surveyed = @the_year')
for(var b in filt_b){
if(b['count'] > 0){
out_str += `${b['count']} ${b['b']}, `
}
}
out_str += '\n'
}
return out_str
And after all that, we get this (I used static input data instead of a real FeatureSet, my output will likely differ form yours):
2005, a: 1 High, b: 1 Low,
2006, a: 1 High, 1 Low, b: 1 Low, 1 Medium,
2007, a: 1 Low, b: 1 High,
2008, a: 2 Medium, b: 1 Low, 1 Medium,
There's the nagging comma at the end of each line. You could deal with this by pushing the bits of text into a series of arrays and using Concatenate, but that's probably overly complicating things.
As a bit of an aside, I think what you're doing here is a bit much for a popup. A Dashboard could present this same data with nice formatting in a series of indicators and charts, and would probably be easier to do.
Hi @jcarlson,
Thank you for supplying code. Great explanation of code breakdown. I have tested code, but not getting output with my data in pop-up. Wondering if I could add you to a group to have a quick look. Interested in looking into dashboard options using code to visualize data as you suggested.
Regards,
Craig