Browse Source

Add module projection, store projection in Session

and restore the projection of the last session.
Johannes Hofmann 7 years ago
parent
commit
2642342dab
4 changed files with 46 additions and 12 deletions
  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 View File

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

+ 5
- 10
src/map_view_gl.rs View File

1
 use context::Context;
1
 use context::Context;
2
 use coord::{MapCoord, ScreenCoord};
2
 use coord::{MapCoord, ScreenCoord};
3
-use ortho_tile_layer::OrthoTileLayer;
4
 use map_view::MapView;
3
 use map_view::MapView;
5
 use marker_layer::MarkerLayer;
4
 use marker_layer::MarkerLayer;
5
+use mercator_tile_layer::MercatorTileLayer;
6
 use mercator_view::MercatorView;
6
 use mercator_view::MercatorView;
7
+use ortho_tile_layer::OrthoTileLayer;
8
+use projection::Projection;
7
 use session::Session;
9
 use session::Session;
8
 use texture::{Texture, TextureFormat};
10
 use texture::{Texture, TextureFormat};
9
 use tile_atlas::TileAtlas;
11
 use tile_atlas::TileAtlas;
10
 use tile_cache::TileCache;
12
 use tile_cache::TileCache;
11
-use mercator_tile_layer::MercatorTileLayer;
12
 use tile_source::TileSource;
13
 use tile_source::TileSource;
13
 
14
 
14
 
15
 
30
     last_draw_type: DrawType,
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
 #[derive(Debug, Eq, PartialEq)]
34
 #[derive(Debug, Eq, PartialEq)]
42
 enum DrawType {
35
 enum DrawType {
43
     Null,
36
     Null,
262
         self.map_view.center = session.view_center;
255
         self.map_view.center = session.view_center;
263
         self.map_view.center.normalize_xy();
256
         self.map_view.center.normalize_xy();
264
         self.map_view.zoom = MIN_ZOOM_LEVEL.max(MAX_ZOOM_LEVEL.min(session.zoom));
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
     pub fn to_session(&self) -> Session {
261
     pub fn to_session(&self) -> Session {
269
             view_center: self.map_view.center,
263
             view_center: self.map_view.center,
270
             zoom: self.map_view.zoom,
264
             zoom: self.map_view.zoom,
271
             tile_source: None,
265
             tile_source: None,
266
+            projection: self.projection,
272
         }
267
         }
273
     }
268
     }
274
 }
269
 }

+ 26
- 0
src/projection.rs View File

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 View File

1
 use coord::MapCoord;
1
 use coord::MapCoord;
2
+use projection::Projection;
2
 use std::fs::File;
3
 use std::fs::File;
3
 use std::io::Read;
4
 use std::io::Read;
4
 use std::path::Path;
5
 use std::path::Path;
5
-use toml;
6
 use toml::Value;
6
 use toml::Value;
7
 use toml::value::Table;
7
 use toml::value::Table;
8
+use toml;
8
 
9
 
9
 
10
 
10
 #[derive(Debug)]
11
 #[derive(Debug)]
12
     pub view_center: MapCoord,
13
     pub view_center: MapCoord,
13
     pub zoom: f64,
14
     pub zoom: f64,
14
     pub tile_source: Option<String>,
15
     pub tile_source: Option<String>,
16
+    pub projection: Projection,
15
 }
17
 }
16
 
18
 
17
 impl Session {
19
 impl Session {
61
                     None => None,
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
                 Ok(
74
                 Ok(
65
                     Session {
75
                     Session {
66
                         view_center: MapCoord::new(x, y),
76
                         view_center: MapCoord::new(x, y),
67
                         zoom,
77
                         zoom,
68
                         tile_source,
78
                         tile_source,
79
+                        projection,
69
                     }
80
                     }
70
                 )
81
                 )
71
             },
82
             },
82
         if let Some(ref tile_source) = self.tile_source {
93
         if let Some(ref tile_source) = self.tile_source {
83
             view.insert("tile_source".to_string(), Value::String(tile_source.clone()));
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
         let mut root = Table::new();
98
         let mut root = Table::new();
87
         root.insert("view".to_string(), Value::Table(view));
99
         root.insert("view".to_string(), Value::Table(view));