83 lines
2.7 KiB
Java
Raw Normal View History

2022-04-07 18:43:21 +02:00
package ch.epfl.alpano.dem;
import static java.lang.Math.max;
import static java.lang.Math.min;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import ch.epfl.alpano.GeoPoint;
import static java.lang.Math.toRadians;
final class DrawHgtDEM {
final static File HGT_FILE = new File("HGT"+File.separatorChar+"N46E006.hgt");
final static double ORIGIN_LON = toRadians(6.25);
final static double ORIGIN_LAT = toRadians(46.25);
final static double WIDTH = toRadians(0.5);
final static int IMAGE_SIZE = 300;
final static double MIN_ELEVATION = 200;
final static double MAX_ELEVATION = 1_500;
public static void main(String[] as) throws Exception {
DiscreteElevationModel dDEM =
new HgtDiscreteElevationModel(HGT_FILE);
ContinuousElevationModel cDEM =
new ContinuousElevationModel(dDEM);
double step = WIDTH / (IMAGE_SIZE - 1);
BufferedImage i = new BufferedImage(IMAGE_SIZE,
IMAGE_SIZE,
BufferedImage.TYPE_INT_RGB);
for (int x = 0; x < IMAGE_SIZE; ++x) {
double lon = ORIGIN_LON + x * step;
for (int y = 0; y < IMAGE_SIZE; ++y) {
double lat = ORIGIN_LAT + y * step;
GeoPoint p = new GeoPoint(lon, lat);
double el =
(cDEM.elevationAt(p) - MIN_ELEVATION)
/ (MAX_ELEVATION - MIN_ELEVATION);
i.setRGB(x, IMAGE_SIZE - 1 - y, gray(el));
}
}
dDEM.close();
ImageIO.write(i, "png", new File("tests/ch/epfl/alpano/dem/dem.png"));
}
public static void drawDem(ContinuousElevationModel cDem){
double step = WIDTH / (IMAGE_SIZE - 1);
BufferedImage i = new BufferedImage(IMAGE_SIZE,
IMAGE_SIZE,
BufferedImage.TYPE_INT_RGB);
for (int x = 0; x < IMAGE_SIZE; ++x) {
double lon = ORIGIN_LON + x * step;
for (int y = 0; y < IMAGE_SIZE; ++y) {
double lat = ORIGIN_LAT + y * step;
GeoPoint p = new GeoPoint(lon, lat);
double el =
(cDem.elevationAt(p) - MIN_ELEVATION)
/ (MAX_ELEVATION - MIN_ELEVATION);
i.setRGB(x, IMAGE_SIZE - 1 - y, gray(el));
}
}
try {
ImageIO.write(i, "png", new File("tests/ch/epfl/alpano/dem/dem.png"));
} catch (IOException e) {
e.printStackTrace();
}
}
private static int gray(double v) {
double clampedV = max(0, min(v, 1));
int gray = (int) (255.9999 * clampedV);
return (gray << 16) | (gray << 8) | gray;
}
}