Skip to content

Commit 21f18d1

Browse files
authored
Merge pull request #78 from Esri/cs/1545-update-query-stats
Update the sample "Query Table Statistics"
2 parents dca03f4 + 8ea0368 commit 21f18d1

File tree

4 files changed

+243
-77
lines changed

4 files changed

+243
-77
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Query table statistics
2+
3+
Query a table to get aggregated statistics back for a specific field.
4+
5+
![Image of query table statistics](query_table_statistics.png)
6+
7+
## Use case
8+
9+
For example, a county boundaries table with population information can be queried to return aggregated results for the total, average, maximum, and minimum population, rather than downloading the values for every county and calculating the statistics manually.
10+
11+
## How to use the sample
12+
13+
Pan and zoom to define the extent for the query. In the Settings panel, use the 'Only cities in current extent' checkbox to control whether the query includes only features in the visible extent, or use the 'Only cities greater than 5M' checkbox to filter the results to only those cities with a population greater than 5 million people. Tap the 'Get statistics' to perform the query. The query will return population-based statistics from the combined results of all features matching the query criteria.
14+
15+
## How it works
16+
17+
1. Create a `ServiceFeatureTable` with a URL to the feature service.
18+
2. Create `StatisticsQueryParameters`, and `StatisticDefinition` objects, and add to the parameters.
19+
3. Execute `queryStatistics` on the `ServiceFeatureTable`. Depending on the state of the two checkboxes, additional parameters are set.
20+
4. Display each `StatisticRecord` in the first returned `QueryStatisticsResult`.
21+
22+
## Relevant API
23+
24+
* QueryParameters
25+
* ServiceFeatureTable
26+
* StatisticDefinition
27+
* StatisticRecord
28+
* StatisticsQueryParameters
29+
* StatisticsQueryResult
30+
* StatisticType
31+
32+
## Tags
33+
34+
analysis, average, bounding geometry, filter, intersect, maximum, mean, minimum, query, spatial query, standard deviation, statistics, sum, variance

lib/samples/query_table_statistics/README.metadata.json

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,44 @@
22
"category": "Search and Query",
33
"description": "Query a table to get aggregated statistics back for a specific field.",
44
"ignore": false,
5-
"images": [],
6-
"keywords": [],
5+
"images": [
6+
"query_table_statistics.png"
7+
],
8+
"keywords": [
9+
"analysis",
10+
"average",
11+
"bounding geometry",
12+
"filter",
13+
"intersect",
14+
"maximum",
15+
"mean",
16+
"minimum",
17+
"query",
18+
"spatial query",
19+
"standard deviation",
20+
"statistics",
21+
"sum",
22+
"variance",
23+
"QueryParameters",
24+
"ServiceFeatureTable",
25+
"StatisticDefinition",
26+
"StatisticRecord",
27+
"StatisticType",
28+
"StatisticsQueryParameters",
29+
"StatisticsQueryResult"
30+
],
731
"redirect_from": [],
8-
"relevant_apis": [],
9-
"snippets": [],
32+
"relevant_apis": [
33+
"QueryParameters",
34+
"ServiceFeatureTable",
35+
"StatisticDefinition",
36+
"StatisticRecord",
37+
"StatisticType",
38+
"StatisticsQueryParameters",
39+
"StatisticsQueryResult"
40+
],
41+
"snippets": [
42+
"query_table_statistics_sample.dart"
43+
],
1044
"title": "Query table statistics"
1145
}
233 KB
Loading

lib/samples/query_table_statistics/query_table_statistics_sample.dart

Lines changed: 171 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
// limitations under the License.
1515
//
1616

17+
import 'dart:math';
18+
1719
import 'package:arcgis_maps/arcgis_maps.dart';
1820
import 'package:flutter/material.dart';
1921

@@ -29,114 +31,210 @@ class QueryTableStatisticsSample extends StatefulWidget {
2931

3032
class _QueryTableStatisticsSampleState extends State<QueryTableStatisticsSample>
3133
with SampleStateSupport {
34+
// Create a controller for the map view.
3235
final _mapViewController = ArcGISMapView.createController();
33-
final _serviceFeatureTable = ServiceFeatureTable.withUri(Uri.parse(
34-
'https://sampleserver6.arcgisonline.com/arcgis/rest/services/SampleWorldCities/MapServer/0'));
35-
bool _onlyCitiesInCurrentExtent = true;
36-
bool _onlyCitiesGreaterThan5M = true;
37-
final _statisticDefinitions = List<StatisticDefinition>.empty(growable: true);
38-
39-
@override
40-
void initState() {
41-
super.initState();
42-
43-
for (final type in StatisticType.values) {
44-
_statisticDefinitions.add(
45-
StatisticDefinition(onFieldName: 'POP', statisticType: type),
46-
);
47-
}
48-
49-
final map = ArcGISMap.withBasemapStyle(BasemapStyle.arcGISTopographic);
50-
final featureLayer = FeatureLayer.withFeatureTable(_serviceFeatureTable);
51-
map.operationalLayers.add(featureLayer);
52-
53-
_mapViewController.arcGISMap = map;
54-
}
36+
// Create a ServiceFeatureTable from a URL.
37+
final _serviceFeatureTable = ServiceFeatureTable.withUri(
38+
Uri.parse(
39+
'https://sampleserver6.arcgisonline.com/arcgis/rest/services/SampleWorldCities/MapServer/0',
40+
),
41+
);
42+
// A flag for when the map view is ready and controls can be used.
43+
var _ready = false;
44+
// A flag for whether to limit the query to cities within the current extent.
45+
var _onlyCitiesInCurrentExtent = true;
46+
// A flag for whether to limit the query to cities with population greater than 5 million.
47+
var _onlyCitiesGreaterThan5M = true;
48+
// A list of statistic definitions to apply to the query.
49+
final _statisticDefinitions = <StatisticDefinition>[];
50+
// A flag to display the query settings.
51+
var _settingsVisible = false;
5552

5653
@override
5754
Widget build(BuildContext context) {
5855
return Scaffold(
59-
body: Stack(
60-
alignment: Alignment.center,
61-
children: [
62-
ArcGISMapView(
63-
controllerProvider: () => _mapViewController,
64-
),
65-
Positioned(
66-
width: 350,
67-
height: 180,
68-
bottom: 60,
69-
child: DecoratedBox(
70-
decoration: BoxDecoration(
71-
color: Colors.white.withOpacity(0.8),
72-
),
73-
child: Column(
74-
mainAxisAlignment: MainAxisAlignment.center,
75-
children: [
76-
SwitchListTile(
77-
title: const Text('Only cities in current extent'),
78-
value: _onlyCitiesInCurrentExtent,
79-
onChanged: (value) {
80-
setState(() => _onlyCitiesInCurrentExtent = value);
81-
},
82-
),
83-
SwitchListTile(
84-
title: const Text('Only cities greater than 5M'),
85-
value: _onlyCitiesGreaterThan5M,
86-
onChanged: (value) {
87-
setState(() => _onlyCitiesGreaterThan5M = value);
88-
},
56+
body: SafeArea(
57+
top: false,
58+
child: Stack(
59+
children: [
60+
Column(
61+
children: [
62+
Expanded(
63+
// Add a map view to the widget tree and set a controller.
64+
child: ArcGISMapView(
65+
controllerProvider: () => _mapViewController,
66+
onMapViewReady: onMapViewReady,
8967
),
90-
TextButton(
91-
onPressed: queryStatistics,
92-
child: const Text(
93-
'Get statistics',
68+
),
69+
Row(
70+
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
71+
children: [
72+
// A button to show the Settings bottom sheet.
73+
ElevatedButton(
74+
onPressed: () => setState(() => _settingsVisible = true),
75+
child: const Text('Settings'),
9476
),
95-
),
96-
],
77+
// A button to calculate the statistics.
78+
ElevatedButton(
79+
onPressed: queryStatistics,
80+
child: const Text('Get statistics'),
81+
),
82+
],
83+
),
84+
],
85+
),
86+
// Display a progress indicator and prevent interaction until state is ready.
87+
Visibility(
88+
visible: !_ready,
89+
child: SizedBox.expand(
90+
child: Container(
91+
color: Colors.white30,
92+
child: const Center(child: CircularProgressIndicator()),
93+
),
9794
),
9895
),
99-
),
100-
],
96+
],
97+
),
10198
),
99+
bottomSheet: _settingsVisible ? querySettings(context) : null,
102100
);
103101
}
104102

103+
// The build method for the query options shown in the bottom sheet.
104+
Widget querySettings(BuildContext context) {
105+
return Container(
106+
color: Colors.white,
107+
padding: EdgeInsets.fromLTRB(
108+
20.0,
109+
0.0,
110+
20.0,
111+
max(
112+
20.0,
113+
View.of(context).viewPadding.bottom /
114+
View.of(context).devicePixelRatio,
115+
),
116+
),
117+
child: Container(
118+
color: Colors.white,
119+
child: Column(
120+
mainAxisSize: MainAxisSize.min,
121+
children: [
122+
Row(
123+
children: [
124+
Text(
125+
'Query Settings',
126+
style: Theme.of(context).textTheme.titleMedium,
127+
),
128+
const Spacer(),
129+
IconButton(
130+
icon: const Icon(Icons.close),
131+
onPressed: () => setState(() => _settingsVisible = false),
132+
),
133+
],
134+
),
135+
Row(
136+
children: [
137+
Checkbox(
138+
value: _onlyCitiesInCurrentExtent,
139+
onChanged: (value) {
140+
setState(() => _onlyCitiesInCurrentExtent = value!);
141+
},
142+
),
143+
const Text('Only cities in current extent'),
144+
],
145+
),
146+
Row(
147+
children: [
148+
Checkbox(
149+
value: _onlyCitiesGreaterThan5M,
150+
onChanged: (value) {
151+
setState(() => _onlyCitiesGreaterThan5M = value!);
152+
},
153+
),
154+
const Text('Only cities greater than 5M'),
155+
],
156+
),
157+
],
158+
),
159+
),
160+
);
161+
}
162+
163+
// Called when the map view is ready.
164+
void onMapViewReady() {
165+
// Add the statistic definitions for the 'POP' (Population) field.
166+
for (final type in StatisticType.values) {
167+
_statisticDefinitions.add(
168+
StatisticDefinition(
169+
onFieldName: 'POP',
170+
statisticType: type,
171+
),
172+
);
173+
}
174+
// Create a map with a topographic basemap.
175+
final map = ArcGISMap.withBasemapStyle(
176+
BasemapStyle.arcGISTopographic,
177+
);
178+
// Create a feature layer from the service feature table.
179+
final featureLayer = FeatureLayer.withFeatureTable(
180+
_serviceFeatureTable,
181+
);
182+
// Add the feature layer to the map.
183+
map.operationalLayers.add(
184+
featureLayer,
185+
);
186+
// Set the map to the map view.
187+
_mapViewController.arcGISMap = map;
188+
setState(() => _ready = true);
189+
}
190+
191+
// Query statistics from the service feature table.
105192
void queryStatistics() async {
106-
final statisticsQueryParameters =
107-
StatisticsQueryParameters(statisticDefinitions: _statisticDefinitions);
193+
// Create a statistics query parameters object.
194+
final statisticsQueryParameters = StatisticsQueryParameters(
195+
statisticDefinitions: _statisticDefinitions,
196+
);
108197

198+
// Set the geometry and spatial relationship if the flag is true.
109199
if (_onlyCitiesInCurrentExtent) {
110200
statisticsQueryParameters.geometry = _mapViewController.visibleArea;
111-
112201
statisticsQueryParameters.spatialRelationship =
113202
SpatialRelationship.intersects;
114203
}
115-
204+
// Set the where clause if the flag is true.
116205
if (_onlyCitiesGreaterThan5M) {
117206
statisticsQueryParameters.whereClause = 'POP_RANK = 1';
118207
}
119-
208+
// Query the statistics.
120209
final statisticsQueryResult = await _serviceFeatureTable.queryStatistics(
121-
statisticsQueryParameters: statisticsQueryParameters);
122-
123-
final statistics = StringBuffer();
210+
statisticsQueryParameters: statisticsQueryParameters,
211+
);
124212

213+
// Prepare the statistics results for display.
214+
final statistics = [];
125215
final records = statisticsQueryResult.statisticRecords();
126216
for (final record in records) {
127217
record.statistics.forEach((key, value) {
128-
statistics.write('\n$key: $value');
218+
final displayName =
219+
key.toLowerCase() == 'count_pop' ? 'CITY_COUNT' : key;
220+
final displayValue = key.toLowerCase() == 'count_pop'
221+
? value.toStringAsFixed(0)
222+
: value.toStringAsFixed(2);
223+
statistics.add('[$displayName] $displayValue');
129224
});
130225
}
131-
226+
// Display the statistics in a dialog.
132227
if (mounted) {
133228
showDialog(
134229
context: context,
135-
builder: (BuildContext context) {
230+
builder: (context) {
136231
return AlertDialog(
137-
title: const Text('Statistical Query Results'),
232+
title: Text(
233+
'Statistical Query Results',
234+
style: Theme.of(context).textTheme.titleMedium,
235+
),
138236
content: Text(
139-
statistics.toString(),
237+
statistics.join('\n').toString(),
140238
),
141239
);
142240
},

0 commit comments

Comments
 (0)