Skip to content

Commit

Permalink
fix: page through results if there are more than the max result count
Browse files Browse the repository at this point in the history
  • Loading branch information
stdavis committed Sep 20, 2023
1 parent 8dd80d5 commit 1118567
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 24 deletions.
54 changes: 30 additions & 24 deletions src/components/Map.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import Map from '@arcgis/core/Map';
import { whenOnce } from '@arcgis/core/core/reactiveUtils';
import Polygon from '@arcgis/core/geometry/Polygon';
import { union } from '@arcgis/core/geometry/geometryEngine';
import FeatureLayer from '@arcgis/core/layers/FeatureLayer';
import GraphicsLayer from '@arcgis/core/layers/GraphicsLayer';
import VectorTileLayer from '@arcgis/core/layers/VectorTileLayer';
Expand All @@ -22,7 +20,11 @@ import appConfig from '../app-config';
import useMap from '../contexts/useMap';
import stateOfUtah from '../data/state-of-utah.json';
import Link from '../utah-design-system/Link';
import { getDefaultRenderer, hasDefaultSymbology } from '../utils';
import {
getDefaultRenderer,
hasDefaultSymbology,
queryFeatures,
} from '../utils';
import { getWhere } from './search-wizard/filters/utils';

const stateOfUtahPolygon = new Polygon(stateOfUtah);
Expand Down Expand Up @@ -298,6 +300,8 @@ export default function MapComponent() {
const featureServiceJson = await ky(
`${featureServiceUrl}?f=json`,
).json();

/** @type {import('@arcgis/core/layers/FeatureLayer').default} */
let featureLayer;
if (featureServiceJson.serviceItemId) {
// this could be a feature layer or group layer
Expand Down Expand Up @@ -368,32 +372,32 @@ export default function MapComponent() {
? `${featureLayer.objectIdField} IN (${ids.join(',')})`
: '1=0';

map.current.add(
featureLayer,
featureLayer.geometryType === 'polygon' ? 1 : null,
);

// I could't get a client-side query on the layer view to work
// since the map extent could be anything
const featureSet = await featureLayer.queryFeatures({
where: featureLayer.definitionExpression,
outFields: [
...layer[fieldNames.queryLayers.resultGridFields],
featureServiceJson.objectIdField,
],
returnGeometry: false,
});
const query = featureLayer.createQuery();
query.where = featureLayer.definitionExpression;
query.outFields = [
...layer[fieldNames.queryLayers.resultGridFields],
featureServiceJson.objectIdField,
];
query.returnGeometry = false;
const features = await queryFeatures(featureLayer, query);

const supportsExportValue = supportsExport(featureLayer.sourceJSON);
if (!supportsExportValue) {
console.warn('Layer does not support exporting', layer);
}

map.current.add(
featureLayer,
featureLayer.geometryType === 'polygon' ? 1 : null,
);

send('RESULT', {
result: {
...layer,
features: featureSet.features,
fields: featureSet.fields,
features,
fields: featureServiceJson.fields,
count,
supportedExportFormats:
featureLayer.sourceJSON.supportedExportFormats,
Expand Down Expand Up @@ -427,13 +431,15 @@ export default function MapComponent() {
);

// join extents
const extent = extents.reduce((totalExtent, extent) => {
if (!totalExtent) {
return extent;
}
const extent = extents
.filter((extent) => extent)
.reduce((totalExtent, extent) => {
if (!totalExtent) {
return extent;
}

return union([totalExtent, extent]);
}, null);
return totalExtent.union(extent);
}, null);

send('COMPLETE', { extent });

Expand Down
30 changes: 30 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,33 @@ export function getDefaultRenderer(geometryType) {

return renderer;
}

/**
* Query all features from a feature layer paging through results if necessary
*
* @param {import('@arcgis/core/layers/FeatureLayer').default} featureLayer
* @param {import('@arcgis/core/rest/support/Query').default} query
* @returns {Promise<import('@arcgis/core/Graphic').default[]>}
*/
export async function queryFeatures(featureLayer, query) {
const features = [];
let start = 0;
let finished = false;
query.maxRecordCountFactor = 4;
query.num =
featureLayer.capabilities.query.maxRecordCount + query.maxRecordCountFactor;
while (!finished) {
query.start = start;
const featureSet = await featureLayer.queryFeatures(query);

features.push(...featureSet.features);

if (featureSet.exceededTransferLimit) {
start += featureLayer.capabilities.query.maxRecordCount;
} else {
finished = true;
}
}

return features;
}

0 comments on commit 1118567

Please sign in to comment.