Search in sources :

Example 1 with TileStoreException

use of com.baremaps.tile.TileStoreException in project baremaps by baremaps.

the class TilesetsResource method getTile.

@Override
public Response getTile(UUID tilesetId, String tileMatrixSetId, Integer tileMatrix, Integer tileRow, Integer tileCol) {
    Tile tile = new Tile(tileCol, tileRow, tileMatrix);
    TileStore tileStore = tileStores.get(tilesetId);
    try {
        return Response.ok(tileStore.read(tile)).header(CONTENT_ENCODING, "gzip").build();
    } catch (TileStoreException e) {
        return Response.serverError().build();
    }
}
Also used : PostgresTileStore(com.baremaps.tile.postgres.PostgresTileStore) TileStore(com.baremaps.tile.TileStore) Tile(com.baremaps.tile.Tile) TileStoreException(com.baremaps.tile.TileStoreException)

Example 2 with TileStoreException

use of com.baremaps.tile.TileStoreException in project baremaps by baremaps.

the class MBTiles method write.

/**
 * {@inheritDoc}
 */
@Override
public void write(Tile tile, Blob blob) throws TileStoreException {
    try (Connection connection = dataSource.getConnection();
        PreparedStatement statement = connection.prepareStatement(INSERT_TILE)) {
        statement.setInt(1, tile.z());
        statement.setInt(2, tile.x());
        statement.setInt(3, reverseY(tile.y(), tile.z()));
        try (InputStream inputStream = blob.getInputStream()) {
            statement.setBytes(4, inputStream.readAllBytes());
        }
        statement.executeUpdate();
    } catch (SQLException | IOException e) {
        throw new TileStoreException(e);
    }
}
Also used : SQLException(java.sql.SQLException) InputStream(java.io.InputStream) Connection(java.sql.Connection) TileStoreException(com.baremaps.tile.TileStoreException) PreparedStatement(java.sql.PreparedStatement) IOException(java.io.IOException)

Example 3 with TileStoreException

use of com.baremaps.tile.TileStoreException in project baremaps by baremaps.

the class PostgresTileStore method read.

/**
 * {@inheritDoc}
 */
@Override
public Blob read(Tile tile) throws TileStoreException {
    try (Connection connection = datasource.getConnection();
        Statement statement = connection.createStatement();
        ByteArrayOutputStream data = new ByteArrayOutputStream()) {
        String sql = withQuery(tile);
        logger.debug("Executing query: {}", sql);
        int length = 0;
        try (GZIPOutputStream gzip = new GZIPOutputStream(data);
            ResultSet resultSet = statement.executeQuery(sql)) {
            while (resultSet.next()) {
                byte[] bytes = resultSet.getBytes(1);
                length += bytes.length;
                gzip.write(bytes);
            }
        }
        if (length > 0) {
            byte[] byteArray = data.toByteArray();
            return Blob.builder().withContentEncoding(CONTENT_ENCODING).withContentType(CONTENT_TYPE).withContentLength(Long.valueOf(byteArray.length)).withByteArray(byteArray).build();
        } else {
            return null;
        }
    } catch (SQLException | IOException e) {
        throw new TileStoreException(e);
    }
}
Also used : GZIPOutputStream(java.util.zip.GZIPOutputStream) SQLException(java.sql.SQLException) Statement(java.sql.Statement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) TileStoreException(com.baremaps.tile.TileStoreException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 4 with TileStoreException

use of com.baremaps.tile.TileStoreException in project baremaps by baremaps.

the class MBTiles method read.

/**
 * {@inheritDoc}
 */
@Override
public Blob read(Tile tile) throws TileStoreException {
    try (Connection connection = dataSource.getConnection();
        PreparedStatement statement = connection.prepareStatement(SELECT_TILE)) {
        statement.setInt(1, tile.z());
        statement.setInt(2, tile.x());
        statement.setInt(3, reverseY(tile.y(), tile.z()));
        try (ResultSet resultSet = statement.executeQuery()) {
            if (resultSet.next()) {
                return Blob.builder().withByteArray(resultSet.getBytes("tile_data")).build();
            } else {
                throw new SQLException("The tile does not exist");
            }
        }
    } catch (SQLException e) {
        throw new TileStoreException(e);
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) TileStoreException(com.baremaps.tile.TileStoreException) PreparedStatement(java.sql.PreparedStatement)

Example 5 with TileStoreException

use of com.baremaps.tile.TileStoreException in project baremaps by baremaps.

the class Export method call.

@Override
public Integer call() throws TileStoreException, BlobStoreException, IOException {
    ObjectMapper mapper = defaultObjectMapper();
    DataSource datasource = PostgresUtils.datasource(database);
    BlobStore blobStore = options.blobStore();
    TileJSON source = mapper.readValue(blobStore.get(this.tileset).getInputStream(), TileJSON.class);
    TileStore tileSource = sourceTileStore(source, datasource);
    TileStore tileTarget = targetTileStore(source, blobStore);
    Stream<Tile> stream;
    if (tiles == null) {
        Envelope envelope = new Envelope(source.getBounds().get(0), source.getBounds().get(2), source.getBounds().get(1), source.getBounds().get(3));
        long count = Tile.count(envelope, source.getMinzoom(), source.getMaxzoom());
        stream = StreamUtils.stream(Tile.iterator(envelope, source.getMinzoom(), source.getMaxzoom())).peek(new StreamProgress<>(count, 5000));
    } else {
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(blobStore.get(tiles).getInputStream()))) {
            stream = reader.lines().flatMap(line -> {
                String[] array = line.split(",");
                int x = Integer.parseInt(array[0]);
                int y = Integer.parseInt(array[1]);
                int z = Integer.parseInt(array[2]);
                Tile tile = new Tile(x, y, z);
                return StreamUtils.stream(Tile.iterator(tile.envelope(), source.getMinzoom(), source.getMaxzoom()));
            });
        }
    }
    logger.info("Exporting tiles");
    StreamUtils.batch(stream, 10).filter(new TileBatchPredicate(batchArraySize, batchArrayIndex)).forEach(new TileChannel(tileSource, tileTarget));
    logger.info("Done");
    return 0;
}
Also used : DefaultObjectMapper.defaultObjectMapper(com.baremaps.server.common.DefaultObjectMapper.defaultObjectMapper) MBTiles(com.baremaps.tile.mbtiles.MBTiles) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) Callable(java.util.concurrent.Callable) Conversions.asPostgresQuery(com.baremaps.server.ogcapi.Conversions.asPostgresQuery) TileBatchPredicate(com.baremaps.tile.TileBatchPredicate) BigDecimal(java.math.BigDecimal) TileChannel(com.baremaps.tile.TileChannel) Map(java.util.Map) DataSource(javax.sql.DataSource) TileStore(com.baremaps.tile.TileStore) URI(java.net.URI) Command(picocli.CommandLine.Command) SQLiteDataSource(org.sqlite.SQLiteDataSource) PostgresUtils(com.baremaps.postgres.jdbc.PostgresUtils) BlobStoreException(com.baremaps.blob.BlobStoreException) Query(com.baremaps.model.Query) PostgresQuery(com.baremaps.tile.postgres.PostgresQuery) Logger(org.slf4j.Logger) TileJSON(com.baremaps.model.TileJSON) PostgresTileStore(com.baremaps.tile.postgres.PostgresTileStore) Mixin(picocli.CommandLine.Mixin) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) TileBlobStore(com.baremaps.tile.TileBlobStore) InputStreamReader(java.io.InputStreamReader) Collectors(java.util.stream.Collectors) TileStoreException(com.baremaps.tile.TileStoreException) List(java.util.List) Option(picocli.CommandLine.Option) Stream(java.util.stream.Stream) Tile(com.baremaps.tile.Tile) StreamUtils(com.baremaps.stream.StreamUtils) BlobStore(com.baremaps.blob.BlobStore) StreamProgress(com.baremaps.osm.progress.StreamProgress) BufferedReader(java.io.BufferedReader) Envelope(org.locationtech.jts.geom.Envelope) TileStore(com.baremaps.tile.TileStore) PostgresTileStore(com.baremaps.tile.postgres.PostgresTileStore) InputStreamReader(java.io.InputStreamReader) Tile(com.baremaps.tile.Tile) Envelope(org.locationtech.jts.geom.Envelope) StreamProgress(com.baremaps.osm.progress.StreamProgress) DataSource(javax.sql.DataSource) SQLiteDataSource(org.sqlite.SQLiteDataSource) TileChannel(com.baremaps.tile.TileChannel) TileJSON(com.baremaps.model.TileJSON) BufferedReader(java.io.BufferedReader) TileBatchPredicate(com.baremaps.tile.TileBatchPredicate) DefaultObjectMapper.defaultObjectMapper(com.baremaps.server.common.DefaultObjectMapper.defaultObjectMapper) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) TileBlobStore(com.baremaps.tile.TileBlobStore) BlobStore(com.baremaps.blob.BlobStore)

Aggregations

TileStoreException (com.baremaps.tile.TileStoreException)7 Connection (java.sql.Connection)5 SQLException (java.sql.SQLException)5 PreparedStatement (java.sql.PreparedStatement)4 IOException (java.io.IOException)3 Tile (com.baremaps.tile.Tile)2 TileStore (com.baremaps.tile.TileStore)2 PostgresTileStore (com.baremaps.tile.postgres.PostgresTileStore)2 ResultSet (java.sql.ResultSet)2 Statement (java.sql.Statement)2 BlobStore (com.baremaps.blob.BlobStore)1 BlobStoreException (com.baremaps.blob.BlobStoreException)1 Query (com.baremaps.model.Query)1 TileJSON (com.baremaps.model.TileJSON)1 StreamProgress (com.baremaps.osm.progress.StreamProgress)1 PostgresUtils (com.baremaps.postgres.jdbc.PostgresUtils)1 DefaultObjectMapper.defaultObjectMapper (com.baremaps.server.common.DefaultObjectMapper.defaultObjectMapper)1 Conversions.asPostgresQuery (com.baremaps.server.ogcapi.Conversions.asPostgresQuery)1 StreamUtils (com.baremaps.stream.StreamUtils)1 TileBatchPredicate (com.baremaps.tile.TileBatchPredicate)1