001: /*
002: * Geotools 2 - OpenSource mapping toolkit
003: * (C) 2006, Geotools Project Managment Committee (PMC)
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: */
019: package org.geotools.gce.imagepyramid;
020:
021: import java.awt.Color;
022: import java.io.BufferedInputStream;
023: import java.io.File;
024: import java.io.FileInputStream;
025: import java.io.IOException;
026: import java.io.RandomAccessFile;
027: import java.net.MalformedURLException;
028: import java.net.URL;
029: import java.net.URLDecoder;
030: import java.util.HashMap;
031: import java.util.Properties;
032: import java.util.logging.Level;
033: import java.util.logging.Logger;
034:
035: import org.geotools.coverage.grid.io.AbstractGridFormat;
036: import org.geotools.coverage.grid.io.imageio.GeoToolsWriteParams;
037: import org.geotools.data.DataSourceException;
038: import org.geotools.data.PrjFileReader;
039: import org.geotools.factory.Hints;
040: import org.geotools.parameter.DefaultParameterDescriptor;
041: import org.geotools.parameter.DefaultParameterDescriptorGroup;
042: import org.geotools.parameter.ParameterGroup;
043: import org.opengis.coverage.grid.Format;
044: import org.opengis.coverage.grid.GridCoverageReader;
045: import org.opengis.coverage.grid.GridCoverageWriter;
046: import org.opengis.parameter.GeneralParameterDescriptor;
047: import org.opengis.referencing.FactoryException;
048: import org.opengis.referencing.crs.CoordinateReferenceSystem;
049:
050: /**
051: * This class implements the basic format capabilities for a coverage format.
052: *
053: * @author Simone Giannecchini (simboss)
054: */
055: public final class ImagePyramidFormat extends AbstractGridFormat
056: implements Format {
057:
058: /** Logger. */
059: private final static Logger LOGGER = org.geotools.util.logging.Logging
060: .getLogger("org.geotools.gce.imagepyramid");
061:
062: /** Control the transparency of the output coverage. */
063: public static final DefaultParameterDescriptor OUTPUT_TRANSPARENT_COLOR = new DefaultParameterDescriptor(
064: "OutputTransparentColor", Color.class, null, null);
065:
066: /** Control the type of the final mosaic. */
067: public static final DefaultParameterDescriptor FADING = new DefaultParameterDescriptor(
068: "Fading", Boolean.class, null, Boolean.FALSE);
069:
070: /** Control the transparency of the output coverage. */
071: public static final DefaultParameterDescriptor INPUT_TRANSPARENT_COLOR = new DefaultParameterDescriptor(
072: "InputTransparentColor", Color.class, null, null);
073:
074: /** Control the thresholding on the input coverage */
075: public static final DefaultParameterDescriptor INPUT_IMAGE_THRESHOLD_VALUE = new DefaultParameterDescriptor(
076: "InputImageThresholdValue", Double.class, null, new Double(
077: Double.NaN));
078:
079: /**
080: * Creates an instance and sets the metadata.
081: */
082: public ImagePyramidFormat() {
083: setInfo();
084: }
085:
086: /**
087: * Sets the metadata information for this format
088: */
089: private void setInfo() {
090: HashMap info = new HashMap();
091:
092: info.put("name", "ImagePyramid");
093: info.put("description", "Image pyramidal plugin");
094: info.put("vendor", "Geotools");
095: info.put("docURL", "");
096: info.put("version", "1.0");
097: mInfo = info;
098:
099: // reading parameters
100: readParameters = new ParameterGroup(
101: new DefaultParameterDescriptorGroup(mInfo,
102: new GeneralParameterDescriptor[] {
103: READ_GRIDGEOMETRY2D,
104: INPUT_TRANSPARENT_COLOR,
105: INPUT_IMAGE_THRESHOLD_VALUE,
106: OUTPUT_TRANSPARENT_COLOR }));
107:
108: // reading parameters
109: writeParameters = null;
110: }
111:
112: /**
113: * Retrieves a reader for this source object in case the provided source can
114: * be read using this plugin.
115: *
116: * @param source
117: * Object
118: * @return An {@link ImagePyramidReader} if the provided object can be read
119: * using this plugin or null.
120: */
121: public GridCoverageReader getReader(Object source) {
122: return getReader(source, null);
123: }
124:
125: /**
126: * This methods throw an {@link UnsupportedOperationException} because this
127: * plugiin si read only.
128: */
129: public GridCoverageWriter getWriter(Object destination) {
130: throw new UnsupportedOperationException(
131: "This plugin is a read only plugin!");
132: }
133:
134: /**
135: * @see org.geotools.data.coverage.grid.AbstractGridFormat#accepts(Object
136: * input)
137: */
138: public boolean accepts(Object source) {
139: try {
140:
141: File sourceFile;
142: // /////////////////////////////////////////////////////////////////////
143: //
144: // Check source
145: //
146: // /////////////////////////////////////////////////////////////////////
147: if (source instanceof File)
148: sourceFile = (File) source;
149: else if (source instanceof URL) {
150:
151: final URL sourceURL = (URL) source;
152: if (sourceURL.getProtocol() != "file")
153: return false;
154:
155: final String temp = URLDecoder.decode(sourceURL
156: .getFile(), "UTF8");
157:
158: sourceFile = new File(temp);
159: } else if (source instanceof String) {
160: sourceFile = new File((String) source);
161: if (!sourceFile.exists())
162: return false;
163: } else
164: return false;
165: // ///////////////////////////////////////////////////////////////////
166: //
167: // Trying to load informations
168: //
169: //
170: // ///////////////////////////////////////////////////////////////////
171: // //
172: //
173: // get the crs if able to
174: //
175: // //
176: String fileName = sourceFile.getAbsolutePath();
177: final int index = fileName.lastIndexOf('.');
178: if (index != -1)
179: fileName = fileName.substring(0, index);
180: PrjFileReader crsReader;
181: try {
182: crsReader = new PrjFileReader(new RandomAccessFile(
183: new StringBuffer(fileName).append(".prj")
184: .toString(), "r").getChannel());
185: } catch (FactoryException e) {
186: if (LOGGER.isLoggable(Level.FINE))
187: LOGGER.log(Level.FINE, e.getLocalizedMessage(), e);
188: throw new DataSourceException(e);
189: }
190: CoordinateReferenceSystem tempcrs = crsReader
191: .getCoordinateReferenceSystem();
192: if (tempcrs == null) {
193: // use the default crs
194: tempcrs = AbstractGridFormat.getDefaultCRS();
195: LOGGER
196: .log(
197: Level.FINE,
198: new StringBuffer(
199: "Unable to find a CRS for this coverage, using a default one: ")
200: .append(tempcrs.toWKT())
201: .toString());
202: }
203: //
204: // ///////////////////////////////////////////////////////////////////
205: //
206: // Load properties file with information about levels and envelope
207: //
208: //
209: // ///////////////////////////////////////////////////////////////////
210: // property file
211: final Properties properties = new Properties();
212: try {
213: properties.load(new BufferedInputStream(
214: new FileInputStream(sourceFile)));
215: } catch (Exception e) {
216: return false;
217: }
218:
219: // load the envelope
220: final String envelope = properties
221: .getProperty("Envelope2D");
222: if (envelope == null)
223: return false;
224: String[] pairs = envelope.split(" ");
225: final double cornersV[][] = new double[2][2];
226: String pair[];
227: for (int i = 0; i < 2; i++) {
228: pair = pairs[i].split(",");
229: cornersV[i][0] = Double.parseDouble(pair[0]);
230: cornersV[i][1] = Double.parseDouble(pair[1]);
231: }
232:
233: // overviews dir
234: int numOverviews = Integer.parseInt(properties
235: .getProperty("LevelsNum")) - 1;
236:
237: // resolutions levels
238: final String levels = properties.getProperty("Levels");
239: pairs = levels.split(" ");
240: double[][] overViewResolutions = numOverviews > 1 ? new double[numOverviews][2]
241: : null;
242: pair = pairs[0].split(",");
243: double[] highestRes = new double[2];
244: highestRes[0] = Double.parseDouble(pair[0]);
245: highestRes[1] = Double.parseDouble(pair[1]);
246: for (int i = 1; i < numOverviews + 1; i++) {
247: pair = pairs[i].split(",");
248: overViewResolutions[i - 1][0] = Double
249: .parseDouble(pair[0]);
250: overViewResolutions[i - 1][1] = Double
251: .parseDouble(pair[1]);
252: }
253:
254: // name
255: if (properties.getProperty("Name") == null)
256: return false;
257:
258: return true;
259: } catch (IOException e) {
260: if (LOGGER.isLoggable(Level.FINE))
261: LOGGER.log(Level.FINE, e.getLocalizedMessage(), e);
262: return false;
263:
264: } catch (NumberFormatException e) {
265: if (LOGGER.isLoggable(Level.FINE))
266: LOGGER.log(Level.FINE, e.getLocalizedMessage(), e);
267: return false;
268: }
269:
270: }
271:
272: /**
273: * Retrieves a reader for this source object in case the provided source can
274: * be read using this plugin.
275: *
276: * @param source
277: * Object
278: * @param hints
279: * {@link Hints} to control the reader behaviour.
280: * @return An {@link ImagePyramidReader} if the provided object can be read
281: * using this plugin or null.
282: */
283: public GridCoverageReader getReader(Object source, Hints hints) {
284: try {
285:
286: return new ImagePyramidReader(source, hints);
287: } catch (MalformedURLException e) {
288: if (LOGGER.isLoggable(Level.SEVERE))
289: LOGGER
290: .severe(new StringBuffer(
291: "impossible to get a reader for the provided source. The error is ")
292: .append(e.getLocalizedMessage())
293: .toString());
294: return null;
295: } catch (IOException e) {
296: if (LOGGER.isLoggable(Level.SEVERE))
297: LOGGER
298: .severe(new StringBuffer(
299: "impossible to get a reader for the provided source. The error is ")
300: .append(e.getLocalizedMessage())
301: .toString());
302: return null;
303: }
304: }
305:
306: /**
307: * Throw an exception since this plugin is readonly.
308: *
309: * @return nothing.
310: */
311: public GeoToolsWriteParams getDefaultImageIOWriteParameters() {
312: throw new UnsupportedOperationException("Unsupported method.");
313: }
314:
315: }
|