sct_reader/package/
symbol.rs

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
use std::default;

use geojson::{Feature, Geometry, Value};
use serde::{Deserialize, Serialize};
use serde_json::Map;

use crate::loaders::euroscope::{
    position::{Position, Valid},
    symbology::SymbologyItemType,
};

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AtcMapSymbol {
    pub name: String,
    pub symbol_type: String,
    pub feature: Feature,
}

impl AtcMapSymbol {
    pub fn try_from_es_position(sector_file_id: String, item_type: String, ident: String, position: Position<Valid>) -> anyhow::Result<Self> {
        let id = format!("{}_{}_{}", sector_file_id.to_string(), item_type.to_string(), ident.to_string());
        // Properties
        let mut props_map = Map::new();
        props_map.insert("text".to_string(), serde_json::to_value(ident.to_string())?);

        Ok(AtcMapSymbol {
            name: id.to_string(),
            symbol_type: item_type.to_string(),
            feature: Feature {
                id: None,
                bbox: None,
                foreign_members: None,
                geometry: Some(Geometry::new(Value::Point(vec![position.lon, position.lat]))),
                properties: Some(props_map),
            },
        })
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum SymbolDrawItem {
    Line {
        start: (i8, i8),
        end: (i8, i8),
    },
    Polygon(Vec<(i8, i8)>),
    Arc {
        center: (i8, i8),
        radius: i8,
        inner_radius: i8,
        start_angle: i16,
        end_angle: i16,
        fill: bool,
    },
    SetPixel((i8, i8)),
    Ellipse {
        center: (i8, i8),
        radius: (i8, i8),
        inner_radius: (i8, i8),
        rotation: i16,
        start_angle: i16,
        end_angle: i16,
        fill: bool
    }
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct SymbolIcon {
    pub symbol_type: String,
    pub draw_items: Vec<SymbolDrawItem>,
}

impl SymbolIcon {
    pub fn try_from_es_symbol_icon(icon_index: u8, icon: Vec<String>) -> anyhow::Result<Self> {
        let name = match icon_index {
            0 => SymbologyItemType::Airports.to_key_string(),
            1 => SymbologyItemType::Ndbs.to_key_string(),
            2 => SymbologyItemType::Vors.to_key_string(),
            3 => SymbologyItemType::Fixes.to_key_string(),
            4 => "aircraft_stby".to_string(),
            5 => "aircraft_prim".to_string(),
            6 => "aircraft_corr_sec_a+c".to_string(),
            7 => "aircraft_corr_sec_s".to_string(),
            8 => "aircraft_corr_prim_a+c".to_string(),
            9 => "aircraft_corr_prim_s".to_string(),
            10 => "aircraft_corr_a+c_ident".to_string(),
            11 => "aircraft_corr_s_ident".to_string(),
            12 => "aircraft_flt_plan".to_string(),
            13 => "aircraft_coast".to_string(),
            14 => "history_dot".to_string(),
            15 => "aircraft_ground".to_string(),
            16 => "aircraft_uncorr_sec_a+c".to_string(),
            17 => "aircraft_uncorr_sec_s".to_string(),
            18 => "aircraft_uncorr_prim_a+c".to_string(),
            19 => "aircraft_uncorr_prim_s".to_string(),
            20 => "aircraft_uncorr_a+c_ident".to_string(),
            21 => "aircraft_uncorr_s_ident".to_string(),
            22 => "ground_vehicle".to_string(),
            23 => "ground_rotorcraft".to_string(),
            _ => "unknown".to_string(),
        };

        let mut draw_items = Vec::new();
        let mut cursor_pos = (0_i8, 0_i8);
        for line in icon {
            let split = line.split(" ").collect::<Vec<&str>>();
            if split.len() > 0 {
                match split[0] {
                    "MOVETO" => cursor_pos = (split[1].parse()?, -split[2].parse()?),
                    "LINETO" => {
                        let end_point = (split[1].parse()?, -split[2].parse::<i8>()?);
                        draw_items.push(SymbolDrawItem::Line {
                            start: cursor_pos,
                            end: end_point,
                        });
                        cursor_pos = end_point;
                    }
                    "SETPIXEL" => {
                        let point = (split[1].parse()?, -split[2].parse::<i8>()?);
                        draw_items.push(SymbolDrawItem::SetPixel(point));
                        cursor_pos = point;
                    }
                    "POLYGON" => {
                        let mut i = 1;
                        let mut points = Vec::new();
                        while i < split.len() - 1 {
                            let point = (split[i].parse::<i8>()?, -split[i + 1].parse::<i8>()?);
                            cursor_pos = point;
                            points.push(point);
                            i += 2;
                        }
                        draw_items.push(SymbolDrawItem::Polygon(points));
                    }
                    "ARC" => {
                        draw_items.push(SymbolDrawItem::Arc {
                            center: (split[1].parse()?, -split[2].parse()?),
                            radius: split[3].parse()?,
                            inner_radius: 0,
                            start_angle: split[4].parse()?,
                            end_angle: split[5].parse()?,
                            fill: false,
                        });
                    }
                    "FILLARC" => {
                        draw_items.push(SymbolDrawItem::Arc {
                            center: (split[1].parse()?, -split[2].parse()?),
                            radius: split[3].parse()?,
                            inner_radius: 0,
                            start_angle: split[4].parse()?,
                            end_angle: split[5].parse()?,
                            fill: true,
                        });
                    }
                    &_ => {}
                }
            }
        }

        Ok(SymbolIcon {
            symbol_type: name.to_string(),
            draw_items: draw_items,
        })
    }
}