/[schmitzm]/trunk/src/skrueger/geotools/io/GeoImportUtilURL.java
ViewVC logotype

Annotation of /trunk/src/skrueger/geotools/io/GeoImportUtilURL.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 377 - (hide annotations)
Fri Sep 4 11:33:03 2009 UTC (15 years, 6 months ago) by alfonx
Original Path: branches/1.0-gt2-2.6/src/skrueger/geotools/io/GeoImportUtilURL.java
File size: 8473 byte(s)
* Patched the ImagePyramid and ImageMosaic classes from GT26-m2 again, because their Transparency doesn't work, nor do they support URLs.
* URL support is not yet fixed.
1 alfonx 244 /*******************************************************************************
2     * Copyright (c) 2009 Martin O. J. Schmitz.
3     *
4     * This file is part of the SCHMITZM library - a collection of utility
5 alfonx 256 * classes based on Java 1.6, focusing (not only) on Java Swing
6 alfonx 244 * and the Geotools library.
7     *
8     * The SCHMITZM project is hosted at:
9     * http://wald.intevation.org/projects/schmitzm/
10     *
11     * This program is free software; you can redistribute it and/or
12     * modify it under the terms of the GNU Lesser General Public License
13     * as published by the Free Software Foundation; either version 3
14     * of the License, or (at your option) any later version.
15     *
16     * This program is distributed in the hope that it will be useful,
17     * but WITHOUT ANY WARRANTY; without even the implied warranty of
18     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19     * GNU General Public License for more details.
20     *
21     * You should have received a copy of the GNU Lesser General Public License (license.txt)
22     * along with this program; if not, write to the Free Software
23     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24     * or try this link: http://www.gnu.org/licenses/lgpl.html
25     *
26     * Contributors:
27     * Martin O. J. Schmitz - initial API and implementation
28     * Stefan A. Krüger - additional utility classes
29     ******************************************************************************/
30     package skrueger.geotools.io;
31    
32     import java.awt.geom.Rectangle2D;
33     import java.awt.image.BufferedImage;
34     import java.io.IOException;
35     import java.net.URL;
36    
37     import javax.imageio.IIOException;
38     import javax.imageio.ImageIO;
39    
40     import org.apache.log4j.Logger;
41     import org.geotools.coverage.grid.GridCoverage2D;
42     import org.geotools.coverage.grid.GridCoverageFactory;
43     import org.geotools.factory.Hints;
44     import org.geotools.gce.geotiff.GeoTiffReader;
45     import org.geotools.geometry.Envelope2D;
46 alfonx 377 import org.opengis.parameter.GeneralParameterValue;
47 alfonx 244 import org.opengis.referencing.crs.CoordinateReferenceSystem;
48    
49     import schmitzm.geotools.io.GeoImportUtil;
50     import schmitzm.io.IOUtil;
51    
52     /**
53 alfonx 377 * Erweiterungen von Martin's {@link GeoImportUtil} classe fuer die konsequente
54     * Benutzung mit {@link URL}s. TODO Diese Klasse sollte vielleicht mit der
55     * {@link GeoImportUtil} zusammengefuegt werden.
56     *
57 alfonx 244 * @author <a href="mailto:[email protected]">Stefan Alfons Kr&uuml;ger</a>
58 alfonx 377 *
59 alfonx 244 */
60     public class GeoImportUtilURL extends GeoImportUtil {
61 alfonx 377 final static private Logger LOGGER = Logger
62     .getLogger(GeoImportUtilURL.class);
63 alfonx 244
64     /**
65 alfonx 377 * Read a {@link GridCoverage2D} from an image file. .prj and .wld files are
66     * usually expected
67 alfonx 244 */
68     public static GridCoverage2D readGridFromImage(URL url) throws IOException {
69     return readGridFromImage(url, null);
70     }
71    
72     /**
73 alfonx 377 * Read a {@link GridCoverage2D} from an image file. .wld file is usually
74     * expected also. The CRS can be given as the crs parameter. null is valid.
75     *
76     * @author <a href="mailto:[email protected]">Martin Schmitz</a>
77     * (University of Bonn/Germany)
78     * @author <a href="mailto:[email protected]">Stefan Alfons
79     * Kr&uuml;ger</a>
80 alfonx 244 */
81     public static GridCoverage2D readGridFromImage(URL url,
82     CoordinateReferenceSystem crs) throws IOException {
83     GridCoverage2D gc = null;
84    
85     BufferedImage im = ImageIO.read(url);
86     if (im == null)
87     throw new IIOException("No image reader found for this image type!");
88    
89     // World-File einlesen
90     double[] tfwInfo = null;
91    
92 alfonx 377 for (WORLD_POSTFIXES pf : WORLD_POSTFIXES.values()) {
93 alfonx 244 if (tfwInfo == null) {
94     try {
95 alfonx 377 tfwInfo = readWorldFile(IOUtil.changeUrlExt(url,
96     pf.toString()).openStream());
97 alfonx 244 } catch (Exception e) {
98     }
99     }
100     }
101    
102     if (tfwInfo == null)
103     throw new IllegalArgumentException(
104     "No georeferencing information found.\n"
105 alfonx 377 + "Attach a .wld file to " + url + "please.");
106 alfonx 244
107     float w = (float) (im.getWidth() * tfwInfo[0]); // reale Breite =
108     // RasterSpalten *
109     // hor. Aufloesung
110     float h = (float) (im.getHeight() * (-tfwInfo[3])); // reale Hoehe =
111     // RasterZeilen
112     // * vert.
113     // Aufloesung
114     float x = (float) tfwInfo[4]; // Suedwestliche Ecke!
115     float y = (float) tfwInfo[5] - h; // Suedwestliche Ecke (im
116     // tfw-File steht die
117     // Nordwestliche!)
118     // ggf. Projektion einlesen
119     if (crs == null) {
120 alfonx 377 crs = determineProjection(IOUtil.changeUrlExt(url, "prj"));
121 alfonx 244 }
122     Envelope2D envelope = new Envelope2D(crs, new Rectangle2D.Float(x, y,
123     w, h));
124     // WICHTIG: Name des Rasters sollte Leer-String sein, da ansonsten
125     // das
126     // Coloring des Rasters nicht klappt.
127 alfonx 377 // --> Name der Categories und ColorMapEntries-Labels muss (warum auch
128     // immer) mit dem Namen
129 alfonx 244 // des Rasters uebereinstimmen!!!
130     gc = new GridCoverageFactory().create("", im, envelope);
131    
132     return gc;
133     }
134    
135     /**
136 alfonx 377 * @param input
137     * URL, File oder InputStream to a GeoTIFF-File
138 alfonx 244 * @param crs
139     * erzwungenes CoordinateReferenceSystem fuer das Raster (kann
140     * {@code null} sein)
141 alfonx 377 *
142 alfonx 244 * @author <a href="mailto:[email protected]">Martin Schmitz</a>
143     * (University of Bonn/Germany)
144 alfonx 377 * @author <a href="mailto:[email protected]">Stefan Alfons
145     * Kr&uuml;ger</a>
146     *
147     * @param readParams
148     * May be {@link NullPointerException}. Otherwise an array of
149     * {@link GeneralParameterValue}s
150 alfonx 244 */
151 alfonx 377 public static GridCoverage2D readGridFromGeoTiff(Object input,
152     CoordinateReferenceSystem crs, GeneralParameterValue[] readParams)
153     throws IOException {
154 alfonx 244 GridCoverage2D gc = null;
155 alfonx 377 LOGGER.debug("Loading GeoTiff from URL = " + input + " now.");
156 alfonx 244
157     // Versuchen Geo-Information aus Tiff zu lesen
158    
159 alfonx 377 // Wenn CRS angegeben, dieses durch Hint erzwingen
160     Hints hints = new Hints();
161 alfonx 244
162 alfonx 377 if (crs != null)
163     hints.put(Hints.DEFAULT_COORDINATE_REFERENCE_SYSTEM, crs);
164     // Reader (mit Metadaten) erzeugen
165 alfonx 244
166 alfonx 377 LOGGER.debug("First try to create GeoTiff reader");
167 alfonx 244
168 alfonx 377 /**
169     * Attention, Attention: The GeoTiffReader fails for URLs like:
170     *
171     * jar:file:/home/sdsd/ad/atlas/raster_textur00949608497.jar!/ad/data/
172     * raster_textur00949608497/textur.tif
173     *
174     * It should be converted to something like:
175     * jar:http:///atlas/raster_textur00949608497
176     * .jar!/ad/data/raster_textur00949608497/textur.tif
177     * jar:http://www.wikisquare
178     * .de/atlas/raster_textur00949608497.jar!/ad/data
179     * /raster_textur00949608497/textur.tif
180     */
181     GeoTiffReader reader = new GeoTiffReader(input, hints);
182 alfonx 244
183 alfonx 377 gc = (GridCoverage2D) reader.read(readParams);
184     LOGGER.debug("... was successfull!");
185 alfonx 244 return gc;
186     }
187    
188 alfonx 377 //
189     // ArcGridRaster reader = new ArcGridRaster(new BufferedReader(
190     // new InputStreamReader(url.openStream())), false);
191     // WritableRaster raster = reader.readRaster();
192     // if (crs == null)
193     // crs = determineProjection(IOUtil.changeUrlExt(url, "prj"));
194     //
195     // LOGGER.debug("Position " + reader.getXlCorner() + " / "
196     // + reader.getYlCorner());
197     // LOGGER.debug("Size " + reader.getNCols() + " / "
198     // + reader.getNRows());
199     // LOGGER.debug("Min/Max " + reader.getMinValue() + " / "
200     // + reader.getMaxValue());
201     // LOGGER.debug("NoData " + reader.getNoData());
202     // LOGGER.debug("CellSize " + reader.getCellSize());
203     //
204     // Double x = reader.getXlCorner(); // Suedwestliche Ecke!
205     // Double y = reader.getYlCorner(); // Suedwestliche Ecke!
206     // if (y.isNaN() || x.isNaN()) {
207     // throw new IllegalArgumentException(
208     // "The parameters xlCorner and/or ylCorner could not be read. Hint: Check if the file defines 'xlcenter' instead of 'xlcorner'. If so, just replace all 'center' with 'corner' and try again.");
209     // }
210     //
211     // Double w = (reader.getNCols() * reader.getCellSize()); // reale Breite =
212     // // RasterSpalten
213     // // * Aufloesung
214     // Double h = (reader.getNRows() * reader.getCellSize()); // reale Hoehe =
215     // // RasterZeilen
216     // // * Aufloesung
217     // Envelope2D envelope = new Envelope2D(crs, new Rectangle2D.Double(x, y,
218     // w, h));
219     //
220     // // WICHTIG: Name des Rasters sollte Leer-String sein, da ansonsten das
221     // // Coloring des Rasters nicht klappt.
222     // // --> Name der Categories muss (warum auch immer) mit dem Namen
223     // // des Rasters uebereinstimmen!!!
224     // return new GridCoverageFactory().create("", raster, envelope);
225     // //
226     // // === OHNE ArcGridRaster ===
227     // // // ArcGridReader reader = new ArcGridReader( new BufferedReader( new
228     // // InputStreamReader( new FileInputStream(file) ) ));
229     // // ArcGridReader reader = new ArcGridReader( file );
230     // // return (GridCoverage2D)reader.read(null);
231 alfonx 244
232     }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26