Last active
February 12, 2022 00:21
-
-
Save raykendo/4aed0da95be65a365569 to your computer and use it in GitHub Desktop.
ArcGIS JSAPI Hack - Getting around the 1000 results limit for queries
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** globals define */ | |
define([ | |
"dojo/_base/declare", | |
"dojo/Deferred", | |
"esri/tasks/query", | |
"esri/tasks/QueryTask", | |
"esri/request" | |
], function ( | |
declare, Deferred, Query, QueryTask, esriRequest | |
) { | |
return declare([QueryTask], { | |
constructor: function (url) { | |
this.inherited(arguments); | |
esriRequest({ | |
url: url, | |
handleAs: "json", | |
content: {f: "json"} | |
}).then(function (response) { | |
var i, il = response.fields.length; | |
for (i = 0; i < il; i++) { | |
if (response.fields[i].type === "esriFieldTypeOID") { | |
this.set("oidFieldName", response.fields[i].name); | |
break; | |
} | |
} | |
}.bind(this)); | |
}, | |
/* Used to search for features that return large amounts of data. | |
* @method bigExecute | |
* @param {esri/tasks/query} params - query parameters | |
* @returns {dojo/Deferred} - a deferred object promising a FeatureSet | |
*/ | |
bigExecute: function (params) { | |
var queryCopy = new Query(), q; | |
for (q in params) { | |
if (params.hasOwnProperty(q)) { | |
if (q === "outFields") { | |
queryCopy.outFields = [this.oidFieldName]; | |
} else { | |
queryCopy[q] = params[q]; | |
} | |
} | |
} | |
return this.executeForIds(queryCopy).then(function (oids) { | |
return this._getAllSearchResults(params, oids, 0); | |
}); | |
}, | |
_getAllSearchResults: function (query, oidList, startIndex, prevResults, def) { | |
var iMax = Math.min(startIndex + 1000, oidList.length); | |
var objectIds = oidList.slice(startIndex, iMax); | |
def = def || new Deferred(); | |
query.objectIds = objectIds; | |
this.execute(query).then( | |
function (featureSet) { | |
// load featureSet if done. | |
if (prevResults === undefined) { | |
prevResults = featureSet; | |
} else { | |
prevResults.features = prevResults.features.concat(featureSet.features); | |
} | |
// if not all results have been collected... | |
if (iMax < oidList.length) { | |
// request the next batch of results | |
this._getAllSearchResults(query, oidList, iMax, prevResults, def); | |
} else { | |
// all results accounted for. | |
def.resolve(prevResults); | |
} | |
}.bind(this) | |
); | |
return def; | |
} | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment