|
| 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