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
use std::fs::File;
use std::io::{BufRead, BufReader, BufWriter, Cursor, Write as _};
#[cfg(feature = "geo_ref")]
use crate::geo_referencing::CrsType;
#[cfg(feature = "geo_ref")]
use geo_types::Coord;
use quick_xml::{
Reader, Writer,
events::{BytesEnd, Event},
};
use crate::{
colors::ColorSet,
format_info::{OmapVersion, XmlDeclaration},
geo_referencing::{AffineMapTransform, GeoRef, MapTransform},
notes,
parts::MapPart,
parts::MapParts,
symbols::SymbolSet,
templates::Templates,
view::View,
{Error, Result},
};
const DEFAULT_ISOM_15000: &[u8] = include_bytes!("default_maps/isom_15000.omap");
const DEFAULT_ISOM_10000: &[u8] = include_bytes!("default_maps/isom_10000.omap");
const DEFAULT_ISSPROM_4000: &[u8] = include_bytes!("default_maps/issprom_4000.omap");
/// All objects are in map coordinates i.e given in mm of paper
/// relative the ref point with positive y towards the magnetic north
///
/// The Undo/Redo history and printer information is ignored
#[derive(Debug, Clone)]
pub struct Omap {
/// Free-text notes embedded in the file.
pub notes: String,
/// Georeferencing information (scale, CRS, reference points).
pub geo_referencing: GeoRef,
/// The ordered set of colors used by symbols.
pub colors: ColorSet,
/// The set of map symbols.
pub symbols: SymbolSet,
/// The map parts (layers) containing objects.
pub parts: MapParts,
/// Background templates attached to the map.
pub templates: Templates,
/// View settings (zoom, grid, visibility).
pub view: View,
}
impl Omap {
/// Create a new georeferenced `1:15_000` map with a complete ISOM symbolset and color order
///
/// # Errors
///
/// Returns an error if georeferencing cannot be initialized or the
/// embedded default map cannot be parsed.
#[cfg(feature = "geo_ref")]
pub fn default_15_000_geo_referenced(
projected_ref_point: Coord,
crs: CrsType,
meters_above_sea: f64,
) -> Result<Self> {
let geo_ref = GeoRef::initialize(projected_ref_point, crs, meters_above_sea, 15_000)?;
let mut omap = Self::from_bytes(DEFAULT_ISOM_15000)?;
omap.geo_referencing = geo_ref;
Ok(omap)
}
/// Create a new georeferenced `1:10_000` map with a complete ISOM symbolset and color order
///
/// # Errors
///
/// Returns an error if georeferencing cannot be initialized or the
/// embedded default map cannot be parsed.
#[cfg(feature = "geo_ref")]
pub fn default_10_000_geo_referenced(
projected_ref_point: Coord,
crs: CrsType,
meters_above_sea: f64,
) -> Result<Self> {
let geo_ref = GeoRef::initialize(projected_ref_point, crs, meters_above_sea, 10_000)?;
let mut omap = Self::from_bytes(DEFAULT_ISOM_10000)?;
omap.geo_referencing = geo_ref;
Ok(omap)
}
/// Create a new georeferenced `1:4_000` map with a complete `ISSprOM` symbolset and color order
///
/// # Errors
///
/// Returns an error if georeferencing cannot be initialized or the
/// embedded default map cannot be parsed.
#[cfg(feature = "geo_ref")]
pub fn default_4_000_geo_referenced(
projected_ref_point: Coord,
crs: CrsType,
meters_above_sea: f64,
) -> Result<Self> {
let geo_ref = GeoRef::initialize(projected_ref_point, crs, meters_above_sea, 4_000)?;
let mut omap = Self::from_bytes(DEFAULT_ISSPROM_4000)?;
omap.geo_referencing = geo_ref;
Ok(omap)
}
/// Create a new `1:15_000` map with a complete ISOM symbolset and color order
///
/// # Errors
///
/// Returns an error if the embedded default map cannot be parsed.
pub fn default_15_000() -> Result<Self> {
Self::from_bytes(DEFAULT_ISOM_15000)
}
/// Create a new `1:10_000` map with a complete ISOM symbolset and color order
///
/// # Errors
///
/// Returns an error if the embedded default map cannot be parsed.
pub fn default_10_000() -> Result<Self> {
Self::from_bytes(DEFAULT_ISOM_10000)
}
/// Create a new `1:4_000` map with a complete `ISSprOM` symbolset and color order
///
/// # Errors
///
/// Returns an error if the embedded default map cannot be parsed.
pub fn default_4_000() -> Result<Self> {
Self::from_bytes(DEFAULT_ISSPROM_4000)
}
/// Create a new empty map
pub fn new(scale_denominator: u32) -> Self {
Self {
notes: Default::default(),
geo_referencing: GeoRef::new(scale_denominator),
colors: ColorSet(Vec::new()),
symbols: SymbolSet {
symbols: Vec::new(),
name: "Custom".to_owned(),
},
parts: MapParts(vec![MapPart::new("Map")]),
templates: Default::default(),
view: Default::default(),
}
}
fn from_bytes(bytes: &'static [u8]) -> Result<Self> {
Self::from_reader(Cursor::new(bytes))
}
fn from_reader<R: BufRead>(reader: R) -> Result<Self> {
let mut reader = Reader::from_reader(reader);
reader.config_mut().expand_empty_elements = true;
// these must be parsed successfully
let mut georef = None;
let mut colors = None;
let mut symbols = None;
let mut parts = None;
// these have sensible defaults and are not worth bailing over if parsing fails
let mut notes = String::new();
let mut templates = Templates::default();
let mut view = View::default();
let mut xml_buf = Vec::new();
loop {
match reader.read_event_into(&mut xml_buf)? {
Event::Decl(dec) => XmlDeclaration::parse(dec)?,
Event::Start(bytes_start) => match bytes_start.local_name().as_ref() {
b"map" => OmapVersion::parse(&bytes_start)?,
b"notes" => notes = notes::parse(&mut reader).unwrap_or_default(),
b"georeferencing" => georef = Some(GeoRef::parse(&mut reader, &bytes_start)?),
b"colors" => colors = Some(ColorSet::parse(&mut reader, &bytes_start)?),
b"symbols" => {
if let Some(colors) = &colors {
symbols = Some(SymbolSet::parse(&mut reader, &bytes_start, colors)?);
} else {
return Err(Error::SectionOutOfOrder {
section: crate::OmapSection::Symbols,
required_before: crate::OmapSection::Colors,
});
}
}
b"parts" => {
if let Some(symbols) = &symbols {
parts = Some(MapParts::parse(&mut reader, symbols)?);
} else {
return Err(Error::SectionOutOfOrder {
section: crate::OmapSection::Parts,
required_before: crate::OmapSection::Symbols,
});
}
}
b"templates" => {
templates = Templates::parse(&mut reader, &bytes_start).unwrap_or_default();
}
b"view" => {
view = View::parse(&mut reader, &bytes_start, &mut templates)
.unwrap_or_default();
}
_ => (),
},
Event::Eof => break,
_ => (),
}
}
Ok(Self {
notes,
geo_referencing: georef.ok_or(Error::MissingRequiredSection(
crate::OmapSection::Georeferencing,
))?,
colors: colors.ok_or(Error::MissingRequiredSection(crate::OmapSection::Colors))?,
symbols: symbols.ok_or(Error::MissingRequiredSection(crate::OmapSection::Symbols))?,
parts: parts.ok_or(Error::MissingRequiredSection(crate::OmapSection::Parts))?,
templates,
view,
})
}
/// Create an [`Omap`] from a path to an `.omap` file.
///
/// Parsing is intentionally permissive for some sections.
/// This function falls back to sensible defaults
/// for `notes`, `templates`, or `view`
/// if those sections cannot be parsed
///
/// `barrier`s, `undo` and `redo` sections of the file are ignored
///
/// The core sections `georeferencing`, `colors`, `symbols`, and `parts`
/// must still parse successfully or else loading fails.
///
/// # Errors
///
/// Returns an error if the file cannot be opened or a required map section
/// cannot be parsed.
pub fn from_path(path: impl AsRef<std::path::Path>) -> Result<Self> {
let file = File::open(path)?;
Self::from_reader(BufReader::new(file))
}
/// Write the map to an `.omap` file at the given path.
///
/// # Errors
///
/// Returns an error if the file cannot be created or any map data cannot
/// be serialized.
pub fn write_to_file(self, path: impl AsRef<std::path::Path>) -> Result<()> {
let file = File::create(path)?;
let mut writer = Writer::new(BufWriter::new(file));
XmlDeclaration::write(&mut writer)?;
writer.get_mut().write_all(b"\n".as_slice())?;
OmapVersion::write(&mut writer)?;
writer.get_mut().write_all(b"\n".as_slice())?;
notes::write(self.notes.as_str(), &mut writer)?;
writer.get_mut().write_all(b"\n".as_slice())?;
self.geo_referencing.write(&mut writer)?;
writer.get_mut().write_all(b"\n".as_slice())?;
// write objects to a buffer
let mut object_writer = Writer::new(Vec::new());
self.parts.write(&mut object_writer, &self.symbols)?;
let written_objects = object_writer.into_inner();
// write symbolset to a buffer
let mut symbol_writer = Writer::new(Vec::new());
self.symbols.write(&mut symbol_writer, &self.colors)?;
let written_symbols = symbol_writer.into_inner();
// write colors
self.colors.write(&mut writer)?;
writer.get_mut().write_all(b"\n".as_slice())?;
writer.get_mut().write_all(b"\n".as_slice())?;
writer.get_mut().flush()?;
writer.get_mut().write_all(&written_symbols)?;
writer.get_mut().write_all(b"\n".as_slice())?;
writer.get_mut().flush()?;
writer.get_mut().write_all(&written_objects)?;
writer.get_mut().write_all(b"\n".as_slice())?;
let vis = self.templates.write(&mut writer)?;
writer.get_mut().write_all(b"\n".as_slice())?;
self.view.write(&mut writer, vis)?;
writer.get_mut().write_all(b"\n".as_slice())?;
writer.write_event(Event::End(BytesEnd::new("map")))?;
Ok(())
}
/// Apply an [`AffineMapTransform`] to every object and non-georeferenced
/// template in the map.
///
/// Use this after changing the georeferencing (within the same projection)
/// to keep objects and non-georeferenced templates at the same real-world
/// positions. Obtain the transform with
/// [`MapTransform::affine_between`].
pub fn apply_affine(&mut self, transform: &AffineMapTransform) {
for part in &mut self.parts.0 {
for object in part.iter_all_objects_mut() {
object.apply_affine(transform);
}
}
self.templates.apply_affine(transform);
}
/// Compute the affine transform between two [`MapTransform`]s and apply it
/// to every object and non-georeferenced template. This is a convenience
/// wrapper around [`MapTransform::affine_between`] + [`Omap::apply_affine`].
///
/// # Errors
///
/// Returns an error if `old` and `new` use different projections.
pub fn apply_affine_between(&mut self, old: &MapTransform, new: &MapTransform) -> Result<()> {
let affine = MapTransform::affine_between(old, new)?;
self.apply_affine(&affine);
Ok(())
}
}