Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TileJSONExtended implementation #773

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions baremaps-cli/src/main/java/org/apache/baremaps/cli/map/Dev.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.apache.baremaps.utils.PostgresUtils;
import org.apache.baremaps.vectortile.style.Style;
import org.apache.baremaps.vectortile.tilejson.TileJSON;
import org.apache.baremaps.vectortile.tilejsonextended.TileJSONExtended;
import org.apache.baremaps.vectortile.tileset.Tileset;
import org.glassfish.hk2.api.TypeLiteral;
import org.glassfish.hk2.utilities.binding.AbstractBinder;
Expand Down Expand Up @@ -103,12 +104,23 @@ public Integer call() throws Exception {
}
};

var tileJSONExtendedSupplierType = new TypeLiteral<Supplier<TileJSONExtended>>() {};
var tileJSONExtendedSupplier = (Supplier<TileJSONExtended>) () -> {
try {
var config = configReader.read(tilesetPath);
return objectMapper.readValue(config, TileJSONExtended.class);
} catch (IOException e) {
throw new RuntimeException(e);
}
};

var application = new ResourceConfig()
.register(CorsFilter.class)
.register(ChangeResource.class)
.register(TileResource.class)
.register(StyleResource.class)
.register(TilesetResource.class)
.register(TileJsonExtendedResource.class)
.register(ChangeResource.class)
.register(ClassPathResource.class)
.register(newContextResolver(objectMapper))
Expand All @@ -122,6 +134,7 @@ protected void configure() {
bind(tileStoreSupplier).to(tileStoreType);
bind(styleSupplier).to(styleSupplierType);
bind(tileJSONSupplier).to(tileJSONSupplierType);
bind(tileJSONExtendedSupplier).to(tileJSONExtendedSupplierType);
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,12 @@
* https://github.com/mapbox/tilejson-spec</a>
*/
public class TileJSON {
@JsonProperty("tilejson")
String tilejson;
@JsonProperty("tiles")
List<String> tiles;
@JsonProperty("vector_layers")
List<VectorLayer> vectorLayers;
List<? extends VectorLayer> vectorLayers;
@JsonProperty("attribution")
String attribution;
@JsonProperty("bounds")
Expand Down Expand Up @@ -71,7 +72,12 @@ public void setTiles(List<String> tiles) {
this.tiles = tiles;
}

public List<VectorLayer> getVectorLayers() {
public void setVectorLayers(
List<? extends VectorLayer> vectorLayers) {
this.vectorLayers = vectorLayers;
}

public List<? extends VectorLayer> getVectorLayers() {
return vectorLayers;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,35 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.Map;

public record VectorLayer(
@JsonProperty("id") String id,
@JsonProperty("fields") Map<String, String> fields,
@JsonProperty("description") String description,
@JsonProperty("maxzoom") Integer maxzoom,
@JsonProperty("minzoom") Integer minzoom) {
public class VectorLayer {
@JsonProperty("id")
String id;
@JsonProperty("fields")
Map<String, String> fields;
@JsonProperty("description")
String description;
@JsonProperty("maxzoom")
Integer maxzoom;
@JsonProperty("minzoom")
Integer minzoom;

public String getId() {
return id;
}

public Map<String, String> getFields() {
return fields;
}

public String getDescription() {
return description;
}

public Integer getMaxzoom() {
return maxzoom;
}

public Integer getMinzoom() {
return minzoom;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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.vectortile.tilejsonextended;

import com.fasterxml.jackson.annotation.JsonGetter;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonSetter;
import java.util.List;
import org.apache.baremaps.vectortile.tilejson.TileJSON;


/**
* Implementation of TileJSON with custom additional fields for baremaps.
*/
public class TileJSONExtended extends TileJSON {

@JsonProperty("vector_layers")
List<VectorLayerExtended> vectorLayersExtended;

@JsonGetter("vector_layers")
public List<VectorLayerExtended> getVectorLayersExtended() {
return this.vectorLayersExtended;
}

@JsonSetter("vector_layers")
public void setVectorLayersExtended(List<VectorLayerExtended> vectorLayersExtended) {
this.vectorLayersExtended = vectorLayersExtended;
super.setVectorLayers(vectorLayersExtended);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* 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.vectortile.tilejsonextended;

import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.ArrayList;
import java.util.List;
import org.apache.baremaps.vectortile.tilejson.VectorLayer;
import org.apache.baremaps.vectortile.tileset.TilesetQuery;

public class VectorLayerExtended extends VectorLayer {

@JsonProperty("queries")
List<TilesetQuery> queries = new ArrayList<>();

public List<TilesetQuery> getQueries() {
return queries;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ public class TestFiles {

public static final Path STYLE_JS = resolve("style.js");

public static final Path TILESET_JSON = resolve("style.js");

public static Path resolve(String resource) {
Path cwd = Path.of("").toAbsolutePath();
Path pathFromRoot = Path.of("baremaps-core", "src", "test", "resources", resource);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,17 @@

import static org.apache.baremaps.utils.ObjectMapperUtils.objectMapper;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import org.apache.baremaps.config.ConfigReader;
import org.apache.baremaps.vectortile.tilejson.TileJSON;
import org.apache.baremaps.vectortile.tilejsonextended.TileJSONExtended;
import org.apache.baremaps.vectortile.tileset.Tileset;
import org.junit.Test;

Expand All @@ -46,7 +49,7 @@ public void testBasemapJSConfig() throws IOException {

assertEquals("jdbc:postgresql://localhost:5432/baremaps?&user=baremaps&password=baremaps",
tileSet.getDatabase());
assertEquals("aerialway", tileJSON.getVectorLayers().get(0).id());
assertEquals("aerialway", tileJSON.getVectorLayers().get(0).getId());
}

@Test
Expand All @@ -55,10 +58,40 @@ public void validateTileset() throws IOException {
var tileSet = objectMapper.readValue(resourceFile(tilesetFile), Tileset.class);
// Mapping to a POJO strictly following TileJSON specifications for API clients.
var tileJSON = objectMapper.readValue(resourceFile(tilesetFile), TileJSON.class);
// Mapping to a POJO following TileJSON with extended fields specific to baremaps
var tileJSONExtended =
objectMapper.readValue(resourceFile(tilesetFile), TileJSONExtended.class);


// Assert on Deserial
assertEquals("jdbc:postgresql://localhost:5432/baremaps?&user=baremaps&password=baremaps",
tileSet.getDatabase());
assertEquals("aeroway", tileJSON.getVectorLayers().get(0).id());
assertEquals("aeroway", tileJSON.getVectorLayers().get(0).getId());
assertEquals("aeroway", tileJSONExtended.getVectorLayers().get(0).getId());
assertEquals("aeroway", tileJSONExtended.getVectorLayersExtended().get(0).getId());
assertEquals("SELECT id, tags, geom FROM osm_nodes WHERE tags ? 'aeroway'",
tileJSONExtended.getVectorLayersExtended().get(0).getQueries().get(0).getSql());


var jsonTileSet = objectMapper.writeValueAsString(tileSet);
var jsonTileJSON = objectMapper.writeValueAsString(tileJSON);
var jsonTileJSONExtended = objectMapper.writeValueAsString(tileJSONExtended);

// Password is only in internal POJO TileSet
assertTrue(jsonTileSet.contains("password=baremaps"));
assertFalse(jsonTileJSON.contains("password=baremaps"));
assertFalse(jsonTileJSONExtended.contains("password=baremaps"));

// Queries are only in internal TileSet and exposed TileJSONExtended
assertTrue(jsonTileSet.contains("SELECT id"));
assertFalse(jsonTileJSON.contains("SELECT id"));
assertTrue(jsonTileJSONExtended.contains("SELECT id"));


// Validating the Deserial/Serial/Deserial is equals to Deserial/Serial
var tileJSONExtendedCopy = objectMapper.readValue(jsonTileJSONExtended, TileJSONExtended.class);
assertEquals(objectMapper.writeValueAsString(tileJSONExtended),
objectMapper.writeValueAsString(tileJSONExtendedCopy));
}

@Test
Expand All @@ -68,10 +101,11 @@ public void validateSpecificationExample() throws IOException {
// Mapping to a POJO strictly following TileJSON specifications for API clients.
var tileJSON = objectMapper.readValue(resourceFile(referenceFile), TileJSON.class);


assertNull(tileSet.getDatabase());
assertEquals("telephone", tileJSON.getVectorLayers().get(0).id());
assertEquals("telephone", tileJSON.getVectorLayers().get(0).getId());
assertEquals("the phone number", tileJSON.getVectorLayers().stream()
.filter(vl -> vl.id().equals("telephone"))
.findFirst().get().fields().get("phone_number"));
.filter(vl -> vl.getId().equals("telephone"))
.findFirst().get().getFields().get("phone_number"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* 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.server;

import java.util.function.Supplier;
import javax.inject.Inject;
import javax.inject.Singleton;
import javax.ws.rs.GET;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import org.apache.baremaps.vectortile.tilejsonextended.TileJSONExtended;

/**
* A resource that provides access to the tileJSON extended version
*/
@Singleton
@javax.ws.rs.Path("/")
public class TileJsonExtendedResource {

private final Supplier<TileJSONExtended> tileJSONSupplier;

@Inject
public TileJsonExtendedResource(Supplier<TileJSONExtended> tileJSONSupplier) {
this.tileJSONSupplier = tileJSONSupplier;
}

@GET
@javax.ws.rs.Path("tiles-extended.json")
@Produces(MediaType.APPLICATION_JSON)
public TileJSONExtended getTileset() {
return tileJSONSupplier.get();
}

}