001: package com.vividsolutions.jump.workbench.imagery.geotiff;
002:
003: /*
004: * The Unified Mapping Platform (JUMP) is an extensible, interactive GUI
005: * for visualizing and manipulating spatial features with geometry and attributes.
006: *
007: * Copyright (C) 2003 Vivid Solutions
008: *
009: * This program is free software; you can redistribute it and/or
010: * modify it under the terms of the GNU General Public License
011: * as published by the Free Software Foundation; either version 2
012: * of the License, or (at your option) any later version.
013: *
014: * This program is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
017: * GNU General Public License for more details.
018: *
019: * You should have received a copy of the GNU General Public License
020: * along with this program; if not, write to the Free Software
021: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
022: *
023: * For more information, contact:
024: *
025: * Vivid Solutions
026: * Suite #1A
027: * 2328 Government Street
028: * Victoria BC V8T 5G5
029: * Canada
030: *
031: * (250)385-6040
032: * www.vividsolutions.com
033: */
034: import java.awt.geom.AffineTransform;
035: import java.util.List;
036:
037: import org.geotiff.image.jai.GeoTIFFDescriptor;
038: import org.geotiff.image.jai.GeoTIFFDirectory;
039: import org.libtiff.jai.codec.XTIFF;
040: import org.libtiff.jai.codec.XTIFFField;
041:
042: import com.vividsolutions.jts.geom.Coordinate;
043: import com.vividsolutions.jump.util.FileUtil;
044:
045: public class GeoTIFFRaster extends GeoReferencedRaster {
046: private final String MSG_GENERAL = "This is not a valid GeoTIFF file.";
047: String fileName;
048:
049: boolean hoPatch = false;
050:
051: /**
052: * Called by Java2XML
053: */
054: public GeoTIFFRaster(String imageFileLocation) throws Exception {
055: super (imageFileLocation);
056: registerWithJAI();
057: readRasterfile();
058: }
059:
060: private void registerWithJAI() {
061: // Register the GeoTIFF descriptor with JAI.
062: GeoTIFFDescriptor.register();
063: }
064:
065: private void parseGeoTIFFDirectory(GeoTIFFDirectory dir)
066: throws Exception {
067: // Find the ModelTiePoints field
068: XTIFFField fieldModelTiePoints = dir
069: .getField(XTIFF.TIFFTAG_GEO_TIEPOINTS);
070: if (fieldModelTiePoints == null)
071: throw new Exception("Missing tiepoints-tag in file.\n"
072: + MSG_GENERAL);
073:
074: // Get the number of modeltiepoints
075: // int numModelTiePoints = fieldModelTiePoints.getCount() / 6;
076:
077: // ToDo: alleen numModelTiePoints == 1 ondersteunen.
078:
079: // Map the modeltiepoints from raster to model space
080:
081: // Read the tiepoints
082: setCoorRasterTiff_tiepointLT(new Coordinate(fieldModelTiePoints
083: .getAsDouble(0), fieldModelTiePoints.getAsDouble(1), 0));
084: setCoorModel_tiepointLT(new Coordinate(fieldModelTiePoints
085: .getAsDouble(3), fieldModelTiePoints.getAsDouble(4), 0));
086: setEnvelope();
087: // Find the ModelPixelScale field
088: XTIFFField fieldModelPixelScale = dir
089: .getField(XTIFF.TIFFTAG_GEO_PIXEL_SCALE);
090: if (fieldModelPixelScale == null)
091: throw new Exception("Missing pixelscale-tag in file."
092: + "\n" + MSG_GENERAL);
093:
094: setDblModelUnitsPerRasterUnit_X(fieldModelPixelScale
095: .getAsDouble(0));
096: setDblModelUnitsPerRasterUnit_Y(fieldModelPixelScale
097: .getAsDouble(1));
098: }
099:
100: /**
101: * @return filename of the tiff worldfile
102: */
103: private String worldFileName() {
104: int posDot = fileName.lastIndexOf('.');
105: if (posDot == -1) {
106: posDot = fileName.length();
107: }
108: return fileName.substring(0, posDot) + ".tfw";
109: }
110:
111: private void parseWorldFile() throws Exception {
112: // Get the name of the tiff worldfile.
113: String name = worldFileName();
114:
115: // Read the tags from the tiff worldfile.
116: List lines = FileUtil.getContents(name);
117: double[] tags = new double[6];
118: for (int i = 0; i < 6; i++) {
119: String line = (String) lines.get(i);
120: tags[i] = Double.parseDouble(line);
121: }
122: setAffineTransformation(new AffineTransform(tags));
123: }
124:
125: protected void readRasterfile() throws Exception {
126: // ImageCodec originalCodec = ImageCodec.getCodec("tiff");
127:
128: try {
129: super .readRasterfile();
130:
131: // Get access to the tags and geokeys.
132: // First, get the TIFF directory
133: GeoTIFFDirectory dir = (GeoTIFFDirectory) src
134: .getProperty("tiff.directory");
135: if (dir == null)
136: throw new Exception("This is not a (geo)tiff file.");
137:
138: try {
139: // Try to parse any embedded geotiff tags.
140: parseGeoTIFFDirectory(dir);
141: } catch (Exception E) {
142: // Embedded geotiff tags have not been found. Try
143: // to use a geotiff world file.
144: try {
145: parseWorldFile();
146: } catch (Exception e) {
147: throw new Exception(
148: "Neither geotiff tags nor valid worldfile found.\n"
149: + MSG_GENERAL);
150: }
151: }
152: } finally {
153: }
154: }
155:
156: }
|