Skip to content

Commit 7f7199d

Browse files
committed
Refactor the code and improve javadoc
1 parent 4eb2d23 commit 7f7199d

File tree

18 files changed

+1078
-489
lines changed

18 files changed

+1078
-489
lines changed

baremaps-cli/src/main/java/org/apache/baremaps/cli/raster/HillShade.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
import java.util.function.Function;
4242
import java.util.function.Supplier;
4343
import javax.imageio.ImageIO;
44-
import org.apache.baremaps.raster.ImageUtils;
44+
import org.apache.baremaps.raster.ElevationUtils;
4545
import org.apache.baremaps.tilestore.TileCoord;
4646
import org.apache.baremaps.tilestore.TileStore;
4747
import org.apache.baremaps.tilestore.TileStoreException;
@@ -104,7 +104,8 @@ public Integer call() throws Exception {
104104
public static class HillShadeTileStore implements TileStore {
105105

106106
// private String url = "https://s3.amazonaws.com/elevation-tiles-prod/geotiff/{z}/{x}/{y}.tif";
107-
private String url = "https://demotiles.maplibre.org/terrain-tiles/{z}/{x}/{y}.png";
107+
// private String url = "https://demotiles.maplibre.org/terrain-tiles/{z}/{x}/{y}.png";
108+
private String url = "https://s3.amazonaws.com/elevation-tiles-prod/terrarium/{z}/{x}/{y}.png";
108109

109110
private final LoadingCache<TileCoord, BufferedImage> cache = Caffeine.newBuilder()
110111
.maximumSize(1000)
@@ -171,8 +172,8 @@ public ByteBuffer read(TileCoord tileCoord) throws TileStoreException {
171172
image.getWidth() + 2,
172173
image.getHeight() + 2);
173174

174-
var grid = ImageUtils.grid(buffer);
175-
var hillshade = org.apache.baremaps.raster.hillshade.HillShade.hillShade(grid,
175+
var grid = ElevationUtils.imageToGrid(buffer);
176+
var hillshade = org.apache.baremaps.raster.elevation.HillShade.hillShade(grid,
176177
buffer.getWidth(), buffer.getHeight(), 45, 315);
177178

178179
// Create an output image

baremaps-raster/pom.xml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@
1111
<dependency>
1212
<groupId>com.twelvemonkeys.imageio</groupId>
1313
<artifactId>imageio-tiff</artifactId>
14-
<version>3.11.0</version>
14+
</dependency>
15+
<dependency>
16+
<groupId>org.locationtech.jts</groupId>
17+
<artifactId>jts-core</artifactId>
1518
</dependency>
1619
</dependencies>
1720
</project>
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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.raster;
19+
20+
import java.awt.image.BufferedImage;
21+
22+
/**
23+
* Provides utility methods for processing raster images, particularly for elevation data.
24+
*/
25+
public class ElevationUtils {
26+
27+
private static final double ELEVATION_SCALE = 256.0 * 256.0;
28+
private static final double ELEVATION_OFFSET = 10000.0;
29+
30+
private ElevationUtils() {
31+
// Private constructor to prevent instantiation
32+
}
33+
34+
/**
35+
* Converts a BufferedImage to a grid of elevation values.
36+
*
37+
* @param image The input BufferedImage
38+
* @return A double array representing the elevation grid
39+
*/
40+
public static double[] imageToGrid(BufferedImage image) {
41+
validateImage(image);
42+
int width = image.getWidth();
43+
int height = image.getHeight();
44+
double[] grid = new double[width * height];
45+
46+
for (int y = 0; y < height; y++) {
47+
for (int x = 0; x < width; x++) {
48+
grid[y * width + x] = pixelToElevation(image.getRGB(x, y));
49+
}
50+
}
51+
52+
return grid;
53+
}
54+
55+
/**
56+
* Converts a grid of elevation values to a BufferedImage.
57+
*
58+
* @param grid The input elevation grid
59+
* @param width The width of the grid
60+
* @param height The height of the grid
61+
* @return A BufferedImage representing the elevation data
62+
*/
63+
public static BufferedImage gridToImage(double[] grid, int width, int height) {
64+
validateGrid(grid, width, height);
65+
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
66+
67+
for (int y = 0; y < height; y++) {
68+
for (int x = 0; x < width; x++) {
69+
image.setRGB(x, y, elevationToPixel(grid[y * width + x]));
70+
}
71+
}
72+
73+
return image;
74+
}
75+
76+
private static double pixelToElevation(int rgb) {
77+
int r = (rgb >> 16) & 0xFF;
78+
int g = (rgb >> 8) & 0xFF;
79+
int b = rgb & 0xFF;
80+
return (r * ELEVATION_SCALE + g * 256.0 + b) / 10.0 - ELEVATION_OFFSET;
81+
}
82+
83+
private static int elevationToPixel(double elevation) {
84+
int value = (int) ((elevation + ELEVATION_OFFSET) * 10.0);
85+
int r = (value >> 16) & 0xFF;
86+
int g = (value >> 8) & 0xFF;
87+
int b = value & 0xFF;
88+
return (r << 16) | (g << 8) | b;
89+
}
90+
91+
private static void validateImage(BufferedImage image) {
92+
if (image == null) {
93+
throw new IllegalArgumentException("Input image cannot be null");
94+
}
95+
if (image.getWidth() <= 0 || image.getHeight() <= 0) {
96+
throw new IllegalArgumentException("Image dimensions must be positive");
97+
}
98+
}
99+
100+
private static void validateGrid(double[] grid, int width, int height) {
101+
if (grid == null || grid.length == 0) {
102+
throw new IllegalArgumentException("Grid array cannot be null or empty");
103+
}
104+
if (width <= 0 || height <= 0) {
105+
throw new IllegalArgumentException("Width and height must be positive");
106+
}
107+
if (grid.length != width * height) {
108+
throw new IllegalArgumentException("Grid array length does not match width * height");
109+
}
110+
}
111+
112+
}

baremaps-raster/src/main/java/org/apache/baremaps/raster/ImageUtils.java

Lines changed: 0 additions & 44 deletions
This file was deleted.

baremaps-raster/src/main/java/org/apache/baremaps/raster/contour/IsoLines.java

Lines changed: 0 additions & 115 deletions
This file was deleted.

0 commit comments

Comments
 (0)