Version 4.12 of Geocortex Essentials introduced the capability to use Arcade Expressions.
In November 2019, the lead developer for GVH did a Tech Tip on how to implement them in Geocortex Essentials.
Below is a collection of Arcade expressions that you can try in your Essentials site.
Solution:
How Many:
How many of a certain feature are contained within this feature.
- Works best if you put the script on a polygon layer, then configure it with a point layer.
- The parameter sent to ‘countIntersects’ is the ID of the *mapservice* associated with the point layer
- The point layer must be a FeatureLayer
function countIntersects(layerId) {
var fs = FeatureSetById($map, layerId, ['*'], true);
return Count(Intersects($feature, fs));
}
countIntersects('WebMap-2')
How Many of Type:
Same as above, but takes a type field and a type string and only returns those results that match
function countIntersectsOfType(layerId, typeField, type) {
var fs = Intersects($feature, FeatureSetById($map, layerId, ['*'], true));
var i = 0;
for (var f in fs) {
if (f[typeField] == type) {
i++;
}
}
return i;
}
countIntersectsOfType('10','Category','911 Receiving')
Degrees to Direction:
Here’s a quick way to convert a value in degrees to a text direction. You could use this on a stream layer of vehicle assets for example…
var directions = ["N","NNE","NE","ENE","E","ESE", "SE", "SSE","S","SSW","SW","WSW","W","WNW","NW","NNW"];
function degreesToDirection(deg) {
var val = Floor((deg/22.5) + 0.5);
return directions[(val % 16)];
}
return degreesToDirection($feature.heading);
Find Within:
What area (country, community, census tract, etc…) is this feature contained within? Put this expression on any layer (point, line, or polygon) and it will attempt to tell you the name of the feature it is contained within.
- The polygon layer must be a FeatureLayer, the other can be anything
- The first parameter is the id of the *mapservice* associated with the polygon layer
- The second is the name field that should be used for display
-
function findWithin(layerId, nameField) {
var fs = FeatureSetById($map, layerId,[nameField],true);
var within = First(Intersects(fs, $feature));
if (!IsEmpty(within)) {
return within[nameField];
} else {
return "Unknown";
}
}
findWithin('6','NAME')
Linear Intersect By Name:
This expression will tell you about all the polygon features a linear feature travels through, sorted alphabetically with duplicate results removed. For example, listing all of the communities a particular road travels through.
To use you must have a polygon layer with a name field added as a FeatureLayer. The polyline layer can be either a MapService layer or FeatureLayer. The first parameter to ‘linearIntersect’ is the id of the *mapservice* associated with the polygon layer, and the second parameter is the name of the name field.
function Intersections(id, nameField) {
var fs = Intersects(FeatureSetById($map, id, [nameField], true), $feature);
var result = "";
var index = 0;
var intersect = [];
for (var feature in fs) {
intersect[index++] = feature[nameField];
}
var sorted = Sort(Distinct(intersect));
for (var i in sorted) {
result += sorted[i] + TextFormatting.NewLine;
}
return result;
}
return Intersections('6','NAME');
Clickable Attachment Links:
Adding this expression will populate the feature details with clickable links for each attachment associated with the feature. Try it with the Service Requests layer in LA County.
Note that the input to the ‘makeAttachmentLinks’ function needs to be the full URL of the layer used in your site, so change it if it is different…
function makeAttachmentLink(name, id, oid, url) {
return '<' + 'a ' + 'href="' + url + '/' + oid + '/attachments/' + id + '" target="_blank">' + name + '<' + '/' + 'a>';
}
function makeAttachmentLinks(serviceUrl, oidField) {
if (IsEmpty(oidField)) {
oidField = "OBJECTID";
}
var a = Attachments($feature);
var result = "";
for (var i in a) {
result += makeAttachmentLink(a[i].name, a[i].id, $feature[oidField], serviceUrl) + TextFormatting.NewLine;
}
if (IsEmpty(result)) {
result = "None"
}
return result;
}
makeAttachmentLinks("https://services.arcgis.com/p3UBboyC0NH1uCie/ArcGIS/rest/services/LA_Editing_AGOL/FeatureServer/0")
Count top feature types by name:
This is a more complicated version of ‘How Many’ that breaks things down by type. Use this to count how many of each type of thing occur in the area delineated by the feature in question. To use you need:
- A polygon layer (MapService OR FeatureService)
- A point layer with different types of features to count (must be FeatureService)
The expression will output the top types of features with their counts that occur within the polygon’s extent.
To use, modify the last line to fit your data:
- The first parameter to ‘topX’ should be the name of the field in the point layer that contains the type (often the same field used for symbology).
- The second should be the ID of the *mapservice* associated with the point feature layer
- The third is the number of results that you want to return
Examples: Top types of tree in a community, top recreational facilities in a census tract.
Warning: This is *very* slow if there are a large number of features in the point layer!
function sortDict(a,b) {
if (a['count'] < b['count']) return 1;
if (a['count'] > b['count']) return -1;
return 0;
}
function topX(typeField, layerId, numResults) {
var features = Intersects(FeatureSetById($map,layerId,[typeField],true),$feature);
var typeDict = {};
for (var feature in features) {
if (HasKey(typeDict, feature[typeField])) {
typeDict[Text(feature[typeField])]++;
} else {
typeDict[Text(feature[typeField])] = 1;
}
}
var i = 0;
var typeArray = [];
for (var key in typeDict) {
typeArray[i++] = {'type': key, 'count': typeDict[key]};
}
var top = Top(Sort(typeArray, sortDict), numResults);
var result = "";
for (var i in top) {
result += top[i].type + ": " + top[i].count + TextFormatting.NewLine;
}
if (IsEmpty(result)) {
result = "None found."
}
return result;
}
return topX('COMMON_NAME','1029',5);
Comments
0 comments
Article is closed for comments.