Skip to content

Instantly share code, notes, and snippets.

@jherico
Created April 12, 2014 21:47
Show Gist options
  • Save jherico/10558530 to your computer and use it in GitHub Desktop.
Save jherico/10558530 to your computer and use it in GitHub Desktop.
import gov.nasa.worldwind.Model;
import gov.nasa.worldwind.WorldWind;
import gov.nasa.worldwind.WorldWindow;
import gov.nasa.worldwind.avlist.AVKey;
import gov.nasa.worldwind.awt.WorldWindowGLCanvas;
import gov.nasa.worldwind.geom.BilinearInterpolator;
import gov.nasa.worldwind.geom.LatLon;
import gov.nasa.worldwind.geom.Sector;
import gov.nasa.worldwind.geom.Vec4;
import gov.nasa.worldwind.globes.Globe;
import gov.nasa.worldwind.layers.Layer;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class WorldWindHeadless {
private static final Logger LOG = LoggerFactory.getLogger(WorldWindHeadless.class);
private static final LatLon HOME = LatLon.fromDegrees(47.5391123,-122.2775141);
public static BilinearInterpolator getInterpolator(Sector s) {
LatLon centroid = s.getCentroid();
double xmin = centroid.getLongitude().degrees - s.getDeltaLonDegrees();
double xmax = centroid.getLongitude().degrees + s.getDeltaLonDegrees();
double ymin = centroid.getLatitude().degrees - s.getDeltaLatDegrees();
double ymax = centroid.getLatitude().degrees + s.getDeltaLatDegrees();
BilinearInterpolator bi = new BilinearInterpolator(
new Vec4(xmin, ymin, 0, 0),
new Vec4(xmax, ymin, 0, 0),
new Vec4(xmax, ymax, 0, 0),
new Vec4(xmin, ymax, 0, 0));
return bi;
}
public static void main(String [] args) throws InterruptedException, IOException {
WorldWindow wwd = new WorldWindowGLCanvas();
Model m = (Model) WorldWind.createConfigurationComponent(AVKey.MODEL_CLASS_NAME);
wwd.setModel(m);
Globe g = m.getGlobe();
Sector s = Sector.boundingSector(g, HOME, 1000 * 2);
BilinearInterpolator bi = getInterpolator(s);
List<LatLon> lv = new ArrayList<>();
int res = 50;
for (int y = 0; y < res; ++y) {
for (int x = 0; x < res; ++x) {
Vec4 v = bi.interpolateAsPoint((float)x / (float)res, (float)y / (float)res);
lv.add(LatLon.fromDegrees(v.y, v.x));
}
}
double elevations[] = new double[lv.size()];
double resolution = s.getDeltaLat().radians / 50.0;
g.getElevations(s, lv, resolution, elevations);
Thread.sleep(5000);
LOG.warn(g.getDiameter() + "");
g.getElevations(s, lv, resolution, elevations);
double max = 0;
for(double e : elevations) {
max = Math.max(e, max);
}
BufferedImage off_Image = new BufferedImage(50, 50, BufferedImage.TYPE_INT_ARGB);
final int ALPHA = 0xff << 24;
for (int y = 0; y < res; ++y) {
for (int x = 0; x < res; ++x) {
int offset = ((res - 1) - y) * res + x;
double elevation = elevations[offset];
if (0 == elevation) {
off_Image.setRGB(x, y, 0xff | ALPHA);
continue;
}
int redi = (int)(0xff * elevation / max) << 16;
off_Image.setRGB(x, y, redi | ALPHA);
}
}
ImageIO.write(off_Image, "png", new File("test.png"));
for (Layer l : m.getLayers()) {
LOG.warn(l.getClass().getName());
LOG.warn(l.getName());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment