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();
}
}
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);
}
}
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);
}
}
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);
}
}
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;
}
Aggregations