pub struct ColorSet(pub Vec<Color>);Expand description
An ordered set of map colors.
The order of the Color values in the Vec determines their priority.
Move colors to change priority, for example with color_set.0.swap(2, 5).
Deleting a color drops its allocation if there are no outstanding Rc
references. Symbols and ColorComponent values only hold
std::rc::Weak references. A dangling weak reference contributes no color
when the map is written.
Tuple Fields§
§0: Vec<Color>Implementations§
Source§impl ColorSet
impl ColorSet
Sourcepub fn insert(
&mut self,
index: usize,
color: impl Into<Color>,
) -> Result<(), Error>
pub fn insert( &mut self, index: usize, color: impl Into<Color>, ) -> Result<(), Error>
Insert a new color into the ColorSet with priority index, fails if index > self.len()
§Errors
Returns Error::ColorError if index is greater than the number of
colors.
Sourcepub fn get_color_by_priority(&self, priority: usize) -> Option<&Color>
pub fn get_color_by_priority(&self, priority: usize) -> Option<&Color>
Get a color by its priority index.
Sourcepub fn get_weak_color_by_priority(&self, priority: usize) -> Option<WeakColor>
pub fn get_weak_color_by_priority(&self, priority: usize) -> Option<WeakColor>
Get a weak reference to a color by its priority index.
Sourcepub fn get_color_by_name(&self, name: &str) -> Option<&Color>
pub fn get_color_by_name(&self, name: &str) -> Option<&Color>
Get the first color with an exact name match
Sourcepub fn get_id_of_color(&self, color: &Color) -> Option<usize>
pub fn get_id_of_color(&self, color: &Color) -> Option<usize>
Get the priority index of a specific color in the set (by pointer identity).
Sourcepub fn iter(&self) -> impl Iterator<Item = &Color>
pub fn iter(&self) -> impl Iterator<Item = &Color>
Access the colors through an iterator
Examples found in repository?
20fn main() -> Result<(), Error> {
21 #[cfg(feature = "geo_ref")]
22 let mut map = {
23 let proj_center = Coord {
24 x: 463_575.5,
25 y: 6_833_849.6,
26 };
27 let map_center_elevation_meters = 2_469.;
28 let crs_epsg_code = 25832;
29 Omap::default_15_000_geo_referenced(
30 proj_center,
31 CrsType::Epsg(crs_epsg_code),
32 map_center_elevation_meters,
33 )?
34 };
35 #[cfg(not(feature = "geo_ref"))]
36 let mut map = Omap::default_15_000()?;
37
38 for color in map.colors.iter() {
39 match color {
40 Color::SpotColor(ref_cell) => {
41 let b = ref_cell.try_borrow().unwrap();
42 println!("{} with spot name {}", b.color_name, b.spotcolor_name);
43 }
44 Color::MixedColor(ref_cell) => {
45 println!("{}", ref_cell.try_borrow().unwrap().color_name);
46 }
47 }
48 }
49
50 let erosion_gully = map
51 .symbols
52 .get_symbol_by_code(Code::new(107, 0, 0))
53 .unwrap()
54 .downgrade();
55
56 let mut ls = LineObject::new(
57 WeakLinePathSymbol::try_from(erosion_gully).unwrap(),
58 // geometry coordinates are always in mm of paper
59 LineString::new(vec![Coord { x: 0., y: 0. }, Coord { x: 200., y: 100. }]),
60 );
61 ls.tags.insert("Some Key".to_owned(), "My value".to_owned());
62
63 map.parts.0[0].add_object(ls);
64
65 println!("\nCombined Line symbols:");
66 for symbol in map.symbols.iter() {
67 if let Symbol::CombinedLine(s) = symbol {
68 println!("{}", s.borrow().common.name);
69 }
70 }
71 if let Some(s) = map.symbols.get_symbol_by_name("Railway, Olive background") {
72 println!("{s:?}");
73 }
74
75 let mut num = 0;
76 for symbol in map.symbols.iter() {
77 if let Symbol::Line(s) = symbol {
78 let borrowed = s.borrow();
79
80 if let Some(_ss_) = &borrowed.start_symbol {
81 num += 1;
82 }
83 if let Some(_ms_) = &borrowed.mid_symbol {
84 num += 1;
85 }
86 if let Some(_ds_) = &borrowed.dash_symbol {
87 num += 1;
88 }
89 if let Some(_es_) = &borrowed.end_symbol {
90 num += 1;
91 }
92 }
93 if let Symbol::CombinedLine(s) = symbol {
94 let borrowed = s.borrow();
95
96 for part in borrowed.components() {
97 if let PublicOrPrivateSymbol::Private(s) = part {
98 if let Some(_ss_) = &s.start_symbol {
99 num += 1;
100 }
101 if let Some(_ms_) = &s.mid_symbol {
102 num += 1;
103 }
104 if let Some(_ds_) = &s.dash_symbol {
105 num += 1;
106 }
107 if let Some(_es_) = &s.end_symbol {
108 num += 1;
109 }
110 }
111 }
112 }
113 }
114 println!("\nNumber of sub symbols in line symbols: {num}");
115
116 map.write_to_file("./test_write.omap")
117}More examples
28fn main() -> Result<(), Error> {
29 let mut map = Omap::from_path("./example_data/from_path.omap")?;
30
31 #[cfg(feature = "geo_ref")]
32 {
33 // we want to move the map center to the average position of all objects
34 let old_transform = map.geo_referencing.get_transform();
35
36 let mut mean_pos = Coord::zero();
37 let mut num_coords = 0;
38 for obj in map.parts.iter().flat_map(|part| part.iter_all_objects()) {
39 match obj {
40 MapObject::Point(object) => {
41 mean_pos = mean_pos + object.get_geometry().0;
42 num_coords += 1;
43 }
44 MapObject::Line(object) => {
45 let geometry = object.get_geometry(0.1)?;
46 mean_pos =
47 mean_pos + geometry.0.iter().copied().reduce(|sum, c| sum + c).unwrap();
48 num_coords += geometry.0.len();
49 }
50 MapObject::Area(object) => {
51 let geometry = object.get_geometry(0.1)?;
52 mean_pos = mean_pos
53 + geometry
54 .exterior()
55 .0
56 .iter()
57 .copied()
58 .reduce(|sum, c| sum + c)
59 .unwrap();
60 num_coords += geometry.exterior().0.len();
61 }
62 MapObject::Text(object) => {
63 match object.get_geometry() {
64 TextGeometry::SingleAnchor(coord) => mean_pos = mean_pos + *coord,
65 TextGeometry::WrapBox(wrap_box) => mean_pos = mean_pos + wrap_box.anchor,
66 }
67 num_coords += 1;
68 }
69 }
70 }
71 mean_pos = mean_pos / num_coords as f64;
72
73 // now transform that into projected coords
74 let mean_proj_pos = old_transform.to_projected(mean_pos);
75
76 // get the new georef info for that position
77 let new_gr = GeoRef::initialize(
78 mean_proj_pos,
79 map.geo_referencing.crs_type,
80 2_469.,
81 map.geo_referencing.scale_denominator,
82 )
83 .unwrap();
84
85 // assign the new info
86 map.geo_referencing = new_gr;
87
88 // get the new map transform
89 let new_transform = map.geo_referencing.get_transform();
90
91 // transfrom every object out of the old map space to projected coords
92 // and from projected coord to the new map space
93 // NB! If the new projection were different than the old,
94 // a transfrom between projections using a proj library like proj-core would be needed
95 // and this function would return Err
96 map.apply_affine_between(&old_transform, &new_transform)
97 .unwrap();
98 };
99
100 println!("Map colors in order:");
101 for color in map.colors.iter() {
102 match color {
103 Color::SpotColor(ref_cell) => {
104 let b = ref_cell.try_borrow().unwrap();
105 println!("{} with spot name {}", b.color_name, b.spotcolor_name);
106 }
107 Color::MixedColor(ref_cell) => {
108 println!("{}", ref_cell.try_borrow().unwrap().color_name);
109 }
110 }
111 }
112
113 let erosion_gully = map
114 .symbols
115 .get_symbol_by_code(Code::new(107, 0, 0))
116 .unwrap()
117 .downgrade();
118
119 let mut ls = LineObject::new(
120 WeakLinePathSymbol::try_from(erosion_gully).unwrap(),
121 // geometry coordinates are always in mm of paper
122 LineString::new(vec![Coord { x: -60., y: -50. }, Coord { x: 60., y: -50. }]),
123 );
124 ls.tags.insert("Some Key".to_owned(), "My value".to_owned());
125
126 map.parts.0[0].add_object(ls);
127
128 let weak_symbol = map
129 .symbols
130 .get_symbol_by_name("Contour value")
131 .unwrap()
132 .downgrade();
133
134 let ts = TextObject::new(
135 Weak::<RefCell<TextSymbol>>::try_from(weak_symbol)
136 .expect("The symbol type of Contour value is not Text"),
137 TextGeometry::SingleAnchor(Coord { x: 0., y: 0. }),
138 "This is the middle of the map".to_owned(),
139 );
140 map.parts.0[0].add_object(ts);
141
142 map.write_to_file("./from_path_out.omap")
143}Trait Implementations§
Auto Trait Implementations§
impl !RefUnwindSafe for ColorSet
impl !Send for ColorSet
impl !Sync for ColorSet
impl !UnwindSafe for ColorSet
impl Freeze for ColorSet
impl Unpin for ColorSet
impl UnsafeUnpin for ColorSet
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more