Query to Get All Features in the ArcGIS Maps SDK for JavaScript

When you’re querying a Feature Service in the ArcGIS Maps SDK for JavaScript, there’s typically a maximum number of features (records) that will be returned from any query – usually set to 1000. If you want to get all the features in the service, you’ll need to make multiple queries to the service. The best way to do this is with a recursive function that checks if the exceededTransferLimit property is true, and if so will continue making additional queries until complete.

One way to write that recursive function is like this

  const _getAllFeaturesRecursive = (layer, featuresSoFar) => {

    return layer
      .queryFeatures({
        start: featuresSoFar.length,
        num: layer.capabilities.query.maxRecordCount,
        outFields: ['*']
      })
      .then((results) => {
      // If "exceededTransferLimit" is true, then make another request (call 
      //  this same function) with a new "start" position. If not, return 
      // with the concatenated results.
        if (
          results.exceededTransferLimit &&
          results.exceededTransferLimit === true
        ) {
          return _getAllFeaturesRecursive(layer, [
            ...featuresSoFar,
            ...results.features
          ]);
        } else {
          return Promise.resolve([...featuresSoFar, ...results.features]);
        }
      });
  };

Wrapping that code in a function to kick it off and running it yields this example:

See the Pen Query to Get ALL the Features - ArcGIS Maps SDK for JavaScript v4 by Gavin Rehkemper (@gavinr) on CodePen.

Check out the full code here.

Update: My colleague Yann Cabon mentioned that using ES6 Generator Functions, if you have access to use them in your environment (caniuse), might be an even easier way to do this. I concur! Check out his Codepen example here.

Update 2022: I’ve wrapped this logic up into a small package that you can use in your own project, called Query All Features. Please try it out and let me know how it works for you: npm install query-all-features.

Get an email summary of my blog posts (four per year):

Join email newsletter

... or follow the blog here:

See Also