|
| 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.io.ByteArrayOutputStream; |
| 22 | +import java.io.IOException; |
| 23 | +import java.nio.ByteBuffer; |
| 24 | +import java.util.ArrayList; |
| 25 | +import java.util.List; |
| 26 | +import java.util.Map; |
| 27 | +import java.util.function.IntToDoubleFunction; |
| 28 | +import java.util.zip.GZIPOutputStream; |
| 29 | +import org.apache.baremaps.maplibre.vectortile.Feature; |
| 30 | +import org.apache.baremaps.maplibre.vectortile.Layer; |
| 31 | +import org.apache.baremaps.maplibre.vectortile.Tile; |
| 32 | +import org.apache.baremaps.maplibre.vectortile.VectorTileEncoder; |
| 33 | +import org.apache.baremaps.raster.ContourTracer; |
| 34 | +import org.apache.baremaps.raster.ElevationUtils; |
| 35 | +import org.apache.baremaps.raster.HillshadeCalculator; |
| 36 | +import org.apache.baremaps.tilestore.TileCoord; |
| 37 | +import org.apache.baremaps.tilestore.TileStore; |
| 38 | +import org.apache.baremaps.tilestore.TileStoreException; |
| 39 | +import org.locationtech.jts.geom.util.AffineTransformation; |
| 40 | +import org.locationtech.jts.simplify.TopologyPreservingSimplifier; |
| 41 | + |
| 42 | +public class VectorHillshadeTileStore implements TileStore<ByteBuffer> { |
| 43 | + |
| 44 | + private final TileStore<BufferedImage> tileStore; |
| 45 | + |
| 46 | + private final IntToDoubleFunction pixelToElevation; |
| 47 | + |
| 48 | + public VectorHillshadeTileStore(TileStore<BufferedImage> tileStore, |
| 49 | + IntToDoubleFunction pixelToElevation) { |
| 50 | + this.tileStore = tileStore; |
| 51 | + this.pixelToElevation = pixelToElevation; |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public ByteBuffer read(TileCoord tileCoord) throws TileStoreException { |
| 56 | + try { |
| 57 | + |
| 58 | + var size = 256; |
| 59 | + |
| 60 | + // Read the elevation data |
| 61 | + var image = BufferedImageTileStore.onion(tileStore, tileCoord, 1).getSubimage( |
| 62 | + size - 16, |
| 63 | + size - 16, |
| 64 | + size + 32, |
| 65 | + size + 32); |
| 66 | + |
| 67 | + var features = new ArrayList<Feature>(); |
| 68 | + |
| 69 | + // Calculate the hillshade |
| 70 | + var grid = new HillshadeCalculator( |
| 71 | + ElevationUtils.clampGrid(ElevationUtils.imageToGrid(image, pixelToElevation), 0, 10000), |
| 72 | + size + 32, size + 32, HillshadeCalculator.getResolution(tileCoord.z())) |
| 73 | + .calculate(45, 315); |
| 74 | + |
| 75 | + contours(grid, 255 - 16, features, "1"); |
| 76 | + contours(grid, 255 - 32, features, "2"); |
| 77 | + |
| 78 | + grid = ElevationUtils.invertGrid(grid); |
| 79 | + contours(grid, 255 - 32, features, "6"); |
| 80 | + contours(grid, 255 - 64, features, "5"); |
| 81 | + contours(grid, 255 - 98, features, "4"); |
| 82 | + contours(grid, 255 - 128, features, "3"); |
| 83 | + |
| 84 | + |
| 85 | + |
| 86 | + var layer = new Layer("elevation", 4096, features); |
| 87 | + var tile = new Tile(List.of(layer)); |
| 88 | + var vectorTile = new VectorTileEncoder().encodeTile(tile); |
| 89 | + try (var baos = new ByteArrayOutputStream()) { |
| 90 | + var gzip = new GZIPOutputStream(baos); |
| 91 | + vectorTile.writeTo(gzip); |
| 92 | + gzip.close(); |
| 93 | + return ByteBuffer.wrap(baos.toByteArray()); |
| 94 | + } |
| 95 | + } catch (IOException e) { |
| 96 | + throw new TileStoreException(e); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + private static void contours(double[] grid, int level, ArrayList<Feature> features, String id) { |
| 101 | + var contours = |
| 102 | + new ContourTracer(grid, (int) Math.sqrt(grid.length), (int) Math.sqrt(grid.length), false, |
| 103 | + true) |
| 104 | + .traceContours(level); |
| 105 | + for (var contour : contours) { |
| 106 | + contour = AffineTransformation |
| 107 | + .translationInstance(-16, -16) |
| 108 | + .scale(16, 16) |
| 109 | + .transform(contour); |
| 110 | + |
| 111 | + contour = TopologyPreservingSimplifier.simplify(contour, 4); |
| 112 | + features.add(new Feature(4, Map.of("level", id), contour)); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + @Override |
| 117 | + public void write(TileCoord tileCoord, ByteBuffer blob) throws TileStoreException { |
| 118 | + throw new UnsupportedOperationException(); |
| 119 | + } |
| 120 | + |
| 121 | + @Override |
| 122 | + public void delete(TileCoord tileCoord) throws TileStoreException { |
| 123 | + throw new UnsupportedOperationException(); |
| 124 | + } |
| 125 | + |
| 126 | + @Override |
| 127 | + public void close() throws Exception { |
| 128 | + // Do nothing |
| 129 | + } |
| 130 | +} |
0 commit comments