planetiler/planetiler-core/src/test/java/com/onthegomap/planetiler/mbtiles/MbtilesTest.java

205 wiersze
6.2 KiB
Java
Czysty Zwykły widok Historia

package com.onthegomap.planetiler.mbtiles;
2021-05-01 20:40:44 +00:00
import static com.onthegomap.planetiler.TestUtils.assertSameJson;
2021-06-03 01:25:16 +00:00
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
2021-05-02 11:00:13 +00:00
import static org.junit.jupiter.api.Assertions.assertEquals;
2021-06-03 01:25:16 +00:00
import static org.junit.jupiter.api.Assertions.assertNull;
2021-05-02 11:00:13 +00:00
import com.onthegomap.planetiler.TestUtils;
import com.onthegomap.planetiler.geo.GeoUtils;
import com.onthegomap.planetiler.geo.TileCoord;
2021-05-02 11:00:13 +00:00
import java.io.IOException;
import java.sql.SQLException;
2021-06-04 09:22:27 +00:00
import java.util.HashSet;
2021-05-02 11:34:33 +00:00
import java.util.Map;
2021-05-02 11:00:13 +00:00
import java.util.Set;
2021-05-02 11:34:33 +00:00
import java.util.TreeMap;
import java.util.TreeSet;
2021-06-04 09:22:27 +00:00
import java.util.stream.Collectors;
2021-05-02 11:00:13 +00:00
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
2021-05-02 11:34:33 +00:00
import org.locationtech.jts.geom.Envelope;
2021-05-02 11:00:13 +00:00
2022-04-23 10:36:24 +00:00
class MbtilesTest {
2021-05-01 20:40:44 +00:00
2021-05-02 11:00:13 +00:00
private static final int BATCH = 999 / 4;
2022-04-23 10:36:24 +00:00
void testWriteTiles(int howMany, boolean deferIndexCreation, boolean optimize)
2021-05-02 11:00:13 +00:00
throws IOException, SQLException {
try (Mbtiles db = Mbtiles.newInMemoryDatabase()) {
2021-09-10 00:46:20 +00:00
db.createTables();
2021-05-02 11:00:13 +00:00
if (!deferIndexCreation) {
2021-09-10 00:46:20 +00:00
db.addTileIndex();
2021-05-02 11:00:13 +00:00
}
2021-06-03 01:25:16 +00:00
assertNull(db.getTile(0, 0, 0));
Set<Mbtiles.TileEntry> expected = new TreeSet<>();
2021-05-02 11:00:13 +00:00
try (var writer = db.newBatchedTileWriter()) {
for (int i = 0; i < howMany; i++) {
2021-05-13 10:25:06 +00:00
var entry = new Mbtiles.TileEntry(TileCoord.ofXYZ(i, i + 1, 14), new byte[]{
2021-05-02 11:00:13 +00:00
(byte) howMany,
(byte) (howMany >> 8),
(byte) (howMany >> 16),
(byte) (howMany >> 24)
});
writer.write(entry.tile(), entry.bytes());
expected.add(entry);
}
}
if (deferIndexCreation) {
2021-09-10 00:46:20 +00:00
db.addTileIndex();
2021-05-02 11:00:13 +00:00
}
if (optimize) {
db.vacuumAnalyze();
}
2021-05-08 10:53:37 +00:00
var all = TestUtils.getAllTiles(db);
2021-05-02 11:00:13 +00:00
assertEquals(howMany, all.size());
assertEquals(expected, all);
2021-06-04 09:22:27 +00:00
assertEquals(expected.stream().map(Mbtiles.TileEntry::tile).collect(Collectors.toSet()),
new HashSet<>(db.getAllTileCoords()));
2021-06-03 01:25:16 +00:00
for (var expectedEntry : expected) {
var tile = expectedEntry.tile();
byte[] data = db.getTile(tile.x(), tile.y(), tile.z());
assertArrayEquals(expectedEntry.bytes(), data);
}
2021-05-02 11:00:13 +00:00
}
}
@ParameterizedTest
@ValueSource(ints = {0, 1, BATCH, BATCH + 1, 2 * BATCH, 2 * BATCH + 1})
2022-04-23 10:36:24 +00:00
void testWriteTilesDifferentSize(int howMany) throws IOException, SQLException {
2021-05-02 11:00:13 +00:00
testWriteTiles(howMany, false, false);
}
@Test
2022-04-23 10:36:24 +00:00
void testDeferIndexCreation() throws IOException, SQLException {
2021-05-02 11:00:13 +00:00
testWriteTiles(10, true, false);
}
@Test
2022-04-23 10:36:24 +00:00
void testVacuumAnalyze() throws IOException, SQLException {
2021-05-02 11:00:13 +00:00
testWriteTiles(10, false, true);
}
2021-05-02 11:34:33 +00:00
@Test
2022-04-23 10:36:24 +00:00
void testAddMetadata() throws IOException {
2021-05-02 11:34:33 +00:00
Map<String, String> expected = new TreeMap<>();
try (Mbtiles db = Mbtiles.newInMemoryDatabase()) {
2021-09-10 00:46:20 +00:00
var metadata = db.createTables().metadata();
2021-05-02 11:34:33 +00:00
metadata.setName("name value");
expected.put("name", "name value");
metadata.setFormat("pbf");
expected.put("format", "pbf");
metadata.setAttribution("attribution value");
expected.put("attribution", "attribution value");
metadata.setBoundsAndCenter(GeoUtils.toLatLonBoundsBounds(new Envelope(0.25, 0.75, 0.25, 0.75)));
expected.put("bounds", "-90,-66.51326,90,66.51326");
expected.put("center", "0,0,1");
metadata.setDescription("description value");
expected.put("description", "description value");
metadata.setMinzoom(1);
expected.put("minzoom", "1");
metadata.setMaxzoom(13);
expected.put("maxzoom", "13");
metadata.setVersion("1.2.3");
expected.put("version", "1.2.3");
metadata.setTypeIsBaselayer();
expected.put("type", "baselayer");
assertEquals(expected, metadata.getAll());
}
}
@Test
2022-04-23 10:36:24 +00:00
void testAddMetadataWorldBounds() throws IOException {
2021-05-02 11:34:33 +00:00
Map<String, String> expected = new TreeMap<>();
try (Mbtiles db = Mbtiles.newInMemoryDatabase()) {
2021-09-10 00:46:20 +00:00
var metadata = db.createTables().metadata();
2021-05-02 11:34:33 +00:00
metadata.setBoundsAndCenter(GeoUtils.WORLD_LAT_LON_BOUNDS);
expected.put("bounds", "-180,-85.05113,180,85.05113");
expected.put("center", "0,0,0");
assertEquals(expected, metadata.getAll());
}
}
@Test
2022-04-23 10:36:24 +00:00
void testAddMetadataSmallBounds() throws IOException {
2021-05-02 11:34:33 +00:00
Map<String, String> expected = new TreeMap<>();
try (Mbtiles db = Mbtiles.newInMemoryDatabase()) {
2021-09-10 00:46:20 +00:00
var metadata = db.createTables().metadata();
2021-05-02 11:34:33 +00:00
metadata.setBoundsAndCenter(new Envelope(-73.6632, -69.7598, 41.1274, 43.0185));
expected.put("bounds", "-73.6632,41.1274,-69.7598,43.0185");
expected.put("center", "-71.7115,42.07295,7");
assertEquals(expected, metadata.getAll());
}
}
2021-05-03 00:42:52 +00:00
private void testMetadataJson(Mbtiles.MetadataJson object, String expected) throws IOException {
try (Mbtiles db = Mbtiles.newInMemoryDatabase()) {
2021-09-10 00:46:20 +00:00
var metadata = db.createTables().metadata();
2021-05-03 00:42:52 +00:00
metadata.setJson(object);
var actual = metadata.getAll().get("json");
2021-05-13 10:25:06 +00:00
assertSameJson(expected, actual);
2021-05-03 00:42:52 +00:00
}
}
@Test
2022-04-23 10:36:24 +00:00
void testMetadataJsonNoLayers() throws IOException {
2021-05-03 00:42:52 +00:00
testMetadataJson(new Mbtiles.MetadataJson(), """
{
"vector_layers": []
}
""");
}
@Test
2022-04-23 10:36:24 +00:00
void testFullMetadataJson() throws IOException {
2021-05-03 00:42:52 +00:00
testMetadataJson(new Mbtiles.MetadataJson(
new Mbtiles.MetadataJson.VectorLayer(
"full",
Map.of(
"NUMBER_FIELD", Mbtiles.MetadataJson.FieldType.NUMBER,
"STRING_FIELD", Mbtiles.MetadataJson.FieldType.STRING,
"boolean field", Mbtiles.MetadataJson.FieldType.BOOLEAN
)
).withDescription("full description")
.withMinzoom(0)
.withMaxzoom(5),
new Mbtiles.MetadataJson.VectorLayer(
"partial",
Map.of()
)
), """
{
"vector_layers": [
{
"id": "full",
"description": "full description",
"minzoom": 0,
"maxzoom": 5,
"fields": {
"NUMBER_FIELD": "Number",
"STRING_FIELD": "String",
"boolean field": "Boolean"
}
},
{
"id": "partial",
"fields": {}
}
]
}
""");
}
2021-05-01 20:40:44 +00:00
}