kopia lustrzana https://github.com/onthegomap/planetiler
finished mbtiles
rodzic
479f150aa3
commit
531a7e9bd1
4
pom.xml
4
pom.xml
|
@ -101,8 +101,8 @@
|
|||
<version>${jackson.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-annotations</artifactId>
|
||||
<groupId>com.fasterxml.jackson.datatype</groupId>
|
||||
<artifactId>jackson-datatype-jdk8</artifactId>
|
||||
<version>${jackson.version}</version>
|
||||
</dependency>
|
||||
|
||||
|
|
|
@ -1,7 +1,11 @@
|
|||
package com.onthegomap.flatmap.write;
|
||||
|
||||
import static com.fasterxml.jackson.annotation.JsonInclude.Include.NON_ABSENT;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
|
||||
import com.onthegomap.flatmap.geo.GeoUtils;
|
||||
import com.onthegomap.flatmap.geo.TileCoord;
|
||||
import java.io.Closeable;
|
||||
|
@ -41,7 +45,9 @@ public record Mbtiles(Connection connection) implements Closeable {
|
|||
public static final String METADATA_COL_VALUE = "value";
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(Mbtiles.class);
|
||||
private static final ObjectMapper objectMapper = new ObjectMapper();
|
||||
private static final ObjectMapper objectMapper = new ObjectMapper()
|
||||
.registerModules(new Jdk8Module())
|
||||
.setSerializationInclusion(NON_ABSENT);
|
||||
|
||||
public static Mbtiles newInMemoryDatabase() {
|
||||
try {
|
||||
|
@ -121,7 +127,7 @@ public record Mbtiles(Connection connection) implements Closeable {
|
|||
public Mbtiles vacuumAnalyze() {
|
||||
return execute(
|
||||
"VACUUM;",
|
||||
"ANALYZE"
|
||||
"ANALYZE;"
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -133,7 +139,14 @@ public record Mbtiles(Connection connection) implements Closeable {
|
|||
return new Metadata();
|
||||
}
|
||||
|
||||
public static record MetadataJson(List<VectorLayer> vectorLayers) {
|
||||
public static record MetadataJson(
|
||||
@JsonProperty("vector_layers")
|
||||
List<VectorLayer> vectorLayers
|
||||
) {
|
||||
|
||||
public MetadataJson(VectorLayer... layers) {
|
||||
this(List.of(layers));
|
||||
}
|
||||
|
||||
public static MetadataJson fromJson(String json) {
|
||||
try {
|
||||
|
@ -152,43 +165,32 @@ public record Mbtiles(Connection connection) implements Closeable {
|
|||
}
|
||||
|
||||
public enum FieldType {
|
||||
NUMBER("Number"),
|
||||
BOOLEAN("Boolean"),
|
||||
STRING("String");
|
||||
|
||||
private final String name;
|
||||
|
||||
FieldType(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return name;
|
||||
}
|
||||
@JsonProperty("Number") NUMBER,
|
||||
@JsonProperty("Boolean") BOOLEAN,
|
||||
@JsonProperty("String") STRING
|
||||
}
|
||||
|
||||
public static record VectorLayer(
|
||||
String id,
|
||||
Map<String, FieldType> fields,
|
||||
Optional<String> description,
|
||||
OptionalInt minzoom,
|
||||
OptionalInt maxzoom
|
||||
@JsonProperty("id") String id,
|
||||
@JsonProperty("fields") Map<String, FieldType> fields,
|
||||
@JsonProperty("description") Optional<String> description,
|
||||
@JsonProperty("minzoom") OptionalInt minzoom,
|
||||
@JsonProperty("maxzoom") OptionalInt maxzoom
|
||||
) {
|
||||
|
||||
public VectorLayer(String id, Map<String, FieldType> fields) {
|
||||
this(id, fields, Optional.empty(), OptionalInt.empty(), OptionalInt.empty());
|
||||
}
|
||||
|
||||
public VectorLayer copyWithDescription(String newDescription) {
|
||||
public VectorLayer withDescription(String newDescription) {
|
||||
return new VectorLayer(id, fields, Optional.of(newDescription), minzoom, maxzoom);
|
||||
}
|
||||
|
||||
public VectorLayer copyWithMinzoom(int newMinzoom) {
|
||||
public VectorLayer withMinzoom(int newMinzoom) {
|
||||
return new VectorLayer(id, fields, description, OptionalInt.of(newMinzoom), maxzoom);
|
||||
}
|
||||
|
||||
public VectorLayer copyWithMaxzoom(int newMaxzoom) {
|
||||
public VectorLayer withMaxzoom(int newMaxzoom) {
|
||||
return new VectorLayer(id, fields, description, minzoom, OptionalInt.of(newMaxzoom));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ package com.onthegomap.flatmap.write;
|
|||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.onthegomap.flatmap.geo.GeoUtils;
|
||||
import com.onthegomap.flatmap.geo.TileCoord;
|
||||
import java.io.IOException;
|
||||
|
@ -134,7 +135,67 @@ public class MbtilesTest {
|
|||
}
|
||||
}
|
||||
|
||||
// TODO: json encoding
|
||||
private void testMetadataJson(Mbtiles.MetadataJson object, String expected) throws IOException {
|
||||
var objectMapper = new ObjectMapper();
|
||||
try (Mbtiles db = Mbtiles.newInMemoryDatabase()) {
|
||||
var metadata = db.setupSchema().tuneForWrites().metadata();
|
||||
metadata.setJson(object);
|
||||
var actual = metadata.getAll().get("json");
|
||||
assertEquals(
|
||||
objectMapper.readTree(expected),
|
||||
objectMapper.readTree(actual)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMetadataJsonNoLayers() throws IOException {
|
||||
testMetadataJson(new Mbtiles.MetadataJson(), """
|
||||
{
|
||||
"vector_layers": []
|
||||
}
|
||||
""");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFullMetadataJson() throws IOException {
|
||||
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": {}
|
||||
}
|
||||
]
|
||||
}
|
||||
""");
|
||||
}
|
||||
|
||||
private static Set<Mbtiles.TileEntry> getAll(Mbtiles db) throws SQLException {
|
||||
Set<Mbtiles.TileEntry> result = new HashSet<>();
|
||||
|
|
Ładowanie…
Reference in New Issue