Hi, I want to build an arcade expression that will count the number of points in a polygon layer.
So for example I have a polygon layer called "Data Zones" and a point layer called "Bus Stops". I want to count the number of Bus Stops that are in each Data Zone. Both Layers will be in the same web map.
I believe I have to use the Intersects and Count functions and the expression has to be in the Polygon layer , I just can't can't get an expression to successfully work.
Thanks in advance
I managed to get this expression to work but don't understand how the 'bus' and 'Bus' text works?!
Hi C McDonald ,
Let me try to provide some explanation on what you have created:
Later on you might want to extend the expression and list some details of each bus facility found. This is also possible with Arcade.
Thanks Xander Bakker, that's helpful. I think I understand a bit more clearly now.
With reference to your last comment I actually have a field in the Bus Facilities Layer called which lists the type. There are 3 types of entries: Bus Stop, Bus Shelter and Unknown.
So in theory I could generate a Count for each of these?
Hi C McDonald ,
That is indeed something you could do, using the GroupBy function. Have a look at the expression below:
// replace "FacilityType" with the real name of the field!
// get the bus layer with only the field that contains the facility type
var bus_fs = FeatureSetByName($map, "Bus Facilities", ["FacilityType"], False);
// Intersect the bus featureset with the current data zone ($feature)
var bus_facilities = Intersects(bus_fs, $feature);
// count al bus facilities
var cnt = Count(bus_facilities);
// validate if there are bus facilities
var result = "";
if (cnt > 0) {
// there are bus facilities found
// apply groupby to create a count per facility type
var stats = GroupBy(bus_facilities, "FacilityType", [{name: "FacilityCount", expression: "FacilityType", statistic: "COUNT" }]);
// loop through the statistics to create the result
result = cnt + " bus facilities found:";
for (var stat in stats) {
result += TextFormatting.NewLine + " - " + stat.FacilityType + " (" + stat.FacilityCount + ")";
}
} else {
// there are NO bus facilities found
result = "No bus facilities found";
}
return result;
Not sure if this will work for you, since I don't have the data to test the expression against.
Unfortunately I was unable to get this code to work despite substituting the field name however I found a way that seems to work.
var busstops = Filter(FeatureSetByName($map,"Bus Facilities"),"STOP_SHELT = 'Bus Stop'")
var countBusStops = Count(Intersects(busstops,$feature))
return countBusStops
Hi C McDonald ,
I see from your expression that the field is called "STOP_SHELT". This might require some small adjustments in the expression like shown below. A field that contains an underscore sometimes causes some problems when retrieving the information like $feature.Field_Name and it is better to use $feature["Field_Name"] .
// using "STOP_SHELT" as name of the field in the code below
// get the bus layer with only the field that contains the facility type
var bus_fs = FeatureSetByName($map, "Bus Facilities", ["STOP_SHELT"], False);
// Intersect the bus featureset with the current data zone ($feature)
var bus_facilities = Intersects(bus_fs, $feature);
// count al bus facilities
var cnt = Count(bus_facilities);
// validate if there are bus facilities
var result = "";
if (cnt > 0) {
// there are bus facilities found
// apply groupby to create a count per facility type
var stats = GroupBy(bus_facilities, "STOP_SHELT", [{name: "FacilityCount", expression: "STOP_SHELT", statistic: "COUNT" }]);
// loop through the statistics to create the result
result = cnt + " bus facilities found:";
for (var stat in stats) {
result += TextFormatting.NewLine + " - " + stat["STOP_SHELT"] + " (" + stat["FacilityCount"] + ")";
}
} else {
// there are NO bus facilities found
result = "No bus facilities found";
}
return result;
Your code will not do a count per bus facility type but will just return the count of the bus stop. If that is what you want, don't use the expression above, but in a previous post you mentioned that you wanted to get a count per facility type and that is why I provided this expression.
You you want to get the count per facility type and run into problems, it would be a possibility to create a group, share the web map and data with that group and invite me to that group using my ArcGIS Online username: "xbakker.spx".