You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@baremaps.apache.org by bc...@apache.org on 2023/06/19 11:14:31 UTC

[incubator-baremaps] 06/09: Add mbtiles command

This is an automated email from the ASF dual-hosted git repository.

bchapuis pushed a commit to branch 688-export
in repository https://gitbox.apache.org/repos/asf/incubator-baremaps.git

commit 2498555787347b0fd09c104471f7106ac79643dc
Author: Bertil Chapuis <bc...@gmail.com>
AuthorDate: Fri Jun 16 22:51:27 2023 +0200

    Add mbtiles command
---
 .run/basemap-mbtiles.run.xml                       |  17 ++++
 .../java/org/apache/baremaps/cli/map/MBTiles.java  | 100 +++++++++++++++++++++
 .../main/java/org/apache/baremaps/cli/map/Map.java |   2 +-
 3 files changed, 118 insertions(+), 1 deletion(-)

diff --git a/.run/basemap-mbtiles.run.xml b/.run/basemap-mbtiles.run.xml
new file mode 100644
index 00000000..3a08cf9a
--- /dev/null
+++ b/.run/basemap-mbtiles.run.xml
@@ -0,0 +1,17 @@
+<component name="ProjectRunConfigurationManager">
+  <configuration default="false" name="basemap-mbtiles" type="Application" factoryName="Application">
+    <option name="MAIN_CLASS_NAME" value="org.apache.baremaps.cli.Baremaps" />
+    <module name="baremaps-cli" />
+    <option name="PROGRAM_PARAMETERS" value="map mbtiles --mbtiles $USER_HOME$/Datasets/Baremaps/tiles.mbtiles --tileset tileset.js --style style.js" />
+    <option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/basemap" />
+    <extension name="coverage">
+      <pattern>
+        <option name="PATTERN" value="org.apache.baremaps.server.ogcapi.*" />
+        <option name="ENABLED" value="true" />
+      </pattern>
+    </extension>
+    <method v="2">
+      <option name="Make" enabled="true" />
+    </method>
+  </configuration>
+</component>
\ No newline at end of file
diff --git a/baremaps-cli/src/main/java/org/apache/baremaps/cli/map/MBTiles.java b/baremaps-cli/src/main/java/org/apache/baremaps/cli/map/MBTiles.java
new file mode 100644
index 00000000..f4478ca9
--- /dev/null
+++ b/baremaps-cli/src/main/java/org/apache/baremaps/cli/map/MBTiles.java
@@ -0,0 +1,100 @@
+/*
+ * 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.cli.map;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.github.benmanes.caffeine.cache.CaffeineSpec;
+import io.servicetalk.http.netty.HttpServers;
+import io.servicetalk.http.router.jersey.HttpJerseyRouterBuilder;
+import org.apache.baremaps.cli.Options;
+import org.apache.baremaps.config.ConfigReader;
+import org.apache.baremaps.postgres.PostgresUtils;
+import org.apache.baremaps.server.CorsFilter;
+import org.apache.baremaps.server.ServerResources;
+import org.apache.baremaps.tilestore.TileCache;
+import org.apache.baremaps.tilestore.TileStore;
+import org.apache.baremaps.tilestore.postgres.PostgresTileStore;
+import org.apache.baremaps.vectortile.tileset.Tileset;
+import org.apache.baremaps.workflow.tasks.ExportVectorTiles;
+import org.glassfish.hk2.utilities.binding.AbstractBinder;
+import org.glassfish.jersey.server.ResourceConfig;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import picocli.CommandLine.Command;
+import picocli.CommandLine.Mixin;
+import picocli.CommandLine.Option;
+
+import java.nio.file.Path;
+import java.util.concurrent.Callable;
+
+import static io.servicetalk.data.jackson.jersey.ServiceTalkJacksonSerializerFeature.contextResolverFor;
+import static io.servicetalk.data.jackson.jersey.ServiceTalkJacksonSerializerFeature.newContextResolver;
+import static org.apache.baremaps.utils.ObjectMapperUtils.objectMapper;
+
+@Command(name = "mbtiles", description = "Start a mbtiles server with caching capabilities.")
+public class MBTiles implements Callable<Integer> {
+
+  private static final Logger logger = LoggerFactory.getLogger(MBTiles.class);
+
+  @Mixin
+  private Options options;
+
+  @Option(names = {"--cache"}, paramLabel = "CACHE", description = "The caffeine cache directive.")
+  private String cache = "";
+
+  @Option(names = {"--mbtiles"}, paramLabel = "MBTILES", description = "The mbtiles file.",
+      required = true)
+  private Path mbtiles;
+
+  @Option(names = {"--tilejson"}, paramLabel = "TILEJSON", description = "The tileJSON file.",
+          required = true)
+  private Path tileset;
+
+  @Option(names = {"--style"}, paramLabel = "STYLE", description = "The style file.",
+      required = true)
+  private Path style;
+
+  @Option(names = {"--port"}, paramLabel = "PORT", description = "The port of the server.")
+  private int port = 9000;
+
+  @Override
+  public Integer call() throws Exception {
+    var objectMapper = objectMapper();
+    var caffeineSpec = CaffeineSpec.parse(cache);
+    var datasource = ExportVectorTiles.createDataSource(mbtiles);
+
+    var tileStore = new org.apache.baremaps.tilestore.mbtiles.MBTiles(datasource);
+    var tileCache = new TileCache(tileStore, caffeineSpec);
+
+    // Configure the application
+    var application =
+        new ResourceConfig().register(CorsFilter.class).register(ServerResources.class)
+            .register(newContextResolver(objectMapper)).register(new AbstractBinder() {
+              @Override
+              protected void configure() {
+                bind(tileset).to(Path.class).named("tileset");
+                bind(style).to(Path.class).named("style");
+                bind(tileCache).to(TileStore.class);
+                bind(objectMapper).to(ObjectMapper.class);
+              }
+            });
+
+    var httpService = new HttpJerseyRouterBuilder().buildBlockingStreaming(application);
+    var serverContext = HttpServers.forPort(port).listenBlockingStreamingAndAwait(httpService);
+
+    logger.info("Listening on {}", serverContext.listenAddress());
+
+    serverContext.awaitShutdown();
+    return 0;
+  }
+}
diff --git a/baremaps-cli/src/main/java/org/apache/baremaps/cli/map/Map.java b/baremaps-cli/src/main/java/org/apache/baremaps/cli/map/Map.java
index 9ed602d0..f84dea54 100644
--- a/baremaps-cli/src/main/java/org/apache/baremaps/cli/map/Map.java
+++ b/baremaps-cli/src/main/java/org/apache/baremaps/cli/map/Map.java
@@ -18,7 +18,7 @@ import picocli.CommandLine;
 import picocli.CommandLine.Command;
 
 @Command(name = "map", description = "Map commands.",
-    subcommands = {Init.class, Export.class, Serve.class, Dev.class}, sortOptions = false)
+    subcommands = {Init.class, Export.class, Serve.class, Dev.class, MBTiles.class}, sortOptions = false)
 public class Map implements Runnable {
 
   @Override