Select to view content in your preferred language

Arcade Expressions for Filtering Data in Map Viewer

1397
4
01-10-2023 08:09 AM
Status: Open
BrettFrahm
Frequent Contributor

Saw @RPGIS's idea for ArcGIS Enterprise and thought I'd add the idea to the ArcGIS Online too.

 

The ability to use Arcade expressions for filtering data in ArcGIS Online's Map Viewer would be extremely useful. We already have the ability to use arcade for symbology and pop-ups, but filtering has been left out.

A simple use-case for this would be checking to see if the CreateDate field is within the Current year. This is easily achievable with Arcade with IIF(Year(Today()) == Year($feature.CreateDate), 'YES', 'NO'). With this I could make it so my map is always showing relevant information, but instead I have to change the year on the filter manually every January. Lots of other use-cases become possible if Arcade Expressions are allowed in map viewer.

4 Comments
RPGIS
by

Hi @BrettFrahm,

I saw that you pushed my idea to Arcgis Online and wanted to let you know that there is a work around that I've implemented that works. I'm hoping that Esri can implement the idea of filtering using Arcade Expressions. The solution/work around that I currently use is a script that updates another field based on certain dates and then setting the filter widget to filter by those/other fields. I can share it if you would like. It can work for both hosted and enterprise feature services. 

TFlamont

Hi @RPGIS, I would be interested in that work around if you wouldn't mind sharing. I am working on a similar project that requires current date information. Thank you for your help!

GISAdminSHN

@RPGIS also interested!

RPGIS
by

Here is what I have used in the past.

 

import arcgis.gis
from arcgis.gis import GIS
from arcgis.features import FeatureLayer
import datetime
import os

gis = GIS('<Portal>',"<Username>", "<Password>")
#print("Logged into AGOL...")

source_outages_layer_item = gis.content.get("<itemID>") #Outages layer
source_layer = source_outages_layer_item.layers[0]
#print (source_layer)

Current = datetime.datetime.now()
#print (Current)
source_layer.calculate(where = "1=1",  calc_expression={"field": "Now",  "sqlExpression" : "CURRENT_TIMESTAMP()"})

# Update The Outage Status
source_layer.calculate(where = "Now > TimeField and Now < TimeField",  calc_expression={"field": "fieldname",  "sqlExpression" : "<Some Value {must match field data type }>"})
source_layer.calculate(where = "Now < TimeField",  calc_expression={"field": "fieldname",  "sqlExpression" : '<Some Value {must match field data type }>' })
source_layer.calculate(where = "Now > TimeField",  calc_expression={"field": "v",  "sqlExpression" : '<Some Value {must match field data type }>'})

# Update The Emergency Outage Timeframe
source_layer.calculate(where = 'SQL Clause',  calc_expression={"field": "fieldname",  "sqlExpression" : "Expression"})

#______________________Write Text File_____________________#

output_location = 'file location'

TxtFileName = ("PythonScriptLog.txt")
TxtFile_Output = os.path.join(output_location, TxtFileName)
    
f = open(TxtFile_Output, "w")
f.write(str(Current) + '\n' + '\n' + 'Successfully logged into AGO' + '\n' + 'Updated ' + 'Featureclass Name')
f.close()

 

Something that I also realized later on is that you can also treat the feature service like any feature if you use the full url and have editing capabilities on the feature.