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
use std::collections::HashMap;
use geo_types::{Coord, LineString};
use linestring2bezier::{BezierCurve, BezierSegment, BezierString};
use quick_xml::{
Reader, Writer,
events::{BytesEnd, BytesStart, BytesText, Event},
};
use super::{FileCoord, PARSE_BEZIER_ERROR};
use crate::{
Error, Result,
symbols::{Symbol, SymbolSet, WeakLinePathSymbol},
utils::{from_file_coords, to_file_coords, try_get_attr_raw},
};
/// A line object represented as a polyline on the map.
#[derive(Debug, Clone)]
pub struct LineObject {
/// The tags associated with the object
pub tags: HashMap<String, String>,
/// The line or combined-line symbol used to render this object.
pub symbol: WeakLinePathSymbol,
/// Whether the coordinates should be written back as bezier curves.
pub write_as_bezier: bool,
geometry: LineString,
// store the raw map-file coords with flags so that the object can be written back unchanged if the coords are untouched
// (so that the errors introduced when mapping from beziers to linestring and back only are introduced when necessary)
raw_map_coords: Vec<FileCoord>,
is_coords_touched: bool,
}
impl LineObject {
/// Create a new line object with the given symbol and geometry.
pub fn new(symbol: impl Into<WeakLinePathSymbol>, geometry: LineString) -> Self {
LineObject {
tags: HashMap::new(),
symbol: symbol.into(),
write_as_bezier: false,
geometry,
raw_map_coords: Vec::new(),
is_coords_touched: true,
}
}
/// Get a shared reference to the line geometry.
pub fn get_geometry(&self) -> &LineString {
&self.geometry
}
/// Get a mutable reference to the line geometry (marks coords as touched).
pub fn get_geometry_mut(&mut self) -> &mut LineString {
self.is_coords_touched = true;
&mut self.geometry
}
/// Consume this object and return its geometry.
pub fn into_geometry(self) -> LineString {
self.geometry
}
/// Create a LineObject for use as a PointSymbol element (no map symbol needed)
pub fn new_element(geometry: LineString) -> Self {
LineObject {
tags: HashMap::new(),
symbol: WeakLinePathSymbol::Line(std::rc::Weak::new()),
write_as_bezier: false,
geometry,
raw_map_coords: Vec::new(),
is_coords_touched: true,
}
}
/// Get coords for element writing
pub fn get_element_coords(&self) -> impl Iterator<Item = &Coord<f64>> {
self.geometry.coords()
}
/// Reverses a geometry and the input xml without marking it as touched
pub fn reverse_linestring(&mut self) {
self.geometry.0.reverse();
self.raw_map_coords = reverse_raw_line_coords(&self.raw_map_coords);
}
pub(super) fn write<W: std::io::Write>(
&self,
writer: &mut Writer<W>,
symbol_set: &SymbolSet,
) -> Result<()> {
let idx = match &self.symbol {
WeakLinePathSymbol::Line(weak) => {
if let Some(sym) = weak.upgrade() {
symbol_set
.iter()
.position(|s| match s {
Symbol::Line(ref_cell) => ref_cell.as_ptr() == sym.as_ptr(),
_ => false,
})
.map(|p| p as i32)
.unwrap_or(-1)
} else {
-1
}
}
WeakLinePathSymbol::CombinedLine(weak) => {
if let Some(sym) = weak.upgrade() {
symbol_set
.iter()
.position(|s| match s {
Symbol::CombinedLine(ref_cell) => ref_cell.as_ptr() == sym.as_ptr(),
_ => false,
})
.map(|p| p as i32)
.unwrap_or(-1)
} else {
-1
}
}
};
self.write_content(writer, Some(idx))?;
Ok(())
}
/// Write a full `<object>...</object>` element - used for point symbol elements
pub(crate) fn write_as_element<W: std::io::Write>(&self, writer: &mut Writer<W>) -> Result<()> {
self.write_content(writer, None)?;
Ok(())
}
/// Write the object
/// Uses raw coords if untouched, otherwise writes geometry
fn write_content<W: std::io::Write>(
&self,
writer: &mut Writer<W>,
symbol_index: Option<i32>,
) -> Result<()> {
let mut bs = BytesStart::new("object").with_attributes([("type", "1")]);
if let Some(sid) = symbol_index {
bs.push_attribute(("symbol", sid.to_string().as_str()));
}
writer.write_event(Event::Start(bs))?;
// elements are not allowed to have tags
if !self.tags.is_empty() && symbol_index.is_some() {
super::write_tags(writer, &self.tags)?;
}
if !self.is_coords_touched && !self.raw_map_coords.is_empty() {
super::write_raw_coords(writer, &self.raw_map_coords)?;
} else {
if self.geometry.0.len() < 2 {
return Err(Error::ObjectError);
}
self.write_geometry_coords(writer)?;
}
writer.write_event(Event::End(BytesEnd::new("object")))?;
Ok(())
}
/// Write coords from the geometry, as bezier if self.write_as_bezier
fn write_geometry_coords<W: std::io::Write>(&self, writer: &mut Writer<W>) -> Result<()> {
let content = if self.write_as_bezier {
let bezier = BezierString::from_line_string(self.geometry.clone(), PARSE_BEZIER_ERROR)?;
let num_coords = bezier.num_points();
let bs = BytesStart::new("coords")
.with_attributes([("count", num_coords.to_string().as_str())]);
writer.write_event(Event::Start(bs))?;
let mut content = String::with_capacity(num_coords * 18);
let mut i = 0;
while i < bezier.num_segments() - 1 {
let current_segment = &bezier.0[i];
match current_segment {
BezierSegment::Bezier(bezier_curve) => {
let s = to_file_coords(bezier_curve.start)?;
content.push_str(&s.x.to_string());
content.push(' ');
content.push_str(&s.y.to_string());
content.push_str(" 1;");
let h1 = to_file_coords(bezier_curve.handle1)?;
content.push_str(&h1.x.to_string());
content.push(' ');
content.push_str(&h1.y.to_string());
content.push(';');
let h2 = to_file_coords(bezier_curve.handle2)?;
content.push_str(&h2.x.to_string());
content.push(' ');
content.push_str(&h2.y.to_string());
content.push(';');
}
BezierSegment::Line(line) => {
let c = to_file_coords(line.start)?;
content.push_str(&c.x.to_string());
content.push(' ');
content.push_str(&c.y.to_string());
content.push(';');
}
}
i += 1;
}
let last_segment = &bezier.0[bezier.num_segments() - 1];
match last_segment {
BezierSegment::Bezier(bezier_curve) => {
let s = to_file_coords(bezier_curve.start)?;
content.push_str(&s.x.to_string());
content.push(' ');
content.push_str(&s.y.to_string());
content.push_str(" 1;");
let h1 = to_file_coords(bezier_curve.handle1)?;
content.push_str(&h1.x.to_string());
content.push(' ');
content.push_str(&h1.y.to_string());
content.push(';');
let h2 = to_file_coords(bezier_curve.handle2)?;
content.push_str(&h2.x.to_string());
content.push(' ');
content.push_str(&h2.y.to_string());
content.push(';');
let e = to_file_coords(bezier_curve.end)?;
content.push_str(&e.x.to_string());
content.push(' ');
content.push_str(&e.y.to_string());
if self.geometry.is_closed() {
content.push_str(" 18;");
} else {
content.push(';');
}
}
BezierSegment::Line(line) => {
let c = to_file_coords(line.start)?;
content.push_str(&c.x.to_string());
content.push(' ');
content.push_str(&c.y.to_string());
content.push(';');
let c = to_file_coords(line.end)?;
content.push_str(&c.x.to_string());
content.push(' ');
content.push_str(&c.y.to_string());
if self.geometry.is_closed() {
content.push_str(" 18;");
} else {
content.push(';');
}
}
}
content
} else {
let num_coords = self.geometry.0.len();
let bs = BytesStart::new("coords")
.with_attributes([("count", num_coords.to_string().as_str())]);
writer.write_event(Event::Start(bs))?;
let mut content = String::with_capacity(num_coords * 16);
for coord in self.geometry.coords() {
let fc = to_file_coords(*coord)?;
content.push_str(&fc.x.to_string());
content.push(' ');
content.push_str(&fc.y.to_string());
content.push(';');
}
content
};
writer.write_event(Event::Text(BytesText::new(&content)))?;
writer.write_event(Event::End(BytesEnd::new("coords")))?;
Ok(())
}
/// Parse a line object. The reader should be positioned right after the `<object>` start event. Reads through `</object>`.
pub(crate) fn parse<R: std::io::BufRead>(
reader: &mut Reader<R>,
symbol: WeakLinePathSymbol,
) -> Result<LineObject> {
let mut tags = HashMap::new();
let mut line = Vec::new();
let mut raw_map_coords = Vec::new();
let mut buf = Vec::new();
loop {
match reader.read_event_into(&mut buf)? {
Event::Start(bytes_start) => match bytes_start.local_name().as_ref() {
b"coords" => {
let num_coords = try_get_attr_raw(&bytes_start, "count").unwrap_or(0);
line.reserve(num_coords);
raw_map_coords.reserve(num_coords);
}
b"tags" => tags = super::parse_tags(reader)?,
_ => (),
},
Event::End(bytes_end) => {
if matches!(bytes_end.local_name().as_ref(), b"object") {
break;
}
}
Event::Text(bytes_text) => {
let raw_xml = str::from_utf8(bytes_text.as_ref())?;
let mut next_handle = 0_u8;
let mut bezier_buf = BezierString::empty();
let mut bezier_curve_buf = BezierCurve::zero();
for vertex in raw_xml.split_terminator(';') {
let mut parts: (i32, i32, u8) = (0, 0, 0);
let mut split = vertex.split_whitespace();
parts.0 = split
.next()
.ok_or(Error::InvalidCoordinate("No x value in split".to_string()))?
.parse()?;
parts.1 = split
.next()
.ok_or(Error::InvalidCoordinate("No y value in split".to_string()))?
.parse()?;
if let Some(e) = split.next() {
parts.2 = e.parse()?;
}
raw_map_coords.push((
Coord {
x: parts.0,
y: parts.1,
},
parts.2,
));
let coord = from_file_coords(Coord {
x: parts.0,
y: parts.1,
});
// for lines we only care about the bezier flag 1
// check for start of bezier flag, and how far along a bezier we are
match (parts.2 & 1 == 1, next_handle) {
(true, 0) => {
// bezier start
bezier_curve_buf.start = coord;
next_handle += 1;
}
(true, 3) => {
// bezier end and next start
bezier_curve_buf.end = coord;
bezier_buf
.0
.push(BezierSegment::Bezier(bezier_curve_buf.clone()));
bezier_curve_buf.start = coord;
next_handle = 1;
}
(false, 1) => {
// bezier first handle
bezier_curve_buf.handle1 = coord;
next_handle += 1;
}
(false, 2) => {
// bezier second handle
bezier_curve_buf.handle2 = coord;
next_handle += 1;
}
(false, 3) => {
// end point
bezier_curve_buf.end = coord;
bezier_buf
.0
.push(BezierSegment::Bezier(bezier_curve_buf.clone()));
// convert the bezier to line string and add to end of line
line.extend(
bezier_buf
.clone()
.to_line_string(PARSE_BEZIER_ERROR)?
.into_inner(),
);
next_handle = 0;
}
(false, 0) => {
// normal coord
line.push(coord);
}
_ => return Err(Error::ObjectError),
}
}
}
Event::Eof => {
return Err(Error::ParseOmapFileError(
"Unexpected EOF in LineObject parsing".to_string(),
));
}
_ => (),
}
}
Ok(LineObject {
tags,
symbol,
write_as_bezier: false,
geometry: LineString::new(line),
raw_map_coords,
is_coords_touched: false,
})
}
}
pub(crate) fn reverse_raw_line_coords(coords: &[FileCoord]) -> Vec<FileCoord> {
// iterate through and check the flags
// flags 1, 2 and 16 must be moved
// 2 and 16 can only exist at the end and must be moved there
// flag 1 must be moved to the other end of the bezier
let mut new_xml = Vec::with_capacity(coords.len());
let mut end_flag = 0;
for i in (0..coords.len()).rev() {
let (coord, mut flag) = coords[i];
// remove a possible bezier flag
flag -= flag & 1;
if i == coords.len() - 1 {
end_flag += (flag & 2) + (flag & 16);
flag -= end_flag;
}
if i > 2 {
// check the flag of i + 3 for a 1
let (_, bez_flag) = coords[i - 3];
flag |= bez_flag & 1;
} else if i == 0 {
flag |= end_flag;
}
new_xml.push((coord, flag));
}
new_xml
}