This is an automated email from the ASF dual-hosted git repository. bchapuis pushed a commit to branch pmtiles in repository https://gitbox.apache.org/repos/asf/incubator-baremaps.git
commit db8c2d80200826635b1cf6d3695b596ca8e8e4c1 Author: Bertil Chapuis <[email protected]> AuthorDate: Wed Sep 6 00:09:17 2023 +0200 Format code --- .../apache/baremaps/tilestore/pmtiles/PMTiles.java | 476 +++++++++++---------- .../baremaps/tilestore/pmtiles/PMTilesTest.java | 289 +++++++------ 2 files changed, 393 insertions(+), 372 deletions(-) diff --git a/baremaps-core/src/main/java/org/apache/baremaps/tilestore/pmtiles/PMTiles.java b/baremaps-core/src/main/java/org/apache/baremaps/tilestore/pmtiles/PMTiles.java index f773c7eb..3ec6d62e 100644 --- a/baremaps-core/src/main/java/org/apache/baremaps/tilestore/pmtiles/PMTiles.java +++ b/baremaps-core/src/main/java/org/apache/baremaps/tilestore/pmtiles/PMTiles.java @@ -1,261 +1,271 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ + package org.apache.baremaps.tilestore.pmtiles; import com.google.common.math.LongMath; - import java.nio.ByteBuffer; import java.nio.ByteOrder; public class PMTiles { - public static long toNum(long low, long high) { - return high * 0x100000000L + low; - } + public static long toNum(long low, long high) { + return high * 0x100000000L + low; + } - public static long readVarintRemainder(long l, ByteBuffer buf) { - long h, b; - b = buf.get() & 0xff; - h = (b & 0x70) >> 4; - if (b < 0x80) { - return toNum(l, h); - } - b = buf.get() & 0xff; - h |= (b & 0x7f) << 3; - if (b < 0x80) { - return toNum(l, h); - } - b = buf.get() & 0xff; - h |= (b & 0x7f) << 10; - if (b < 0x80) { - return toNum(l, h); - } - b = buf.get() & 0xff; - h |= (b & 0x7f) << 17; - if (b < 0x80) { - return toNum(l, h); - } - b = buf.get() & 0xff; - h |= (b & 0x7f) << 24; - if (b < 0x80) { - return toNum(l, h); - } - b = buf.get() & 0xff; - h |= (b & 0x01) << 31; - if (b < 0x80) { - return toNum(l, h); - } - throw new RuntimeException("Expected varint not more than 10 bytes"); + public static long readVarintRemainder(long l, ByteBuffer buf) { + long h, b; + b = buf.get() & 0xff; + h = (b & 0x70) >> 4; + if (b < 0x80) { + return toNum(l, h); + } + b = buf.get() & 0xff; + h |= (b & 0x7f) << 3; + if (b < 0x80) { + return toNum(l, h); + } + b = buf.get() & 0xff; + h |= (b & 0x7f) << 10; + if (b < 0x80) { + return toNum(l, h); } + b = buf.get() & 0xff; + h |= (b & 0x7f) << 17; + if (b < 0x80) { + return toNum(l, h); + } + b = buf.get() & 0xff; + h |= (b & 0x7f) << 24; + if (b < 0x80) { + return toNum(l, h); + } + b = buf.get() & 0xff; + h |= (b & 0x01) << 31; + if (b < 0x80) { + return toNum(l, h); + } + throw new RuntimeException("Expected varint not more than 10 bytes"); + } - public static long readVarint(ByteBuffer buf) { - long val, b; - b = buf.get() & 0xff; - val = b & 0x7f; - if (b < 0x80) { - return val; - } - b = buf.get() & 0xff; - val |= (b & 0x7f) << 7; - if (b < 0x80) { - return val; - } - b = buf.get() & 0xff; - val |= (b & 0x7f) << 14; - if (b < 0x80) { - return val; - } - b = buf.get() & 0xff; - val |= (b & 0x7f) << 21; - if (b < 0x80) { - return val; - } - val |= (b & 0x0f) << 28; - return readVarintRemainder(val, buf); + public static long readVarint(ByteBuffer buf) { + long val, b; + b = buf.get() & 0xff; + val = b & 0x7f; + if (b < 0x80) { + return val; + } + b = buf.get() & 0xff; + val |= (b & 0x7f) << 7; + if (b < 0x80) { + return val; } + b = buf.get() & 0xff; + val |= (b & 0x7f) << 14; + if (b < 0x80) { + return val; + } + b = buf.get() & 0xff; + val |= (b & 0x7f) << 21; + if (b < 0x80) { + return val; + } + val |= (b & 0x0f) << 28; + return readVarintRemainder(val, buf); + } - public static void rotate(long n, long[] xy, long rx, long ry) { - if (ry == 0) { - if (rx == 1) { - xy[0] = n - 1 - xy[0]; - xy[1] = n - 1 - xy[1]; - } - long t = xy[0]; - xy[0] = xy[1]; - xy[1] = t; - } + public static void rotate(long n, long[] xy, long rx, long ry) { + if (ry == 0) { + if (rx == 1) { + xy[0] = n - 1 - xy[0]; + xy[1] = n - 1 - xy[1]; + } + long t = xy[0]; + xy[0] = xy[1]; + xy[1] = t; } + } - public static long[] idOnLevel(int z, long pos) { - long n = LongMath.pow(2, z); - long rx, ry, t = pos; - long[] xy = new long[]{0, 0}; - long s = 1; - while (s < n) { - rx = 1 & (t / 2); - ry = 1 & (t ^ rx); - rotate(s, xy, rx, ry); - xy[0] += s * rx; - xy[1] += s * ry; - t = t / 4; - s *= 2; - } - return new long[]{z, xy[0], xy[1]}; + public static long[] idOnLevel(int z, long pos) { + long n = LongMath.pow(2, z); + long rx, ry, t = pos; + long[] xy = new long[] {0, 0}; + long s = 1; + while (s < n) { + rx = 1 & (t / 2); + ry = 1 & (t ^ rx); + rotate(s, xy, rx, ry); + xy[0] += s * rx; + xy[1] += s * ry; + t = t / 4; + s *= 2; } + return new long[] {z, xy[0], xy[1]}; + } - private static long[] tzValues = new long[]{ - 0, 1, 5, 21, 85, 341, 1365, 5461, 21845, 87381, 349525, 1398101, 5592405, - 22369621, 89478485, 357913941, 1431655765, 5726623061L, 22906492245L, - 91625968981L, 366503875925L, 1466015503701L, 5864062014805L, 23456248059221L, - 93824992236885L, 375299968947541L, 1501199875790165L, - }; + private static long[] tzValues = new long[] { + 0, 1, 5, 21, 85, 341, 1365, 5461, 21845, 87381, 349525, 1398101, 5592405, + 22369621, 89478485, 357913941, 1431655765, 5726623061L, 22906492245L, + 91625968981L, 366503875925L, 1466015503701L, 5864062014805L, 23456248059221L, + 93824992236885L, 375299968947541L, 1501199875790165L, + }; - public static long zxyToTileId(int z, long x, long y) { - if (z > 26) { - throw new RuntimeException("Tile zoom level exceeds max safe number limit (26)"); - } - if (x > Math.pow(2, z) - 1 || y > Math.pow(2, z) - 1) { - throw new RuntimeException("tile x/y outside zoom level bounds"); - } - long acc = tzValues[z]; - long n = LongMath.pow(2, z); - long rx = 0; - long ry = 0; - long d = 0; - long[] xy = new long[]{x, y}; - long s = n / 2; - while (s > 0) { - rx = (xy[0] & s) > 0 ? 1 : 0; - ry = (xy[1] & s) > 0 ? 1 : 0; - d += s * s * ((3 * rx) ^ ry); - rotate(s, xy, rx, ry); - s = s / 2; - } - return acc + d; + public static long zxyToTileId(int z, long x, long y) { + if (z > 26) { + throw new RuntimeException("Tile zoom level exceeds max safe number limit (26)"); } - - public static long[] tileIdToZxy(long i) { - long acc = 0; - for (int z = 0; z < 27; z++) { - long numTiles = (0x1L << z) * (0x1L << z); - if (acc + numTiles > i) { - return idOnLevel(z, i - acc); - } - acc += numTiles; - } - throw new RuntimeException("Tile zoom level exceeds max safe number limit (26)"); + if (x > Math.pow(2, z) - 1 || y > Math.pow(2, z) - 1) { + throw new RuntimeException("tile x/y outside zoom level bounds"); } - - enum Compression { - Unknown, - None, - Gzip, - Brotli, - Zstd, + long acc = tzValues[z]; + long n = LongMath.pow(2, z); + long rx = 0; + long ry = 0; + long d = 0; + long[] xy = new long[] {x, y}; + long s = n / 2; + while (s > 0) { + rx = (xy[0] & s) > 0 ? 1 : 0; + ry = (xy[1] & s) > 0 ? 1 : 0; + d += s * s * ((3 * rx) ^ ry); + rotate(s, xy, rx, ry); + s = s / 2; } + return acc + d; + } - enum TileType { - Unknown, - Mvt, - Png, - Jpeg, - Webp, - Avif, + public static long[] tileIdToZxy(long i) { + long acc = 0; + for (int z = 0; z < 27; z++) { + long numTiles = (0x1L << z) * (0x1L << z); + if (acc + numTiles > i) { + return idOnLevel(z, i - acc); + } + acc += numTiles; } + throw new RuntimeException("Tile zoom level exceeds max safe number limit (26)"); + } - public record Header( - int specVersion, - long rootDirectoryOffset, - long rootDirectoryLength, - long jsonMetadataOffset, - long jsonMetadataLength, - long leafDirectoryOffset, - long leafDirectoryLength, - long tileDataOffset, - long tileDataLength, - long numAddressedTiles, - long numTileEntries, - long numTileContents, - boolean clustered, - Compression internalCompression, - Compression tileCompression, - TileType tileType, - int minZoom, - int maxZoom, - double minLon, - double minLat, - double maxLon, - double maxLat, - int centerZoom, - double centerLon, - double centerLat, - String etag - ) {} + enum Compression { + Unknown, + None, + Gzip, + Brotli, + Zstd, + } - public static Header bytesToHeader(ByteBuffer buf, String etag) { - buf.order(ByteOrder.LITTLE_ENDIAN); - return new Header( - buf.get(7), - buf.getLong(8), - buf.getLong(16), - buf.getLong(24), - buf.getLong(32), - buf.getLong(40), - buf.getLong(48), - buf.getLong(56), - buf.getLong(64), - buf.getLong(72), - buf.getLong(80), - buf.getLong(88), - buf.get(96) == 1, - Compression.values()[buf.get(97)], - Compression.values()[buf.get(98)], - TileType.values()[buf.get(99)], - buf.get(100), - buf.get(101), - (double) buf.getInt(102) / 10000000, - (double) buf.getInt(106) / 10000000, - (double) buf.getInt(110) / 10000000, - (double) buf.getInt(114) / 10000000, - buf.get(118), - (double) buf.getInt(119) / 10000000, - (double) buf.getInt(123) / 10000000, - etag - ); - } + enum TileType { + Unknown, + Mvt, + Png, + Jpeg, + Webp, + Avif, + } - public static void headerToBytes(Header header, ByteBuffer buf) { - buf.order(ByteOrder.LITTLE_ENDIAN); - buf.put(0, (byte) 0x4d); - buf.put(1, (byte) 0x42); - buf.put(2, (byte) 0x54); - buf.put(3, (byte) 0x42); - buf.put(4, (byte) 0x49); - buf.put(5, (byte) 0x4e); - buf.put(6, (byte) 0x41); - buf.put(7, (byte) header.specVersion); - buf.putLong(8, header.rootDirectoryOffset); - buf.putLong(16, header.rootDirectoryLength); - buf.putLong(24, header.jsonMetadataOffset); - buf.putLong(32, header.jsonMetadataLength); - buf.putLong(40, header.leafDirectoryOffset); - buf.putLong(48, header.leafDirectoryLength); - buf.putLong(56, header.tileDataOffset); - buf.putLong(64, header.tileDataLength); - buf.putLong(72, header.numAddressedTiles); - buf.putLong(80, header.numTileEntries); - buf.putLong(88, header.numTileContents); - buf.put(96, (byte) (header.clustered ? 1 : 0)); - buf.put(97, (byte) header.internalCompression.ordinal()); - buf.put(98, (byte) header.tileCompression.ordinal()); - buf.put(99, (byte) header.tileType.ordinal()); - buf.put(100, (byte) header.minZoom); - buf.put(101, (byte) header.maxZoom); - buf.putInt(102, (int) (header.minLon * 10000000)); - buf.putInt(106, (int) (header.minLat * 10000000)); - buf.putInt(110, (int) (header.maxLon * 10000000)); - buf.putInt(114, (int) (header.maxLat * 10000000)); - buf.put(118, (byte) header.centerZoom); - buf.putInt(119, (int) (header.centerLon * 10000000)); - buf.putInt(123, (int) (header.centerLat * 10000000)); - } + public record Header( + int specVersion, + long rootDirectoryOffset, + long rootDirectoryLength, + long jsonMetadataOffset, + long jsonMetadataLength, + long leafDirectoryOffset, + long leafDirectoryLength, + long tileDataOffset, + long tileDataLength, + long numAddressedTiles, + long numTileEntries, + long numTileContents, + boolean clustered, + Compression internalCompression, + Compression tileCompression, + TileType tileType, + int minZoom, + int maxZoom, + double minLon, + double minLat, + double maxLon, + double maxLat, + int centerZoom, + double centerLon, + double centerLat, + String etag) { + } + + public static Header bytesToHeader(ByteBuffer buf, String etag) { + buf.order(ByteOrder.LITTLE_ENDIAN); + return new Header( + buf.get(7), + buf.getLong(8), + buf.getLong(16), + buf.getLong(24), + buf.getLong(32), + buf.getLong(40), + buf.getLong(48), + buf.getLong(56), + buf.getLong(64), + buf.getLong(72), + buf.getLong(80), + buf.getLong(88), + buf.get(96) == 1, + Compression.values()[buf.get(97)], + Compression.values()[buf.get(98)], + TileType.values()[buf.get(99)], + buf.get(100), + buf.get(101), + (double) buf.getInt(102) / 10000000, + (double) buf.getInt(106) / 10000000, + (double) buf.getInt(110) / 10000000, + (double) buf.getInt(114) / 10000000, + buf.get(118), + (double) buf.getInt(119) / 10000000, + (double) buf.getInt(123) / 10000000, + etag); + } + + public static void headerToBytes(Header header, ByteBuffer buf) { + buf.order(ByteOrder.LITTLE_ENDIAN); + buf.put(0, (byte) 0x50); + buf.put(1, (byte) 0x4D); + buf.put(2, (byte) 0x54); + buf.put(3, (byte) 0x69); + buf.put(4, (byte) 0x6C); + buf.put(5, (byte) 0x65); + buf.put(6, (byte) 0x73); + buf.put(7, (byte) header.specVersion); + buf.putLong(8, header.rootDirectoryOffset); + buf.putLong(16, header.rootDirectoryLength); + buf.putLong(24, header.jsonMetadataOffset); + buf.putLong(32, header.jsonMetadataLength); + buf.putLong(40, header.leafDirectoryOffset); + buf.putLong(48, header.leafDirectoryLength); + buf.putLong(56, header.tileDataOffset); + buf.putLong(64, header.tileDataLength); + buf.putLong(72, header.numAddressedTiles); + buf.putLong(80, header.numTileEntries); + buf.putLong(88, header.numTileContents); + buf.put(96, (byte) (header.clustered ? 1 : 0)); + buf.put(97, (byte) header.internalCompression.ordinal()); + buf.put(98, (byte) header.tileCompression.ordinal()); + buf.put(99, (byte) header.tileType.ordinal()); + buf.put(100, (byte) header.minZoom); + buf.put(101, (byte) header.maxZoom); + buf.putInt(102, (int) (header.minLon * 10000000)); + buf.putInt(106, (int) (header.minLat * 10000000)); + buf.putInt(110, (int) (header.maxLon * 10000000)); + buf.putInt(114, (int) (header.maxLat * 10000000)); + buf.put(118, (byte) header.centerZoom); + buf.putInt(119, (int) (header.centerLon * 10000000)); + buf.putInt(123, (int) (header.centerLat * 10000000)); + } } diff --git a/baremaps-core/src/test/java/org/apache/baremaps/tilestore/pmtiles/PMTilesTest.java b/baremaps-core/src/test/java/org/apache/baremaps/tilestore/pmtiles/PMTilesTest.java index 3eda409b..1fdf7129 100644 --- a/baremaps-core/src/test/java/org/apache/baremaps/tilestore/pmtiles/PMTilesTest.java +++ b/baremaps-core/src/test/java/org/apache/baremaps/tilestore/pmtiles/PMTilesTest.java @@ -1,161 +1,172 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ + package org.apache.baremaps.tilestore.pmtiles; +import static org.junit.jupiter.api.Assertions.*; + import com.google.common.math.LongMath; +import java.io.IOException; +import java.nio.ByteBuffer; +import java.nio.file.Files; import org.apache.baremaps.testing.TestFiles; import org.apache.baremaps.tilestore.pmtiles.PMTiles.Compression; import org.apache.baremaps.tilestore.pmtiles.PMTiles.TileType; import org.junit.jupiter.api.Test; -import java.io.IOException; -import java.nio.ByteBuffer; -import java.nio.file.Files; - -import static org.junit.jupiter.api.Assertions.*; - class PMTilesTest { - @Test - void varint() { - var b = ByteBuffer.wrap(new byte[]{ - (byte) 0, (byte) 1, - (byte) 127, (byte) 0xe5, - (byte) 0x8e, (byte) 0x26 - }); - assertEquals(PMTiles.readVarint(b), 0); - assertEquals(PMTiles.readVarint(b), 1); - assertEquals(PMTiles.readVarint(b), 127); - assertEquals(PMTiles.readVarint(b), 624485); - b = ByteBuffer.wrap(new byte[]{ - (byte) 0xff, (byte) 0xff, - (byte) 0xff, (byte) 0xff, - (byte) 0xff, (byte) 0xff, - (byte) 0xff, (byte) 0x0f, - }); - assertEquals(PMTiles.readVarint(b), 9007199254740991L); - } + @Test + void varint() { + var b = ByteBuffer.wrap(new byte[] { + (byte) 0, (byte) 1, + (byte) 127, (byte) 0xe5, + (byte) 0x8e, (byte) 0x26 + }); + assertEquals(PMTiles.readVarint(b), 0); + assertEquals(PMTiles.readVarint(b), 1); + assertEquals(PMTiles.readVarint(b), 127); + assertEquals(PMTiles.readVarint(b), 624485); + b = ByteBuffer.wrap(new byte[] { + (byte) 0xff, (byte) 0xff, + (byte) 0xff, (byte) 0xff, + (byte) 0xff, (byte) 0xff, + (byte) 0xff, (byte) 0x0f, + }); + assertEquals(PMTiles.readVarint(b), 9007199254740991L); + } - @Test - void zxyToTileId() { - assertEquals(PMTiles.zxyToTileId(0, 0, 0), 0); - assertEquals(PMTiles.zxyToTileId(1, 0, 0), 1); - assertEquals(PMTiles.zxyToTileId(1, 0, 1), 2); - assertEquals(PMTiles.zxyToTileId(1, 1, 1), 3); - assertEquals(PMTiles.zxyToTileId(1, 1, 0), 4); - assertEquals(PMTiles.zxyToTileId(2, 0, 0), 5); - } + @Test + void zxyToTileId() { + assertEquals(PMTiles.zxyToTileId(0, 0, 0), 0); + assertEquals(PMTiles.zxyToTileId(1, 0, 0), 1); + assertEquals(PMTiles.zxyToTileId(1, 0, 1), 2); + assertEquals(PMTiles.zxyToTileId(1, 1, 1), 3); + assertEquals(PMTiles.zxyToTileId(1, 1, 0), 4); + assertEquals(PMTiles.zxyToTileId(2, 0, 0), 5); + } - @Test - void tileIdToZxy() { - assertArrayEquals(PMTiles.tileIdToZxy(0), new long[]{0, 0, 0}); - assertArrayEquals(PMTiles.tileIdToZxy(1), new long[]{1, 0, 0}); - assertArrayEquals(PMTiles.tileIdToZxy(2), new long[]{1, 0, 1}); - assertArrayEquals(PMTiles.tileIdToZxy(3), new long[]{1, 1, 1}); - assertArrayEquals(PMTiles.tileIdToZxy(4), new long[]{1, 1, 0}); - assertArrayEquals(PMTiles.tileIdToZxy(5), new long[]{2, 0, 0}); - } + @Test + void tileIdToZxy() { + assertArrayEquals(PMTiles.tileIdToZxy(0), new long[] {0, 0, 0}); + assertArrayEquals(PMTiles.tileIdToZxy(1), new long[] {1, 0, 0}); + assertArrayEquals(PMTiles.tileIdToZxy(2), new long[] {1, 0, 1}); + assertArrayEquals(PMTiles.tileIdToZxy(3), new long[] {1, 1, 1}); + assertArrayEquals(PMTiles.tileIdToZxy(4), new long[] {1, 1, 0}); + assertArrayEquals(PMTiles.tileIdToZxy(5), new long[] {2, 0, 0}); + } - @Test - void aLotOfTiles() { - for (int z = 0; z < 9; z++) { - for (long x = 0; x < 1 << z; x++) { - for (long y = 0; y < 1 << z; y++) { - var result = PMTiles.tileIdToZxy(PMTiles.zxyToTileId(z, x, y)); - if (result[0] != z || result[1] != x || result[2] != y) { - fail("roundtrip failed"); - } - } - } + @Test + void aLotOfTiles() { + for (int z = 0; z < 9; z++) { + for (long x = 0; x < 1 << z; x++) { + for (long y = 0; y < 1 << z; y++) { + var result = PMTiles.tileIdToZxy(PMTiles.zxyToTileId(z, x, y)); + if (result[0] != z || result[1] != x || result[2] != y) { + fail("roundtrip failed"); + } } + } } + } - @Test - void tileExtremes() { - for (var z = 0; z < 27; z++) { - var dim = LongMath.pow(2, z) - 1; - var tl = PMTiles.tileIdToZxy(PMTiles.zxyToTileId(z, 0, 0)); - assertArrayEquals(new long[]{z, 0, 0}, tl); - var tr = PMTiles.tileIdToZxy(PMTiles.zxyToTileId(z, dim, 0)); - assertArrayEquals(new long[]{z, dim, 0}, tr); - var bl = PMTiles.tileIdToZxy(PMTiles.zxyToTileId(z, 0, dim)); - assertArrayEquals(new long[]{z, 0, dim}, bl); - var br = PMTiles.tileIdToZxy(PMTiles.zxyToTileId(z, dim, dim)); - assertArrayEquals(new long[]{z, dim, dim}, br); - } + @Test + void tileExtremes() { + for (var z = 0; z < 27; z++) { + var dim = LongMath.pow(2, z) - 1; + var tl = PMTiles.tileIdToZxy(PMTiles.zxyToTileId(z, 0, 0)); + assertArrayEquals(new long[] {z, 0, 0}, tl); + var tr = PMTiles.tileIdToZxy(PMTiles.zxyToTileId(z, dim, 0)); + assertArrayEquals(new long[] {z, dim, 0}, tr); + var bl = PMTiles.tileIdToZxy(PMTiles.zxyToTileId(z, 0, dim)); + assertArrayEquals(new long[] {z, 0, dim}, bl); + var br = PMTiles.tileIdToZxy(PMTiles.zxyToTileId(z, dim, dim)); + assertArrayEquals(new long[] {z, dim, dim}, br); } + } - @Test - void invalidTiles() { - assertThrows(RuntimeException.class, () -> PMTiles.tileIdToZxy(9007199254740991L)); - assertThrows(RuntimeException.class, () -> PMTiles.zxyToTileId(27, 0, 0)); - assertThrows(RuntimeException.class, () -> PMTiles.zxyToTileId(0, 1, 1)); - } + @Test + void invalidTiles() { + assertThrows(RuntimeException.class, () -> PMTiles.tileIdToZxy(9007199254740991L)); + assertThrows(RuntimeException.class, () -> PMTiles.zxyToTileId(27, 0, 0)); + assertThrows(RuntimeException.class, () -> PMTiles.zxyToTileId(0, 1, 1)); + } - @Test - void bytesToHeader() throws IOException { - var file = TestFiles.resolve("pmtiles/test_fixture_1.pmtiles"); - try (var channel = Files.newByteChannel(file)) { - var buffer = ByteBuffer.allocate(127); - channel.read(buffer); - buffer.flip(); - var header = PMTiles.bytesToHeader(buffer, "1"); - assertEquals(header.rootDirectoryOffset(), 127); - assertEquals(header.rootDirectoryLength(), 25); - assertEquals(header.jsonMetadataOffset(), 152); - assertEquals(header.jsonMetadataLength(), 247); - assertEquals(header.leafDirectoryOffset(), 0); - assertEquals(header.leafDirectoryLength(), 0); - assertEquals(header.tileDataOffset(), 399); - assertEquals(header.tileDataLength(), 69); - assertEquals(header.numAddressedTiles(), 1); - assertEquals(header.numTileEntries(), 1); - assertEquals(header.numTileContents(), 1); - assertFalse(header.clustered()); - assertEquals(header.internalCompression(), Compression.Gzip); - assertEquals(header.tileCompression(), Compression.Gzip); - assertEquals(header.tileType(), TileType.Mvt); - assertEquals(header.minZoom(), 0); - assertEquals(header.maxZoom(), 0); - assertEquals(header.minLon(), 0); - assertEquals(header.minLat(), 0); - assertEquals(Math.round(header.maxLon()), 1); - assertEquals(Math.round(header.maxLat()), 1); - } + @Test + void bytesToHeader() throws IOException { + var file = TestFiles.resolve("pmtiles/test_fixture_1.pmtiles"); + try (var channel = Files.newByteChannel(file)) { + var buffer = ByteBuffer.allocate(127); + channel.read(buffer); + buffer.flip(); + var header = PMTiles.bytesToHeader(buffer, "1"); + assertEquals(header.rootDirectoryOffset(), 127); + assertEquals(header.rootDirectoryLength(), 25); + assertEquals(header.jsonMetadataOffset(), 152); + assertEquals(header.jsonMetadataLength(), 247); + assertEquals(header.leafDirectoryOffset(), 0); + assertEquals(header.leafDirectoryLength(), 0); + assertEquals(header.tileDataOffset(), 399); + assertEquals(header.tileDataLength(), 69); + assertEquals(header.numAddressedTiles(), 1); + assertEquals(header.numTileEntries(), 1); + assertEquals(header.numTileContents(), 1); + assertFalse(header.clustered()); + assertEquals(header.internalCompression(), Compression.Gzip); + assertEquals(header.tileCompression(), Compression.Gzip); + assertEquals(header.tileType(), TileType.Mvt); + assertEquals(header.minZoom(), 0); + assertEquals(header.maxZoom(), 0); + assertEquals(header.minLon(), 0); + assertEquals(header.minLat(), 0); + assertEquals(Math.round(header.maxLon()), 1); + assertEquals(Math.round(header.maxLat()), 1); } + } - @Test - void headerToBytes() throws IOException { - var etag = "1"; - var buffer = ByteBuffer.allocate(127); - var header = new PMTiles.Header( - 127, - 25, - 152, - 247, - 0, - 0, - 399, - 69, - 1, - 1, - 1, - 10, - false, - Compression.Gzip, - Compression.Gzip, - TileType.Mvt, - 0, - 0, - 0, - 1, - 1, - 0, - 0, - 0, - 0, - etag); - PMTiles.headerToBytes(header, buffer); - var header2 = PMTiles.bytesToHeader(buffer, etag); - assertEquals(header, header2); - } + @Test + void headerToBytes() throws IOException { + var etag = "1"; + var buffer = ByteBuffer.allocate(127); + var header = new PMTiles.Header( + 127, + 25, + 152, + 247, + 0, + 0, + 399, + 69, + 1, + 1, + 1, + 10, + false, + Compression.Gzip, + Compression.Gzip, + TileType.Mvt, + 0, + 0, + 0, + 1, + 1, + 0, + 0, + 0, + 0, + etag); + PMTiles.headerToBytes(header, buffer); + var header2 = PMTiles.bytesToHeader(buffer, etag); + assertEquals(header, header2); + } -} \ No newline at end of file +}
