Quellcode durchsuchen

Add module projection, store projection in Session

and restore the projection of the last session.
Johannes Hofmann vor 7 Jahren
Ursprung
Commit
2642342dab
4 geänderte Dateien mit 46 neuen und 12 gelöschten Zeilen
  1. 2
    1
      src/main.rs
  2. 5
    10
      src/map_view_gl.rs
  3. 26
    0
      src/projection.rs
  4. 13
    1
      src/session.rs

+ 2
- 1
src/main.rs Datei anzeigen

@@ -31,6 +31,7 @@ pub mod mercator_view;
31 31
 pub mod ortho_tile_layer;
32 32
 pub mod orthografic_view;
33 33
 pub mod program;
34
+pub mod projection;
34 35
 pub mod search;
35 36
 pub mod session;
36 37
 pub mod texture;
@@ -43,8 +44,8 @@ pub mod url_template;
43 44
 pub mod vertex_attrib;
44 45
 
45 46
 use coord::{LatLonDeg, ScreenCoord};
46
-use glutin::{ControlFlow, ElementState, Event, GlContext, MouseButton, MouseScrollDelta, VirtualKeyCode, WindowEvent};
47 47
 use glutin::dpi::{LogicalPosition, LogicalSize, PhysicalPosition};
48
+use glutin::{ControlFlow, ElementState, Event, GlContext, MouseButton, MouseScrollDelta, VirtualKeyCode, WindowEvent};
48 49
 use map_view_gl::MapViewGl;
49 50
 use std::error::Error;
50 51
 use std::sync::mpsc;

+ 5
- 10
src/map_view_gl.rs Datei anzeigen

@@ -1,14 +1,15 @@
1 1
 use context::Context;
2 2
 use coord::{MapCoord, ScreenCoord};
3
-use ortho_tile_layer::OrthoTileLayer;
4 3
 use map_view::MapView;
5 4
 use marker_layer::MarkerLayer;
5
+use mercator_tile_layer::MercatorTileLayer;
6 6
 use mercator_view::MercatorView;
7
+use ortho_tile_layer::OrthoTileLayer;
8
+use projection::Projection;
7 9
 use session::Session;
8 10
 use texture::{Texture, TextureFormat};
9 11
 use tile_atlas::TileAtlas;
10 12
 use tile_cache::TileCache;
11
-use mercator_tile_layer::MercatorTileLayer;
12 13
 use tile_source::TileSource;
13 14
 
14 15
 
@@ -30,14 +31,6 @@ pub struct MapViewGl {
30 31
     last_draw_type: DrawType,
31 32
 }
32 33
 
33
-#[derive(Debug, Eq, PartialEq)]
34
-enum Projection {
35
-    // EPSG:3857: WGS 84 / Pseudo-Mercator
36
-    Mercator,
37
-    // Orthographic projection, WGS 84 coordinates mapped to the sphere
38
-    Orthografic,
39
-}
40
-
41 34
 #[derive(Debug, Eq, PartialEq)]
42 35
 enum DrawType {
43 36
     Null,
@@ -262,6 +255,7 @@ impl MapViewGl {
262 255
         self.map_view.center = session.view_center;
263 256
         self.map_view.center.normalize_xy();
264 257
         self.map_view.zoom = MIN_ZOOM_LEVEL.max(MAX_ZOOM_LEVEL.min(session.zoom));
258
+        self.projection = session.projection;
265 259
     }
266 260
 
267 261
     pub fn to_session(&self) -> Session {
@@ -269,6 +263,7 @@ impl MapViewGl {
269 263
             view_center: self.map_view.center,
270 264
             zoom: self.map_view.zoom,
271 265
             tile_source: None,
266
+            projection: self.projection,
272 267
         }
273 268
     }
274 269
 }

+ 26
- 0
src/projection.rs Datei anzeigen

@@ -0,0 +1,26 @@
1
+
2
+#[derive(Clone, Copy, Debug, Eq, PartialEq)]
3
+pub enum Projection {
4
+    // EPSG:3857: WGS 84 / Pseudo-Mercator
5
+    Mercator,
6
+    // Orthographic projection, WGS 84 coordinates mapped to the sphere
7
+    Orthografic,
8
+}
9
+
10
+
11
+impl Projection {
12
+    pub fn to_str(&self) -> &str {
13
+        match *self {
14
+            Projection::Mercator => "mercator",
15
+            Projection::Orthografic => "orthografic",
16
+        }
17
+    }
18
+
19
+    pub fn from_str(s: &str) -> Option<Projection> {
20
+        match s {
21
+            "mercator" => Some(Projection::Mercator),
22
+            "orthografic" => Some(Projection::Orthografic),
23
+            _ => None,
24
+        }
25
+    }
26
+}

+ 13
- 1
src/session.rs Datei anzeigen

@@ -1,10 +1,11 @@
1 1
 use coord::MapCoord;
2
+use projection::Projection;
2 3
 use std::fs::File;
3 4
 use std::io::Read;
4 5
 use std::path::Path;
5
-use toml;
6 6
 use toml::Value;
7 7
 use toml::value::Table;
8
+use toml;
8 9
 
9 10
 
10 11
 #[derive(Debug)]
@@ -12,6 +13,7 @@ pub struct Session {
12 13
     pub view_center: MapCoord,
13 14
     pub zoom: f64,
14 15
     pub tile_source: Option<String>,
16
+    pub projection: Projection,
15 17
 }
16 18
 
17 19
 impl Session {
@@ -61,11 +63,20 @@ impl Session {
61 63
                     None => None,
62 64
                 };
63 65
 
66
+                let projection = match view.get("projection") {
67
+                    Some(&Value::String(ref s)) => {
68
+                        Projection::from_str(s).unwrap_or_else(|| Projection::Mercator)
69
+                    },
70
+                    Some(_) => return Err("projection has to be a string.".to_string()),
71
+                    None => Projection::Mercator,
72
+                };
73
+
64 74
                 Ok(
65 75
                     Session {
66 76
                         view_center: MapCoord::new(x, y),
67 77
                         zoom,
68 78
                         tile_source,
79
+                        projection,
69 80
                     }
70 81
                 )
71 82
             },
@@ -82,6 +93,7 @@ impl Session {
82 93
         if let Some(ref tile_source) = self.tile_source {
83 94
             view.insert("tile_source".to_string(), Value::String(tile_source.clone()));
84 95
         }
96
+        view.insert("projection".to_string(), Value::String(self.projection.to_str().to_string()));
85 97
 
86 98
         let mut root = Table::new();
87 99
         root.insert("view".to_string(), Value::Table(view));