fallback when tile fallback is missing (#937)

pull/943/head v0.8.1
Michael Barry 2024-06-28 07:25:17 -04:00 zatwierdzone przez GitHub
rodzic db0eb8afb8
commit 09374db575
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: B5690EEEBB952194
4 zmienionych plików z 50042 dodań i 12 usunięć

Wyświetl plik

@ -188,6 +188,16 @@
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>buildinfo.properties</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<filtering>false</filtering>
<excludes>
<exclude>buildinfo.properties</exclude>
</excludes>
</resource>
</resources>
<testResources>

Wyświetl plik

@ -15,6 +15,7 @@ import com.onthegomap.planetiler.geo.TileCoord;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Comparator;
@ -84,14 +85,33 @@ public class TileWeights {
/**
* Load tile weights from a gzipped TSV file with {@code z, x, y, loads} columns.
* <p>
* Duplicate entries will be added together.
* Duplicate entries will be added together. If the file is missing, will fall back to embedded file with top 50k OSM
* tiles.
*/
public static TileWeights readFromFile(Path path) {
try (var fileStream = new GZIPInputStream(new BufferedInputStream(Files.newInputStream(path)))) {
var result = readFrom(fileStream);
if (!result.weights.isEmpty()) {
return result;
}
} catch (IOException | RuntimeJsonMappingException e) {
LOGGER.info("Unable to load tile weights from {}, falling back to top 100k", path);
}
try (var resourceStream = TileWeights.class.getResourceAsStream("/top_50k_osm_tiles.tsv")) {
return readFrom(resourceStream);
} catch (IOException e) {
LOGGER.warn("Unable to load top 100k tile weights, falling back to unweighted average", e);
return new TileWeights();
}
}
private static TileWeights readFrom(InputStream input) throws IOException {
if (input == null) {
throw new IOException("No input provided");
}
TileWeights result = new TileWeights();
try (
var input = new GZIPInputStream(new BufferedInputStream(Files.newInputStream(path)));
var reader = READER.<Row>readValues(input)
) {
try (var reader = READER.<Row>readValues(input)) {
while (reader.hasNext()) {
var row = reader.next();
if (row.z >= PlanetilerConfig.MIN_MINZOOM && row.z <= PlanetilerConfig.MAX_MAXZOOM) {
@ -100,9 +120,6 @@ public class TileWeights {
result.put(TileCoord.ofXYZ(x, y, row.z()), row.loads());
}
}
} catch (IOException | RuntimeJsonMappingException e) {
LOGGER.warn("Unable to load tile weights from {}, will fall back to unweighted average: {}", path, e);
return new TileWeights();
}
return result;
}

Wyświetl plik

@ -42,7 +42,8 @@ class TileWeightsTest {
Path file = path.resolve("test.tsv.gz");
new TileWeights().writeToFile(file);
var read = TileWeights.readFromFile(file);
assertEquals(0, read.getWeight(TileCoord.ofXYZ(0, 0, 0)));
// when read is empty, fall back to embedded
assertEquals(376201934, read.getWeight(TileCoord.ofXYZ(0, 0, 0)));
}
@Test
@ -67,18 +68,19 @@ class TileWeightsTest {
void testReadCorruptFile(@TempDir Path path) throws IOException {
Path file = path.resolve("test.tsv.gz");
var result = TileWeights.readFromFile(file);
assertEquals(0, result.getWeight(TileCoord.ofXYZ(0, 0, 0)));
int expected = 376201934;
assertEquals(expected, result.getWeight(TileCoord.ofXYZ(0, 0, 0)));
Files.write(file, Gzip.gzip("""
garbage
""".getBytes(StandardCharsets.UTF_8)));
assertEquals(0, TileWeights.readFromFile(file).getWeight(TileCoord.ofXYZ(0, 0, 0)));
assertEquals(expected, TileWeights.readFromFile(file).getWeight(TileCoord.ofXYZ(0, 0, 0)));
Files.write(file, Gzip.gzip("""
z x y loads
a b c d
""".getBytes(StandardCharsets.UTF_8)));
assertEquals(0, TileWeights.readFromFile(file).getWeight(TileCoord.ofXYZ(0, 0, 0)));
assertEquals(expected, TileWeights.readFromFile(file).getWeight(TileCoord.ofXYZ(0, 0, 0)));
Files.write(file, Gzip.gzip("""
z x d loads