1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
//! Download OpenStreetMap-tiles to your disk en-masse.
//!
//! **Use with absolute caution.** Downloading tiles en-masse can hog
//! down a tile server easily. I am not responsible for any damage this
//! tool may cause.
//!
//! # Usage
//!
//! This tool is available on [crates.io](https://crates.io) and can be
//! installed via `cargo install osm-tile-downloader`. It features a helpful
//! CLI you can access via `-h` / `--help`.
//!
//! It is also available as a library.
//!
//! # CLI Example
//!
//! ```bash
//! osm-tile-downloader \
//!   --north 50.811 \
//!   --east 6.1649 \
//!   --south 50.7492 \
//!   --west 6.031 \
//!   --url https://\{s\}.tile.openstreetmap.de/\{z\}/\{x\}/\{y\}.png \
//!   --output ./tiles \
//!   --rate 10
//! ```
//!
//! # Library Example
//! ```rust
//! use osm_tile_downloader::{fetch, BoundingBox, Config};
//! use std::path::Path;
//!
//! # #[tokio::main]
//! # async fn main() {
//! let config = Config {
//!     bounding_box: BoundingBox::new_deg(50.811, 6.1649, 50.7492, 6.031),
//!     fetch_rate: 10,
//!     output_folder: Path::new("./tiles"),
//!     request_retries_amount: 3,
//!     url: "https://{s}.tile.openstreetmap.de/{z}/{x}/{y}.png",
//!     timeout_secs: 30,
//!     zoom_level: 10,
//! };
//!
//! fetch(config).await.expect("failed fetching tiles");
//! # }
//! ```

use anyhow::{Context, Result};
use futures::{prelude::*, stream};
use indicatif::ProgressBar;
use rand::{self, seq::SliceRandom};
use reqwest::{Client, StatusCode};
use std::{
    collections::HashMap,
    f64,
    fmt::Debug,
    io::{Error as IoError, ErrorKind},
    path::Path,
    time::Duration,
    u64,
};
use tokio::{
    self,
    fs::{self, File},
    io, time,
};
use url::Url;

const BACKOFF_DELAY: Duration = Duration::from_secs(10);
const ZERO_DURATION: Duration = Duration::from_secs(0);

/// A bounding box consisting of north, east, south and west coordinate boundaries
/// given from 0 to 2π.
///
/// # Example
/// ```rust
/// # use osm_tile_downloader::BoundingBox;
/// let aachen_germany = BoundingBox::new_deg(50.811, 6.1649, 50.7492, 6.031);
/// ```
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct BoundingBox {
    north: f64,
    west: f64,
    east: f64,
    south: f64,
}

/// Tile fetching configuration.
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct Config<'a> {
    /// Bounding box in top, right, bottom, left order.
    pub bounding_box: BoundingBox,

    /// Maximum number of parallel downloads.
    pub fetch_rate: u8,

    /// The folder to output the data to.
    pub output_folder: &'a Path,

    /// How many times to retry a failed HTTP request.
    pub request_retries_amount: u8,

    /// The URL to download individual tiles from including the replacement
    /// specifiers `{x}`, `{y}` and `{z}`.
    pub url: &'a str,

    /// Timeout for fetching a single tile.
    ///
    /// Pass the zero duration to disable the timeout.
    pub timeout: Duration,

    /// The zoom level to download to.
    pub zoom_level: u8,
}

/// An OSM slippy-map tile with x, y and z-coordinate.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct Tile {
    x: usize,
    y: usize,
    z: u8,
}

/// Asynchronously fetch the open street map tiles specified in `cfg` and save them
/// to the file system.
///
/// Creates the required directories recursively and overwrites any existing files
/// at the destination.
///
/// # Example
/// ```rust
/// use osm_tile_downloader::{fetch, BoundingBox, Config};
/// # use std::path::Path;
///
/// # #[tokio::main]
/// # async fn main() {
/// let config = Config {
///     bounding_box: BoundingBox::new_deg(50.811, 6.1649, 50.7492, 6.031),
///     fetch_rate: 10,
///     output_folder: Path::new("./tiles"),
///     request_retries_amount: 3,
///     url: "https://{s}.tile.openstreetmap.de/{z}/{x}/{y}.png",
///     timeout_secs: 30,
///     zoom_level: 10,
/// };
///
/// fetch(config).await.expect("failed fetching tiles");
/// # }
/// ```
///
/// # Panics
/// Panics if the specified output folder exists and is not a folder but a file.
pub async fn fetch(cfg: Config<'_>) -> Result<()> {
    assert!(
        !cfg.output_folder.exists() || cfg.output_folder.is_dir(),
        "output must be a directory",
    );

    if !cfg.output_folder.exists() {
        fs::create_dir_all(cfg.output_folder)
            .await
            .context("failed to create root output directory")?;
    }

    let pb = ProgressBar::new(cfg.tiles().count() as u64);

    let mut builder = Client::builder();
    if cfg.timeout > ZERO_DURATION {
        builder = builder.timeout(cfg.timeout);
    }

    let client = builder
        .build()
        .with_context(|| "failed creating HTTP client")?;

    stream::iter(pb.wrap_iter(cfg.tiles()))
        .for_each_concurrent(cfg.fetch_rate as usize, |tile| {
            let http_client = client.clone();

            async move {
                let mut res = Ok(());

                for _ in 0..cfg.request_retries_amount {
                    res = tile
                        .fetch_from(&http_client, cfg.url, cfg.output_folder)
                        .await;

                    if res.is_ok() {
                        return;
                    }

                    time::delay_for(BACKOFF_DELAY).await;
                }

                eprintln!(
                    "Failed fetching tile {}x{}x{}: {:?}",
                    tile.z,
                    tile.x,
                    tile.y,
                    res.unwrap_err(),
                );
            }
        })
        .await;

    pb.finish();

    Ok(())
}

impl BoundingBox {
    /// Create a new bounding box from the specified coordinates specified in degrees (0-360°).
    ///
    /// # Example
    /// ```rust
    /// # use osm_tile_downloader::BoundingBox;
    /// let aachen_germany = BoundingBox::new_deg(50.811, 6.1649, 50.7492, 6.031);
    /// ```
    ///
    /// # Panics
    /// Panics if the coordinates are < 0, or >= 360.
    pub fn new_deg(north: f64, east: f64, south: f64, west: f64) -> Self {
        Self::new(
            north.to_radians(),
            east.to_radians(),
            south.to_radians(),
            west.to_radians(),
        )
    }

    /// Create a new bounding box from the specified coordinates specified in radians (0-2π).
    ///
    /// # Panics
    /// Panics if the coordinates are < 0, or >= 2π.
    pub fn new(north: f64, east: f64, south: f64, west: f64) -> Self {
        assert!(north >= 0.0 && north < 2f64 * f64::consts::PI);
        assert!(east >= 0.0 && east < 2f64 * f64::consts::PI);
        assert!(south >= 0.0 && south < 2f64 * f64::consts::PI);
        assert!(west >= 0.0 && west < 2f64 * f64::consts::PI);

        BoundingBox {
            north,
            east,
            south,
            west,
        }
    }

    /// Gets the north coordinate.
    pub fn north(&self) -> f64 {
        self.north
    }

    /// Gets the east coordinate.
    pub fn east(&self) -> f64 {
        self.east
    }

    /// Gets the south coordinate.
    pub fn south(&self) -> f64 {
        self.south
    }

    /// Gets the west coordinate.
    pub fn west(&self) -> f64 {
        self.west
    }

    /// Creates an iterator iterating over all tiles in the bounding box.
    ///
    /// # Panics
    /// Panics if `upto_zoom` is 0.
    pub fn tiles(&self, upto_zoom: u8) -> impl Iterator<Item = Tile> + Debug {
        assert!(upto_zoom >= 1);

        let (north, east, south, west) =
            (self.north, self.east, self.south, self.west);

        (1..=upto_zoom).flat_map(move |level| {
            let (top_x, top_y) = tile_indices(level, west, north);
            let (bot_x, bot_y) = tile_indices(level, east, south);

            (top_x..=bot_x).flat_map(move |x| {
                (top_y..=bot_y).map(move |y| Tile { x, y, z: level })
            })
        })
    }
}

impl Config<'_> {
    /// Creates an iterator iterating over all tiles in the contained bounding box.
    pub fn tiles(&self) -> impl Iterator<Item = Tile> + Debug {
        self.bounding_box.tiles(self.zoom_level)
    }
}

impl Tile {
    /// Fetches the given tile from the given URL using the given HTTP client.
    pub async fn fetch_from(
        self,
        client: &Client,
        url_fmt: &str,
        output_folder: &Path,
    ) -> Result<()> {
        const OSM_SERVERS: &[&'static str] = &["a", "b", "c"];

        let formatted_url = {
            let mut map = HashMap::with_capacity(4);
            map.insert(
                "s".to_owned(),
                OSM_SERVERS
                    .choose(&mut rand::thread_rng())
                    .unwrap()
                    .to_string(),
            );
            map.insert("x".to_owned(), self.x.to_string());
            map.insert("y".to_owned(), self.y.to_string());
            map.insert("z".to_owned(), self.z.to_string());

            strfmt::strfmt(url_fmt, &map).context("failed formatting URL")?
        };

        let mut response_reader = loop {
            let url = Url::parse(&formatted_url).context("parse url")?;

            let raw_response = client.get(url).send().await.with_context(|| {
                format!("failed fetching tile {}x{}x{}", self.x, self.y, self.z)
            })?;

            if raw_response.status() == StatusCode::TOO_MANY_REQUESTS {
                let retry_after = raw_response
                    .headers()
                    .get("Retry-After")
                    .and_then(|v| v.to_str().ok())
                    .and_then(|val| val.parse::<u64>().ok())
                    .map(Duration::from_secs)
                    .unwrap_or(BACKOFF_DELAY);

                time::delay_for(retry_after).await;
                continue;
            }

            let response_stream = raw_response
                .error_for_status()
                .with_context(|| {
                    format!(
                        "received invalid status code fetching tile {}x{}x{}",
                        self.x, self.y, self.z
                    )
                })?
                .bytes_stream()
                .map_err(|e| IoError::new(ErrorKind::Other, e));

            break io::stream_reader(response_stream);
        };

        let mut output_file = {
            let mut target = output_folder.join(self.z.to_string());
            target.push(self.x.to_string());
            fs::create_dir_all(&target).await.with_context(|| {
                format!(
                    "failed creating output directory for tile {}x{}x{}",
                    self.x, self.y, self.z
                )
            })?;
            target.push(self.y.to_string());

            File::create(target).await?
        };

        io::copy(&mut response_reader, &mut output_file)
            .await
            .with_context(|| {
                format!(
                    "failed streaming tile {}x{}x{} to disk",
                    self.x, self.y, self.z
                )
            })?;

        Ok(())
    }
}

fn tile_indices(zoom: u8, lon_rad: f64, lat_rad: f64) -> (usize, usize) {
    assert!(zoom > 0);
    assert!(lon_rad >= 0.0);
    assert!(lat_rad >= 0.0);

    let tile_x = {
        let deg = (lon_rad + f64::consts::PI) / (2f64 * f64::consts::PI);

        deg * 2f64.powi(zoom as i32)
    };
    let tile_y = {
        let trig = (lat_rad.tan() + (1f64 / lat_rad.cos())).ln();
        let inner = 1f64 - (trig / f64::consts::PI);

        inner * 2f64.powi(zoom as i32 - 1)
    };

    (tile_x as usize, tile_y as usize)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    #[should_panic]
    fn bbox_panics_deg() {
        BoundingBox::new_deg(360.0, 0.0, 0.0, 0.0);
    }

    #[test]
    #[should_panic]
    fn bbox_panics_rad() {
        BoundingBox::new(7.0, 3.0, 3.0, 3.0);
    }

    #[test]
    fn tile_index() {
        assert_eq!(
            tile_indices(18, 6.0402f64.to_radians(), 50.7929f64.to_radians()),
            (135470, 87999)
        );
    }
}