-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDepthCalculator.java
More file actions
278 lines (231 loc) · 8.66 KB
/
DepthCalculator.java
File metadata and controls
278 lines (231 loc) · 8.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
import com.onthegomap.planetiler.pmtiles.ReadablePmtiles;
import com.onthegomap.planetiler.archive.ReadableTileArchive;
import com.onthegomap.planetiler.geo.TileCoord;
import com.onthegomap.planetiler.geo.GeoUtils;
import org.locationtech.jts.geom.Coordinate;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.file.Path;
import java.util.LinkedHashMap;
import java.util.Map;
/**
* DepthCalculator - Calculates missing depth values for wrecks and rocks from Terrarium DEM tiles
*
* Terrarium Encoding:
* elevation = (red * 256 + green + blue / 256) - 32768
* depth = -elevation (positive values for underwater)
*/
public class DepthCalculator {
private final ReadableTileArchive pmtiles;
private final int maxZoom;
private final int tileSize;
private final Map<String, BufferedImage> cache;
private final int cacheSize;
/**
* Constructor with PMTiles path
*
* @param fname Path to bathymetry PMTiles file
*/
public DepthCalculator(Path path, int tileSize) throws IOException {
ReadablePmtiles pm = (ReadablePmtiles) ReadablePmtiles.newReadFromFile(path);
this.pmtiles = pm;
this.maxZoom = pm.getHeader().maxZoom();
this.tileSize = tileSize;
this.cacheSize = 100;
this.cache = new LinkedHashMap<>(cacheSize, 0.75f, true) {
@Override
protected boolean removeEldestEntry(Map.Entry<String, BufferedImage> eldest) {
return size() > DepthCalculator.this.cacheSize;
}
};
}
public DepthCalculator(Path path) throws IOException {
this(path, 512);
}
/**
* Calculates tile coordinates from geographic coordinates
*/
private TileCoord getTileCoord(double lon, double lat, int zoom) {
int n = 1 << zoom;
int x = (int) Math.floor((lon + 180.0) / 360.0 * n);
double latRad = Math.toRadians(lat);
int y = (int) Math.floor((1.0 - Math.log(Math.tan(latRad) + 1.0 / Math.cos(latRad)) / Math.PI) / 2.0 * n);
// System.err.println("Berechne Tile für lon=" + lon + " lat=" + lat + " zoom=" + zoom + " => " + zoom + "/" + x + "/" + y);
return TileCoord.ofXYZ(x, y, zoom);
}
/**
* Calculates pixel position within a tile
*/
private int[] getPixelInTile(double lon, double lat, int zoom) {
int n = 1 << zoom;
double x = (lon + 180.0) / 360.0 * n;
double latRad = Math.toRadians(lat);
double y = (1.0 - Math.log(Math.tan(latRad) + 1.0 / Math.cos(latRad)) / Math.PI) / 2.0 * n;
int tileX = (int) Math.floor(x);
int tileY = (int) Math.floor(y);
int pixelX = (int) Math.floor((x - tileX) * tileSize);
int pixelY = (int) Math.floor((y - tileY) * tileSize);
return new int[]{pixelX, pixelY};
}
/**
* Generates cache key for a tile
*/
private String getTileKey(TileCoord coord) {
return coord.z() + "/" + coord.x() + "/" + coord.y();
}
/**
* Loads a DEM tile
*/
private BufferedImage loadTile(TileCoord coord) throws IOException {
String key = getTileKey(coord);
// Check cache
if (cache.containsKey(key)) {
return cache.get(key);
}
byte[] tileData = pmtiles.getTile(coord);
if (tileData == null || tileData.length == 0) {
System.err.println("No depth tile found for " + key + " (coord=" + coord + ")");
return null;
}
BufferedImage image = null;
try {
image = ImageIO.read(new ByteArrayInputStream(tileData));
if (image == null) {
System.err.println("Error decoding tile " + key);
System.err.println("Make sure imageio-webp.jar and dependencies are in classpath.");
return null;
}
} catch (IOException e) {
System.err.println("IOException decoding tile " + key + ": " + e.getMessage());
return null;
}
// Update cache
cache.put(key, image);
return image;
}
/**
* Samples a single pixel and loads neighboring tiles if needed
*
* @param baseTileX X coordinate of base tile
* @param baseTileY Y coordinate of base tile
* @param pixelX Pixel X position (can be > tileSize)
* @param pixelY Pixel Y position (can be > tileSize)
* @return Elevation value of the pixel
*/
private double samplePixel(int baseTileX, int baseTileY, int pixelX, int pixelY) throws IOException {
int actualTileX = baseTileX;
int actualTileY = baseTileY;
int actualPixelX = pixelX;
int actualPixelY = pixelY;
// Check if pixel is outside base tile
if (pixelX >= tileSize) {
actualTileX++;
actualPixelX = pixelX - tileSize;
}
if (pixelY >= tileSize) {
actualTileY++;
actualPixelY = pixelY - tileSize;
}
// Load the appropriate tile
TileCoord coord = TileCoord.ofXYZ(actualTileX, actualTileY, maxZoom);
BufferedImage img = loadTile(coord);
if (img == null) {
// Fallback: use value 0 (sea level)
return 0.0;
}
// Ensure pixel is within bounds
actualPixelX = Math.max(0, Math.min(actualPixelX, tileSize - 1));
actualPixelY = Math.max(0, Math.min(actualPixelY, tileSize - 1));
return decodeTerrarium(img.getRGB(actualPixelX, actualPixelY));
}
/**
* Decodes Terrarium elevation value from RGB
*
* @param r Red channel (0-255)
* @param g Green channel (0-255)
* @param b Blue channel (0-255)
* @return Elevation in meters (negative = underwater)
*/
private double decodeTerrarium(int r, int g, int b) {
return (r * 256.0 + g + b / 256.0) - 32768.0;
}
/**
* Decodes Terrarium elevation value from RGB int value
*
* @param rgb RGB value as int
* @return Elevation in meters (negative = underwater)
*/
private double decodeTerrarium(int rgb) {
int r = (rgb >> 16) & 0xFF;
int g = (rgb >> 8) & 0xFF;
int b = rgb & 0xFF;
return decodeTerrarium(r, g, b);
}
/**
* Calculates depth at a geographic position
*
* @param lon Longitude
* @param lat Latitude
* @return Depth in meters (null if not available)
*/
public Double getDepthAtLocation(double lon, double lat) {
try {
// Use maxZoom for best resolution
TileCoord tileCoord = getTileCoord(lon, lat, maxZoom);
int[] pixel = getPixelInTile(lon, lat, maxZoom);
// Load tile
BufferedImage image = loadTile(tileCoord);
if (image == null) {
return null;
}
// Bilinear interpolation between 4 surrounding pixels
int n = 1 << maxZoom;
double x = (lon + 180.0) / 360.0 * n;
double y = (1.0 - Math.log(Math.tan(Math.toRadians(lat)) + 1.0 / Math.cos(Math.toRadians(lat))) / Math.PI) / 2.0 * n;
int tileX = (int) Math.floor(x);
int tileY = (int) Math.floor(y);
double pixelXf = (x - tileX) * tileSize;
double pixelYf = (y - tileY) * tileSize;
int x0 = (int) Math.floor(pixelXf);
int y0 = (int) Math.floor(pixelYf);
double fx = pixelXf - x0;
double fy = pixelYf - y0;
// Sample 4 pixels - handle tile boundaries
double e00 = samplePixel(tileX, tileY, x0, y0);
double e10 = samplePixel(tileX, tileY, x0 + 1, y0);
double e01 = samplePixel(tileX, tileY, x0, y0 + 1);
double e11 = samplePixel(tileX, tileY, x0 + 1, y0 + 1);
// Bilinear interpolation
double e0 = e00 * (1 - fx) + e10 * fx;
double e1 = e01 * (1 - fx) + e11 * fx;
double elevation = e0 * (1 - fy) + e1 * fy;
// Depth is negated elevation (positive values for underwater)
double depth = -elevation;
// Round to one decimal place
depth = Math.round(depth * 10.0) / 10.0;
// System.err.println("Koordinate: lon=" + lon + ", lat=" + lat +
// " => tile(" + tileX + "," + tileY + ") pixel(" + x0 + "," + y0 + ")" +
// " => samples[(" + x0 + "," + y0 + ")=" + String.format("%.1f", e00) +
// ", (" + (x0+1) + "," + y0 + ")=" + String.format("%.1f", e10) +
// ", (" + x0 + "," + (y0+1) + ")=" + String.format("%.1f", e01) +
// ", (" + (x0+1) + "," + (y0+1) + ")=" + String.format("%.1f", e11) +
// "] => elevation=" + String.format("%.1f", elevation) + " => depth=" + depth);
return depth > 0 ? depth : 0.0;
} catch (IOException e) {
System.err.println("Error calculating depth for position " + lon + ", " + lat + ": " + e.getMessage());
return null;
}
}
/**
* Calculates depth for a JTS coordinate (World Mercator 0-1)
* Converts from World Mercator to Lon/Lat before calculation
*/
public Double getDepthAtLocation(Coordinate coord) {
// Convert from World Mercator (0-1) to Lon/Lat degrees
double lon = GeoUtils.getWorldLon(coord.x);
double lat = GeoUtils.getWorldLat(coord.y);
return getDepthAtLocation(lon, lat);
}
}