arcgis 0.1.3

Type-safe Rust SDK for the ArcGIS REST API with compile-time guarantees
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
//! Query builder for Feature Service queries.

use crate::{
    ArcGISGeometry, FeatureQueryParams, FeatureServiceClient, FeatureSet, GeometryType, LayerId,
    ObjectId, ResponseFormat, Result, SpatialRel,
};
use tracing::instrument;

/// A fluent builder for constructing and executing feature queries.
///
/// This provides an ergonomic API for building complex queries without
/// manually constructing [`FeatureQueryParams`].
///
/// # Example
///
/// ```no_run
/// use arcgis::{ArcGISClient, ApiKeyAuth, FeatureServiceClient, LayerId};
///
/// # async fn example() -> arcgis::Result<()> {
/// let auth = ApiKeyAuth::new("YOUR_API_KEY");
/// let client = ArcGISClient::new(auth);
/// let service = FeatureServiceClient::new("https://example.com/FeatureServer", &client);
///
/// // Simple query
/// let features = service
///     .query(LayerId::new(0))
///     .where_clause("POPULATION > 100000")
///     .out_fields(&["NAME", "POPULATION"])
///     .execute()
///     .await?;
///
/// // Auto-paginated query
/// let all_features = service
///     .query(LayerId::new(0))
///     .where_clause("STATE = 'CA'")
///     .execute_all()
///     .await?;
/// # Ok(())
/// # }
/// ```
pub struct QueryBuilder<'a> {
    client: &'a FeatureServiceClient<'a>,
    layer_id: LayerId,
    params: FeatureQueryParams,
}

impl<'a> QueryBuilder<'a> {
    /// Creates a new query builder.
    ///
    /// Typically you don't call this directly - use [`FeatureServiceClient::query`] instead.
    #[instrument(skip(client))]
    pub(crate) fn new(client: &'a FeatureServiceClient<'a>, layer_id: LayerId) -> Self {
        tracing::debug!(layer_id = %layer_id, "Creating QueryBuilder");
        Self {
            client,
            layer_id,
            params: FeatureQueryParams::default(),
        }
    }

    /// Sets the WHERE clause for the query.
    ///
    /// # Example
    /// ```no_run
    /// # use arcgis::{ArcGISClient, ApiKeyAuth, FeatureServiceClient, LayerId};
    /// # async fn example(service: &FeatureServiceClient<'_>) -> arcgis::Result<()> {
    /// let features = service
    ///     .query(LayerId::new(0))
    ///     .where_clause("POPULATION > 1000000")
    ///     .execute()
    ///     .await?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn where_clause(mut self, clause: impl Into<String>) -> Self {
        self.params.set_where_clause(clause.into());
        self
    }

    /// Sets the fields to return in the response.
    ///
    /// Pass `&["*"]` to return all fields.
    ///
    /// # Example
    /// ```no_run
    /// # use arcgis::{ArcGISClient, ApiKeyAuth, FeatureServiceClient, LayerId};
    /// # async fn example(service: &FeatureServiceClient<'_>) -> arcgis::Result<()> {
    /// let features = service
    ///     .query(LayerId::new(0))
    ///     .out_fields(&["NAME", "POPULATION", "CITY_ID"])
    ///     .execute()
    ///     .await?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn out_fields(mut self, fields: &[&str]) -> Self {
        self.params
            .set_out_fields(Some(fields.iter().map(|s| s.to_string()).collect()));
        self
    }

    /// Sets whether to return geometry with features.
    ///
    /// Default is `true`.
    pub fn return_geometry(mut self, return_geom: bool) -> Self {
        self.params.set_return_geometry(return_geom);
        self
    }

    /// Sets the response format.
    ///
    /// Default is [`ResponseFormat::Json`].
    pub fn format(mut self, format: ResponseFormat) -> Self {
        self.params.set_format(format);
        self
    }

    /// Requests Protocol Buffer (PBF) format for 3-5x performance improvement.
    ///
    /// PBF is a binary format that's more efficient than JSON for large datasets.
    /// Supported by ArcGIS Enterprise 10.7+ and ArcGIS Online.
    ///
    /// # Example
    /// ```no_run
    /// # use arcgis::{ArcGISClient, ApiKeyAuth, FeatureServiceClient, LayerId};
    /// # async fn example(service: &FeatureServiceClient<'_>) -> arcgis::Result<()> {
    /// // Get large dataset efficiently with PBF
    /// let features = service
    ///     .query(LayerId::new(0))
    ///     .where_clause("1=1")
    ///     .pbf()
    ///     .execute()
    ///     .await?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn pbf(self) -> Self {
        self.format(ResponseFormat::Pbf)
    }

    /// Requests JSON format (default).
    ///
    /// This is the standard Esri JSON format.
    pub fn json(self) -> Self {
        self.format(ResponseFormat::Json)
    }

    /// Requests GeoJSON format.
    ///
    /// GeoJSON is an open standard format for geographic data.
    pub fn geojson(self) -> Self {
        self.format(ResponseFormat::GeoJson)
    }

    /// Sets a spatial filter for the query.
    ///
    /// # Example
    /// ```no_run
    /// # use arcgis::{ArcGISClient, ApiKeyAuth, FeatureServiceClient, LayerId, ArcGISPoint, ArcGISGeometry, GeometryType, SpatialRel};
    /// # async fn example(service: &FeatureServiceClient<'_>) -> arcgis::Result<()> {
    /// let point = ArcGISPoint::new(-118.0, 34.0);
    /// let features = service
    ///     .query(LayerId::new(0))
    ///     .spatial_filter(ArcGISGeometry::Point(point), GeometryType::Point, SpatialRel::Intersects)
    ///     .execute()
    ///     .await?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn spatial_filter(
        mut self,
        geometry: ArcGISGeometry,
        geometry_type: GeometryType,
        spatial_rel: SpatialRel,
    ) -> Self {
        self.params.set_geometry(Some(geometry));
        self.params.set_geometry_type(Some(geometry_type));
        self.params.set_spatial_rel(Some(spatial_rel));
        self
    }

    /// Sets the maximum number of features to return.
    ///
    /// Used for pagination. If not set, the service default is used.
    pub fn limit(mut self, count: u32) -> Self {
        self.params.set_result_record_count(Some(count));
        self
    }

    /// Sets the offset for pagination.
    ///
    /// Skips the first `offset` features in the result set.
    pub fn offset(mut self, offset: u32) -> Self {
        self.params.set_result_offset(Some(offset));
        self
    }

    /// Queries specific features by object IDs.
    ///
    /// # Example
    /// ```no_run
    /// # use arcgis::{ArcGISClient, ApiKeyAuth, FeatureServiceClient, LayerId, ObjectId};
    /// # async fn example(service: &FeatureServiceClient<'_>) -> arcgis::Result<()> {
    /// let features = service
    ///     .query(LayerId::new(0))
    ///     .object_ids(&[ObjectId::new(1), ObjectId::new(2), ObjectId::new(3)])
    ///     .execute()
    ///     .await?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn object_ids(mut self, ids: &[ObjectId]) -> Self {
        self.params.set_object_ids(Some(ids.to_vec()));
        self
    }

    /// Returns only distinct values.
    pub fn distinct(mut self, distinct: bool) -> Self {
        self.params.set_return_distinct_values(Some(distinct));
        self
    }

    /// Returns only object IDs (no attributes or geometry).
    pub fn ids_only(mut self, ids_only: bool) -> Self {
        self.params.set_return_ids_only(Some(ids_only));
        self
    }

    /// Returns only a count of features (no features).
    pub fn count_only(mut self, count_only: bool) -> Self {
        self.params.set_return_count_only(Some(count_only));
        self
    }

    /// Sets the ORDER BY clause.
    ///
    /// # Example
    /// ```no_run
    /// # use arcgis::{ArcGISClient, ApiKeyAuth, FeatureServiceClient, LayerId};
    /// # async fn example(service: &FeatureServiceClient<'_>) -> arcgis::Result<()> {
    /// let features = service
    ///     .query(LayerId::new(0))
    ///     .order_by(&["POPULATION DESC", "NAME ASC"])
    ///     .execute()
    ///     .await?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn order_by(mut self, fields: &[&str]) -> Self {
        self.params
            .set_order_by_fields(Some(fields.iter().map(|s| s.to_string()).collect()));
        self
    }

    /// Sets the GROUP BY clause for statistics.
    ///
    /// # Example
    /// ```no_run
    /// # use arcgis::{ArcGISClient, ApiKeyAuth, FeatureServiceClient, LayerId, StatisticDefinition, StatisticType};
    /// # async fn example(service: &FeatureServiceClient<'_>) -> arcgis::Result<()> {
    /// let stats = service
    ///     .query(LayerId::new(0))
    ///     .statistics(vec![
    ///         StatisticDefinition::new(StatisticType::Avg, "POPULATION".to_string(), "avg_pop".to_string())
    ///     ])
    ///     .group_by(&["STATE"])
    ///     .execute()
    ///     .await?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn group_by(mut self, fields: &[&str]) -> Self {
        self.params
            .set_group_by_fields(Some(fields.iter().map(|s| s.to_string()).collect()));
        self
    }

    /// Sets statistics to calculate (aggregate query).
    ///
    /// When using statistics, the query can only include these additional parameters:
    /// `group_by`, `order_by`, `where_clause`, `time`, and `return_distinct_values`.
    ///
    /// # Example
    /// ```no_run
    /// # use arcgis::{ArcGISClient, ApiKeyAuth, FeatureServiceClient, LayerId, StatisticDefinition, StatisticType};
    /// # async fn example(service: &FeatureServiceClient<'_>) -> arcgis::Result<()> {
    /// let stats = service
    ///     .query(LayerId::new(0))
    ///     .statistics(vec![
    ///         StatisticDefinition::new(StatisticType::Count, "OBJECTID".to_string(), "total_count".to_string()),
    ///         StatisticDefinition::new(StatisticType::Sum, "POPULATION".to_string(), "total_population".to_string()),
    ///         StatisticDefinition::new(StatisticType::Avg, "AREA".to_string(), "avg_area".to_string()),
    ///     ])
    ///     .group_by(&["STATE"])
    ///     .execute()
    ///     .await?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn statistics(mut self, stats: Vec<crate::StatisticDefinition>) -> Self {
        self.params.set_out_statistics(Some(stats));
        self
    }

    /// Sets the HAVING clause for filtering aggregated results.
    ///
    /// Only valid when `statistics()` has been called. The HAVING clause filters
    /// the results after aggregation, similar to SQL's HAVING.
    ///
    /// # Example
    /// ```no_run
    /// # use arcgis::{ArcGISClient, ApiKeyAuth, FeatureServiceClient, LayerId, StatisticDefinition, StatisticType};
    /// # async fn example(service: &FeatureServiceClient<'_>) -> arcgis::Result<()> {
    /// let stats = service
    ///     .query(LayerId::new(0))
    ///     .statistics(vec![
    ///         StatisticDefinition::new(StatisticType::Count, "OBJECTID".to_string(), "count".to_string())
    ///     ])
    ///     .group_by(&["STATE"])
    ///     .having("count > 1000")
    ///     .execute()
    ///     .await?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn having(mut self, clause: impl Into<String>) -> Self {
        self.params.set_having(Some(clause.into()));
        self
    }

    /// Sets the output spatial reference WKID.
    ///
    /// # Example
    /// ```no_run
    /// # use arcgis::{ArcGISClient, ApiKeyAuth, FeatureServiceClient, LayerId};
    /// # async fn example(service: &FeatureServiceClient<'_>) -> arcgis::Result<()> {
    /// let features = service
    ///     .query(LayerId::new(0))
    ///     .out_sr(4326)  // WGS84
    ///     .execute()
    ///     .await?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn out_sr(mut self, wkid: i32) -> Self {
        self.params.set_out_sr(Some(wkid));
        self
    }

    /// Executes the query and returns a single page of results.
    ///
    /// This method sends a single request to the server and returns
    /// whatever fits in the response (subject to server limits).
    ///
    /// For queries that may return more results than fit in a single
    /// response, use [`execute_all`](Self::execute_all) instead.
    ///
    /// # Example
    /// ```no_run
    /// # use arcgis::{ArcGISClient, ApiKeyAuth, FeatureServiceClient, LayerId};
    /// # async fn example(service: &FeatureServiceClient<'_>) -> arcgis::Result<()> {
    /// let features = service
    ///     .query(LayerId::new(0))
    ///     .where_clause("STATE = 'CA'")
    ///     .limit(100)
    ///     .execute()
    ///     .await?;
    /// # Ok(())
    /// # }
    /// ```
    #[instrument(skip(self), fields(layer_id = %self.layer_id))]
    pub async fn execute(self) -> Result<FeatureSet> {
        tracing::debug!("Executing single-page query");
        self.client
            .query_with_params(self.layer_id, self.params)
            .await
    }

    /// Executes the query with automatic pagination, returning all results.
    ///
    /// This method automatically handles pagination by making multiple requests
    /// if necessary. It continues fetching until all matching features are retrieved
    /// or the server indicates no more results.
    ///
    /// # Performance Note
    ///
    /// This method may make many requests for large result sets. Consider using
    /// [`execute`](Self::execute) with manual pagination for very large queries.
    ///
    /// # Example
    /// ```no_run
    /// # use arcgis::{ArcGISClient, ApiKeyAuth, FeatureServiceClient, LayerId};
    /// # async fn example(service: &FeatureServiceClient<'_>) -> arcgis::Result<()> {
    /// // Automatically fetches all matching features across multiple requests
    /// let all_features = service
    ///     .query(LayerId::new(0))
    ///     .where_clause("POPULATION > 100000")
    ///     .execute_all()
    ///     .await?;
    ///
    /// println!("Retrieved {} total features", all_features.features().len());
    /// # Ok(())
    /// # }
    /// ```
    #[instrument(skip(self), fields(layer_id = %self.layer_id))]
    pub async fn execute_all(mut self) -> Result<FeatureSet> {
        tracing::debug!("Executing auto-paginated query");

        let mut all_features = Vec::new();
        let mut offset = 0u32;
        let page_size = (*self.params.result_record_count()).unwrap_or(1000);

        // Store geometry type from first response
        let mut geometry_type = None;

        loop {
            // Set pagination parameters
            self.params.set_result_offset(Some(offset));
            self.params.set_result_record_count(Some(page_size));

            tracing::debug!(
                offset = offset,
                page_size = page_size,
                "Fetching page of results"
            );

            // Execute query for this page
            let mut page = self
                .client
                .query_with_params(self.layer_id, self.params.clone())
                .await?;

            // Capture geometry type from first response
            if geometry_type.is_none() {
                geometry_type = *page.geometry_type();
            }

            let feature_count = page.features().len();
            tracing::debug!(
                feature_count = feature_count,
                exceeded_limit = page.exceeded_transfer_limit(),
                "Page retrieved"
            );

            // Add features to our collection
            all_features.append(page.features_mut());

            // Check if we're done
            if feature_count == 0 || !*page.exceeded_transfer_limit() {
                tracing::debug!(
                    total_features = all_features.len(),
                    "Auto-pagination complete"
                );
                break;
            }

            // Move to next page
            offset += page_size;
        }

        Ok(FeatureSet::new(geometry_type, all_features, None, false))
    }
}