glidertestapp.java

来自「world wind java sdk 源码」· Java 代码 · 共 233 行

JAVA
233
字号
/*Copyright (C) 2001, 2008 United States Governmentas represented by the Administrator of theNational Aeronautics and Space Administration.All Rights Reserved.*/package gov.nasa.worldwind.applications.glider;import gov.nasa.worldwind.awt.WorldWindowGLCanvas;import gov.nasa.worldwind.examples.ApplicationTemplate;import gov.nasa.worldwind.geom.*;import gov.nasa.worldwind.util.*;import javax.imageio.ImageIO;import javax.swing.*;import javax.swing.Timer;import java.awt.*;import java.awt.event.*;import java.awt.image.*;import java.beans.*;import java.io.*;import java.nio.*;import java.util.*;import java.util.List;/** * @author tag * @version $Id: GliderTestApp.java 9226 2009-03-06 03:42:35Z tgaskins $ */public class GliderTestApp extends ApplicationTemplate{    public static class GliderAppPanel extends AppPanel    {        public GliderAppPanel(Dimension canvasSize, boolean includeStatusBar)        {            super(canvasSize, includeStatusBar);        }        @Override        protected WorldWindowGLCanvas createWorldWindow()        {            return new GliderWorldWindow();        }    }    public static class GliderAppFrame extends AppFrame    {        @Override        protected AppPanel createAppPanel(Dimension canvasSize, boolean includeStatusBar)        {            return new GliderAppPanel(canvasSize, includeStatusBar);        }    }//    private static Sector cloudSector = Sector//        .fromDegrees(10.879667319999989, 48.55774732, -134.459224670811, -96.781144670811);    private static LatLon nw = LatLon.fromDegrees(48.55774732, -134.459224670811);    private static LatLon ne = nw.add(LatLon.fromDegrees(0, 0.036795 * 950));    private static LatLon se = nw.add(LatLon.fromDegrees(-0.036795 * 900, 0.036795 * 950));    private static LatLon sw = nw.add(LatLon.fromDegrees(-0.036795 * 900, 0));    private static List<LatLon> corners = Arrays.asList(sw, se, ne, nw);    //    private static String cloudImagePath = "/Users/tag/No Backup/euromize/1km_6165_708.tif";    private static String cloudImagePath = "/Users/tag/No Backup/testimage.jpg";//    private static String cloudImagePath = "/Users/tag/No Backup/0812181945/0812181945.png";//    private static String cloudImagePath = "/Users/tag/No Backup/0. Test Files/20080704-123736-JDBJYM/20080704-123736-JDBJYM.13.TerrainTexture.png";//    "src/images/earth-map-512x256.png";    private static String currentPath = null;    private static float[][] readLocations()    {        try        {            MappedByteBuffer bufferLon = WWIO.mapFile(new File("/Users/tag/No Backup/lon.txt"));            MappedByteBuffer bufferLat = WWIO.mapFile(new File("/Users/tag/No Backup/lat.txt"));            bufferLon.order(ByteOrder.BIG_ENDIAN);            bufferLat.order(ByteOrder.BIG_ENDIAN);            FloatBuffer latBuffer = bufferLat.asFloatBuffer();            FloatBuffer lonBuffer = bufferLon.asFloatBuffer();            float[] lats = new float[latBuffer.limit()];            float[] lons = new float[lonBuffer.limit()];            for (int i = 0; i < lats.length; i++)            {                lats[i] = latBuffer.get(i);                lons[i] = lonBuffer.get(i);            }            return new float[][] {lats, lons};        }        catch (IOException e)        {            e.printStackTrace();            return null;        }    }    private static float[][] makeField(List<LatLon> corners, int width, int height, Angle angle)    {        Sector sector = Sector.boundingSector(corners);        double dLat = sector.getDeltaLatDegrees() / (height - 1d);        double dLon = sector.getDeltaLonDegrees() / (width - 1d);        float[] lons = new float[width * height];        float[] lats = new float[lons.length];        for (int j = 0; j < height; j++)        {            for (int i = 0; i < width; i++)            {                lons[j * width + i] = (float) (sector.getMinLongitude().degrees + i * dLon);                lats[j * width + i] = (float) (sector.getMaxLatitude().degrees - j * dLat);            }        }        double cosAngle = angle.cos();        double sinAngle = angle.sin();        LatLon c = sector.getCentroid();        float cx = (float) c.getLongitude().degrees;        float cy = (float) c.getLatitude().degrees;        for (int j = 0; j < height; j++)        {            for (int i = 0; i < width; i++)            {                int index = j * width + i;                                float x = lons[index];                float y = lats[index];                lons[index] = (float) ((x - cx) * cosAngle - (y - cy) * sinAngle + cx);                lats[index] = (float) ((x - cx) * sinAngle + (y - cy) * cosAngle + cy);            }        }        return new float[][] {lats, lons};    }    private static ImageUtil.AlignedImage projectImage(BufferedImage image, List<LatLon> corners)    {        float[][] field = readLocations();//makeField(corners, image.getWidth(), image.getHeight(), Angle.fromDegrees(0));        return GliderImage.alignImage(image, field[0], field[1]);    }    public static void main(String[] args)    {        final ImageUtil.AlignedImage projectedImage;        final String imageName;        final AppFrame frame = start("GLIDER Test Application", GliderAppFrame.class);        try        {            File imageFile = new File(cloudImagePath);            long start = System.currentTimeMillis();            projectedImage = projectImage(ImageIO.read(imageFile), corners);            System.out.printf("Image projected, %d ms\n", System.currentTimeMillis() - start);            imageName = imageFile.getName();        }        catch (IOException e)        {            e.printStackTrace();            return;        }        frame.getWwd().addPropertyChangeListener(GliderImage.GLIDER_IMAGE_SOURCE, new PropertyChangeListener()        {            public void propertyChange(PropertyChangeEvent evt)            {                frame.getLayerPanel().update(frame.getWwd());            }        });        SwingUtilities.invokeLater(new Runnable()        {            public void run()            {//                final GliderImage image = new GliderImage(cloudImagePath, corners, 100);                final GliderImage image = new GliderImage(imageName, projectedImage, 100);//                    new ImageUtil.AlignedImage(projectedImage.image, Sector.boundingSector(corners)), 100);                image.setOpacity(0.7);                final Timer timer = new Timer(100, new ActionListener()                {                    @SuppressWarnings({"StringEquality"})                    public void actionPerformed(ActionEvent evt)                    {                        if (currentPath == null)                        {                            currentPath = cloudImagePath;                            try                            {                                ((GliderWorldWindow) ((GliderAppFrame) frame).getWwd()).addImage(image);//                                Sector[] sectors = image.getSector().subdivide();//                                image.addRegionOfInterest(new GliderRegionOfInterest(image.getCorners(), Color.RED));                            }                            catch (IOException e)                            {                                e.printStackTrace();                            }                            ((Timer) evt.getSource()).stop();                        }                        else                        {                            GliderRegionOfInterest.RegionSet rs = image.getRegionsOfInterest();                            if (rs == null || rs.regions.size() == 0)                                return;                            GliderRegionOfInterest r = (GliderRegionOfInterest) rs.regions.toArray()[0];                            ArrayList<LatLon> newLocations = new ArrayList<LatLon>();                            LatLon delta = LatLon.fromDegrees(0.1, 0.1);                            for (LatLon ll : r.getLocations())                            {                                newLocations.add(ll.add(delta));                            }                            r.setLocations(newLocations);                        }                    }                });                timer.start();            }        });    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?