Преглед изворни кода

Add member min_zoom to TileSource

Johannes Hofmann пре 7 година
родитељ
комит
100562fdc1
4 измењених фајлова са 38 додато и 4 уклоњено
  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 Прегледај датотеку

@@ -85,18 +85,36 @@ impl Config {
85 85
                 let mut sources_vec: Vec<(String, TileSource)> = Vec::with_capacity(sources_table.len());
86 86
 
87 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 100
                     let max_zoom = source.get("max_zoom")
89 101
                         .ok_or_else(|| format!("source {:?} is missing \"max_zoom\" entry", name))?
90 102
                         .as_integer()
91 103
                         .ok_or_else(|| "max_zoom has to be an integer".to_string())
92 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 107
                             } else {
96 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 118
                     let url_template = source.get("url_template")
101 119
                         .ok_or_else(|| format!("source {:?} is missing \"url_template\" entry", name))?
102 120
                         .as_str()
@@ -121,6 +139,7 @@ impl Config {
121 139
                             url_template.to_string(),
122 140
                             path,
123 141
                             extension.to_string(),
142
+                            min_zoom as u32,
124 143
                             max_zoom as u32,
125 144
                         ),
126 145
                     ));

+ 1
- 1
src/main.rs Прегледај датотеку

@@ -266,7 +266,7 @@ fn main() {
266 266
 
267 267
     let fps: f64 = matches.value_of("fps").map(|s| s.parse().unwrap()).unwrap_or_else(|| config.fps());
268 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 271
     // estimated draw duration
272 272
     let mut est_draw_dur = duration_per_frame;

+ 9
- 1
src/tile_loader.rs Прегледај датотеку

@@ -214,7 +214,9 @@ impl TileLoader {
214 214
     }
215 215
 
216 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 220
             return;
219 221
         }
220 222
 
@@ -254,6 +256,12 @@ impl TileLoader {
254 256
     }
255 257
 
256 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 265
         match image::open(source.local_tile_path(tile)) {
258 266
             Ok(img) => {
259 267
                 Some(img)

+ 7
- 0
src/tile_source.rs Прегледај датотеку

@@ -8,6 +8,7 @@ pub struct TileSource {
8 8
     url_template: String,
9 9
     directory: PathBuf,
10 10
     extension: String,
11
+    min_zoom: u32,
11 12
     max_zoom: u32,
12 13
 }
13 14
 
@@ -22,6 +23,7 @@ impl TileSource {
22 23
         url_template: S,
23 24
         directory: P,
24 25
         extension: String,
26
+        min_zoom: u32,
25 27
         max_zoom: u32,
26 28
     ) -> Self {
27 29
         TileSource {
@@ -29,6 +31,7 @@ impl TileSource {
29 31
             url_template: url_template.into(),
30 32
             directory: directory.into(),
31 33
             extension: extension,
34
+            min_zoom: min_zoom,
32 35
             max_zoom: max_zoom,
33 36
         }
34 37
     }
@@ -52,6 +55,10 @@ impl TileSource {
52 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 62
     pub fn max_tile_zoom(&self) -> u32 {
56 63
         self.max_zoom
57 64
     }