Skip to content

Commit 2c520c7

Browse files
committed
Minor cleanups
1 parent a438510 commit 2c520c7

File tree

4 files changed

+34
-13
lines changed

4 files changed

+34
-13
lines changed

justfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ test:
2121
cargo test
2222
RUSTDOCFLAGS="-D warnings" cargo doc --no-deps
2323

24-
test_writer:
24+
test-writer:
2525
cargo test --features mmap-async-tokio
2626

2727
# Run all tests and checks

src/async_reader.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,7 @@ impl<B: AsyncBackend + Sync + Send, C: DirectoryCache + Sync + Send> AsyncPmTile
6464

6565
/// Fetches tile bytes from the archive.
6666
pub async fn get_tile(&self, z: u8, x: u64, y: u64) -> PmtResult<Option<Bytes>> {
67-
let tile_id = tile_id(z, x, y);
68-
self.get_tile_by_id(tile_id).await
67+
self.get_tile_by_id(tile_id(z, x, y)).await
6968
}
7069

7170
pub(crate) async fn get_tile_by_id(&self, tile_id: u64) -> PmtResult<Option<Bytes>> {

src/directory.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,17 @@ impl Directory {
2424
entries: Vec::with_capacity(capacity),
2525
}
2626
}
27+
2728
pub(crate) fn from_entries(entries: &[DirEntry]) -> Self {
2829
Self {
2930
entries: entries.to_vec(),
3031
}
3132
}
33+
3234
pub(crate) fn entries(&self) -> &[DirEntry] {
3335
&self.entries
3436
}
37+
3538
/// Find the directory entry for a given tile ID.
3639
#[must_use]
3740
pub fn find_tile_id(&self, tile_id: u64) -> Option<&DirEntry> {

src/writer.rs

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ pub struct PmTilesStreamWriter<W: Write + Seek> {
2828

2929
pub(crate) trait WriteTo {
3030
fn write_to<W: Write>(&self, writer: &mut W) -> std::io::Result<()>;
31+
3132
fn write_compressed_to<W: Write>(
3233
&self,
3334
writer: &mut W,
@@ -43,6 +44,7 @@ pub(crate) trait WriteTo {
4344
}
4445
Ok(())
4546
}
47+
4648
fn write_compressed_to_counted<W: Write>(
4749
&self,
4850
writer: &mut Counter<W>,
@@ -52,6 +54,7 @@ pub(crate) trait WriteTo {
5254
self.write_compressed_to(writer, compression)?;
5355
Ok(writer.writer_bytes() - pos)
5456
}
57+
5558
fn compressed_size(&self, compression: Compression) -> PmtResult<usize> {
5659
let mut devnull = Counter::new(std::io::sink());
5760
self.write_compressed_to(&mut devnull, compression)?;
@@ -73,6 +76,7 @@ impl PmTilesWriter {
7376
TileType::Mvt => Compression::Gzip,
7477
_ => Compression::None,
7578
};
79+
#[allow(clippy::excessive_precision, clippy::unreadable_literal)]
7680
let header = Header {
7781
version: 3,
7882
root_offset: HEADER_SIZE as u64,
@@ -93,9 +97,9 @@ impl PmTilesWriter {
9397
min_zoom: 0,
9498
max_zoom: 22,
9599
min_longitude: -180.0,
96-
min_latitude: -85.0,
100+
min_latitude: -85.051129,
97101
max_longitude: 180.0,
98-
max_latitude: 85.0,
102+
max_latitude: 85.051129,
99103
center_zoom: 0,
100104
center_longitude: 0.0,
101105
center_latitude: 0.0,
@@ -105,30 +109,35 @@ impl PmTilesWriter {
105109
metadata: "{}".to_string(),
106110
}
107111
}
112+
108113
/// Set the compression for metadata and directories.
109114
#[must_use]
110115
pub fn internal_compression(mut self, compression: Compression) -> Self {
111116
self.header.internal_compression = compression;
112117
self
113118
}
119+
114120
/// Set the compression for tile data.
115121
#[must_use]
116122
pub fn tile_compression(mut self, compression: Compression) -> Self {
117123
self.header.tile_compression = compression;
118124
self
119125
}
126+
120127
/// Set the minimum zoom level of the tiles
121128
#[must_use]
122129
pub fn min_zoom(mut self, level: u8) -> Self {
123130
self.header.min_zoom = level;
124131
self
125132
}
133+
126134
/// Set the maximum zoom level of the tiles
127135
#[must_use]
128136
pub fn max_zoom(mut self, level: u8) -> Self {
129137
self.header.max_zoom = level;
130138
self
131139
}
140+
132141
/// Set the bounds of the tiles
133142
#[must_use]
134143
pub fn bounds(mut self, min_lon: f32, min_lat: f32, max_lon: f32, max_lat: f32) -> Self {
@@ -138,19 +147,22 @@ impl PmTilesWriter {
138147
self.header.max_longitude = max_lon;
139148
self
140149
}
150+
141151
/// Set the center zoom level.
142152
#[must_use]
143153
pub fn center_zoom(mut self, level: u8) -> Self {
144154
self.header.center_zoom = level;
145155
self
146156
}
157+
147158
/// Set the center position.
148159
#[must_use]
149160
pub fn center(mut self, lon: f32, lat: f32) -> Self {
150161
self.header.center_latitude = lat;
151162
self.header.center_longitude = lon;
152163
self
153164
}
165+
154166
/// Set the metadata, which must contain a valid JSON object.
155167
///
156168
/// If the tile type has a value of MVT Vector Tile, the object must contain a key of `vector_layers` as described in the `TileJSON` 3.0 specification.
@@ -159,6 +171,7 @@ impl PmTilesWriter {
159171
self.metadata = metadata.to_string();
160172
self
161173
}
174+
162175
/// Create a new `PMTiles` writer.
163176
pub fn create<W: Write + Seek>(self, writer: W) -> PmtResult<PmTilesStreamWriter<W>> {
164177
let mut out = Counter::new(BufWriter::new(writer));
@@ -194,8 +207,10 @@ impl PmTilesWriter {
194207
Ok(writer)
195208
}
196209
}
210+
197211
impl<W: Write + Seek> PmTilesStreamWriter<W> {
198-
/// Add tile to writer
212+
/// Add tile to writer.
213+
///
199214
/// Tiles are deduplicated and written to output.
200215
/// `tile_id` should be increasing.
201216
pub fn add_tile(&mut self, tile_id: u64, data: &[u8]) -> PmtResult<()> {
@@ -215,7 +230,7 @@ impl<W: Write + Seek> PmTilesStreamWriter<W> {
215230

216231
self.n_addressed_tiles += 1;
217232
if !is_first
218-
&& compare_blobs(&self.prev_tile_data, data)
233+
&& self.prev_tile_data == data
219234
&& tile_id == last_entry.tile_id + u64::from(last_entry.run_length)
220235
{
221236
last_entry.run_length += 1;
@@ -285,6 +300,7 @@ impl<W: Write + Seek> PmTilesStreamWriter<W> {
285300
leaf_size += leaf_size / 5; // go-pmtiles: leaf_size *= 1.2
286301
}
287302
}
303+
288304
fn build_roots_leaves(&self, leaf_size: usize) -> PmtResult<(Directory, usize)> {
289305
let mut root_dir = Directory::with_capacity(self.entries.len() / leaf_size);
290306
let mut offset = 0;
@@ -302,6 +318,7 @@ impl<W: Write + Seek> PmTilesStreamWriter<W> {
302318
let num_leaves = root_dir.entries().len();
303319
Ok((root_dir, num_leaves))
304320
}
321+
305322
fn dir_size(&self, entries: &[DirEntry]) -> PmtResult<usize> {
306323
let dir = Directory::from_entries(entries);
307324
dir.compressed_size(self.header.internal_compression)
@@ -333,10 +350,6 @@ impl<W: Write + Seek> PmTilesStreamWriter<W> {
333350
}
334351
}
335352

336-
fn compare_blobs(a: &[u8], b: &[u8]) -> bool {
337-
a.len() == b.len() && a.iter().zip(b.iter()).all(|(a, b)| a == b)
338-
}
339-
340353
fn into_u32(v: usize) -> PmtResult<u32> {
341354
v.try_into().map_err(|_| PmtError::IndexEntryOverflow)
342355
}
@@ -395,8 +408,14 @@ mod tests {
395408
assert_eq!(header_in.center_zoom, header_out.center_zoom);
396409
assert_eq!(header_in.center_latitude, header_out.center_latitude);
397410
assert_eq!(header_in.center_longitude, header_out.center_longitude);
398-
assert_eq!(header_in.min_latitude, header_out.min_latitude);
399-
assert_eq!(header_in.max_latitude, header_out.max_latitude);
411+
assert_eq!(
412+
header_in.min_latitude.round(),
413+
header_out.min_latitude.round()
414+
);
415+
assert_eq!(
416+
header_in.max_latitude.round(),
417+
header_out.max_latitude.round()
418+
);
400419
assert_eq!(header_in.min_longitude, header_out.min_longitude);
401420
assert_eq!(header_in.max_longitude, header_out.max_longitude);
402421
assert_eq!(header_in.clustered, header_out.clustered);

0 commit comments

Comments
 (0)