Browse Source

Add member min_zoom to TileSource

Johannes Hofmann 7 years ago
parent
commit
100562fdc1
4 changed files with 38 additions and 4 deletions
  1. 21
    2
      src/config.rs
  2. 1
    1
      src/main.rs
  3. 9
    1
      src/tile_loader.rs
  4. 7
    0
      src/tile_source.rs

+ 21
- 2
src/config.rs View File

85
                 let mut sources_vec: Vec<(String, TileSource)> = Vec::with_capacity(sources_table.len());
85
                 let mut sources_vec: Vec<(String, TileSource)> = Vec::with_capacity(sources_table.len());
86
 
86
 
87
                 for (id, (name, source)) in sources_table.iter().enumerate() {
87
                 for (id, (name, source)) in sources_table.iter().enumerate() {
88
+                    let min_zoom = source.get("min_zoom")
89
+                        .unwrap_or_else(|| &Value::Integer(0))
90
+                        .as_integer()
91
+                        .ok_or_else(|| "min_zoom has to be an integer".to_string())
92
+                        .and_then(|m| {
93
+                            if m < 0 || m > 30 {
94
+                                Err(format!("min_zoom = {} is out of bounds, has to be in interval [0, 30]", m))
95
+                            } else {
96
+                                Ok(m)
97
+                            }
98
+                        })?;
99
+
88
                     let max_zoom = source.get("max_zoom")
100
                     let max_zoom = source.get("max_zoom")
89
                         .ok_or_else(|| format!("source {:?} is missing \"max_zoom\" entry", name))?
101
                         .ok_or_else(|| format!("source {:?} is missing \"max_zoom\" entry", name))?
90
                         .as_integer()
102
                         .as_integer()
91
                         .ok_or_else(|| "max_zoom has to be an integer".to_string())
103
                         .ok_or_else(|| "max_zoom has to be an integer".to_string())
92
                         .and_then(|m| {
104
                         .and_then(|m| {
93
-                            if m <= 0 || m > 30 {
94
-                                Err(format!("max_zoom = {} is out of bounds, has to be in interval [1, 30]", m))
105
+                            if m < 0 || m > 30 {
106
+                                Err(format!("max_zoom = {} is out of bounds, has to be in interval [0, 30]", m))
95
                             } else {
107
                             } else {
96
                                 Ok(m)
108
                                 Ok(m)
97
                             }
109
                             }
98
                         })?;
110
                         })?;
99
 
111
 
112
+                    if min_zoom > max_zoom {
113
+                        warn!("min_zoom ({}) and max_zoom ({}) allow no valid tiles", min_zoom, max_zoom);
114
+                    } else if min_zoom == max_zoom {
115
+                        warn!("min_zoom ({}) and max_zoom ({}) allow only one zoom level", min_zoom, max_zoom);
116
+                    }
117
+
100
                     let url_template = source.get("url_template")
118
                     let url_template = source.get("url_template")
101
                         .ok_or_else(|| format!("source {:?} is missing \"url_template\" entry", name))?
119
                         .ok_or_else(|| format!("source {:?} is missing \"url_template\" entry", name))?
102
                         .as_str()
120
                         .as_str()
121
                             url_template.to_string(),
139
                             url_template.to_string(),
122
                             path,
140
                             path,
123
                             extension.to_string(),
141
                             extension.to_string(),
142
+                            min_zoom as u32,
124
                             max_zoom as u32,
143
                             max_zoom as u32,
125
                         ),
144
                         ),
126
                     ));
145
                     ));

+ 1
- 1
src/main.rs View File

266
 
266
 
267
     let fps: f64 = matches.value_of("fps").map(|s| s.parse().unwrap()).unwrap_or_else(|| config.fps());
267
     let fps: f64 = matches.value_of("fps").map(|s| s.parse().unwrap()).unwrap_or_else(|| config.fps());
268
     let duration_per_frame = Duration::from_millis((1000.0 / fps - 0.5).max(0.0).floor() as u64);
268
     let duration_per_frame = Duration::from_millis((1000.0 / fps - 0.5).max(0.0).floor() as u64);
269
-    info!("milliseconds per frame: {}", dur_to_sec(duration_per_frame) * 0.001);
269
+    info!("milliseconds per frame: {}", dur_to_sec(duration_per_frame) * 1000.0);
270
 
270
 
271
     // estimated draw duration
271
     // estimated draw duration
272
     let mut est_draw_dur = duration_per_frame;
272
     let mut est_draw_dur = duration_per_frame;

+ 9
- 1
src/tile_loader.rs View File

214
     }
214
     }
215
 
215
 
216
     pub fn async_request(&mut self, tile_coord: TileCoord, source: &TileSource, write_to_file: bool) {
216
     pub fn async_request(&mut self, tile_coord: TileCoord, source: &TileSource, write_to_file: bool) {
217
-        if tile_coord.zoom > source.max_tile_zoom() {
217
+        if tile_coord.zoom > source.max_tile_zoom() ||
218
+           tile_coord.zoom < source.min_tile_zoom()
219
+        {
218
             return;
220
             return;
219
         }
221
         }
220
 
222
 
254
     }
256
     }
255
 
257
 
256
     pub fn get_sync(&mut self, tile: TileCoord, source: &TileSource, write_to_file: bool) -> Option<DynamicImage> {
258
     pub fn get_sync(&mut self, tile: TileCoord, source: &TileSource, write_to_file: bool) -> Option<DynamicImage> {
259
+        if tile.zoom > source.max_tile_zoom() ||
260
+           tile.zoom < source.min_tile_zoom()
261
+        {
262
+            return None;
263
+        }
264
+
257
         match image::open(source.local_tile_path(tile)) {
265
         match image::open(source.local_tile_path(tile)) {
258
             Ok(img) => {
266
             Ok(img) => {
259
                 Some(img)
267
                 Some(img)

+ 7
- 0
src/tile_source.rs View File

8
     url_template: String,
8
     url_template: String,
9
     directory: PathBuf,
9
     directory: PathBuf,
10
     extension: String,
10
     extension: String,
11
+    min_zoom: u32,
11
     max_zoom: u32,
12
     max_zoom: u32,
12
 }
13
 }
13
 
14
 
22
         url_template: S,
23
         url_template: S,
23
         directory: P,
24
         directory: P,
24
         extension: String,
25
         extension: String,
26
+        min_zoom: u32,
25
         max_zoom: u32,
27
         max_zoom: u32,
26
     ) -> Self {
28
     ) -> Self {
27
         TileSource {
29
         TileSource {
29
             url_template: url_template.into(),
31
             url_template: url_template.into(),
30
             directory: directory.into(),
32
             directory: directory.into(),
31
             extension: extension,
33
             extension: extension,
34
+            min_zoom: min_zoom,
32
             max_zoom: max_zoom,
35
             max_zoom: max_zoom,
33
         }
36
         }
34
     }
37
     }
52
         Self::fill_template(&self.url_template, tile_coord)
55
         Self::fill_template(&self.url_template, tile_coord)
53
     }
56
     }
54
 
57
 
58
+    pub fn min_tile_zoom(&self) -> u32 {
59
+        self.min_zoom
60
+    }
61
+
55
     pub fn max_tile_zoom(&self) -> u32 {
62
     pub fn max_tile_zoom(&self) -> u32 {
56
         self.max_zoom
63
         self.max_zoom
57
     }
64
     }