geotifffile.java

来自「OpenMap是一个基于JavaBeansTM的开发工具包。利用OpenMap你」· Java 代码 · 共 611 行 · 第 1/2 页

JAVA
611
字号
    }    /**     * Helper function for taking a code from the Geo KeyRegistry, and getting     * the field value as a single int. Should be called for codes that have     * only one int value, you should read the GeoTIFF spec to know what they     * are.     *      * @param codeFromKeyRegistry     * @return the code value, or -1 if not found.     */    protected int getGeoKeyIntValue(int codeFromKeyRegistry) {        XTIFFField field = getGeoFieldForCode(codeFromKeyRegistry);        if (field != null) {            int type = field.getType();            if (logger.isLoggable(Level.FINE)) {                logger.fine("field type is " + getStringOfType(type));            }            if (type == XTIFFField.TIFF_SHORT) {                return field.getAsInt(0);            }        }        return -1;    }    /**     * Helper function for taking a code from the TIFF spec, and getting the     * field value as a single int. Should be called for codes that have only     * one int value, you should read the TIFF spec to know what they are.     *      * @param tiffCode     * @return the code value, or -1 if not found.     */    protected int getFieldIntValue(int tiffCode) {        XTIFFField field = getFieldWithTag(tiffCode);        if (field != null) {            int type = field.getType();            if (type == XTIFFField.TIFF_SHORT) {                return field.getAsInt(0);            }        }        return -1;    }    /**     * Prints out the values of the XTIFF Fields provided to it.     *      * @param gtfFields You can get all of the XTIFFFields from the directory     *        object, or ask this class for the geokeys.     */    public void dumpTags(XTIFFField[] gtfFields) {        StringBuffer buf = new StringBuffer();        for (int i = 0; i < gtfFields.length; i++) {            XTIFFField xtff = gtfFields[i];            int type = xtff.getType();            int tag = xtff.getTag();            buf.append("\n\tfield (" + i + ") - " + tag + " ("                    + KeyRegistry.getKey(KeyRegistry.GEOKEY, tag) + "): [");            switch (type) {            case XTIFFField.TIFF_ASCII:                String[] fieldStrings = xtff.getAsStrings();                for (int j = 0; j < fieldStrings.length; j++) {                    buf.append(fieldStrings[j]);                    if (j < fieldStrings.length - 1) {                        buf.append(", ");                    }                }                buf.append("]");                break;            case XTIFFField.TIFF_DOUBLE:                double[] fieldDoubles = xtff.getAsDoubles();                for (int j = 0; j < fieldDoubles.length; j++) {                    buf.append(fieldDoubles[j]);                    if (j < fieldDoubles.length - 1) {                        buf.append(", ");                    }                }                buf.append("]");                break;            case XTIFFField.TIFF_FLOAT:                double[] fieldFloats = xtff.getAsDoubles();                for (int j = 0; j < fieldFloats.length; j++) {                    buf.append(fieldFloats[j]);                    if (j < fieldFloats.length - 1) {                        buf.append(", ");                    }                }                buf.append("]");                break;            case XTIFFField.TIFF_BYTE:            case XTIFFField.TIFF_SBYTE:                byte[] fieldBytes = xtff.getAsBytes();                for (int j = 0; j < fieldBytes.length; j++) {                    buf.append(fieldBytes[j]);                    if (j < fieldBytes.length - 1) {                        buf.append(", ");                    }                }                buf.append("]");                break;            case XTIFFField.TIFF_SSHORT:                short[] fieldShorts = xtff.getAsShorts();                for (int j = 0; j < fieldShorts.length; j++) {                    buf.append(fieldShorts[j]);                    if (j < fieldShorts.length - 1) {                        buf.append(", ");                    }                }                buf.append("]");                break;            case XTIFFField.TIFF_LONG:            case XTIFFField.TIFF_SHORT:                long[] fieldLongs = xtff.getAsLongs();                for (int j = 0; j < fieldLongs.length; j++) {                    buf.append(fieldLongs[j]);                    if (j < fieldLongs.length - 1) {                        buf.append(", ");                    }                }                buf.append("]");                break;            case XTIFFField.TIFF_SLONG:                int[] fieldInts = xtff.getAsInts();                for (int j = 0; j < fieldInts.length; j++) {                    buf.append(fieldInts[j]);                    if (j < fieldInts.length - 1) {                        buf.append(", ");                    }                }                buf.append("]");                break;            case XTIFFField.TIFF_RATIONAL:                long[][] fieldRationals = xtff.getAsRationals();                for (int k = 0; k < fieldRationals.length; k++) {                    buf.append("\n\t");                    for (int j = 0; j < fieldRationals[0].length; j++) {                        buf.append(fieldRationals[k][j]);                        if (j < fieldRationals[k].length - 1) {                            buf.append(", ");                        }                    }                }                buf.append("\n]");                break;            case XTIFFField.TIFF_SRATIONAL:                int[][] fieldSRationals = xtff.getAsSRationals();                for (int k = 0; k < fieldSRationals.length; k++) {                    buf.append("\n\t");                    for (int j = 0; j < fieldSRationals[0].length; j++) {                        buf.append(fieldSRationals[k][j]);                        if (j < fieldSRationals[k].length - 1) {                            buf.append(", ");                        }                    }                }                buf.append("\n]");                break;            default:                // TIFF_UNDEFINED                buf.append("Can't handle " + type + " type.]");            }        }        logger.info(buf.toString());    }    /**     * Helper function that coverts type codes to string representation.     *      * @param type code from XTIFFField.     * @return String interpretation of type code.     */    public String getStringOfType(int type) {        switch (type) {        case XTIFFField.TIFF_ASCII:            return "ASCII";        case XTIFFField.TIFF_DOUBLE:            return "double";        case XTIFFField.TIFF_FLOAT:            return "float";        case XTIFFField.TIFF_BYTE:            return "byte";        case XTIFFField.TIFF_SBYTE:            return "sbyte";        case XTIFFField.TIFF_SSHORT:            return "sshort";        case XTIFFField.TIFF_LONG:            return "long";        case XTIFFField.TIFF_SHORT:            return "short";        case XTIFFField.TIFF_SLONG:            return "slong";        case XTIFFField.TIFF_RATIONAL:            return "rational";        case XTIFFField.TIFF_SRATIONAL:            return "srational";        default:            return "unknown";        }    }    /**     * Uses a GeoTIFFModelFactory to create a georeferenced ImageTile image.     * Only handles 4326 projection right now (WGS84).     *      * @return     * @throws IOException     */    public ImageTile getImageTile() throws IOException {        GeoTIFFModelFactory gtmf = new GeoTIFFModelFactory(this);        return gtmf.getImageTile();    }    public ImageTile getImageTile(GeoTIFFImageReader id, ImageTile.Cache cache)            throws IOException {        GeoTIFFModelFactory gtmf = new GeoTIFFModelFactory(this);        return gtmf.getImageTile(id, cache);    }    public static void main(String[] args) {        if (args.length < 1) {            System.out.println("GeoTIFFFile:  Need a path/filename");            System.exit(0);        }        logger.info("GeoTIFFFile: " + args[0]);        String filePath = null;        if (args.length > 0) {            filePath = args[0];        }        if (filePath != null) {            try {                URL fileURL = PropUtils.getResourceOrFileOrURL(filePath);                if (fileURL != null) {                    GeoTIFFFile gtfFile = new GeoTIFFFile(fileURL);                    BufferedImage bi = gtfFile.getBufferedImage();                    GeoTIFFDirectory gtfd = gtfFile.getGtfDirectory();                    double[] tiePoints = gtfd.getTiepoints();                    System.out.println("------ Tie Point Values ------");                    for (int i = 0; i < tiePoints.length; i++) {                        System.out.println(tiePoints[i]);                    }                    double[] scaleMatrix = gtfd.getPixelScale();                    System.out.println("------ Pixel Scale Values ------");                    for (int i = 0; i < scaleMatrix.length; i++) {                        System.out.println(scaleMatrix[i]);                    }                    System.out.println("----- Geo Keys -------");                    gtfFile.dumpTags(gtfFile.getGeoKeys());                    System.out.println("------------");                    System.out.println("----- All Keys -------");                    gtfFile.dumpTags(gtfFile.getGtfDirectory().getFields());                    System.out.println("------------");                    CADRG crg = new CADRG(new com.bbn.openmap.LatLonPoint(0f, 0f), 1500000, 600, 600);                    final OMRaster omsr = new OMRaster(0, 0, bi);                    omsr.generate(crg);                    java.awt.Frame window = new java.awt.Frame(filePath) {                        public void paint(java.awt.Graphics g) {                            if (omsr != null) {                                omsr.render(g);                            }                        }                    };                    window.addWindowListener(new java.awt.event.WindowAdapter() {                        public void windowClosing(java.awt.event.WindowEvent e) {                            // need a shutdown event to notify other gui beans                            // and                            // then exit.                            System.exit(0);                        }                    });                    window.setSize(omsr.getWidth(), omsr.getHeight());                    window.setVisible(true);                    window.repaint();                }            } catch (MalformedURLException murle) {            } catch (IOException ioe) {            }        }    }}

⌨️ 快捷键说明

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