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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
//! Elevation Service types and parameters.

use crate::{ArcGISGeometryError, ArcGISGeometryErrorKind, FeatureSet};
use derive_getters::Getters;
use serde::{Deserialize, Serialize};
use tracing::instrument;

/// Parameters for generating an elevation profile.
#[derive(Debug, Clone, Serialize, derive_builder::Builder, Getters)]
#[builder(setter(into, strip_option))]
#[serde(rename_all = "camelCase")]
pub struct ProfileParameters {
    /// Input line features as a FeatureSet JSON string.
    ///
    /// Must be a valid FeatureSet with geometryType, features array, and spatialReference.
    /// Example: `{"geometryType":"esriGeometryPolyline","features":[{"geometry":{"paths":[...]}}],"spatialReference":{"wkid":4326}}`
    #[serde(rename = "InputLineFeatures")]
    input_line_features: String,

    /// DEM resolution (FINEST, 10m, 30m, 90m).
    #[builder(default)]
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(rename = "DEMResolution")]
    dem_resolution: Option<String>,

    /// Profile ID field for grouping multiple profiles.
    #[builder(default)]
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(rename = "ProfileIDField")]
    profile_id_field: Option<String>,

    /// Maximum distance between sample points.
    #[builder(default)]
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(rename = "MaximumSampleDistance")]
    maximum_sample_distance: Option<f64>,

    /// Units for maximum sample distance (Meters, Kilometers, etc).
    #[builder(default)]
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(rename = "MaximumSampleDistanceUnits")]
    maximum_sample_distance_units: Option<String>,

    /// Return Z values (elevation) in output geometry.
    ///
    /// Must be true to get elevation data.
    #[builder(default = "true")]
    #[serde(rename = "returnZ")]
    return_z: bool,

    /// Return M values (distance along profile) in output geometry.
    ///
    /// Must be true to get distance data.
    #[builder(default = "true")]
    #[serde(rename = "returnM")]
    return_m: bool,

    /// Input spatial reference WKID.
    #[builder(default)]
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(rename = "inSR")]
    in_sr: Option<u32>,

    /// Output spatial reference WKID.
    #[builder(default)]
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(rename = "outSR")]
    out_sr: Option<u32>,
}

/// Result from elevation profile operation.
///
/// Contains elevation profile data as a FeatureSet with Z (elevation) and M (distance) values
/// in the geometry coordinates.
#[derive(Debug, Clone, PartialEq, Getters)]
pub struct ProfileResult {
    /// Profile feature set with elevation data.
    ///
    /// The geometry contains Z values (elevation in meters) and M values (distance in meters)
    /// along the profile path.
    output_profile: FeatureSet,
}

impl ProfileResult {
    /// Creates a new profile result from a FeatureSet.
    pub fn new(output_profile: FeatureSet) -> Self {
        Self { output_profile }
    }

    /// Gets the first point elevation in meters.
    ///
    /// # Errors
    ///
    /// Returns an error if the geometry is missing or invalid.
    #[instrument(skip(self))]
    pub fn first_point_z(&self) -> Result<f64, ArcGISGeometryError> {
        let points = self.elevation_points()?;
        points
            .first()
            .map(|p| *p.elevation_meters())
            .ok_or_else(|| {
                ArcGISGeometryError::new(ArcGISGeometryErrorKind::InvalidGeometry(
                    "Profile has no points".to_string(),
                ))
            })
    }

    /// Gets the last point elevation in meters.
    ///
    /// # Errors
    ///
    /// Returns an error if the geometry is missing or invalid.
    #[instrument(skip(self))]
    pub fn last_point_z(&self) -> Result<f64, ArcGISGeometryError> {
        let points = self.elevation_points()?;
        points.last().map(|p| *p.elevation_meters()).ok_or_else(|| {
            ArcGISGeometryError::new(ArcGISGeometryErrorKind::InvalidGeometry(
                "Profile has no points".to_string(),
            ))
        })
    }

    /// Extracts elevation profile points from the feature set.
    ///
    /// Returns a vector of elevation points ordered by distance along the profile.
    /// The elevation data is stored in the geometry Z values and distance in M values.
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - Profile geometry is missing or invalid
    /// - Geometry is not a polyline
    /// - Coordinates don't have Z or M values
    #[instrument(skip(self))]
    pub fn elevation_points(&self) -> Result<Vec<ElevationPoint>, ArcGISGeometryError> {
        use crate::ArcGISGeometry;

        tracing::debug!("Extracting elevation points from profile result");

        let feature_count = self.output_profile.features().len();
        tracing::debug!(feature_count, "Processing profile features");

        if self.output_profile.features().is_empty() {
            let err = ArcGISGeometryError::new(ArcGISGeometryErrorKind::InvalidGeometry(
                "Profile result has no features".to_string(),
            ));
            tracing::error!(error = %err, "No features in profile");
            return Err(err);
        }

        // Get the first feature (profile is typically a single polyline)
        let feature = &self.output_profile.features()[0];

        let geometry = feature.geometry().as_ref().ok_or_else(|| {
            let err = ArcGISGeometryError::new(ArcGISGeometryErrorKind::InvalidGeometry(
                "Profile feature missing geometry".to_string(),
            ));
            tracing::error!(error = %err, "Missing geometry");
            err
        })?;

        // Extract polyline paths
        let polyline = match geometry {
            ArcGISGeometry::Polyline(polyline) => polyline,
            _ => {
                let err = ArcGISGeometryError::new(ArcGISGeometryErrorKind::InvalidGeometry(
                    format!("Expected polyline geometry, got {:?}", geometry),
                ));
                tracing::error!(error = %err, "Wrong geometry type");
                return Err(err);
            }
        };

        let paths = polyline.paths();
        if paths.is_empty() {
            let err = ArcGISGeometryError::new(ArcGISGeometryErrorKind::InvalidGeometry(
                "Polyline has no paths".to_string(),
            ));
            tracing::error!(error = %err, "No paths in polyline");
            return Err(err);
        }

        // Get the first path (profile is a single path)
        let path = &paths[0];
        tracing::debug!(coord_count = path.len(), "Processing path coordinates");

        let points: Result<Vec<ElevationPoint>, ArcGISGeometryError> = path
            .iter()
            .enumerate()
            .map(|(idx, coord)| {
                // Coordinates are [x, y, z, m] when hasZ and hasM are true
                if coord.len() < 4 {
                    let err = ArcGISGeometryError::new(ArcGISGeometryErrorKind::InvalidGeometry(
                        format!(
                            "Coordinate {} missing Z or M values (length: {}, expected 4)",
                            idx,
                            coord.len()
                        ),
                    ));
                    tracing::error!(
                        coord_index = idx,
                        coord_length = coord.len(),
                        error = %err,
                        "Invalid coordinate"
                    );
                    return Err(err);
                }

                let elevation = coord[2]; // Z value (elevation in meters)
                let distance = coord[3]; // M value (distance in meters)

                tracing::trace!(
                    coord_index = idx,
                    distance_m = distance,
                    elevation_m = elevation,
                    "Parsed elevation point"
                );

                Ok(ElevationPoint::new(distance, elevation))
            })
            .collect();

        let points = points?;
        tracing::debug!(
            point_count = points.len(),
            "Successfully extracted elevation points"
        );
        Ok(points)
    }
}

/// A single point along an elevation profile.
#[derive(Debug, Clone, PartialEq, Getters)]
pub struct ElevationPoint {
    /// Distance from start in meters.
    distance_meters: f64,

    /// Elevation in meters.
    elevation_meters: f64,
}

impl ElevationPoint {
    /// Creates a new elevation point.
    pub fn new(distance_meters: f64, elevation_meters: f64) -> Self {
        Self {
            distance_meters,
            elevation_meters,
        }
    }
}

/// Parameters for summarizing elevation statistics.
///
/// Computes elevation, slope, and aspect statistics for input features (points, lines, or polygons).
/// This operation runs asynchronously via the Elevation GPServer.
///
/// # Example
///
/// ```no_run
/// use arcgis::{SummarizeElevationParametersBuilder, DemResolution};
///
/// let polygon_featureset = r#"{"geometryType":"esriGeometryPolygon","spatialReference":{"wkid":4326},"features":[{"geometry":{"rings":[[[-119.5,37.8],[-119.4,37.8],[-119.4,37.9],[-119.5,37.9],[-119.5,37.8]]]},"attributes":{"OID":1}}]}"#;
///
/// let params = SummarizeElevationParametersBuilder::default()
///     .input_features(polygon_featureset)
///     .dem_resolution(DemResolution::ThirtyMeter.as_str())
///     .include_slope_aspect(true)
///     .build()
///     .expect("Valid parameters");
/// ```
#[derive(Debug, Clone, Serialize, derive_builder::Builder, Getters)]
#[builder(setter(into, strip_option))]
#[serde(rename_all = "camelCase")]
pub struct SummarizeElevationParameters {
    /// Input features as FeatureSet JSON string.
    ///
    /// Must be a valid FeatureSet with geometryType, features array, and spatialReference.
    /// Accepts point, line, or polygon geometries.
    ///
    /// Example: `{"geometryType":"esriGeometryPolygon","spatialReference":{"wkid":4326},"features":[...]}`
    #[serde(rename = "InputFeatures")]
    input_features: String,

    /// Field name for feature IDs (optional).
    ///
    /// Used to match input features with output statistics.
    #[builder(default)]
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(rename = "FeatureIDField")]
    feature_id_field: Option<String>,

    /// DEM resolution.
    ///
    /// Use `DemResolution` enum and call `.as_str()` to get the string value.
    /// Default: 90m
    #[builder(default)]
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(rename = "DEMResolution")]
    dem_resolution: Option<String>,

    /// Include slope and aspect statistics in output.
    ///
    /// When true, output includes MinSlope, MeanSlope, MaxSlope, and MeanAspect fields.
    /// Default: false
    #[builder(default)]
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(rename = "IncludeSlopeAspect")]
    include_slope_aspect: Option<bool>,
}

/// Result from summarize elevation operation.
///
/// Contains elevation statistics extracted from the FeatureSet returned by the GP service.
/// Statistics are stored in feature attributes.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Getters)]
#[serde(rename_all = "PascalCase")]
pub struct SummarizeElevationResult {
    /// Minimum elevation in meters.
    #[serde(skip_serializing_if = "Option::is_none")]
    min_elevation: Option<f64>,

    /// Mean elevation in meters.
    #[serde(skip_serializing_if = "Option::is_none")]
    mean_elevation: Option<f64>,

    /// Maximum elevation in meters.
    #[serde(skip_serializing_if = "Option::is_none")]
    max_elevation: Option<f64>,

    /// Minimum slope in degrees (when IncludeSlopeAspect=true).
    #[serde(skip_serializing_if = "Option::is_none")]
    min_slope: Option<f64>,

    /// Mean slope in degrees (when IncludeSlopeAspect=true).
    #[serde(skip_serializing_if = "Option::is_none")]
    mean_slope: Option<f64>,

    /// Maximum slope in degrees (when IncludeSlopeAspect=true).
    #[serde(skip_serializing_if = "Option::is_none")]
    max_slope: Option<f64>,

    /// Mean aspect in degrees (when IncludeSlopeAspect=true).
    ///
    /// Aspect is the direction the slope faces, measured clockwise from north (0-360 degrees).
    #[serde(skip_serializing_if = "Option::is_none")]
    mean_aspect: Option<f64>,
}

impl SummarizeElevationResult {
    /// Creates a result by extracting statistics from a GP FeatureSet.
    ///
    /// The FeatureSet should contain a single feature with elevation statistics in its attributes.
    ///
    /// # Errors
    ///
    /// Returns an error if the FeatureSet is empty or missing required attributes.
    #[instrument(skip(feature_set))]
    pub fn from_feature_set(feature_set: &FeatureSet) -> Result<Self, ArcGISGeometryError> {
        tracing::debug!("Parsing SummarizeElevationResult from FeatureSet");

        let features = feature_set.features();
        if features.is_empty() {
            let err = ArcGISGeometryError::new(ArcGISGeometryErrorKind::InvalidGeometry(
                "SummarizeElevation result has no features".to_string(),
            ));
            tracing::error!(error = %err, "No features in result");
            return Err(err);
        }

        let feature = &features[0];
        let attrs = feature.attributes();

        // Extract elevation statistics from attributes
        let result = Self {
            min_elevation: attrs
                .get("MinElevation")
                .and_then(|v: &serde_json::Value| v.as_f64()),
            mean_elevation: attrs
                .get("MeanElevation")
                .and_then(|v: &serde_json::Value| v.as_f64()),
            max_elevation: attrs
                .get("MaxElevation")
                .and_then(|v: &serde_json::Value| v.as_f64()),
            min_slope: attrs
                .get("MinSlope")
                .and_then(|v: &serde_json::Value| v.as_f64()),
            mean_slope: attrs
                .get("MeanSlope")
                .and_then(|v: &serde_json::Value| v.as_f64()),
            max_slope: attrs
                .get("MaxSlope")
                .and_then(|v: &serde_json::Value| v.as_f64()),
            mean_aspect: attrs
                .get("MeanAspect")
                .and_then(|v: &serde_json::Value| v.as_f64()),
        };

        tracing::debug!(
            min_elevation = ?result.min_elevation,
            mean_elevation = ?result.mean_elevation,
            max_elevation = ?result.max_elevation,
            "Parsed elevation statistics"
        );

        Ok(result)
    }
}

/// Parameters for viewshed analysis.
///
/// Determines visible areas from observer points based on terrain and viewing parameters.
/// This operation runs asynchronously via the Elevation GPServer.
///
/// # Example
///
/// ```no_run
/// use arcgis::{ViewshedParametersBuilder, DemResolution};
///
/// let observer_points = r#"{"geometryType":"esriGeometryMultipoint","spatialReference":{"wkid":4326},"points":[[-119.5,37.85]]}"#;
///
/// let params = ViewshedParametersBuilder::default()
///     .input_points(observer_points)
///     .maximum_distance(5000.0)  // 5 km
///     .maximum_distance_units("Meters")
///     .observer_height(1.75)  // Default human eye height
///     .dem_resolution(DemResolution::ThirtyMeter.as_str())
///     .build()
///     .expect("Valid parameters");
/// ```
#[derive(Debug, Clone, Serialize, derive_builder::Builder, Getters)]
#[builder(setter(into, strip_option))]
#[serde(rename_all = "camelCase")]
pub struct ViewshedParameters {
    /// Observer point(s) as FeatureSet JSON string.
    ///
    /// Must be a valid FeatureSet with geometryType (esriGeometryPoint or esriGeometryMultipoint),
    /// features array, and spatialReference.
    ///
    /// Example: `{"geometryType":"esriGeometryMultipoint","spatialReference":{"wkid":4326},"points":[[-119.5,37.85]]}`
    #[serde(rename = "InputPoints")]
    input_points: String,

    /// Maximum viewing distance (visibility cutoff).
    ///
    /// Up to 50 km maximum. Use with `maximum_distance_units` to specify units.
    #[builder(default)]
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(rename = "MaximumDistance")]
    maximum_distance: Option<f64>,

    /// Units for maximum distance.
    ///
    /// Valid values: "Meters", "Kilometers", "Feet", "Yards", "Miles"
    #[builder(default)]
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(rename = "MaximumDistanceUnits")]
    maximum_distance_units: Option<String>,

    /// DEM resolution.
    ///
    /// Use `DemResolution` enum and call `.as_str()` to get the string value.
    /// Default: 90m
    #[builder(default)]
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(rename = "DEMResolution")]
    dem_resolution: Option<String>,

    /// Observer height above ground.
    ///
    /// Default: 1.75 meters (average human eye height)
    /// Use with `observer_height_units` to specify units.
    #[builder(default)]
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(rename = "ObserverHeight")]
    observer_height: Option<f64>,

    /// Units for observer height.
    ///
    /// Valid values: "Meters", "Kilometers", "Feet", "Yards", "Miles"
    #[builder(default)]
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(rename = "ObserverHeightUnits")]
    observer_height_units: Option<String>,

    /// Surface offset (target object height above surface).
    ///
    /// Default: 0.0 meters (ground level)
    /// Use with `surface_offset_units` to specify units.
    #[builder(default)]
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(rename = "SurfaceOffset")]
    surface_offset: Option<f64>,

    /// Units for surface offset.
    ///
    /// Valid values: "Meters", "Kilometers", "Feet", "Yards", "Miles"
    #[builder(default)]
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(rename = "SurfaceOffsetUnits")]
    surface_offset_units: Option<String>,

    /// Generalize viewshed polygons for smoother output.
    ///
    /// Default: true
    #[builder(default)]
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(rename = "GeneralizeViewshedPolygons")]
    generalize_viewshed_polygons: Option<bool>,
}

/// Result from viewshed analysis.
///
/// Contains the viewshed polygon(s) showing visible areas from observer points.
#[derive(Debug, Clone, PartialEq, Getters)]
pub struct ViewshedResult {
    /// Viewshed polygon feature set.
    ///
    /// Contains polygon features representing areas visible from observer points.
    /// Attributes include: Frequency, DEMResolution, Product Name, Source, Source URL
    output_viewshed: FeatureSet,
}

impl ViewshedResult {
    /// Creates a viewshed result from a FeatureSet.
    pub fn new(output_viewshed: FeatureSet) -> Self {
        Self { output_viewshed }
    }

    /// Gets the number of viewshed polygons.
    pub fn viewshed_count(&self) -> usize {
        self.output_viewshed.features().len()
    }
}

/// DEM resolution options.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum DemResolution {
    /// Finest available resolution.
    Finest,
    /// 10 meter resolution.
    #[serde(rename = "10m")]
    TenMeter,
    /// 30 meter resolution.
    #[serde(rename = "30m")]
    ThirtyMeter,
    /// 90 meter resolution.
    #[serde(rename = "90m")]
    NinetyMeter,
}

impl DemResolution {
    /// Convert to API string representation.
    pub fn as_str(&self) -> &'static str {
        match self {
            DemResolution::Finest => "FINEST",
            DemResolution::TenMeter => "10m",
            DemResolution::ThirtyMeter => "30m",
            DemResolution::NinetyMeter => "90m",
        }
    }
}