/[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 244 - (hide annotations)
Wed Jul 29 09:33:33 2009 UTC (15 years, 7 months ago) by alfonx
File size: 7632 byte(s)
* Updated all .java and .properties headers with a recent LGPL 3.0 and a link to the project webpage.
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     * classes based on Java 1.6, focussing (not only) on Java Swing
6     * 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     import org.opengis.referencing.crs.CoordinateReferenceSystem;
47    
48     import schmitzm.geotools.io.GeoImportUtil;
49     import schmitzm.io.IOUtil;
50    
51     /**
52     * Erweiterungen von Martin's {@link GeoImportUtil} classe fuer die konsequente Benutzung mit {@link URL}s.
53     * TODO Diese Klasse sollte vielleicht mit der {@link GeoImportUtil} zusammengefuegt werden.
54     *
55     * @author <a href="mailto:[email protected]">Stefan Alfons Kr&uuml;ger</a>
56     *
57     */
58     public class GeoImportUtilURL extends GeoImportUtil {
59     final static private Logger LOGGER = Logger.getLogger(GeoImportUtilURL.class);
60    
61     /**
62     * Read a {@link GridCoverage2D} from an image file. .prj and .wld files are usually expected
63     */
64     public static GridCoverage2D readGridFromImage(URL url) throws IOException {
65     return readGridFromImage(url, null);
66     }
67    
68     /**
69     * 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.
70     *
71     * @author <a href="mailto:[email protected]">Martin Schmitz</a> (University of Bonn/Germany)
72     * @author <a href="mailto:[email protected]">Stefan Alfons Kr&uuml;ger</a>
73     */
74     public static GridCoverage2D readGridFromImage(URL url,
75     CoordinateReferenceSystem crs) throws IOException {
76     GridCoverage2D gc = null;
77    
78     BufferedImage im = ImageIO.read(url);
79     if (im == null)
80     throw new IIOException("No image reader found for this image type!");
81    
82     // World-File einlesen
83     double[] tfwInfo = null;
84    
85     for (WORLD_POSTFIXES pf : WORLD_POSTFIXES.values()){
86     if (tfwInfo == null) {
87     try {
88     tfwInfo = readWorldFile( IOUtil.changeUrlExt(url, pf.toString() ).openStream());
89     } catch (Exception e) {
90     }
91     }
92     }
93    
94     if (tfwInfo == null)
95     throw new IllegalArgumentException(
96     "No georeferencing information found.\n"
97     + "Attach a .wld file to "+url+"please.");
98    
99     float w = (float) (im.getWidth() * tfwInfo[0]); // reale Breite =
100     // RasterSpalten *
101     // hor. Aufloesung
102     float h = (float) (im.getHeight() * (-tfwInfo[3])); // reale Hoehe =
103     // RasterZeilen
104     // * vert.
105     // Aufloesung
106     float x = (float) tfwInfo[4]; // Suedwestliche Ecke!
107     float y = (float) tfwInfo[5] - h; // Suedwestliche Ecke (im
108     // tfw-File steht die
109     // Nordwestliche!)
110     // ggf. Projektion einlesen
111     if (crs == null) {
112     crs = determineProjection( IOUtil.changeUrlExt(url, "prj") );
113     }
114     Envelope2D envelope = new Envelope2D(crs, new Rectangle2D.Float(x, y,
115     w, h));
116     // WICHTIG: Name des Rasters sollte Leer-String sein, da ansonsten
117     // das
118     // Coloring des Rasters nicht klappt.
119     // --> Name der Categories und ColorMapEntries-Labels muss (warum auch immer) mit dem Namen
120     // des Rasters uebereinstimmen!!!
121     gc = new GridCoverageFactory().create("", im, envelope);
122    
123     return gc;
124     }
125    
126     /**
127     * Diese Methode importiert ein Raster aus einer Datei im GeoTIFF-Format.
128     * Zunaechst wird versucht, die Geo-Informationen (Referenz+CRS) aus den
129     * TIFF-Metadaten zu ermitteln. Ist dies nicht erfolgreich, werden ein
130     * gleichnamiges World-File (.tfw) und Projection-File (.prj) herangezogen.
131     * Als Projektion (.prj) ist sowohl ein EPSG-Code "EPSG:...", als auch eine
132     * WKT-Definition erlaubt. Kann kein CRS ermitteln werden, wird
133     * {@link #DEFAULT_CRS} als CRS verwendet.
134     *
135     * @param geotiffURL
136     * URL to GeoTIFF-File
137     * @param crs
138     * erzwungenes CoordinateReferenceSystem fuer das Raster (kann
139     * {@code null} sein)
140     * @throws java.lang.Exception
141     * bei irgendeinem Fehler
142     *
143     * @author <a href="mailto:[email protected]">Martin Schmitz</a>
144     * (University of Bonn/Germany)
145     * @author <a href="mailto:[email protected]">Stefan Alfons Kr&uuml;ger</a>
146     */
147     public static GridCoverage2D readGridFromGeoTiff(URL geotiffURL,
148     CoordinateReferenceSystem crs) throws Exception {
149     GridCoverage2D gc = null;
150     LOGGER.debug("Loading GeoTiff from URL = " + geotiffURL + " now.");
151    
152     // Versuchen Geo-Information aus Tiff zu lesen
153     try {
154    
155     // Wenn CRS angegeben, dieses durch Hint erzwingen
156     Hints hints = new Hints(null);
157    
158     if (crs != null)
159     hints.put(Hints.DEFAULT_COORDINATE_REFERENCE_SYSTEM, crs);
160     // Reader (mit Metadaten) erzeugen
161    
162     LOGGER.debug("First try to create GeoTiff reader");
163    
164     /**
165     * Attention, Attention: The GeoTiffReader fails for URLs like:
166     *
167     * jar:file:/home/sdsd/ad/atlas/raster_textur00949608497.jar!/ad/data/raster_textur00949608497/textur.tif
168     *
169     * It should be converted to something like:
170     * jar:http:///atlas/raster_textur00949608497.jar!/ad/data/raster_textur00949608497/textur.tif
171     * jar:http://www.wikisquare.de/atlas/raster_textur00949608497.jar!/ad/data/raster_textur00949608497/textur.tif
172     */
173     GeoTiffReader reader = new GeoTiffReader(geotiffURL, hints);
174    
175     // Wenn kein Referenzsystem vorhanden, versuchen ein prj-File zu
176     // verwenden
177     if (reader.getOriginalEnvelope().getCoordinateReferenceSystem() == null) {
178     LOGGER
179     .warn("Second try because no projection information found in GeoTIFF. Using prj-file...");
180    
181     final CoordinateReferenceSystem determineProjection = determineProjection(IOUtil.changeUrlExt(
182     geotiffURL, "prj"));
183     hints.put(Hints.DEFAULT_COORDINATE_REFERENCE_SYSTEM,
184     determineProjection);
185     reader = new GeoTiffReader(geotiffURL, hints);
186     }
187     gc = (GridCoverage2D) reader.read(null);
188     LOGGER.debug("... was successfull!");
189     } catch (UnsupportedOperationException err) {
190     LOGGER.debug("... failed.");
191     LOGGER.error(err);
192     LOGGER.warn("No GeoTIFF information found. Using simple image + world- and prj-file...");
193     LOGGER.debug("Second try to use readGridFromImage()");
194     gc = readGridFromImage(geotiffURL, crs); //TODO style
195     }
196     return gc;
197     }
198    
199    
200     }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26