forked from Maximkaaa/galileo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhighlight_features.rs
197 lines (172 loc) · 5.68 KB
/
highlight_features.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
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
//! This example shows how to change a feature appearance based on use input - pointing with a
//! mouse in this case.
use std::sync::Arc;
use galileo::control::{EventPropagation, UserEvent, UserEventHandler};
use galileo::decoded_image::DecodedImage;
use galileo::layer::feature_layer::symbol::Symbol;
use galileo::layer::feature_layer::{Feature, FeatureLayer};
use galileo::layer::raster_tile_layer::RasterTileLayerBuilder;
use galileo::render::point_paint::PointPaint;
use galileo::render::render_bundle::RenderPrimitive;
use galileo::{Map, MapBuilder};
use galileo_types::cartesian::{CartesianPoint3d, Point2d};
use galileo_types::geo::{Crs, Projection};
use galileo_types::geometry::Geom;
use galileo_types::geometry_type::CartesianSpace2d;
use galileo_types::impls::{Contour, Polygon};
use galileo_types::{latlon, CartesianGeometry2d, Geometry};
use nalgebra::Vector2;
use num_traits::AsPrimitive;
use parking_lot::RwLock;
const YELLOW_PIN: &[u8] = include_bytes!("data/pin-yellow.png");
const GREEN_PIN: &[u8] = include_bytes!("data/pin-green.png");
#[cfg(not(target_arch = "wasm32"))]
fn main() {
run()
}
pub(crate) fn run() {
let projection = Crs::EPSG3857
.get_projection()
.expect("must find projection");
let feature_layer = FeatureLayer::new(
[
latlon!(53.732562, -1.863383),
latlon!(53.728265, -1.839966),
latlon!(53.704014, -1.786128),
]
.iter()
.map(|point| PointMarker {
point: projection
.project(point)
.expect("point cannot be projected"),
..Default::default()
})
.collect(),
ColoredPointSymbol {
default_image: Arc::new(
DecodedImage::decode(YELLOW_PIN).expect("Must have Yellow Pin Image"),
),
highlighted_image: Arc::new(
DecodedImage::decode(GREEN_PIN).expect("Must have Green Pin Image"),
),
},
Crs::EPSG3857,
);
let feature_layer = Arc::new(RwLock::new(feature_layer));
let handler = create_mouse_handler(feature_layer.clone());
let mut map = create_map();
map.layers_mut().push(feature_layer);
galileo_egui::init(map, [Box::new(handler) as Box<dyn UserEventHandler>])
.expect("failed to initialize");
}
#[derive(Debug, PartialEq, Default)]
pub(crate) struct PointMarker {
pub(crate) point: Point2d,
pub(crate) highlighted: bool,
}
impl Feature for PointMarker {
type Geom = Self;
fn geometry(&self) -> &Self::Geom {
self
}
}
impl Geometry for PointMarker {
type Point = Point2d;
fn project<P: Projection<InPoint = Self::Point> + ?Sized>(
&self,
projection: &P,
) -> Option<Geom<P::OutPoint>> {
self.point.project(projection)
}
}
impl CartesianGeometry2d<Point2d> for PointMarker {
fn is_point_inside<
Other: galileo_types::cartesian::CartesianPoint2d<
Num = <Point2d as galileo_types::cartesian::CartesianPoint2d>::Num,
>,
>(
&self,
point: &Other,
tolerance: <Point2d as galileo_types::cartesian::CartesianPoint2d>::Num,
) -> bool {
self.point.is_point_inside(point, tolerance)
}
fn bounding_rectangle(
&self,
) -> Option<
galileo_types::cartesian::Rect<
<Point2d as galileo_types::cartesian::CartesianPoint2d>::Num,
>,
> {
None
}
}
fn create_mouse_handler(
feature_layer: Arc<
RwLock<FeatureLayer<Point2d, PointMarker, ColoredPointSymbol, CartesianSpace2d>>,
>,
) -> impl UserEventHandler {
move |ev: &UserEvent, map: &mut Map| {
if let UserEvent::PointerMoved(event) = ev {
let mut layer = feature_layer.write();
let Some(position) = map.view().screen_to_map(event.screen_pointer_position) else {
return EventPropagation::Stop;
};
for mut feature_container in layer.features_mut().iter_mut() {
feature_container.as_mut().highlighted = false;
}
for mut feature_container in
layer.get_features_at_mut(&position, map.view().resolution() * 20.0)
{
let state = feature_container.as_ref().highlighted;
feature_container.as_mut().highlighted = !state;
}
map.redraw();
}
galileo::control::EventPropagation::Propagate
}
}
fn create_map() -> Map {
let raster_layer = RasterTileLayerBuilder::new_osm()
.with_file_cache_checked(".tile_cache")
.build()
.expect("failed to create layer");
MapBuilder::default()
.with_latlon(53.732562, -1.863383)
.with_resolution(30.0)
.with_layer(raster_layer)
.build()
}
struct ColoredPointSymbol {
default_image: Arc<DecodedImage>,
highlighted_image: Arc<DecodedImage>,
}
impl Symbol<PointMarker> for ColoredPointSymbol {
fn render<'a, N, P>(
&self,
feature: &PointMarker,
geometry: &'a Geom<P>,
_min_resolution: f64,
) -> Vec<RenderPrimitive<'a, N, P, Contour<P>, Polygon<P>>>
where
N: AsPrimitive<f32>,
P: CartesianPoint3d<Num = N> + Clone,
{
if let Geom::Point(point) = geometry {
vec![RenderPrimitive::new_point(
point.clone(),
PointPaint::image(
if feature.highlighted {
self.default_image.clone()
} else {
self.highlighted_image.clone()
},
Vector2::new(0.5, 1.0),
1.0,
),
)]
} else {
vec![]
}
}
}