Select to view content in your preferred language

Query features in the View on the client when feature is off screen

781
3
Jump to solution
02-28-2023 12:38 PM
GregoryBologna
Frequent Contributor

I have a buffer featureLayer query that does not return features if part of the buffered area in the view is off screen. How can I get buffer results in this situation? Thanks.

What is meant by "available to or visible" here in the documentation?  I always want to return available regardless of visibile.

To query features/graphics available to or visible in the View on the client rather than making a server-side query, you must use the FeatureLayerView.queryFeatures() method.

https://developers.arcgis.com/javascript/latest/api-reference/esri-layers-FeatureLayer.html#queryFea...

Here's my procedural code fragment. 

 

Step 1. Create queryFeatureLayer 

    const queryFeatureLayer = new FeatureLayer({
      url: url,
      geometryType: 'polygon',
      title: 'myfeatureLayer',
      spatialReference: {
        wkid: 102659,
      },
      outFields: [
        'OBJECTID',
	and others...
      ]
    });

Step 2. Wait for global object mapLayerView to be ready then run queryLayerView()

    reactiveUtils
     .whenOnce(() => !mapLayerView.updating)
     .then(() => {
       queryLayerView();

Step 3. Execute the queryLayerView

const queryObject = mapLayerView.createQuery();
queryObject.geometry = sketchGeometryMode();
queryObject.units = unitType;
queryObject.distance = bufferSize;
queryObject.spatialRelationship = 'intersects';
queryObject.outSpatialReference = view.spatialReference;
queryObject.returnQueryGeometry = true;
queryObject.returnDistinctValues = true;
queryObject.outFields = ['*'];

Step 4. Get results

return mapLayerView
  .queryFeatures(queryObject)
  .then((layerView) => { ... }

 

 

 

 

0 Kudos
1 Solution

Accepted Solutions
GregoryBologna
Frequent Contributor

Thank you. Your answer indeed solves the drawback to client-side buffering. I made the change and it works well, performance is close to client-side with some minor noticeable delay in the buffer polygon. I also had enable returnGeometry and remove returnDistinctValues because it's not supported with it. I am curious to know why "queryFeatures" throws an error "TypeError: Cannot read properties of undefined (reading 'featureResult')" after several continuous queries are made. Basically, after a few queries, the response from ArgGIS server is empty and results in the error above. The error doesn't prevent subsequent queries, but I decided to handle the error by detecting it and continuing. Has to be a better way.

View solution in original post

0 Kudos
3 Replies
Sage_Wall
Esri Contributor

Hi @GregoryBologna ,

The LayerView is responsible for rendering the layer in your map view and it contains only the necessary information draw the symbols. The LayerView likely doesn't contain all the features in your layer, but only a subset of the features based on what features are currently visible in the map view.  When you are querying the layer view and your buffer goes beyond the view's extent those features aren't guaranteed to be loaded into the layer view.

You would want to do a server-side layer query to return the results properly.  The layer query will return all the results that satisfy the query parameters regardless of whether they are visible in your browser.  Simply switching to querying your 'queryFeatureLayer' instead of your 'mapLayerView' should give you the results your expecting.

 

 

 

Step 1. Create queryFeatureLayer 

    const queryFeatureLayer = new FeatureLayer({
      url: url,
      geometryType: 'polygon',
      title: 'myfeatureLayer',
      spatialReference: {
        wkid: 102659,
      },
      outFields: [
        'OBJECTID',
	and others...
      ]
    });

Step 2. Execute the query

const queryObject = queryFeatureLayer.createQuery();
queryObject.geometry = sketchGeometryMode();
queryObject.units = unitType;
queryObject.distance = bufferSize;
queryObject.spatialRelationship = 'intersects';
queryObject.outSpatialReference = view.spatialReference;
queryObject.returnQueryGeometry = true;
queryObject.returnDistinctValues = true;
queryObject.outFields = ['*'];

queryFeatureLayer
  .queryFeatures(queryObject)
  .then((featureSet) => { ... }

 

 

 

0 Kudos
GregoryBologna
Frequent Contributor

Thank you. Your answer indeed solves the drawback to client-side buffering. I made the change and it works well, performance is close to client-side with some minor noticeable delay in the buffer polygon. I also had enable returnGeometry and remove returnDistinctValues because it's not supported with it. I am curious to know why "queryFeatures" throws an error "TypeError: Cannot read properties of undefined (reading 'featureResult')" after several continuous queries are made. Basically, after a few queries, the response from ArgGIS server is empty and results in the error above. The error doesn't prevent subsequent queries, but I decided to handle the error by detecting it and continuing. Has to be a better way.

0 Kudos
Sage_Wall
Esri Contributor

I'm glad it's mostly working. The error is unexpected. Do you have access to the ArcGIS Server logs? There might be something going on server side with data access. 

0 Kudos