001: /*
002: * Geotools2 - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2002, 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: /*
018: * Created on Apr 12, 2005
019: *
020: * TODO To change the template for this generated file go to
021: * Window - Preferences - Java - Code Style - Code Templates
022: */
023: package org.geotools.gce.gtopo30;
024:
025: import java.io.File;
026: import java.io.IOException;
027: import java.net.MalformedURLException;
028: import java.net.URL;
029: import java.util.HashMap;
030: import java.util.logging.Level;
031: import java.util.logging.Logger;
032:
033: import org.geotools.coverage.grid.io.AbstractGridFormat;
034: import org.geotools.coverage.grid.io.imageio.GeoToolsWriteParams;
035: import org.geotools.data.DataSourceException;
036: import org.geotools.factory.Hints;
037: import org.geotools.parameter.DefaultParameterDescriptorGroup;
038: import org.geotools.parameter.ParameterGroup;
039: import org.opengis.coverage.grid.Format;
040: import org.opengis.coverage.grid.GridCoverageReader;
041: import org.opengis.coverage.grid.GridCoverageWriter;
042: import org.opengis.parameter.GeneralParameterDescriptor;
043:
044: /**
045: * Provides basic information about the GeoTIFF format IO.
046: *
047: * @author Simone Giannecchini
048: * @author mkraemer
049: * @source $URL:
050: * http://svn.geotools.org/geotools/trunk/gt/plugin/gtopo30/src/org/geotools/gce/gtopo30/GTopo30Format.java $
051: */
052: public final class GTopo30Format extends AbstractGridFormat implements
053: Format {
054:
055: /** Logger. */
056: private final static Logger LOGGER = org.geotools.util.logging.Logging
057: .getLogger("org.geotools.gce.gtopo30");
058:
059: /**
060: * Creates an instance and sets the metadata.
061: */
062: public GTopo30Format() {
063: mInfo = new HashMap();
064: mInfo.put("name", "Gtopo30");
065: mInfo.put("description", "Gtopo30 Coverage Format");
066: mInfo.put("vendor", "Geotools");
067: mInfo.put("docURL",
068: "http://edcdaac.usgs.gov/gtopo30/gtopo30.asp");
069: mInfo.put("version", "1.0");
070:
071: // reading parameters
072: readParameters = new ParameterGroup(
073: new DefaultParameterDescriptorGroup(
074: mInfo,
075: new GeneralParameterDescriptor[] { READ_GRIDGEOMETRY2D }));
076:
077: // reading parameters
078: writeParameters = new ParameterGroup(
079: new DefaultParameterDescriptorGroup(
080: mInfo,
081: new GeneralParameterDescriptor[] { GEOTOOLS_WRITE_PARAMS }));
082: }
083:
084: /**
085: * Returns a reader object which you can use to read GridCoverages from a
086: * given source
087: *
088: * @param o
089: * the the source object. This can be a File, an URL or a String
090: * (representing a filename or an URL)
091: *
092: * @return a GridCoverageReader object or null if the source object could
093: * not be accessed.
094: */
095: public GridCoverageReader getReader(final Object o) {
096: return getReader(o, null);
097: }
098:
099: /**
100: * Returns a writer object which you can use to write GridCoverages to a
101: * given destination.
102: *
103: * @param destination
104: * The destination object
105: *
106: * @return a GridCoverageWriter object
107: */
108: public GridCoverageWriter getWriter(final Object destination) {
109: try {
110: return new GTopo30Writer(destination);
111: } catch (DataSourceException e) {
112: if (LOGGER.isLoggable(Level.WARNING))
113: LOGGER.log(Level.WARNING, e.getLocalizedMessage(), e);
114: return null;
115: }
116: }
117:
118: /**
119: * Returns a writer object which you can use to write GridCoverages to a
120: * given destination.
121: *
122: * @param destination
123: * The destination object
124: *
125: * @return a GridCoverageWriter object
126: * @throws DataSourceException
127: */
128: public GridCoverageWriter getWriter(final Object destination,
129: Hints hints) {
130: try {
131: return new GTopo30Writer(destination, hints);
132: } catch (DataSourceException e) {
133: if (LOGGER.isLoggable(Level.WARNING))
134: LOGGER.log(Level.WARNING, e.getLocalizedMessage(), e);
135: return null;
136: }
137: }
138:
139: /**
140: * Checks if the GTopo30DataSource supports a given file
141: *
142: * @param o
143: * the source object to test for compatibility with this format.
144: * This can be a File, an URL or a String (representing a
145: * filename or an URL)
146: *
147: * @return if the source object is compatible
148: */
149: public boolean accepts(final Object o) {
150: URL urlToUse;
151:
152: if (o instanceof File) {
153: try {
154: urlToUse = ((File) o).toURL();
155: } catch (MalformedURLException e) {
156: if (LOGGER.isLoggable(Level.FINE))
157: LOGGER.log(Level.FINE, e.getLocalizedMessage(), e);
158: return false;
159: }
160: } else if (o instanceof URL) {
161: // we only allow files
162: urlToUse = (URL) o;
163: } else if (o instanceof String) {
164: try {
165: // is it a filename?
166: urlToUse = new File((String) o).toURL();
167: } catch (MalformedURLException e) {
168: // is it a URL
169: try {
170: urlToUse = new URL((String) o);
171: } catch (MalformedURLException e1) {
172: if (LOGGER.isLoggable(Level.FINE))
173: LOGGER.log(Level.FINE, e.getLocalizedMessage(),
174: e);
175: return false;
176: }
177: }
178: } else {
179: return false;
180: }
181:
182: // trying to create a reader
183: try {
184: final GTopo30Reader reader = new GTopo30Reader(urlToUse);
185: } catch (IOException e) {
186: if (LOGGER.isLoggable(Level.FINE))
187: LOGGER.log(Level.FINE, e.getLocalizedMessage(), e);
188: return false;
189: }
190:
191: return true;
192: }
193:
194: /**
195: * Returns a reader object which you can use to read GridCoverages from a
196: * given source
197: *
198: * @param o
199: * the the source object. This can be a File, an URL or a String
200: * (representing a filename or an URL)
201: *
202: * @return a GridCoverageReader object or null if the source object could
203: * not be accessed.
204: */
205: public GridCoverageReader getReader(final Object o, Hints hints) {
206:
207: try {
208: return new GTopo30Reader(o);
209: } catch (IOException e) {
210: if (LOGGER.isLoggable(Level.SEVERE))
211: LOGGER.log(Level.SEVERE, e.getLocalizedMessage(), e);
212: return null;
213: }
214:
215: }
216:
217: /**
218: * Always returns null since for the moment there are no
219: * {@link GeoToolsWriteParams} availaible for this format.
220: *
221: * @return always null.
222: */
223: public GeoToolsWriteParams getDefaultImageIOWriteParameters() {
224: return null;
225: }
226:
227: }
|