/[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 346 - (hide annotations)
Mon Aug 31 11:37:01 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: 9643 byte(s)
* ArcASCII mit GT neu
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 alfonx 346 import java.io.File;
35 alfonx 244 import java.io.IOException;
36     import java.net.URL;
37    
38     import javax.imageio.IIOException;
39     import javax.imageio.ImageIO;
40    
41     import org.apache.log4j.Logger;
42     import org.geotools.coverage.grid.GridCoverage2D;
43     import org.geotools.coverage.grid.GridCoverageFactory;
44     import org.geotools.factory.Hints;
45 alfonx 346 import org.geotools.gce.arcgrid.ArcGridReader;
46 alfonx 244 import org.geotools.gce.geotiff.GeoTiffReader;
47     import org.geotools.geometry.Envelope2D;
48     import org.opengis.referencing.crs.CoordinateReferenceSystem;
49    
50     import schmitzm.geotools.io.GeoImportUtil;
51     import schmitzm.io.IOUtil;
52    
53     /**
54     * Erweiterungen von Martin's {@link GeoImportUtil} classe fuer die konsequente Benutzung mit {@link URL}s.
55     * TODO Diese Klasse sollte vielleicht mit der {@link GeoImportUtil} zusammengefuegt werden.
56     *
57     * @author <a href="mailto:[email protected]">Stefan Alfons Kr&uuml;ger</a>
58     *
59     */
60     public class GeoImportUtilURL extends GeoImportUtil {
61     final static private Logger LOGGER = Logger.getLogger(GeoImportUtilURL.class);
62    
63     /**
64     * Read a {@link GridCoverage2D} from an image file. .prj and .wld files are usually expected
65     */
66     public static GridCoverage2D readGridFromImage(URL url) throws IOException {
67     return readGridFromImage(url, null);
68     }
69    
70     /**
71     * Read a {@link GridCoverage2D} from an image file. .wld file is usually expected also. The CRS can be given as the crs parameter. null is valid.
72     *
73     * @author <a href="mailto:[email protected]">Martin Schmitz</a> (University of Bonn/Germany)
74     * @author <a href="mailto:[email protected]">Stefan Alfons Kr&uuml;ger</a>
75     */
76     public static GridCoverage2D readGridFromImage(URL url,
77     CoordinateReferenceSystem crs) throws IOException {
78     GridCoverage2D gc = null;
79    
80     BufferedImage im = ImageIO.read(url);
81     if (im == null)
82     throw new IIOException("No image reader found for this image type!");
83    
84     // World-File einlesen
85     double[] tfwInfo = null;
86    
87     for (WORLD_POSTFIXES pf : WORLD_POSTFIXES.values()){
88     if (tfwInfo == null) {
89     try {
90     tfwInfo = readWorldFile( IOUtil.changeUrlExt(url, pf.toString() ).openStream());
91     } catch (Exception e) {
92     }
93     }
94     }
95    
96     if (tfwInfo == null)
97     throw new IllegalArgumentException(
98     "No georeferencing information found.\n"
99     + "Attach a .wld file to "+url+"please.");
100    
101     float w = (float) (im.getWidth() * tfwInfo[0]); // reale Breite =
102     // RasterSpalten *
103     // hor. Aufloesung
104     float h = (float) (im.getHeight() * (-tfwInfo[3])); // reale Hoehe =
105     // RasterZeilen
106     // * vert.
107     // Aufloesung
108     float x = (float) tfwInfo[4]; // Suedwestliche Ecke!
109     float y = (float) tfwInfo[5] - h; // Suedwestliche Ecke (im
110     // tfw-File steht die
111     // Nordwestliche!)
112     // ggf. Projektion einlesen
113     if (crs == null) {
114     crs = determineProjection( IOUtil.changeUrlExt(url, "prj") );
115     }
116     Envelope2D envelope = new Envelope2D(crs, new Rectangle2D.Float(x, y,
117     w, h));
118     // WICHTIG: Name des Rasters sollte Leer-String sein, da ansonsten
119     // das
120     // Coloring des Rasters nicht klappt.
121     // --> Name der Categories und ColorMapEntries-Labels muss (warum auch immer) mit dem Namen
122     // des Rasters uebereinstimmen!!!
123     gc = new GridCoverageFactory().create("", im, envelope);
124    
125     return gc;
126     }
127    
128     /**
129     * Diese Methode importiert ein Raster aus einer Datei im GeoTIFF-Format.
130     * Zunaechst wird versucht, die Geo-Informationen (Referenz+CRS) aus den
131     * TIFF-Metadaten zu ermitteln. Ist dies nicht erfolgreich, werden ein
132     * gleichnamiges World-File (.tfw) und Projection-File (.prj) herangezogen.
133     * Als Projektion (.prj) ist sowohl ein EPSG-Code "EPSG:...", als auch eine
134     * WKT-Definition erlaubt. Kann kein CRS ermitteln werden, wird
135     * {@link #DEFAULT_CRS} als CRS verwendet.
136     *
137     * @param geotiffURL
138     * URL to GeoTIFF-File
139     * @param crs
140     * erzwungenes CoordinateReferenceSystem fuer das Raster (kann
141     * {@code null} sein)
142     * @throws java.lang.Exception
143     * bei irgendeinem Fehler
144     *
145     * @author <a href="mailto:[email protected]">Martin Schmitz</a>
146     * (University of Bonn/Germany)
147     * @author <a href="mailto:[email protected]">Stefan Alfons Kr&uuml;ger</a>
148     */
149     public static GridCoverage2D readGridFromGeoTiff(URL geotiffURL,
150     CoordinateReferenceSystem crs) throws Exception {
151     GridCoverage2D gc = null;
152     LOGGER.debug("Loading GeoTiff from URL = " + geotiffURL + " now.");
153    
154     // Versuchen Geo-Information aus Tiff zu lesen
155     try {
156    
157     // Wenn CRS angegeben, dieses durch Hint erzwingen
158 alfonx 323 Hints hints = new Hints();
159 alfonx 244
160     if (crs != null)
161     hints.put(Hints.DEFAULT_COORDINATE_REFERENCE_SYSTEM, crs);
162     // Reader (mit Metadaten) erzeugen
163    
164     LOGGER.debug("First try to create GeoTiff reader");
165    
166     /**
167     * Attention, Attention: The GeoTiffReader fails for URLs like:
168     *
169     * jar:file:/home/sdsd/ad/atlas/raster_textur00949608497.jar!/ad/data/raster_textur00949608497/textur.tif
170     *
171     * It should be converted to something like:
172     * jar:http:///atlas/raster_textur00949608497.jar!/ad/data/raster_textur00949608497/textur.tif
173     * jar:http://www.wikisquare.de/atlas/raster_textur00949608497.jar!/ad/data/raster_textur00949608497/textur.tif
174     */
175     GeoTiffReader reader = new GeoTiffReader(geotiffURL, hints);
176    
177     // Wenn kein Referenzsystem vorhanden, versuchen ein prj-File zu
178     // verwenden
179     if (reader.getOriginalEnvelope().getCoordinateReferenceSystem() == null) {
180     LOGGER
181     .warn("Second try because no projection information found in GeoTIFF. Using prj-file...");
182    
183     final CoordinateReferenceSystem determineProjection = determineProjection(IOUtil.changeUrlExt(
184     geotiffURL, "prj"));
185     hints.put(Hints.DEFAULT_COORDINATE_REFERENCE_SYSTEM,
186     determineProjection);
187     reader = new GeoTiffReader(geotiffURL, hints);
188     }
189     gc = (GridCoverage2D) reader.read(null);
190     LOGGER.debug("... was successfull!");
191     } catch (UnsupportedOperationException err) {
192     LOGGER.debug("... failed.");
193     LOGGER.error(err);
194     LOGGER.warn("No GeoTIFF information found. Using simple image + world- and prj-file...");
195     LOGGER.debug("Second try to use readGridFromImage()");
196     gc = readGridFromImage(geotiffURL, crs); //TODO style
197     }
198     return gc;
199     }
200    
201 alfonx 346 //
202     // ArcGridRaster reader = new ArcGridRaster(new BufferedReader(
203     // new InputStreamReader(url.openStream())), false);
204     // WritableRaster raster = reader.readRaster();
205     // if (crs == null)
206     // crs = determineProjection(IOUtil.changeUrlExt(url, "prj"));
207     //
208     // LOGGER.debug("Position " + reader.getXlCorner() + " / "
209     // + reader.getYlCorner());
210     // LOGGER.debug("Size " + reader.getNCols() + " / "
211     // + reader.getNRows());
212     // LOGGER.debug("Min/Max " + reader.getMinValue() + " / "
213     // + reader.getMaxValue());
214     // LOGGER.debug("NoData " + reader.getNoData());
215     // LOGGER.debug("CellSize " + reader.getCellSize());
216     //
217     // Double x = reader.getXlCorner(); // Suedwestliche Ecke!
218     // Double y = reader.getYlCorner(); // Suedwestliche Ecke!
219     // if (y.isNaN() || x.isNaN()) {
220     // throw new IllegalArgumentException(
221     // "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.");
222     // }
223     //
224     // Double w = (reader.getNCols() * reader.getCellSize()); // reale Breite =
225     // // RasterSpalten
226     // // * Aufloesung
227     // Double h = (reader.getNRows() * reader.getCellSize()); // reale Hoehe =
228     // // RasterZeilen
229     // // * Aufloesung
230     // Envelope2D envelope = new Envelope2D(crs, new Rectangle2D.Double(x, y,
231     // w, h));
232     //
233     // // WICHTIG: Name des Rasters sollte Leer-String sein, da ansonsten das
234     // // Coloring des Rasters nicht klappt.
235     // // --> Name der Categories muss (warum auch immer) mit dem Namen
236     // // des Rasters uebereinstimmen!!!
237     // return new GridCoverageFactory().create("", raster, envelope);
238     // //
239     // // === OHNE ArcGridRaster ===
240     // // // ArcGridReader reader = new ArcGridReader( new BufferedReader( new
241     // // InputStreamReader( new FileInputStream(file) ) ));
242     // // ArcGridReader reader = new ArcGridReader( file );
243     // // return (GridCoverage2D)reader.read(null);
244    
245 alfonx 244
246 alfonx 346
247 alfonx 244 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26