arcgis 0.1.1

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
//! 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 within a polygon.
#[derive(Debug, Clone, Serialize, derive_builder::Builder, Getters)]
#[builder(setter(into, strip_option))]
#[serde(rename_all = "camelCase")]
pub struct SummarizeElevationParameters {
    /// Input polygon geometry.
    #[serde(rename = "InputPolygon")]
    input_geometry: String,

    /// Geometry type.
    #[builder(default)]
    #[serde(skip_serializing_if = "Option::is_none")]
    geometry_type: Option<String>,

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

    /// Include slope statistics.
    #[builder(default)]
    #[serde(skip_serializing_if = "Option::is_none")]
    include_slope: Option<bool>,

    /// Include aspect statistics.
    #[builder(default)]
    #[serde(skip_serializing_if = "Option::is_none")]
    include_aspect: Option<bool>,

    /// 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 summarize elevation operation.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Getters)]
#[serde(rename_all = "camelCase")]
pub struct SummarizeElevationResult {
    /// Summary feature set with statistics.
    #[serde(skip_serializing_if = "Option::is_none")]
    output_summary: Option<FeatureSet>,

    /// Minimum elevation.
    #[serde(skip_serializing_if = "Option::is_none")]
    min_elevation: Option<f64>,

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

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

    /// Area in square meters.
    #[serde(skip_serializing_if = "Option::is_none")]
    area: Option<f64>,
}

/// Parameters for viewshed analysis.
#[derive(Debug, Clone, Serialize, derive_builder::Builder, Getters)]
#[builder(setter(into, strip_option))]
#[serde(rename_all = "camelCase")]
pub struct ViewshedParameters {
    /// Observer point(s) geometry.
    #[serde(rename = "InputPoints")]
    input_points: String,

    /// Geometry type.
    #[builder(default)]
    #[serde(skip_serializing_if = "Option::is_none")]
    geometry_type: Option<String>,

    /// Maximum viewing distance in meters.
    #[builder(default)]
    #[serde(skip_serializing_if = "Option::is_none")]
    maximum_distance: Option<f64>,

    /// Maximum horizontal viewing angle (degrees).
    #[builder(default)]
    #[serde(skip_serializing_if = "Option::is_none")]
    maximum_horizontal_angle: Option<f64>,

    /// Maximum vertical angle (degrees).
    #[builder(default)]
    #[serde(skip_serializing_if = "Option::is_none")]
    maximum_vertical_angle: Option<f64>,

    /// Observer height above ground (meters).
    #[builder(default)]
    #[serde(skip_serializing_if = "Option::is_none")]
    observer_height: Option<f64>,

    /// Observer offset (additional height).
    #[builder(default)]
    #[serde(skip_serializing_if = "Option::is_none")]
    observer_offset: Option<f64>,

    /// Surface offset (target height above ground).
    #[builder(default)]
    #[serde(skip_serializing_if = "Option::is_none")]
    surface_offset: Option<f64>,

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

    /// Generalize viewshed polygons.
    #[builder(default)]
    #[serde(skip_serializing_if = "Option::is_none")]
    generalize: Option<bool>,

    /// 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 viewshed analysis.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Getters)]
#[serde(rename_all = "camelCase")]
pub struct ViewshedResult {
    /// Viewshed polygon feature set.
    #[serde(skip_serializing_if = "Option::is_none")]
    output_viewshed: Option<FeatureSet>,

    /// Visible area in square meters.
    #[serde(skip_serializing_if = "Option::is_none")]
    visible_area: Option<f64>,

    /// Total area analyzed in square meters.
    #[serde(skip_serializing_if = "Option::is_none")]
    total_area: Option<f64>,

    /// Percentage visible (0-100).
    #[serde(skip_serializing_if = "Option::is_none")]
    percent_visible: Option<f64>,
}

/// 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",
        }
    }
}