Search code examples
arcgisarcgis-js-apispatial-query

ARCGIS: Hide polygons resulting from a spatial query


I have a bunch of web layers and want to hide all polygons that are intersect with a given geometry/other layer.

I filter these intersecting polygons using spatial query, but then I don't know how to hide them. I was thinking that can manipulate renderer of resulting polygons, something like: hide(), opacity = 0, visible=false... Is this right approach, or I need first to query polygons that are not intersecting and then add results to a new layer and render only them? In such case what should be query.spatialRelationship?

Here is my query:

    view.whenLayerView(layer).then(function(layerView){
       var query = layer.createQuery();
       query.geometry = new Extent({ 
         xmin: 6902682.7633,
         ymin: -3519872.5095,
         xmax: 11221869.7958,
         ymax: -2276864.0272,
         spatialReference: 102100
       });
       query.spatialRelationship = "intersects";    

       layer.queryFeatures(query).then(function(results){
         for (var index in results.features) { 
           //hide as manipulate its rendering    
         }    
        // or something like layerView.highlight(results.features)    
       })
    });

Solution

  • If you don't want to display the features at all, you can use a QueryTask to retrieve only the features that intersects the extent from the MapService. Then you could create a FeatureLayer with the results.

    require(["esri/tasks/QueryTask", "esri/tasks/support/Query", "esri/geometry/Extent", "esri/layers/FeatureLayer"], function(QueryTask, Query, Extent, FeatureLayer){
      var layerUrl = " ... "; // Represents the REST endpoint for your layer
      var queryTask = new QueryTask({
        url: layerUrl 
      });
      var query = new Query();
      query.returnGeometry = true;
      query.outFields = ["*"];
      query.geometry = new Extent({ 
         xmin: 6902682.7633,
         ymin: -3519872.5095,
         xmax: 11221869.7958,
         ymax: -2276864.0272,
         spatialReference: 102100
      });
      query.spatialRelationship = "intersects";
    
      // When resolved, create the featureLayer with the results
      queryTask.execute(query).then(function(results){
        var layer = new FeatureLayer({
          source: results.features
        });
      });
    });
    

    This answer might be the best from a performance stand of view because the intersection is made on the server side and the client won't have to download features that are not needed.