pmtiles2 0.3.2

A low level implementation of the PMTiles format based on the standard Read and Write (or AsyncRead and AsyncWrite) traits.
Documentation
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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
use duplicate::duplicate_item;
use integer_encoding::{VarIntReader, VarIntWriter};
use std::io::{Read, Result, Write};
use std::ops::{Index, IndexMut, Range};
use std::slice::{Iter, SliceIndex};

#[cfg(feature = "async")]
use futures::{AsyncReadExt, AsyncWrite, AsyncWriteExt};
#[cfg(feature = "async")]
use integer_encoding::{VarIntAsyncReader, VarIntAsyncWriter};

use crate::util::{compress, decompress};
#[cfg(feature = "async")]
use crate::util::{compress_async, decompress_async};
use crate::Compression;

/// A structure representing a directory entry.
///
/// A entry includes information on where to find either a leaf directory or one/multiple tiles.
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Entry {
    /// The first tile id this entry is valid for
    pub tile_id: u64,

    /// Offset (in bytes) of first byte of tile of leaf-directory data
    ///
    /// For tiles this offset is relative to the start of the tile data sections.
    /// For leaf directories this offset is relative to the start of the leaf directory sections.
    pub offset: u64,

    /// Amount of bytes
    pub length: u32,

    /// The run length indicates the amount of tiles this entry is valid for.
    /// A run length of `0` indicates that this is in fact a entry containing information
    /// of a leaf directory.
    pub run_length: u32,
}

impl Entry {
    /// Returns the range of tile ids this entry is valid for.
    pub const fn tile_id_range(&self) -> Range<u64> {
        self.tile_id..self.tile_id + self.run_length as u64
    }

    /// Returns `true` if this entry is for a leaf directory and
    /// `false` if this entry is for tile data.
    pub const fn is_leaf_dir_entry(&self) -> bool {
        self.run_length == 0
    }
}

/// A structure representing a directory.
///
/// A directory holds an arbitrary amount of [`Entry`]. You can use [`len`](Self::len), [`is_empty`](Self::is_empty) and
/// [`iter`](Self::iter) to obtain information about that list of entries.
///
/// Use [`from_reader`](Self::from_reader) and [`to_writer`](Self::to_writer) or their respective asynchronous versions ([`from_async_reader`](Self::from_async_reader) and [`to_async_writer`](Self::to_async_writer)) to read and write the directory from / to bytes.
#[derive(Debug, PartialEq, Eq, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(transparent))]
pub struct Directory {
    entries: Vec<Entry>,
}

impl Directory {
    /// Returns the number of entries in the directory, also referred to as its 'length'.
    pub const fn len(&self) -> usize {
        self.entries.len()
    }

    /// Returns `true` if the directory contains no entries.
    pub const fn is_empty(&self) -> bool {
        self.entries.is_empty()
    }

    /// Returns an iterator over the directory.
    ///
    /// The iterator yields all entries from start to end.
    #[deprecated(
        since = "0.3.0",
        note = "Directory implements IntoIterator trait, which should be used instead"
    )]
    pub fn iter(&self) -> Iter<'_, Entry> {
        self.into_iter()
    }
}

impl<'a> IntoIterator for &'a Directory {
    type IntoIter = Iter<'a, Entry>;
    type Item = &'a Entry;
    fn into_iter(self) -> Self::IntoIter {
        self.entries.iter()
    }
}

impl Directory {
    #[duplicate_item(
        fn_name                  cfg_async_filter       input_traits                         decompress(compression, binding)              read_varint(type, reader)                  async;
        [from_reader_impl]       [cfg(all())]           [impl Read]                          [decompress(compression, &mut binding)]       [reader.read_varint::<type>()]             [];
        [from_async_reader_impl] [cfg(feature="async")] [(impl Unpin + Send + AsyncReadExt)] [decompress_async(compression, &mut binding)] [reader.read_varint_async::<type>().await] [async];
    )]
    #[allow(clippy::needless_range_loop)]
    #[cfg_async_filter]
    async fn fn_name(
        input: &mut input_traits,
        length: u64,
        compression: Compression,
    ) -> Result<Self> {
        let mut binding = input.take(length);
        let mut reader = decompress([compression], [binding])?;

        let num_entries = read_varint([usize], [reader])?;

        let mut entries = Vec::<Entry>::with_capacity(num_entries);

        // read tile_id
        let mut last_id = 0u64;
        for _ in 0..num_entries {
            let tmp = read_varint([u64], [reader])?;

            last_id += tmp;
            entries.push(Entry {
                tile_id: last_id,
                length: 0,
                offset: 0,
                run_length: 0,
            });
        }

        // read run_length
        for i in 0..num_entries {
            entries[i].run_length = read_varint([_], [reader])?;
        }

        // read length
        for i in 0..num_entries {
            let len = read_varint([_], [reader])?;

            if len == 0 {
                return Err(std::io::Error::new(
                    std::io::ErrorKind::InvalidData,
                    "Length of a directory entry must be greater than 0.",
                ));
            }

            entries[i].length = len;
        }

        // read offset
        for i in 0..num_entries {
            let val = read_varint([u64], [reader])?;

            entries[i].offset = if i > 0 && val == 0 {
                entries[i - 1].offset + u64::from(entries[i - 1].length)
            } else {
                val - 1
            };
        }

        Ok(Self { entries })
    }

    #[duplicate_item(
        fn_name                cfg_async_filter       input_traits                       compress         flush   write_varint(writer, value)              add_await(code) async;
        [to_writer_impl]       [cfg(all())]           [impl Write]                       [compress]       [flush] [writer.write_varint(value)]             [code]          [];
        [to_async_writer_impl] [cfg(feature="async")] [(impl AsyncWrite + Unpin + Send)] [compress_async] [close] [writer.write_varint_async(value).await] [code.await]    [async];
    )]
    #[cfg_async_filter]
    async fn fn_name(&self, output: &mut input_traits, compression: Compression) -> Result<()> {
        let mut writer = compress(compression, output)?;

        write_varint([writer], [self.entries.len()])?;

        // write tile_id
        let mut last_id = 0u64;
        for entry in &self.entries {
            write_varint([writer], [entry.tile_id - last_id])?;
            last_id = entry.tile_id;
        }

        // write run_length
        for entry in &self.entries {
            write_varint([writer], [entry.run_length])?;
        }

        // write length
        for entry in &self.entries {
            if entry.length == 0 {
                return Err(std::io::Error::new(
                    std::io::ErrorKind::InvalidData,
                    "Length of a directory entry must be greater than 0.",
                ));
            }
            write_varint([writer], [entry.length])?;
        }

        // write offset
        let mut next_byte = 0u64;
        for (index, entry) in self.into_iter().enumerate() {
            let val = if index > 0 && entry.offset == next_byte {
                0
            } else {
                entry.offset + 1
            };

            write_varint([writer], [val])?;

            next_byte = entry.offset + u64::from(entry.length);
        }

        add_await([writer.flush()])?;

        Ok(())
    }
}

impl Directory {
    /// Reads a directory from a [`std::io::Read`] and returns it.
    ///
    /// # Arguments
    /// * `input` - Reader including directory bytes
    /// * `length` - Length of the directory (in bytes)
    /// * `compression` - Compression of the directory
    ///
    /// # Errors
    /// Will return [`Err`] if `compression` is set to [`Compression::Unknown`], the data is not compressed correctly
    /// according to `compression`, the directory includes a entry with a length of 0  or an I/O error occurred while
    /// reading from `input`.
    ///
    /// # Example
    /// ```rust
    /// # use pmtiles2::{Directory, Compression};
    /// # use std::io::{Cursor, Seek, SeekFrom};
    /// let bytes = include_bytes!("../test/stamen_toner(raster)CC-BY+ODbL_z3.pmtiles");
    /// let mut reader = Cursor::new(bytes);
    /// reader.seek(SeekFrom::Start(127)).unwrap();
    ///
    /// let directory = Directory::from_reader(&mut reader, 246, Compression::GZip).unwrap();
    /// ```
    pub fn from_reader(
        input: &mut impl Read,
        length: u64,
        compression: Compression,
    ) -> Result<Self> {
        Self::from_reader_impl(input, length, compression)
    }

    /// Reads a directory from anything that can be turned into a byte slice (e.g. [`Vec<u8>`]).
    ///
    /// # Arguments
    /// * `bytes` - Input bytes
    /// * `compression` - Compression of the directory
    ///
    /// # Errors
    /// Will return [`Err`] if `compression` is set to [`Compression::Unknown`], the data is not compressed correctly
    /// according to `compression`, the directory includes a entry with a length of 0  or an I/O error occurred while
    /// reading from `input`.
    ///
    /// # Example
    /// ```rust
    /// # use pmtiles2::{Directory, Compression};
    /// let bytes = include_bytes!("../test/stamen_toner(raster)CC-BY+ODbL_z3.pmtiles");
    /// let directory = Directory::from_bytes(&bytes[127..], Compression::GZip).unwrap();
    /// ```
    ///
    pub fn from_bytes(bytes: impl AsRef<[u8]>, compression: Compression) -> std::io::Result<Self> {
        let length = bytes.as_ref().len() as u64;
        let mut reader = std::io::Cursor::new(bytes);

        Self::from_reader(&mut reader, length, compression)
    }

    /// Async version of [`from_reader`](Self::from_reader).
    ///
    /// Reads a directory from a [`futures::io::AsyncRead`](https://docs.rs/futures/latest/futures/io/trait.AsyncRead.html) and returns it.
    ///
    /// # Arguments
    /// * `input` - Reader including directory bytes
    /// * `length` - Length of the directory (in bytes)
    /// * `compression` - Compression of the directory
    ///
    /// # Errors
    /// Will return [`Err`] if `compression` is set to [`Compression::Unknown`], the data is not compressed correctly
    /// according to `compression`, the directory includes a entry with a length of 0  or an I/O error occurred while
    /// reading from `input`.
    ///
    /// # Example
    /// ```rust
    /// # use pmtiles2::{Directory, Compression};
    /// # use futures::io::{AsyncReadExt, AsyncSeekExt, SeekFrom};
    /// # tokio_test::block_on(async {
    /// let bytes = include_bytes!("../test/stamen_toner(raster)CC-BY+ODbL_z3.pmtiles");
    /// let mut reader = futures::io::Cursor::new(bytes);
    /// reader.seek(SeekFrom::Start(127)).await.unwrap();
    ///
    /// let directory = Directory::from_async_reader(&mut reader, 246, Compression::GZip).await.unwrap();
    /// # })
    /// ```
    #[cfg(feature = "async")]
    pub async fn from_async_reader(
        input: &mut (impl Unpin + Send + AsyncReadExt),
        length: u64,
        compression: Compression,
    ) -> Result<Self> {
        Self::from_async_reader_impl(input, length, compression).await
    }

    /// Writes the directory to a [`std::io::Write`].
    ///
    /// # Arguments
    /// * `output` - Writer to write directory to
    /// * `compression` - Compression to use
    ///
    /// # Errors
    /// Will return [`Err`] if `compression` is set to [`Compression::Unknown`], the
    /// directory includes a entry with a length of 0 or an I/O error occurred
    /// while writing to `output`.
    ///
    /// # Example
    /// ```rust
    /// # use pmtiles2::{Directory, Compression};
    /// let directory: Directory = Vec::new().into();
    ///
    /// let mut output = std::io::Cursor::new(Vec::<u8>::new());
    ///
    /// directory.to_writer(&mut output, Compression::GZip).unwrap();
    /// ```
    pub fn to_writer(&self, output: &mut impl Write, compression: Compression) -> Result<()> {
        self.to_writer_impl(output, compression)
    }

    /// Async version of [`to_writer`](Self::to_writer).
    ///
    /// Writes the directory to a [`futures::io::AsyncWrite`](https://docs.rs/futures/latest/futures/io/trait.AsyncWrite.html).
    ///
    /// # Arguments
    /// * `output` - Writer to write directory to
    /// * `compression` - Compression to use
    ///
    /// # Errors
    /// Will return [`Err`] if `compression` is set to [`Compression::Unknown`], the
    /// directory includes a entry with a length of 0 or an I/O error occurred
    /// while writing to `output`.
    ///
    /// # Example
    /// ```rust
    /// # use pmtiles2::{Directory, Compression};
    /// # tokio_test::block_on(async {
    /// let directory: Directory = Vec::new().into();
    ///
    /// let mut output = futures::io::Cursor::new(Vec::<u8>::new());
    ///
    /// directory.to_async_writer(&mut output, Compression::GZip).await.unwrap();
    /// # })
    /// ```
    #[cfg(feature = "async")]
    pub async fn to_async_writer(
        &self,
        output: &mut (impl AsyncWrite + Unpin + Send),
        compression: Compression,
    ) -> Result<()> {
        self.to_async_writer_impl(output, compression).await
    }
}

impl Directory {
    /// Find a entry, which includes given `tile_id`.
    ///
    /// Returns [`None`] if the directory does not include a [`Entry`] that matches `tile_id`.
    ///
    pub fn find_entry_for_tile_id(&self, tile_id: u64) -> Option<&Entry> {
        self.into_iter()
            .find(|e| !e.is_leaf_dir_entry() && e.tile_id_range().contains(&tile_id))
    }
}

impl<I: SliceIndex<[Entry]>> Index<I> for Directory {
    type Output = I::Output;

    fn index(&self, index: I) -> &Self::Output {
        self.entries.index(index)
    }
}

impl<I: SliceIndex<[Entry]>> IndexMut<I> for Directory {
    fn index_mut(&mut self, index: I) -> &mut Self::Output {
        self.entries.index_mut(index)
    }
}

impl From<Vec<Entry>> for Directory {
    fn from(entries: Vec<Entry>) -> Self {
        Self { entries }
    }
}

impl From<Directory> for Vec<Entry> {
    fn from(val: Directory) -> Self {
        val.entries
    }
}

#[cfg(test)]
#[allow(clippy::cast_possible_truncation)]
mod test {
    use std::io::{Cursor, Seek, SeekFrom};

    use crate::util::decompress_all;

    use super::*;

    const PM_TILES_BYTES: &[u8] =
        include_bytes!("../test/stamen_toner(raster)CC-BY+ODbL_z3.pmtiles");

    const ROOT_DIR_OFFSET: u64 = 127;
    const ROOT_DIR_LENGTH: u64 = 246;
    const ROOT_DIR_COMPRESSION: Compression = Compression::GZip;

    #[test]
    fn test_from_reader() -> Result<()> {
        let mut reader = Cursor::new(PM_TILES_BYTES);
        reader.seek(SeekFrom::Start(ROOT_DIR_OFFSET))?;

        let dir = Directory::from_reader(&mut reader, ROOT_DIR_LENGTH, ROOT_DIR_COMPRESSION)?;

        assert_eq!(reader.position(), ROOT_DIR_OFFSET + ROOT_DIR_LENGTH);
        assert_eq!(dir.entries.len(), 84);
        assert_eq!(
            dir.entries[0],
            Entry {
                tile_id: 0,
                offset: 0,
                length: 18404,
                run_length: 1
            }
        );

        assert_eq!(
            dir.entries[58],
            Entry {
                tile_id: 58,
                offset: 422_070,
                length: 850,
                run_length: 2
            }
        );

        assert_eq!(
            dir.entries[83],
            Entry {
                tile_id: 84,
                offset: 243_790,
                length: 914,
                run_length: 1
            }
        );

        Ok(())
    }

    #[test]
    fn test_to_writer() -> Result<()> {
        let mut reader = Cursor::new(PM_TILES_BYTES);
        reader.seek(SeekFrom::Start(ROOT_DIR_OFFSET))?;

        let dir = Directory::from_reader(&mut reader, ROOT_DIR_LENGTH, ROOT_DIR_COMPRESSION)?;

        let mut buf = Vec::<u8>::with_capacity(ROOT_DIR_LENGTH as usize);
        let mut writer = Cursor::new(&mut buf);
        dir.to_writer(&mut writer, ROOT_DIR_COMPRESSION)?;

        // we compare the decompressed versions of the directory, as the compressed
        // bytes may not match 100% due to different compression parameters
        let output = decompress_all(ROOT_DIR_COMPRESSION, &buf)?;
        let expected = decompress_all(
            ROOT_DIR_COMPRESSION,
            &PM_TILES_BYTES[ROOT_DIR_OFFSET as usize..(ROOT_DIR_OFFSET + ROOT_DIR_LENGTH) as usize],
        )?;

        assert_eq!(output, expected);

        Ok(())
    }

    #[test]
    fn test_to_writer_invalid_entry() {
        let mut dir = Directory {
            entries: Vec::new(),
        };

        dir.entries.push(Entry {
            length: 0,
            offset: 0,
            run_length: 0,
            tile_id: 0,
        });

        let mut buf = Vec::<u8>::with_capacity(10);
        let mut writer = Cursor::new(&mut buf);
        assert!(dir.to_writer(&mut writer, ROOT_DIR_COMPRESSION).is_err());
    }
}