regular metadata attributes

pull/1/head
Mike Barry 2021-05-02 07:34:33 -04:00
rodzic 08e7e41882
commit 479f150aa3
2 zmienionych plików z 105 dodań i 10 usunięć

Wyświetl plik

@ -11,12 +11,15 @@ import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.OptionalInt;
import java.util.TreeMap;
import java.util.stream.Collectors;
import java.util.stream.DoubleStream;
import org.locationtech.jts.geom.Coordinate;
@ -78,7 +81,7 @@ public record Mbtiles(Connection connection) implements Closeable {
private Mbtiles execute(String... queries) {
for (String query : queries) {
try (var statement = connection.createStatement()) {
LOGGER.info("Executing: " + query);
LOGGER.info("Execute mbtiles: " + query);
statement.execute(query);
} catch (SQLException throwables) {
throw new IllegalStateException("Error executing queries " + Arrays.toString(queries), throwables);
@ -130,10 +133,6 @@ public record Mbtiles(Connection connection) implements Closeable {
return new Metadata();
}
public static record MetadataRow(String name, String value) {
}
public static record MetadataJson(List<VectorLayer> vectorLayers) {
public static MetadataJson fromJson(String json) {
@ -263,13 +262,27 @@ public record Mbtiles(Connection connection) implements Closeable {
public class Metadata {
private static final NumberFormat nf = NumberFormat.getNumberInstance();
static {
nf.setMaximumFractionDigits(5);
}
private static String join(double... items) {
return DoubleStream.of(items).mapToObj(Double::toString).collect(Collectors.joining(","));
return DoubleStream.of(items).mapToObj(nf::format).collect(Collectors.joining(","));
}
public Metadata setMetadata(String name, Object value) {
if (value != null) {
LOGGER.info("Set mbtiles metadata: " + name + "=" + value);
try (PreparedStatement statement = connection.prepareStatement(
"INSERT INTO " + METADATA_TABLE + " (" + METADATA_COL_NAME + "," + METADATA_COL_VALUE + ") VALUES(?, ?);")) {
statement.setString(1, name);
statement.setString(2, value.toString());
statement.execute();
} catch (SQLException throwables) {
LOGGER.error("Error setting metadata " + name + "=" + value, throwables);
}
}
return this;
}
@ -300,7 +313,7 @@ public record Mbtiles(Connection connection) implements Closeable {
public Metadata setCenter(Envelope envelope) {
Coordinate center = envelope.centre();
double zoom = GeoUtils.getZoomFromLonLatBounds(envelope);
double zoom = Math.ceil(GeoUtils.getZoomFromLonLatBounds(envelope));
return setCenter(center.x, center.y, zoom);
}
@ -309,7 +322,7 @@ public record Mbtiles(Connection connection) implements Closeable {
}
public Metadata setMaxzoom(int maxZoom) {
return setMetadata("minzoom", maxZoom);
return setMetadata("maxzoom", maxZoom);
}
public Metadata setAttribution(String value) {
@ -345,7 +358,20 @@ public record Mbtiles(Connection connection) implements Closeable {
}
public Map<String, String> getAll() {
return Map.of();
TreeMap<String, String> result = new TreeMap<>();
try (Statement statement = connection.createStatement()) {
var resultSet = statement
.executeQuery("SELECT " + METADATA_COL_NAME + ", " + METADATA_COL_VALUE + " FROM " + METADATA_TABLE);
while (resultSet.next()) {
result.put(
resultSet.getString(METADATA_COL_NAME),
resultSet.getString(METADATA_COL_VALUE)
);
}
} catch (SQLException throwables) {
LOGGER.warn("Error retrieving metadata", throwables);
}
return result;
}
}

Wyświetl plik

@ -2,16 +2,20 @@ package com.onthegomap.flatmap.write;
import static org.junit.jupiter.api.Assertions.assertEquals;
import com.onthegomap.flatmap.geo.GeoUtils;
import com.onthegomap.flatmap.geo.TileCoord;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.locationtech.jts.geom.Envelope;
public class MbtilesTest {
@ -67,6 +71,71 @@ public class MbtilesTest {
testWriteTiles(10, false, true);
}
@Test
public void testAddMetadata() throws IOException {
Map<String, String> expected = new TreeMap<>();
try (Mbtiles db = Mbtiles.newInMemoryDatabase()) {
var metadata = db.setupSchema().tuneForWrites().metadata();
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
public void testAddMetadataWorldBounds() throws IOException {
Map<String, String> expected = new TreeMap<>();
try (Mbtiles db = Mbtiles.newInMemoryDatabase()) {
var metadata = db.setupSchema().tuneForWrites().metadata();
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
public void testAddMetadataSmallBounds() throws IOException {
Map<String, String> expected = new TreeMap<>();
try (Mbtiles db = Mbtiles.newInMemoryDatabase()) {
var metadata = db.setupSchema().tuneForWrites().metadata();
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());
}
}
// TODO: json encoding
private static Set<Mbtiles.TileEntry> getAll(Mbtiles db) throws SQLException {
Set<Mbtiles.TileEntry> result = new HashSet<>();
try (Statement statement = db.connection().createStatement()) {