ArcHoover

Example code showing how to page out all the records from a map service with more than 1000 records. Once fetched, stuff them into another feature service after doing some minor data conversions

by dbouwman

HTML

<script src="http://underscorejs.org/underscore-min.js"></script>
<script src="https://raw.github.com/douglascrockford/JSON-js/master/json2.js"></script>
<div id="console"></div>

JavaScript

//just some ugly hacking on the way to something slicker... ;-)

var url = 'http://wildfire.cr.usgs.gov/ArcGIS/rest/services/geomac_dyn/MapServer/8';
var destinationUrl = 'http://services.arcgis.com/NZyViox5cwNIE6Fd/arcgis/rest/services/harvester/FeatureServer/0';
$(function () {
    $.ajax(url + '/query', {
        data: {
            where: '1=1',
            returnIdsOnly: true,
            f: 'json'
        },
        dataType: 'jsonp',
        success: parseIds,
        error: function (jqXHR, status) {},
        complete: function (jqXHR, status) {}
    });
});
//array for our features
var featureSet  = [];
var totalBatches = 0;
function parseIds(data, status, jqXHR) {
    //break the id's into batches and make iterative requests
    //accumulating the features into an array
    var i, j, temparray, chunk = 10;//200
    totalBatches = 2; //Math.ceil(data.objectIds.length / chunk);
    $('#console').append('Queried Layer and got ' + data.objectIds.length + ' objectIds <br/> Will fetch in ' + totalBatches + ' batches of ' + chunk + '<br/>');
    for (i = 0, j = data.objectIds.length; i < 21; i += chunk) {
        temparray = data.objectIds.slice(i, i + chunk);
        getFeatures(temparray);
    }
}

function getFeatures(objectIdArray){
    $.ajax(url + '/query',{
        data:{
            objectIds:objectIdArray.join(),
            outFields:'report_dat,fire_name,start_date,cont_date,location,state,area_,area_meas',
            f:'json'
        },
        dataType:'jsonp',
        success: parseFeatures,
        error: function (jqXHR, status) {},
        complete: function (jqXHR, status) {}
    });
}
function parseFeatures(data,status,jxXHR){
     //$('#console').append('  fetched set of features  ' + data.features.length);
    //union the new features
    featureSet = _.union(featureSet, data.features);
    totalBatches -=1;
    //$('#console').append(' Batches left: ' + totalBatches + '<br/>');
    if(totalBatches === 0){
       ...