Skip to content

Commit ead787e

Browse files
committed
Improve hillshade and add gdal api
1 parent 2ee536a commit ead787e

File tree

38 files changed

+1869
-389
lines changed

38 files changed

+1869
-389
lines changed

baremaps-cli/src/main/java/org/apache/baremaps/cli/hillshade/Serve.java

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,10 @@
2828
import com.linecorp.armeria.server.file.HttpFile;
2929
import java.util.concurrent.Callable;
3030
import org.apache.baremaps.raster.ElevationUtils;
31-
import org.apache.baremaps.server.RasterTileResource;
31+
import org.apache.baremaps.server.BufferedImageResource;
3232
import org.apache.baremaps.server.VectorTileResource;
33-
import org.apache.baremaps.tilestore.BufferedImageTileCache;
34-
import org.apache.baremaps.tilestore.raster.BufferedImageTileStore;
35-
import org.apache.baremaps.tilestore.raster.ContourTileStore;
36-
import org.apache.baremaps.tilestore.raster.RasterHillshadeTileStore;
37-
import org.apache.baremaps.tilestore.raster.VectorHillshadeTileStore;
33+
import org.apache.baremaps.tilestore.raster.*;
34+
import org.apache.baremaps.tilestore.raster.RasterTileCache;
3835
import picocli.CommandLine.Command;
3936
import picocli.CommandLine.Option;
4037

@@ -56,22 +53,18 @@ public Integer call() throws Exception {
5653
var objectMapper = objectMapper();
5754
var jsonResponseConverter = new JacksonResponseConverterFunction(objectMapper);
5855

59-
var bufferedImageTileStore = new BufferedImageTileStore(
60-
"https://s3.amazonaws.com/elevation-tiles-prod/terrarium/{z}/{x}/{y}.png");
61-
var bufferedImageTileCache =
62-
new BufferedImageTileCache(bufferedImageTileStore, CaffeineSpec.parse("maximumSize=1000"));
63-
var hillshadeTileStore =
64-
new RasterHillshadeTileStore(bufferedImageTileCache,
65-
ElevationUtils::pixelToElevationTerrarium);
66-
67-
var contourTileStore = new ContourTileStore(bufferedImageTileCache);
68-
69-
var vectorHillshadeTileStore = new VectorHillshadeTileStore(bufferedImageTileCache,
56+
var elevationTileStore =
57+
new RasterTileCache(new RasterElevationTileStore(), CaffeineSpec.parse("maximumSize=1000"));
58+
var rasterHillshadeTileStore =
59+
new RasterHillshadeTileStore(elevationTileStore, ElevationUtils::pixelToElevationTerrarium);
60+
var vectorHillshadeTileStore = new VectorHillshadeTileStore(elevationTileStore,
7061
ElevationUtils::pixelToElevationTerrarium);
62+
var contourTileStore = new VectorContourTileStore(elevationTileStore);
63+
7164

7265
serverBuilder.annotatedService(
7366
"/raster",
74-
new RasterTileResource(() -> hillshadeTileStore),
67+
new BufferedImageResource(() -> rasterHillshadeTileStore),
7568
jsonResponseConverter);
7669

7770
serverBuilder.annotatedService(

baremaps-cli/src/main/java/org/apache/baremaps/cli/map/MBTiles.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@
3737
import org.apache.baremaps.maplibre.style.Style;
3838
import org.apache.baremaps.maplibre.tilejson.TileJSON;
3939
import org.apache.baremaps.server.*;
40-
import org.apache.baremaps.tilestore.ByteBufferTileCache;
4140
import org.apache.baremaps.tilestore.TileStore;
4241
import org.apache.baremaps.tilestore.mbtiles.MBTilesStore;
42+
import org.apache.baremaps.tilestore.vector.VectorTileCache;
4343
import org.apache.baremaps.utils.SqliteUtils;
4444
import org.slf4j.Logger;
4545
import org.slf4j.LoggerFactory;
@@ -84,7 +84,7 @@ public Integer call() throws Exception {
8484

8585
var datasource = SqliteUtils.createDataSource(mbtilesPath, true);
8686
try (var tileStore = new MBTilesStore(datasource);
87-
var tileCache = new ByteBufferTileCache(tileStore, caffeineSpec)) {
87+
var tileCache = new VectorTileCache(tileStore, caffeineSpec)) {
8888
var tileStoreSupplier = (Supplier<TileStore<ByteBuffer>>) () -> tileCache;
8989

9090
var style = objectMapper.readValue(configReader.read(stylePath), Style.class);

baremaps-cli/src/main/java/org/apache/baremaps/cli/map/Serve.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@
4141
import org.apache.baremaps.server.StyleResource;
4242
import org.apache.baremaps.server.TileJSONResource;
4343
import org.apache.baremaps.server.VectorTileResource;
44-
import org.apache.baremaps.tilestore.ByteBufferTileCache;
4544
import org.apache.baremaps.tilestore.TileStore;
4645
import org.apache.baremaps.tilestore.postgres.PostgresTileStore;
46+
import org.apache.baremaps.tilestore.vector.VectorTileCache;
4747
import org.apache.baremaps.utils.PostgresUtils;
4848
import org.slf4j.Logger;
4949
import org.slf4j.LoggerFactory;
@@ -96,7 +96,7 @@ public Integer call() throws Exception {
9696

9797
try (
9898
var tileStore = new PostgresTileStore(datasource, tileset);
99-
var tileCache = new ByteBufferTileCache(tileStore, caffeineSpec)) {
99+
var tileCache = new VectorTileCache(tileStore, caffeineSpec)) {
100100

101101
var tileStoreSupplier = (Supplier<TileStore<ByteBuffer>>) () -> tileCache;
102102

baremaps-core/src/main/java/org/apache/baremaps/tilestore/postgres/PostgresQueryGenerator.java

Lines changed: 0 additions & 112 deletions
This file was deleted.
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to you under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.baremaps.tilestore.raster;
19+
20+
import java.awt.image.BufferedImage;
21+
import java.nio.file.Path;
22+
import java.nio.file.Paths;
23+
import java.util.Vector;
24+
import org.apache.baremaps.openstreetmap.utils.GeometryUtils;
25+
import org.apache.baremaps.raster.ElevationUtils;
26+
import org.apache.baremaps.tilestore.TileCoord;
27+
import org.apache.baremaps.tilestore.TileStore;
28+
import org.apache.baremaps.tilestore.TileStoreException;
29+
import org.gdal.gdal.Band;
30+
import org.gdal.gdal.Dataset;
31+
import org.gdal.gdal.WarpOptions;
32+
import org.gdal.gdal.gdal;
33+
import org.locationtech.jts.geom.Coordinate;
34+
import org.locationtech.jts.geom.GeometryFactory;
35+
import org.locationtech.proj4j.ProjCoordinate;
36+
37+
public class RasterElevationTileStore implements TileStore<BufferedImage> {
38+
39+
private final Path path;
40+
41+
public RasterElevationTileStore() {
42+
gdal.AllRegister();
43+
path = Paths.get("/data/gebco_2024_web_mercator.tif");
44+
}
45+
46+
@Override
47+
public BufferedImage read(TileCoord tileCoord) throws TileStoreException {
48+
Dataset sourceDataset = null;
49+
Dataset targetDataset = null;
50+
Band targetBand = null;
51+
52+
try {
53+
sourceDataset = gdal.Open(path.toString());
54+
55+
var envelope = tileCoord.envelope();
56+
var transformer = GeometryUtils.coordinateTransform(4326, 3857);
57+
var rasterMin = transformer.transform(
58+
new ProjCoordinate(envelope.getMinX(), envelope.getMinY()),
59+
new ProjCoordinate());
60+
var rasterMax = transformer.transform(
61+
new ProjCoordinate(envelope.getMaxX(), envelope.getMaxY()),
62+
new ProjCoordinate());
63+
var rasterEnvelope = new GeometryFactory().createPolygon(new Coordinate[] {
64+
new Coordinate(rasterMin.x, rasterMin.y),
65+
new Coordinate(rasterMin.x, rasterMax.y),
66+
new Coordinate(rasterMax.x, rasterMax.y),
67+
new Coordinate(rasterMax.x, rasterMin.y),
68+
new Coordinate(rasterMin.x, rasterMin.y)
69+
}).getEnvelopeInternal();
70+
71+
Vector<String> rasterOptions = new Vector<>();
72+
rasterOptions.add("-of");
73+
rasterOptions.add("MEM");
74+
75+
rasterOptions.add("-te");
76+
rasterOptions.add(String.valueOf(rasterEnvelope.getMinX()));
77+
rasterOptions.add(String.valueOf(rasterEnvelope.getMinY()));
78+
rasterOptions.add(String.valueOf(rasterEnvelope.getMaxX()));
79+
rasterOptions.add(String.valueOf(rasterEnvelope.getMaxY()));
80+
81+
rasterOptions.add("-ts");
82+
rasterOptions.add(String.valueOf(256));
83+
rasterOptions.add(String.valueOf(256));
84+
85+
rasterOptions.add("-r");
86+
rasterOptions.add("cubicspline");
87+
88+
rasterOptions.add("-et");
89+
rasterOptions.add("10");
90+
91+
targetDataset = gdal.Warp("", new Dataset[] {sourceDataset}, new WarpOptions(rasterOptions));
92+
targetBand = targetDataset.GetRasterBand(1);
93+
94+
// Copy the data of the band into a byte array
95+
double[] values = new double[256 * 256];
96+
targetBand.ReadRaster(0, 0, 256, 256, values);
97+
98+
// Create a BufferedImage from the byte array
99+
BufferedImage image = new BufferedImage(256, 256, BufferedImage.TYPE_INT_RGB);
100+
for (int x = 0; x < 256; x++) {
101+
for (int y = 0; y < 256; y++) {
102+
double value = values[y * 256 + x];
103+
int pixel = ElevationUtils.elevationToPixelTerrarium(value);
104+
image.setRGB(x, y, pixel);
105+
}
106+
}
107+
108+
return image;
109+
110+
} catch (Exception e) {
111+
throw new TileStoreException(e);
112+
113+
} finally {
114+
if (sourceDataset != null) {
115+
sourceDataset.delete();
116+
}
117+
if (targetDataset != null) {
118+
targetDataset.delete();
119+
}
120+
if (targetBand != null) {
121+
targetBand.delete();
122+
}
123+
}
124+
}
125+
126+
@Override
127+
public void write(TileCoord tileCoord, BufferedImage blob) throws TileStoreException {
128+
throw new UnsupportedOperationException();
129+
}
130+
131+
@Override
132+
public void delete(TileCoord tileCoord) throws TileStoreException {
133+
throw new UnsupportedOperationException();
134+
}
135+
136+
@Override
137+
public void close() throws Exception {
138+
throw new UnsupportedOperationException();
139+
}
140+
141+
142+
public static void main(String... args) throws TileStoreException {
143+
gdal.AllRegister();
144+
new RasterElevationTileStore().read(new TileCoord(8511, 5821, 14));
145+
}
146+
}

0 commit comments

Comments
 (0)