Add spatial filter to layer in webmap or in WABde

1960
1
11-22-2019 09:20 AM
JoeRogan
New Contributor III

Hi, we are trying to avoid making copies of data and use ArcGIS REST services from other departments in our webmaps and apps.  Our client wants to only show some features within a certain extent for one of these layers.

The easiest to implement solution for this applies only to hosted feature layers: 

ArcGIS Online: Filter by Polygon Feature 

Is there an easy solution to apply a spatial filter to a regular feature layer added to a webmap?  If not, has anyone applied a quick hack to a Web AppBuilder (Dev Edition) app to apply a spatial filter as the map is loaded?  Thanks in advance!

We are using Server/Portal 10.7.1

0 Kudos
1 Reply
JoeRogan
New Contributor III

I think I've figured this out, although there might be a better spot to stick the hack.

In MapManager.js deep into the _show2DWebMap function:

              if(layerObject){
                // START CUSTOM CODE
                if (layerObject.layerId == 28)
                {
                  console.log(layerObject);
                  var extent = new Extent({
                    "xmin":-180,
                    "xmax":180,
                    "ymin":70,
                    "ymax":90,
                    "spatialReference":{"wkid":4326}
                  });
                  var qTask = new QueryTask(layerObject.url);
                  var query = new Query();
                  query.geometry = extent;
                  query.where = "1=1";
                  qTask.executeForIds(query, lang.hitch(thisfunction(results){
                    var whereClause = layerObject.objectIdField + " IN (" + results.join() + ")";
                    layerObject.setDefinitionExpression(whereClause);
                  }));
                }
                // END CUSTOM CODE
                lang.setObject("_wabProperties.originalRefreshinterval", layerObject.refreshInterval, layerObject);
              }
I just did this for a different layer than what I intend for now, so I will change the initial custom if statement and geometry variable to match my desired layer and spatial filter requirements.
Modified Robert Scheitlin's solution at:
Thanks!