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
//! Image service client implementation.

use crate::{ArcGISClient, ArcGISGeometry, Result};
use tracing::instrument;

use super::types::{
    ExportImageParameters, ExportImageResult, HistogramParameters, HistogramResult,
    IdentifyParameters, ImageIdentifyResult, RasterInfo, SampleParameters, SampleResult,
};

/// Client for interacting with ArcGIS Image Services (ImageServer).
///
/// Image services provide access to raster datasets with dynamic rendering,
/// analysis, and processing capabilities.
///
/// # Example
///
/// ```no_run
/// use arcgis::{ApiKeyAuth, ArcGISClient, ImageServiceClient, ArcGISGeometry, ArcGISPoint};
///
/// # async fn example() -> arcgis::Result<()> {
/// let auth = ApiKeyAuth::new("YOUR_API_KEY");
/// let client = ArcGISClient::new(auth);
/// let image_service = ImageServiceClient::new(
///     "https://sampleserver6.arcgisonline.com/arcgis/rest/services/NLCDLandCover2001/ImageServer",
///     &client
/// );
///
/// // Identify pixel value at a point
/// let geometry = ArcGISGeometry::Point(ArcGISPoint::new(-120.0, 40.0));
/// let result = image_service.identify(&geometry).await?;
/// # Ok(())
/// # }
/// ```
#[derive(Clone)]
pub struct ImageServiceClient<'a> {
    /// Base URL of the image service.
    url: String,

    /// Reference to the ArcGIS client.
    client: &'a ArcGISClient,
}

impl<'a> ImageServiceClient<'a> {
    /// Creates a new image service client.
    ///
    /// # Arguments
    ///
    /// * `url` - Base URL of the image service (e.g., `https://server/arcgis/rest/services/ImageService/ImageServer`)
    /// * `client` - Reference to an [`ArcGISClient`] for making requests
    ///
    /// # Example
    ///
    /// ```no_run
    /// use arcgis::{ApiKeyAuth, ArcGISClient, ImageServiceClient};
    ///
    /// let auth = ApiKeyAuth::new("YOUR_API_KEY");
    /// let client = ArcGISClient::new(auth);
    /// let image_service = ImageServiceClient::new(
    ///     "https://sampleserver6.arcgisonline.com/arcgis/rest/services/NLCDLandCover2001/ImageServer",
    ///     &client
    /// );
    /// ```
    pub fn new(url: impl Into<String>, client: &'a ArcGISClient) -> Self {
        ImageServiceClient {
            url: url.into(),
            client,
        }
    }

    /// Exports an image from the image service.
    ///
    /// Returns a URL to the exported image with the specified parameters.
    ///
    /// # Arguments
    ///
    /// * `params` - Export parameters (bounding box, size, format, etc.)
    ///
    /// # Returns
    ///
    /// Export result containing the image URL and metadata.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use arcgis::{ApiKeyAuth, ArcGISClient, ImageServiceClient, ExportImageParametersBuilder};
    ///
    /// # async fn example() -> arcgis::Result<()> {
    /// let auth = ApiKeyAuth::new("YOUR_API_KEY");
    /// let client = ArcGISClient::new(auth);
    /// let image_service = ImageServiceClient::new(
    ///     "https://sampleserver6.arcgisonline.com/arcgis/rest/services/NLCDLandCover2001/ImageServer",
    ///     &client
    /// );
    ///
    /// let params = ExportImageParametersBuilder::default()
    ///     .bbox("-120,40,-119,41")
    ///     .size("400,400")
    ///     .format("png")
    ///     .build()
    ///     .expect("Valid parameters");
    ///
    /// let result = image_service.export_image(params).await?;
    /// tracing::info!(url = %result.href(), "Image exported");
    /// # Ok(())
    /// # }
    /// ```
    #[instrument(skip(self, params))]
    pub async fn export_image(&self, params: ExportImageParameters) -> Result<ExportImageResult> {
        tracing::debug!("Exporting image");

        let export_url = format!("{}/exportImage", self.url);

        let response = self
            .client
            .http()
            .get(&export_url)
            .query(&params)
            .query(&[("f", "json")])
            .send()
            .await?;

        let result: ExportImageResult = response.json().await?;

        tracing::debug!(url = %result.href(), "Image exported");

        Ok(result)
    }

    /// Identifies pixel values at a location.
    ///
    /// Returns the pixel value(s) and optionally catalog items and geometry.
    ///
    /// # Arguments
    ///
    /// * `geometry` - Point or polygon geometry to identify at
    ///
    /// # Returns
    ///
    /// Identify result containing pixel values and metadata.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use arcgis::{ApiKeyAuth, ArcGISClient, ImageServiceClient, ArcGISGeometry, ArcGISPoint};
    ///
    /// # async fn example() -> arcgis::Result<()> {
    /// let auth = ApiKeyAuth::new("YOUR_API_KEY");
    /// let client = ArcGISClient::new(auth);
    /// let image_service = ImageServiceClient::new(
    ///     "https://sampleserver6.arcgisonline.com/arcgis/rest/services/NLCDLandCover2001/ImageServer",
    ///     &client
    /// );
    ///
    /// let geometry = ArcGISGeometry::Point(ArcGISPoint::new(-120.0, 40.0));
    /// let result = image_service.identify(&geometry).await?;
    ///
    /// if let Some(value) = result.value() {
    ///     tracing::info!(value = %value, "Pixel value");
    /// }
    /// # Ok(())
    /// # }
    /// ```
    #[instrument(skip(self))]
    pub async fn identify(&self, geometry: &ArcGISGeometry) -> Result<ImageIdentifyResult> {
        tracing::debug!("Identifying pixel value");

        let identify_url = format!("{}/identify", self.url);

        let geometry_json = serde_json::to_string(geometry)?;
        let geometry_type = match geometry {
            ArcGISGeometry::Point(_) => "esriGeometryPoint",
            ArcGISGeometry::Polygon(_) => "esriGeometryPolygon",
            _ => "esriGeometryPoint",
        };

        let response = self
            .client
            .http()
            .get(&identify_url)
            .query(&[
                ("f", "json"),
                ("geometry", &geometry_json),
                ("geometryType", geometry_type),
            ])
            .send()
            .await?;

        let result: ImageIdentifyResult = response.json().await?;

        tracing::debug!("Identification complete");

        Ok(result)
    }

    /// Identifies pixel values with custom parameters.
    ///
    /// Allows full control over mosaic rules, rendering rules, and return options.
    ///
    /// # Arguments
    ///
    /// * `params` - Identify parameters
    ///
    /// # Returns
    ///
    /// Identify result containing pixel values and metadata.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use arcgis::{ApiKeyAuth, ArcGISClient, ImageServiceClient, IdentifyParametersBuilder, ArcGISGeometry, ArcGISPoint};
    ///
    /// # async fn example() -> arcgis::Result<()> {
    /// let auth = ApiKeyAuth::new("YOUR_API_KEY");
    /// let client = ArcGISClient::new(auth);
    /// let image_service = ImageServiceClient::new(
    ///     "https://sampleserver6.arcgisonline.com/arcgis/rest/services/NLCDLandCover2001/ImageServer",
    ///     &client
    /// );
    ///
    /// let geometry = ArcGISGeometry::Point(ArcGISPoint::new(-120.0, 40.0));
    /// let geometry_json = serde_json::to_string(&geometry)?;
    ///
    /// let params = IdentifyParametersBuilder::default()
    ///     .geometry(geometry_json)
    ///     .geometry_type("esriGeometryPoint")
    ///     .return_catalog_items(true)
    ///     .build()
    ///     .expect("Valid parameters");
    ///
    /// let result = image_service.identify_with_params(params).await?;
    /// # Ok(())
    /// # }
    /// ```
    #[instrument(skip(self, params), fields(geometry_type))]
    pub async fn identify_with_params(
        &self,
        params: IdentifyParameters,
    ) -> Result<ImageIdentifyResult> {
        tracing::debug!("Identifying with custom parameters");

        let identify_url = format!("{}/identify", self.url);

        // Build query parameters
        let mut query_params = vec![
            ("f", "json".to_string()),
            ("geometry", params.geometry().to_string()),
            ("geometryType", params.geometry_type().to_string()),
        ];

        // Add optional parameters
        if let Some(sr) = params.geometry_sr() {
            query_params.push(("geometrySR", sr.to_string()));
        }
        if let Some(return_geom) = params.return_geometry() {
            query_params.push(("returnGeometry", return_geom.to_string()));
        }
        if let Some(return_catalog) = params.return_catalog_items() {
            query_params.push(("returnCatalogItems", return_catalog.to_string()));
        }
        if let Some(mosaic_rule) = params.mosaic_rule() {
            let mosaic_json = serde_json::to_string(mosaic_rule)?;
            query_params.push(("mosaicRule", mosaic_json));
        }
        if let Some(rendering_rule) = params.rendering_rule() {
            let rendering_json = serde_json::to_string(rendering_rule)?;
            query_params.push(("renderingRule", rendering_json));
        }

        let response = self
            .client
            .http()
            .get(&identify_url)
            .query(&query_params)
            .send()
            .await?;

        let result: ImageIdentifyResult = response.json().await?;

        tracing::debug!("Identification complete");

        Ok(result)
    }

    /// Samples pixel values along a geometry.
    ///
    /// Useful for creating elevation profiles or extracting values along transects.
    ///
    /// # Arguments
    ///
    /// * `params` - Sample parameters (geometry, sample count/distance, etc.)
    ///
    /// # Returns
    ///
    /// Sample result containing sample points and values.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use arcgis::{ApiKeyAuth, ArcGISClient, ImageServiceClient, SampleParametersBuilder};
    ///
    /// # async fn example() -> arcgis::Result<()> {
    /// let auth = ApiKeyAuth::new("YOUR_API_KEY");
    /// let client = ArcGISClient::new(auth);
    /// let image_service = ImageServiceClient::new(
    ///     "https://sampleserver6.arcgisonline.com/arcgis/rest/services/NLCDLandCover2001/ImageServer",
    ///     &client
    /// );
    ///
    /// let params = SampleParametersBuilder::default()
    ///     .geometry("{\"paths\":[[[-120,40],[-119,41]]]}")
    ///     .geometry_type("esriGeometryPolyline")
    ///     .sample_count(100u32)
    ///     .build()
    ///     .expect("Valid parameters");
    ///
    /// let result = image_service.get_samples(params).await?;
    /// tracing::info!(count = result.samples().len(), "Samples retrieved");
    /// # Ok(())
    /// # }
    /// ```
    #[instrument(skip(self, params))]
    pub async fn get_samples(&self, params: SampleParameters) -> Result<SampleResult> {
        tracing::debug!("Getting samples");

        let samples_url = format!("{}/getSamples", self.url);

        let response = self
            .client
            .http()
            .get(&samples_url)
            .query(&params)
            .query(&[("f", "json")])
            .send()
            .await?;

        let result: SampleResult = response.json().await?;

        tracing::debug!(count = result.samples().len(), "Samples retrieved");

        Ok(result)
    }

    /// Computes histograms for the image.
    ///
    /// Returns histogram data and statistics for each band.
    ///
    /// # Arguments
    ///
    /// * `params` - Histogram parameters (geometry, mosaic rule, etc.)
    ///
    /// # Returns
    ///
    /// Histogram result containing per-band histograms and statistics.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use arcgis::{ApiKeyAuth, ArcGISClient, ImageServiceClient, HistogramParametersBuilder};
    ///
    /// # async fn example() -> arcgis::Result<()> {
    /// let auth = ApiKeyAuth::new("YOUR_API_KEY");
    /// let client = ArcGISClient::new(auth);
    /// let image_service = ImageServiceClient::new(
    ///     "https://sampleserver6.arcgisonline.com/arcgis/rest/services/NLCDLandCover2001/ImageServer",
    ///     &client
    /// );
    ///
    /// let params = HistogramParametersBuilder::default()
    ///     .build()
    ///     .expect("Valid parameters");
    ///
    /// let result = image_service.compute_histograms(params).await?;
    /// tracing::info!(bands = result.histograms().len(), "Histograms computed");
    /// # Ok(())
    /// # }
    /// ```
    #[instrument(skip(self, params))]
    pub async fn compute_histograms(&self, params: HistogramParameters) -> Result<HistogramResult> {
        tracing::debug!("Computing histograms");

        let histogram_url = format!("{}/computeHistograms", self.url);

        let response = self
            .client
            .http()
            .get(&histogram_url)
            .query(&params)
            .query(&[("f", "json")])
            .send()
            .await?;

        let result: HistogramResult = response.json().await?;

        tracing::debug!(bands = result.histograms().len(), "Histograms computed");

        Ok(result)
    }

    /// Retrieves raster information and metadata.
    ///
    /// Returns details about the raster dataset including bands, extent, and pixel type.
    ///
    /// # Returns
    ///
    /// Raster information metadata.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use arcgis::{ApiKeyAuth, ArcGISClient, ImageServiceClient};
    ///
    /// # async fn example() -> arcgis::Result<()> {
    /// let auth = ApiKeyAuth::new("YOUR_API_KEY");
    /// let client = ArcGISClient::new(auth);
    /// let image_service = ImageServiceClient::new(
    ///     "https://sampleserver6.arcgisonline.com/arcgis/rest/services/NLCDLandCover2001/ImageServer",
    ///     &client
    /// );
    ///
    /// let info = image_service.get_raster_info().await?;
    /// if let Some(band_count) = info.band_count() {
    ///     tracing::info!(bands = band_count, "Raster info");
    /// }
    /// # Ok(())
    /// # }
    /// ```
    #[instrument(skip(self))]
    pub async fn get_raster_info(&self) -> Result<RasterInfo> {
        tracing::debug!("Getting raster info");

        let response = self
            .client
            .http()
            .get(&self.url)
            .query(&[("f", "json")])
            .send()
            .await?;

        let info: RasterInfo = response.json().await?;

        tracing::debug!("Raster info retrieved");

        Ok(info)
    }
}