omap 0.5.0

Interact with or write new Open Orienteering Mapper omap-files
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
use std::rc::Weak;

use quick_xml::{
    Reader, Writer,
    events::{BytesEnd, BytesStart, BytesText, Event},
};

use super::{AreaSymbol, LineSymbol};
use crate::{
    Code, Error, NonNegativeF64, Result,
    colors::{ColorSet, SymbolColor},
    objects::{AreaObject, LineObject, PointObject},
    symbols::{SymbolCommon, WeakAreaPathSymbol, WeakLinePathSymbol},
    utils::try_get_attr_raw,
};

/// Temporary enum used during element parsing
enum ElementSymbolData {
    Point(Box<PointSymbol>),
    Line(Box<LineSymbol>),
    Area(Box<AreaSymbol>),
}

/// Temporary enum used during element parsing
enum ElementObjectData {
    Point(Box<PointObject>),
    Line(Box<LineObject>),
    Area(Box<AreaObject>),
}

/// An element within a point symbol definition.
#[derive(Debug, Clone)]
pub enum Element {
    /// A nested point sub-symbol with its object.
    Point {
        /// The point sub-symbol.
        symbol: Box<PointSymbol>,
        /// The object rendered by this element.
        object: Box<PointObject>,
    },
    /// A line sub-symbol with its object.
    Line {
        /// The line sub-symbol.
        symbol: Box<LineSymbol>,
        /// The object rendered by this element.
        object: Box<LineObject>,
    },
    /// An area sub-symbol with its object.
    Area {
        /// The area sub-symbol.
        symbol: Box<AreaSymbol>,
        /// The object rendered by this element.
        object: Box<AreaObject>,
    },
}

impl Element {
    fn write<W: std::io::Write>(&self, writer: &mut Writer<W>, color_set: &ColorSet) -> Result<()> {
        writer.write_event(Event::Start(BytesStart::new("element")))?;
        match self {
            Element::Point { symbol, object } => {
                symbol.write(writer, color_set, None)?;
                object.write_as_element(writer, symbol.is_rotatable)?;
            }
            Element::Line { symbol, object } => {
                symbol.write(writer, color_set, None)?;
                object.write_as_element(writer)?;
            }
            Element::Area { symbol, object } => {
                symbol.write(writer, color_set, None)?;
                object.write_as_element(writer)?;
            }
        }
        writer.write_event(Event::End(BytesEnd::new("element")))?;
        Ok(())
    }

    /// Parse a single element inside point_symbol
    fn parse_element<R: std::io::BufRead>(
        reader: &mut Reader<R>,
        color_set: &ColorSet,
    ) -> Result<Element> {
        let mut symbol_data = None;
        let mut object_data = None;
        let mut buf = Vec::new();
        loop {
            match reader.read_event_into(&mut buf)? {
                Event::Start(e) => match e.local_name().as_ref() {
                    b"symbol" => {
                        let sym_type = try_get_attr_raw(&e, "type").unwrap_or(0_u8);
                        symbol_data = Some(match sym_type {
                            1 => ElementSymbolData::Point(Box::new(PointSymbol::parse(
                                reader,
                                color_set,
                                Default::default(),
                            )?)),
                            2 => ElementSymbolData::Line(Box::new(LineSymbol::parse(
                                reader,
                                color_set,
                                Default::default(),
                            )?)),
                            4 => ElementSymbolData::Area(Box::new(AreaSymbol::parse(
                                reader,
                                color_set,
                                Default::default(),
                            )?)),
                            _ => {
                                return Err(Error::ParseOmapFileError(format!(
                                    "Unknown element symbol type {sym_type}"
                                )));
                            }
                        });
                    }
                    b"object" => {
                        // Parse the object based on what symbol we have
                        let obj_type = try_get_attr_raw(&e, "type").unwrap_or(6_u8);
                        object_data = Some(match obj_type {
                            0 => ElementObjectData::Point(Box::new(PointObject::parse(
                                reader,
                                Weak::new(),
                                0.,
                            )?)),
                            1 => match &symbol_data {
                                Some(s) => match s {
                                    ElementSymbolData::Line(_) => {
                                        ElementObjectData::Line(Box::new(LineObject::parse(
                                            reader,
                                            WeakLinePathSymbol::Line(Weak::new()),
                                        )?))
                                    }
                                    ElementSymbolData::Area(_) => {
                                        ElementObjectData::Area(Box::new(AreaObject::parse(
                                            reader,
                                            WeakAreaPathSymbol::Area(Weak::new()),
                                        )?))
                                    }
                                    _ => {
                                        return Err(Error::ParseOmapFileError(
                                            "Symbol Object mismatch in element".to_string(),
                                        ));
                                    }
                                },
                                None => {
                                    return Err(Error::ParseOmapFileError(
                                        "Object before symbol in element".to_string(),
                                    ));
                                }
                            },
                            _ => {
                                return Err(Error::ParseOmapFileError(format!(
                                    "Unknown element object type {obj_type}"
                                )));
                            }
                        });
                    }
                    _ => {}
                },
                Event::End(e) => {
                    if e.local_name().as_ref() == b"element" {
                        break;
                    }
                }
                Event::Eof => {
                    return Err(Error::ParseOmapFileError(
                        "Unexpected EOF parsing element".to_string(),
                    ));
                }
                _ => {}
            }
        }
        if let Some(sd) = symbol_data
            && let Some(od) = object_data
        {
            match (sd, od) {
                (
                    ElementSymbolData::Point(point_symbol),
                    ElementObjectData::Point(point_object),
                ) => {
                    return Ok(Element::Point {
                        symbol: point_symbol,
                        object: point_object,
                    });
                }
                (ElementSymbolData::Line(line_symbol), ElementObjectData::Line(line_object)) => {
                    return Ok(Element::Line {
                        symbol: line_symbol,
                        object: line_object,
                    });
                }
                (ElementSymbolData::Area(area_symbol), ElementObjectData::Area(area_object)) => {
                    return Ok(Element::Area {
                        symbol: area_symbol,
                        object: area_object,
                    });
                }
                _ => {
                    return Err(Error::ParseOmapFileError(
                        "Mismatch between object and symbol type in element".to_string(),
                    ));
                }
            }
        }
        Err(Error::ParseOmapFileError(
            "Either object or symbol data was not present in element".to_string(),
        ))
    }
}

/// A point symbol definition.
#[derive(Debug, Clone)]
pub struct PointSymbol {
    /// Common symbol properties.
    pub common: SymbolCommon,

    /// Whether the symbol is rotatable.
    pub is_rotatable: bool,
    /// The graphical elements that make up this symbol.
    pub elements: Vec<Element>,

    /// Inner circle colour.
    pub inner_color: SymbolColor,
    /// Outer ring colour.
    pub outer_color: SymbolColor,
    /// Inner circle radius in mm.
    pub inner_radius: NonNegativeF64,
    /// Outer ring width in mm.
    pub outer_width: NonNegativeF64,
}

impl PointSymbol {
    /// Create a new empty point symbol with the given code and name.
    pub fn new(code: Code, name: impl Into<String>) -> PointSymbol {
        let common = SymbolCommon {
            code,
            name: name.into(),
            ..Default::default()
        };
        PointSymbol {
            common,
            is_rotatable: true,
            elements: Vec::new(),
            inner_color: SymbolColor::NoColor,
            outer_color: SymbolColor::NoColor,
            inner_radius: NonNegativeF64::default(),
            outer_width: NonNegativeF64::default(),
        }
    }

    /// Get the display name of this point symbol.
    pub fn get_name(&self) -> &str {
        &self.common.name
    }

    pub(super) fn parse<R: std::io::BufRead>(
        reader: &mut Reader<R>,
        color_set: &ColorSet,
        mut common: SymbolCommon,
    ) -> Result<PointSymbol> {
        let mut is_rotatable = false;
        let mut inner_radius = NonNegativeF64::default();
        let mut inner_color = SymbolColor::NoColor;
        let mut outer_width = NonNegativeF64::default();
        let mut outer_color = SymbolColor::NoColor;
        let mut elements = Vec::new();

        let mut buf = Vec::new();
        loop {
            match reader.read_event_into(&mut buf)? {
                Event::Start(e) => match e.local_name().as_ref() {
                    b"description" => {
                        if let Event::Text(text) = reader.read_event_into(&mut buf)? {
                            common.description = String::from_utf8(text.to_vec())?;
                        }
                    }
                    b"point_symbol" => {
                        is_rotatable = try_get_attr_raw(&e, "rotatable").unwrap_or(is_rotatable);
                        inner_radius = NonNegativeF64::from_file_value(
                            try_get_attr_raw(&e, "inner_radius").unwrap_or(0),
                        );
                        inner_color = SymbolColor::from_index(
                            try_get_attr_raw(&e, "inner_color").unwrap_or(-1),
                            color_set,
                        );
                        outer_width = NonNegativeF64::from_file_value(
                            try_get_attr_raw(&e, "outer_width").unwrap_or(0),
                        );
                        outer_color = SymbolColor::from_index(
                            try_get_attr_raw(&e, "outer_color").unwrap_or(-1),
                            color_set,
                        );
                    }
                    b"element" => elements.push(Element::parse_element(reader, color_set)?),
                    _ => {}
                },
                Event::Empty(e) => {
                    if e.local_name().as_ref() == b"icon"
                        && let Some(src) = try_get_attr_raw(&e, "src")
                    {
                        common.custom_icon = Some(src);
                    }
                }
                Event::End(e) => {
                    if e.local_name().as_ref() == b"symbol" {
                        break;
                    }
                }
                Event::Eof => {
                    return Err(Error::ParseOmapFileError(
                        "Unexpected EOF in PointSymbol parsing".to_string(),
                    ));
                }
                _ => {}
            }
        }

        // Check the point symbol for empty elements. Drop them
        let mut drop_elements = Vec::with_capacity(elements.len());
        for (i, element) in elements.iter().enumerate() {
            match element {
                Element::Point { symbol, object: _ } => {
                    if symbol.inner_color == SymbolColor::NoColor
                        && symbol.outer_color == SymbolColor::NoColor
                        && symbol.elements.is_empty()
                    {
                        drop_elements.push(i);
                    }
                }
                Element::Line { symbol: _, object } => {
                    if object.get_geometry().0.is_empty() {
                        drop_elements.push(i);
                    }
                }
                Element::Area { symbol: _, object } => {
                    if object.get_geometry().exterior().0.is_empty() {
                        drop_elements.push(i);
                    }
                }
            }
        }
        for i in drop_elements.into_iter().rev() {
            let _ = elements.swap_remove(i);
        }

        Ok(PointSymbol {
            common,
            is_rotatable,
            elements,
            inner_color,
            outer_color,
            inner_radius,
            outer_width,
        })
    }

    pub(super) fn write<W: std::io::Write>(
        &self,
        writer: &mut Writer<W>,
        color_set: &ColorSet,
        index: Option<usize>,
    ) -> Result<()> {
        // Elements do not have codes so we should skip code writing for the default code
        let code_str = if self.common.code != Default::default() {
            self.common.code.to_string()
        } else {
            String::new()
        };

        let mut bs = BytesStart::new("symbol").with_attributes([
            ("type", "1"),
            ("code", code_str.as_str()),
            (
                "name",
                quick_xml::escape::escape(self.common.name.as_str()).as_ref(),
            ),
        ]);
        if let Some(id) = index {
            bs.push_attribute(("id", id.to_string().as_str()));
        }
        if self.common.is_hidden {
            bs.push_attribute(("is_hidden", "true"));
        }
        if self.common.is_helper_symbol {
            bs.push_attribute(("is_helper_symbol", "true"));
        }
        if self.common.is_protected {
            bs.push_attribute(("is_protected", "true"));
        }
        writer.write_event(Event::Start(bs))?;

        if !self.common.description.is_empty() {
            writer.write_event(Event::Start(BytesStart::new("description")))?;
            writer.write_event(Event::Text(BytesText::new(&self.common.description)))?;
            writer.write_event(Event::End(BytesEnd::new("description")))?;
        }

        let mut bs = BytesStart::new("point_symbol");
        if self.is_rotatable {
            bs.push_attribute(("rotatable", "true"));
        }
        bs.push_attribute((
            "inner_radius",
            self.inner_radius.to_file_value()?.to_string().as_str(),
        ));
        bs.push_attribute((
            "inner_color",
            self.inner_color
                .get_priority(color_set)
                .to_string()
                .as_str(),
        ));
        bs.push_attribute((
            "outer_width",
            self.outer_width.to_file_value()?.to_string().as_str(),
        ));
        bs.push_attribute((
            "outer_color",
            self.outer_color
                .get_priority(color_set)
                .to_string()
                .as_str(),
        ));
        bs.push_attribute(("elements", self.elements.len().to_string().as_str()));
        writer.write_event(Event::Start(bs))?;

        for element in &self.elements {
            element.write(writer, color_set)?;
        }

        writer.write_event(Event::End(BytesEnd::new("point_symbol")))?;

        if let Some(icon) = &self.common.custom_icon {
            writer.write_event(Event::Empty(
                BytesStart::new("icon").with_attributes([("src", icon.as_str())]),
            ))?;
        }
        writer.write_event(Event::End(BytesEnd::new("symbol")))?;
        Ok(())
    }
}