@@ -95,8 +95,8 @@ impl TileSchemaBuilder {
9595
9696 // Resolution is bound by the maximum tile index that can be represented
9797 let min_resolution = f64:: min (
98- self . world_bounds . width ( ) / self . tile_width as f64 / u64 :: MAX as f64 ,
99- self . world_bounds . height ( ) / self . tile_height as f64 / u64 :: MAX as f64 ,
98+ self . world_bounds . width ( ) / self . tile_width as f64 / i64 :: MAX as f64 ,
99+ self . world_bounds . height ( ) / self . tile_height as f64 / i64 :: MAX as f64 ,
100100 ) ;
101101
102102 let lods = match self . lods {
@@ -245,7 +245,24 @@ impl TileSchemaBuilder {
245245 self
246246 }
247247
248- fn logarithmic_z_levels ( mut self , z_levels : impl IntoIterator < Item = u32 > ) -> Self {
248+ /// Set z-levels of the tile schema with the logarithmic scale with base 2.
249+ ///
250+ /// This is the usual approach for most tile schemas to approach the levels of detail. It
251+ /// considers the top level with z-index `0` to be a single tile that includes the whole
252+ /// world map. Then the next level divides this tile into 4 parts (2x2 along x and y axis).
253+ /// Next level further divides every tile into 4 and so on.
254+ ///
255+ /// With this approach the whole world map is divided into `2^z_level` tiles along each axis.
256+ /// The resolution for each level is calculated accordingly.
257+ ///
258+ /// For the builder to be able to calculate logarithmic scale, it must have world bounds
259+ /// specified correctly.
260+ ///
261+ /// The max z-level that can be set with logarithmic scale is limited by the number of X and Y
262+ /// indices that can be expressed with `i64`, which for tiles of size 256 pixels in Web Mercator
263+ /// projection equals `63`. If you have tiles with z-indices larger than this value, use `z-levels`
264+ /// method instead to set resolutions manually.
265+ pub fn logarithmic_z_levels ( mut self , z_levels : impl IntoIterator < Item = u32 > ) -> Self {
249266 self . lods = Lods :: Logarithmic ( z_levels. into_iter ( ) . collect ( ) ) ;
250267
251268 self
@@ -493,19 +510,19 @@ mod tests {
493510
494511 #[ test]
495512 fn resolution_at_boundary_of_precision ( ) {
496- let result = TileSchemaBuilder :: web_mercator ( 0 ..=64 ) . build ( ) ;
513+ let result = TileSchemaBuilder :: web_mercator ( 0 ..=63 ) . build ( ) ;
497514 assert ! (
498515 result. is_ok( ) ,
499516 "Expected z=0..=64 to be valid, got {:?}" ,
500517 result
501518 ) ;
502519
503- let result = TileSchemaBuilder :: web_mercator ( 0 ..=65 ) . build ( ) ;
520+ let result = TileSchemaBuilder :: web_mercator ( 0 ..=64 ) . build ( ) ;
504521
505522 assert ! (
506523 matches!(
507524 result,
508- Err ( TileSchemaError :: ResolutionTooSmall { z_level: 65 , .. } )
525+ Err ( TileSchemaError :: ResolutionTooSmall { z_level: 64 , .. } )
509526 ) ,
510527 "Expected ResolutionTooSmall error, got {:?}" ,
511528 result
0 commit comments