001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2005-2006, GeoTools Project Managment Committee (PMC)
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation;
009: * version 2.1 of the License.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: */
016: /*
017: * NOTICE OF RELEASE TO THE PUBLIC DOMAIN
018: *
019: * This work was created by employees of the USDA Forest Service's
020: * Fire Science Lab for internal use. It is therefore ineligible for
021: * copyright under title 17, section 105 of the United States Code. You
022: * may treat it as you would treat any public domain work: it may be used,
023: * changed, copied, or redistributed, with or without permission of the
024: * authors, for free or for compensation. You may not claim exclusive
025: * ownership of this code because it is already owned by everyone. Use this
026: * software entirely at your own risk. No warranty of any kind is given.
027: *
028: * A copy of 17-USC-105 should have accompanied this distribution in the file
029: * 17USC105.html. If not, you may access the law via the US Government's
030: * public websites:
031: * - http://www.copyright.gov/title17/92chap1.html#105
032: * - http://www.gpoaccess.gov/uscode/ (enter "17USC105" in the search box.)
033: */
034: package org.geotools.gce.geotiff;
035:
036: import java.awt.geom.AffineTransform;
037: import java.io.IOException;
038: import java.io.PrintWriter;
039: import java.io.StringWriter;
040:
041: import org.geotools.gce.geotiff.IIOMetadataAdpaters.GeoKeyEntry;
042: import org.geotools.gce.geotiff.IIOMetadataAdpaters.GeoTiffIIOMetadataDecoder;
043: import org.geotools.gce.geotiff.IIOMetadataAdpaters.PixelScale;
044: import org.geotools.gce.geotiff.IIOMetadataAdpaters.TiePoint;
045:
046: /**
047: * This exception is thrown when the problem with reading the GeoTiff file has
048: * to do with constructing either the raster to model transform, or the
049: * coordinate system. A GeoTiffException:
050: *
051: * <p>
052: *
053: * <ul>
054: * <li> encapsulates the salient information in the GeoTiff tags, making the
055: * values available as read only properties. </li>
056: * <li> sends the appropriate log message to the log stream </li>
057: * <li> produces a readable message property for later retrieval </li>
058: * </ul>
059: * </p>
060: *
061: * <p>
062: * This exception is expected to be thrown when there is absolutely nothing
063: * wrong with the GeoTiff file which produced it. In this case, the exception is
064: * reporting an unsupported coordinate system description or raster to model
065: * transform, or some other unrecognized configuration of the
066: * GeoTIFFWritingUtilities tags. By doing so, it attempts to record enough
067: * information so that the maintainers can support it in the future.
068: * </p>
069: *
070: * @author Bryce Nordgren / USDA Forest Service
071: * @author Simone Giannecchini
072: * @source $URL:
073: * http://svn.geotools.org/geotools/trunk/gt/plugin/geotiff/src/org/geotools/gce/geotiff/GeoTiffException.java $
074: */
075: public final class GeoTiffException extends IOException {
076:
077: /**
078: *
079: */
080: private static final long serialVersionUID = 1008533682021487024L;
081:
082: private GeoTiffIIOMetadataDecoder metadata = null;
083:
084: private GeoKeyEntry[] geoKeys = null;
085:
086: /**
087: * Constructs an instance of <code>GeoTiffException</code> with the
088: * specified detail message.
089: *
090: * @param metadata
091: * The metadata from the GeoTIFFWritingUtilities image causing
092: * the error.
093: * @param msg
094: * the detail message.
095: */
096: public GeoTiffException(GeoTiffIIOMetadataDecoder metadata,
097: String msg, Throwable t) {
098: super (msg);
099: this .metadata = metadata;
100: if (t != null)
101: this .initCause(t);
102:
103: if (metadata != null) {
104: final int numGeoKeys = metadata.getNumGeoKeys();
105: if (numGeoKeys > 0) {
106: geoKeys = new GeoKeyEntry[numGeoKeys];
107:
108: for (int i = 0; i < numGeoKeys; i++) {
109: geoKeys[i] = metadata.getGeoKeyRecordByIndex(i);
110: }
111: }
112: }
113: }
114:
115: /**
116: * Getter for property modelTransformation.
117: *
118: * @return Value of property modelTransformation.
119: */
120: public AffineTransform getModelTransformation() {
121: if (metadata != null)
122: return metadata.getModelTransformation();
123: return null;
124: }
125:
126: /**
127: * Getter for property geoKeys.
128: *
129: * @return Value of property geoKeys.
130: */
131: public GeoKeyEntry[] getGeoKeys() {
132: return this .geoKeys;
133: }
134:
135: public String getMessage() {
136: final StringWriter text = new StringWriter(1024);
137: final PrintWriter message = new PrintWriter(text);
138:
139: // Header
140: message.println("GEOTIFF Module Error Report");
141:
142: // start with the message the user specified
143: message.println(super .getMessage());
144:
145: // do the model pixel scale tags
146: message.print("ModelPixelScaleTag: ");
147: if (metadata != null) {
148: final PixelScale modelPixelScales = metadata
149: .getModelPixelScales();
150: if (modelPixelScales != null) {
151: message.println("[" + modelPixelScales.getScaleX()
152: + "," + modelPixelScales.getScaleY() + ","
153: + modelPixelScales.getScaleZ() + "]");
154: } else {
155: message.println("NOT AVAILABLE");
156: }
157: } else
158: message.println("NOT AVAILABLE");
159:
160: // do the model tie point tags
161: message.print("ModelTiePointTag: ");
162: if (metadata != null) {
163: final TiePoint[] modelTiePoints = metadata
164: .getModelTiePoints();
165: if (modelTiePoints != null) {
166: final int numTiePoints = modelTiePoints.length;
167: message.println("(" + (numTiePoints) + " tie points)");
168: for (int i = 0; i < (numTiePoints); i++) {
169: message.print("TP #" + i + ": ");
170: message
171: .print("["
172: + modelTiePoints[i].getValueAt(0));
173: message
174: .print(","
175: + modelTiePoints[i].getValueAt(1));
176: message
177: .print(","
178: + modelTiePoints[i].getValueAt(2));
179: message.print("] -> ["
180: + modelTiePoints[i].getValueAt(3));
181: message
182: .print(","
183: + modelTiePoints[i].getValueAt(4));
184: message.println(","
185: + modelTiePoints[i].getValueAt(5) + "]");
186: }
187: } else
188: message.println("NOT AVAILABLE");
189: } else
190: message.println("NOT AVAILABLE");
191:
192: // do the transformation tag
193: message.print("ModelTransformationTag: ");
194:
195: AffineTransform modelTransformation = getModelTransformation();
196:
197: if (modelTransformation != null) {
198: message.println("[");
199:
200: message.print(" [" + modelTransformation.getScaleX());
201: message.print("," + modelTransformation.getShearX());
202: message.print("," + modelTransformation.getScaleY());
203: message.print("," + modelTransformation.getShearY());
204: message.print("," + modelTransformation.getTranslateX());
205: message.print("," + modelTransformation.getTranslateY()
206: + "]");
207:
208: message.println("]");
209: } else {
210: message.println("NOT AVAILABLE");
211: }
212:
213: // do all the GeoKeys
214: if (geoKeys != null) {
215: int numTags = geoKeys.length;
216: for (int i = 0; i < numTags; i++) {
217: message.print("GeoKey #" + (i + 1) + ": ");
218: message.println("Key = " + geoKeys[i].getKeyID()
219: + ", Value = "
220: + metadata.getGeoKey(geoKeys[i].getKeyID()));
221: }
222: }
223:
224: // print out the localized message
225: Throwable t = getCause();
226: if (t != null)
227: t.printStackTrace(message);
228:
229: // close and return
230: message.close();
231: return text.toString();
232: }
233: }
|