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
//! Types for geometry service operations.

use crate::ArcGISGeometry;
use derive_getters::Getters;
use serde::{Deserialize, Serialize};

/// Parameters for the project operation.
///
/// Use [`ProjectParameters::builder()`] to construct instances.
#[derive(Debug, Clone, Serialize, derive_builder::Builder, Getters)]
#[builder(setter(into, strip_option))]
#[serde(rename_all = "camelCase")]
pub struct ProjectParameters {
    /// Geometries to project (REQUIRED).
    #[serde(serialize_with = "serialize_geometries")]
    geometries: Vec<ArcGISGeometry>,

    /// Input spatial reference WKID (REQUIRED).
    in_sr: i32,

    /// Output spatial reference WKID (REQUIRED).
    out_sr: i32,

    /// Datum transformation WKID (optional).
    #[serde(skip_serializing_if = "Option::is_none")]
    #[builder(default)]
    transformation: Option<i32>,

    /// Whether to transform forward or reverse.
    #[serde(skip_serializing_if = "Option::is_none")]
    #[builder(default)]
    transform_forward: Option<bool>,
}

impl ProjectParameters {
    /// Creates a builder for ProjectParameters.
    pub fn builder() -> ProjectParametersBuilder {
        ProjectParametersBuilder::default()
    }
}

/// Helper to serialize geometries with geometryType wrapper.
///
/// ArcGIS expects geometries in this format:
/// ```json
/// {
///   "geometryType": "esriGeometryPoint",
///   "geometries": [{...}, {...}]
/// }
/// ```
fn serialize_geometries<S>(geoms: &[ArcGISGeometry], serializer: S) -> Result<S::Ok, S::Error>
where
    S: serde::Serializer,
{
    use serde::ser::SerializeStruct;

    // Determine geometry type from first geometry
    let geometry_type = match geoms.first() {
        Some(ArcGISGeometry::Point(_)) => "esriGeometryPoint",
        Some(ArcGISGeometry::Multipoint(_)) => "esriGeometryMultipoint",
        Some(ArcGISGeometry::Polyline(_)) => "esriGeometryPolyline",
        Some(ArcGISGeometry::Polygon(_)) => "esriGeometryPolygon",
        Some(ArcGISGeometry::Envelope(_)) => "esriGeometryEnvelope",
        None => {
            // If no geometries, serialize as empty array
            use serde::ser::SerializeSeq;
            let seq = serializer.serialize_seq(Some(0))?;
            return seq.end();
        }
    };

    // Serialize as wrapper object
    let mut state = serializer.serialize_struct("GeometriesWrapper", 2)?;
    state.serialize_field("geometryType", geometry_type)?;
    state.serialize_field("geometries", geoms)?;
    state.end()
}

/// Helper to serialize polygons as plain array (no geometryType wrapper).
///
/// Used for areasAndLengths which expects plain array format:
/// ```json
/// [{"rings": [...]}, {"rings": [...]}]
/// ```
fn serialize_plain_polygons<S>(geoms: &[ArcGISGeometry], serializer: S) -> Result<S::Ok, S::Error>
where
    S: serde::Serializer,
{
    use serde::ser::SerializeSeq;

    let mut seq = serializer.serialize_seq(Some(geoms.len()))?;
    for geom in geoms {
        match geom {
            ArcGISGeometry::Polygon(polygon) => {
                seq.serialize_element(polygon)?;
            }
            _ => {
                return Err(serde::ser::Error::custom(
                    "areasAndLengths only accepts Polygon geometries",
                ));
            }
        }
    }
    seq.end()
}

/// Response from project operation.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Getters)]
pub struct ProjectResult {
    /// Projected geometries.
    geometries: Vec<ArcGISGeometry>,
}

/// Parameters for the buffer operation.
///
/// Use [`BufferParameters::builder()`] to construct instances.
#[derive(Debug, Clone, Serialize, derive_builder::Builder, Getters)]
#[builder(setter(into, strip_option))]
#[serde(rename_all = "camelCase")]
pub struct BufferParameters {
    /// Geometries to buffer (REQUIRED).
    #[serde(serialize_with = "serialize_geometries")]
    geometries: Vec<ArcGISGeometry>,

    /// Spatial reference of input geometries (REQUIRED).
    in_sr: i32,

    /// Buffer distances (REQUIRED).
    /// One distance per geometry, or a single distance for all.
    distances: Vec<f64>,

    /// Distance unit (REQUIRED).
    unit: LinearUnit,

    /// Whether to union results.
    #[serde(skip_serializing_if = "Option::is_none")]
    #[builder(default)]
    union_results: Option<bool>,

    /// Whether to use geodesic buffers.
    #[serde(skip_serializing_if = "Option::is_none")]
    #[builder(default)]
    geodesic: Option<bool>,

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

impl BufferParameters {
    /// Creates a builder for BufferParameters.
    pub fn builder() -> BufferParametersBuilder {
        BufferParametersBuilder::default()
    }
}

/// Response from buffer operation.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Getters)]
pub struct BufferResult {
    /// Buffer polygon geometries.
    geometries: Vec<ArcGISGeometry>,
}

/// Linear units for distance measurements and buffers.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum LinearUnit {
    /// Meters
    #[serde(rename = "esriMeters")]
    Meters,
    /// Kilometers
    #[serde(rename = "esriKilometers")]
    Kilometers,
    /// Feet
    #[serde(rename = "esriFeet")]
    Feet,
    /// Miles
    #[serde(rename = "esriMiles")]
    Miles,
    /// Nautical miles
    #[serde(rename = "esriNauticalMiles")]
    NauticalMiles,
    /// Yards
    #[serde(rename = "esriYards")]
    Yards,
}

/// Datum transformation information.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Getters)]
#[serde(rename_all = "camelCase")]
pub struct Transformation {
    /// Well-Known ID of the transformation.
    wkid: i32,

    /// Well-Known Text representation.
    #[serde(skip_serializing_if = "Option::is_none")]
    wkt: Option<String>,

    /// Name of the transformation.
    #[serde(skip_serializing_if = "Option::is_none")]
    name: Option<String>,
}

/// Parameters for the simplify operation.
///
/// Use [`SimplifyParameters::builder()`] to construct instances.
#[derive(Debug, Clone, Serialize, derive_builder::Builder, Getters)]
#[builder(setter(into, strip_option))]
#[serde(rename_all = "camelCase")]
pub struct SimplifyParameters {
    /// Geometries to simplify (REQUIRED).
    #[serde(serialize_with = "serialize_geometries")]
    geometries: Vec<ArcGISGeometry>,

    /// Spatial reference of input geometries (REQUIRED).
    sr: i32,
}

impl SimplifyParameters {
    /// Creates a builder for SimplifyParameters.
    pub fn builder() -> SimplifyParametersBuilder {
        SimplifyParametersBuilder::default()
    }
}

/// Deserializes plain polygon objects into ArcGISGeometry::Polygon variants.
///
/// The API returns geometries as plain objects like `{"rings": [...]}`,
/// but we need them wrapped in the ArcGISGeometry enum.
fn deserialize_plain_polygons<'de, D>(deserializer: D) -> Result<Vec<ArcGISGeometry>, D::Error>
where
    D: serde::Deserializer<'de>,
{
    use serde::Deserialize;
    let polygons: Vec<crate::ArcGISPolygon> = Vec::deserialize(deserializer)?;
    Ok(polygons.into_iter().map(ArcGISGeometry::Polygon).collect())
}

/// Response from simplify operation.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Getters)]
pub struct SimplifyResult {
    /// Simplified geometries (as plain polygons, not enum-wrapped).
    #[serde(deserialize_with = "deserialize_plain_polygons")]
    geometries: Vec<ArcGISGeometry>,
}

/// Parameters for the union operation.
///
/// Use [`UnionParameters::builder()`] to construct instances.
#[derive(Debug, Clone, Serialize, derive_builder::Builder, Getters)]
#[builder(setter(into, strip_option))]
#[serde(rename_all = "camelCase")]
pub struct UnionParameters {
    /// Geometries to union (REQUIRED).
    #[serde(serialize_with = "serialize_geometries")]
    geometries: Vec<ArcGISGeometry>,

    /// Spatial reference of input geometries (REQUIRED).
    sr: i32,
}

impl UnionParameters {
    /// Creates a builder for UnionParameters.
    pub fn builder() -> UnionParametersBuilder {
        UnionParametersBuilder::default()
    }
}

/// Deserializes a plain polygon object into ArcGISGeometry::Polygon variant.
///
/// The API returns geometry as a plain object like `{"rings": [...]}`,
/// but we need it wrapped in the ArcGISGeometry enum.
fn deserialize_plain_polygon<'de, D>(deserializer: D) -> Result<ArcGISGeometry, D::Error>
where
    D: serde::Deserializer<'de>,
{
    use serde::Deserialize;
    let polygon: crate::ArcGISPolygon = crate::ArcGISPolygon::deserialize(deserializer)?;
    Ok(ArcGISGeometry::Polygon(polygon))
}

/// Response from union operation.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Getters)]
#[serde(rename_all = "camelCase")]
pub struct UnionResult {
    /// Type of the resulting geometry.
    #[serde(skip_serializing_if = "Option::is_none")]
    geometry_type: Option<String>,

    /// Unioned geometry (as plain polygon, not enum-wrapped).
    #[serde(deserialize_with = "deserialize_plain_polygon")]
    geometry: ArcGISGeometry,
}

/// Parameters for calculating areas and lengths.
///
/// Use [`AreasAndLengthsParameters::builder()`] to construct instances.
#[derive(Debug, Clone, Serialize, derive_builder::Builder, Getters)]
#[builder(setter(into, strip_option))]
#[serde(rename_all = "camelCase")]
pub struct AreasAndLengthsParameters {
    /// Polygon geometries to calculate (REQUIRED).
    /// Note: API expects plain array of polygons, not wrapped with geometryType.
    #[serde(serialize_with = "serialize_plain_polygons")]
    polygons: Vec<ArcGISGeometry>,

    /// Spatial reference of input geometries (REQUIRED).
    sr: i32,

    /// Length unit for calculations.
    #[serde(skip_serializing_if = "Option::is_none")]
    length_unit: Option<LinearUnit>,

    /// Area unit for calculations.
    #[serde(skip_serializing_if = "Option::is_none")]
    area_unit: Option<AreaUnit>,

    /// Whether to use geodesic calculations.
    #[serde(skip_serializing_if = "Option::is_none")]
    calculation_type: Option<CalculationType>,
}

impl AreasAndLengthsParameters {
    /// Creates a builder for AreasAndLengthsParameters.
    pub fn builder() -> AreasAndLengthsParametersBuilder {
        AreasAndLengthsParametersBuilder::default()
    }
}

/// Response from areas and lengths calculation.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Getters)]
pub struct AreasAndLengthsResult {
    /// Calculated areas for each polygon.
    areas: Vec<f64>,

    /// Calculated perimeter lengths for each polygon.
    lengths: Vec<f64>,
}

/// Parameters for distance calculation.
///
/// Use [`DistanceParameters::builder()`] to construct instances.
#[derive(Debug, Clone, Serialize, derive_builder::Builder, Getters)]
#[builder(setter(into, strip_option))]
#[serde(rename_all = "camelCase")]
pub struct DistanceParameters {
    /// First geometry (REQUIRED).
    geometry1: ArcGISGeometry,

    /// Second geometry (REQUIRED).
    geometry2: ArcGISGeometry,

    /// Spatial reference of input geometries (REQUIRED).
    sr: i32,

    /// Distance unit for result.
    #[serde(skip_serializing_if = "Option::is_none")]
    #[builder(default)]
    distance_unit: Option<LinearUnit>,

    /// Whether to use geodesic calculations.
    #[serde(skip_serializing_if = "Option::is_none")]
    #[builder(default)]
    geodesic: Option<bool>,
}

impl DistanceParameters {
    /// Creates a builder for DistanceParameters.
    pub fn builder() -> DistanceParametersBuilder {
        DistanceParametersBuilder::default()
    }
}

/// Response from distance calculation.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Getters)]
pub struct DistanceResult {
    /// Calculated distance.
    distance: f64,
}

/// Area units for measurements.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum AreaUnit {
    /// Square meters
    #[serde(rename = "esriSquareMeters")]
    SquareMeters,
    /// Square kilometers
    #[serde(rename = "esriSquareKilometers")]
    SquareKilometers,
    /// Square feet
    #[serde(rename = "esriSquareFeet")]
    SquareFeet,
    /// Square miles
    #[serde(rename = "esriSquareMiles")]
    SquareMiles,
    /// Acres
    #[serde(rename = "esriAcres")]
    Acres,
    /// Hectares
    #[serde(rename = "esriHectares")]
    Hectares,
}

/// Calculation type for geometric operations.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum CalculationType {
    /// Planar (projected) calculations.
    #[serde(rename = "planar")]
    Planar,
    /// Geodesic (spherical) calculations.
    #[serde(rename = "geodesic")]
    Geodesic,
    /// Preserves shape calculations.
    #[serde(rename = "preserveShape")]
    PreserveShape,
}