Browse Source

Add coord::LatLon

Johannes Hofmann 7 years ago
parent
commit
fc394f3d20
4 changed files with 40 additions and 19 deletions
  1. 24
    7
      src/coord.rs
  2. 6
    6
      src/main.rs
  3. 9
    5
      src/search.rs
  4. 1
    1
      src/tile_layer.rs

+ 24
- 7
src/coord.rs View File

1
 use std::f64::consts::PI;
1
 use std::f64::consts::PI;
2
 use tile_source::TileSourceId;
2
 use tile_source::TileSourceId;
3
 
3
 
4
+
5
+/// A position in latitude, longitude.
6
+#[derive(Copy, Debug, PartialEq, Clone)]
7
+pub struct LatLon {
8
+    pub lat: f64,
9
+    pub lon: f64,
10
+}
11
+
12
+impl LatLon {
13
+    pub fn new(lat: f64, lon: f64) -> Self {
14
+        LatLon { lat, lon }
15
+    }
16
+}
17
+
4
 /// A position in map coordinates.
18
 /// A position in map coordinates.
5
 /// Valid values for x and y lie in the interval [0.0, 1.0].
19
 /// Valid values for x and y lie in the interval [0.0, 1.0].
6
 #[derive(Copy, Debug, PartialEq, Clone)]
20
 #[derive(Copy, Debug, PartialEq, Clone)]
9
     pub y: f64,
23
     pub y: f64,
10
 }
24
 }
11
 
25
 
12
-impl MapCoord {
13
-    pub fn new(x: f64, y: f64) -> MapCoord {
26
+impl From<LatLon> for MapCoord
27
+{
28
+    fn from(pos: LatLon) -> MapCoord {
29
+        let x = pos.lon * (1.0 / 360.0) + 0.5;
30
+        let pi_lat = pos.lat * (PI / 180.0);
31
+        let y = f64::ln(f64::tan(pi_lat) + 1.0 / f64::cos(pi_lat)) * (-0.5 / PI) + 0.5;
32
+
14
         MapCoord { x, y }
33
         MapCoord { x, y }
15
     }
34
     }
35
+}
16
 
36
 
17
-    pub fn from_latlon(latitude: f64, longitude: f64) -> MapCoord {
18
-        let x = longitude * (1.0 / 360.0) + 0.5;
19
-        let pi_lat = latitude * (PI / 180.0);
20
-        let y = f64::ln(f64::tan(pi_lat) + 1.0 / f64::cos(pi_lat)) * (-0.5 / PI) + 0.5;
21
-
37
+impl MapCoord {
38
+    pub fn new(x: f64, y: f64) -> MapCoord {
22
         MapCoord { x, y }
39
         MapCoord { x, y }
23
     }
40
     }
24
 
41
 

+ 6
- 6
src/main.rs View File

36
 pub mod url_template;
36
 pub mod url_template;
37
 pub mod vertex_attrib;
37
 pub mod vertex_attrib;
38
 
38
 
39
-use coord::ScreenCoord;
39
+use coord::{LatLon, ScreenCoord};
40
 use glutin::{ControlFlow, ElementState, Event, GlContext, MouseButton, MouseScrollDelta, VirtualKeyCode, WindowEvent};
40
 use glutin::{ControlFlow, ElementState, Event, GlContext, MouseButton, MouseScrollDelta, VirtualKeyCode, WindowEvent};
41
 use map_view_gl::MapViewGl;
41
 use map_view_gl::MapViewGl;
42
 use std::error::Error;
42
 use std::error::Error;
76
     map: &mut MapViewGl,
76
     map: &mut MapViewGl,
77
     input_state: &mut InputState,
77
     input_state: &mut InputState,
78
     sources: &mut TileSources,
78
     sources: &mut TileSources,
79
-    marker_rx: &mpsc::Receiver<(f64, f64)>,
79
+    marker_rx: &mpsc::Receiver<LatLon>,
80
 ) -> Action {
80
 ) -> Action {
81
     match *event {
81
     match *event {
82
         Event::Awakened => {
82
         Event::Awakened => {
83
-            for (lat, lon) in marker_rx.try_iter() {
84
-                map.add_marker(coord::MapCoord::from_latlon(lat, lon));
83
+            for pos in marker_rx.try_iter() {
84
+                map.add_marker(pos.into());
85
             }
85
             }
86
             Action::Redraw
86
             Action::Redraw
87
         },
87
         },
250
         search::search_pbf(
250
         search::search_pbf(
251
             path,
251
             path,
252
             pattern,
252
             pattern,
253
-            move |lat, lon| {
254
-                if marker_tx.send((lat, lon)).is_err() {
253
+            move |latlon| {
254
+                if marker_tx.send(latlon).is_err() {
255
                     return search::ControlFlow::Break;
255
                     return search::ControlFlow::Break;
256
                 }
256
                 }
257
                 proxy.wakeup().into()
257
                 proxy.wakeup().into()

+ 9
- 5
src/search.rs View File

2
 use regex::Regex;
2
 use regex::Regex;
3
 use std::path::{Path, PathBuf};
3
 use std::path::{Path, PathBuf};
4
 use std::thread;
4
 use std::thread;
5
+use coord::LatLon;
6
+
5
 
7
 
6
 #[derive(Debug, Eq, PartialEq)]
8
 #[derive(Debug, Eq, PartialEq)]
7
 pub enum ControlFlow {
9
 pub enum ControlFlow {
26
     update_func: F,
28
     update_func: F,
27
 ) -> Result<thread::JoinHandle<()>, String>
29
 ) -> Result<thread::JoinHandle<()>, String>
28
 where P: AsRef<Path>,
30
 where P: AsRef<Path>,
29
-      F: Fn(f64, f64) -> ControlFlow + Send + 'static,
31
+      F: Fn(LatLon) -> ControlFlow + Send + 'static,
30
 {
32
 {
31
     let pathbuf = PathBuf::from(pbf_path.as_ref());
33
     let pathbuf = PathBuf::from(pbf_path.as_ref());
32
     let re = Regex::new(search_pattern)
34
     let re = Regex::new(search_pattern)
40
                 Element::Node(node) => {
42
                 Element::Node(node) => {
41
                     for (_key, val) in node.tags() {
43
                     for (_key, val) in node.tags() {
42
                         if re.is_match(val) {
44
                         if re.is_match(val) {
43
-                            if update_func(node.lat(), node.lon()) == ControlFlow::Break {
45
+                            let pos = LatLon::new(node.lat(), node.lon());
46
+                            if update_func(pos) == ControlFlow::Break {
44
                                 return;
47
                                 return;
45
                             }
48
                             }
46
                             break;
49
                             break;
47
                         }
50
                         }
48
                     }
51
                     }
49
                 },
52
                 },
50
-                Element::DenseNode(dnode) => {
51
-                    for (_key, val) in dnode.tags() {
53
+                Element::DenseNode(node) => {
54
+                    for (_key, val) in node.tags() {
52
                         if re.is_match(val) {
55
                         if re.is_match(val) {
53
-                            if update_func(dnode.lat(), dnode.lon()) == ControlFlow::Break {
56
+                            let pos = LatLon::new(node.lat(), node.lon());
57
+                            if update_func(pos) == ControlFlow::Break {
54
                                 return;
58
                                 return;
55
                             }
59
                             }
56
                             break;
60
                             break;

+ 1
- 1
src/tile_layer.rs View File

83
             program,
83
             program,
84
             buffer,
84
             buffer,
85
             cache: TileCache::new(update_func, use_network),
85
             cache: TileCache::new(update_func, use_network),
86
-            atlas: TileAtlas::new(cx, atlas_tex, 256, use_async),
86
+            atlas: TileAtlas::new(cx, atlas_tex, tile_size, use_async),
87
         }
87
         }
88
     }
88
     }
89
 
89