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 |
alfonx |
862 |
* Stefan A. Tzeggai - additional utility classes |
29 |
alfonx |
244 |
******************************************************************************/ |
30 |
|
|
package skrueger.geotools.io; |
31 |
|
|
|
32 |
|
|
import java.awt.geom.Rectangle2D; |
33 |
|
|
import java.awt.image.BufferedImage; |
34 |
alfonx |
382 |
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 |
|
|
import org.geotools.gce.geotiff.GeoTiffReader; |
46 |
|
|
import org.geotools.geometry.Envelope2D; |
47 |
alfonx |
377 |
import org.opengis.parameter.GeneralParameterValue; |
48 |
alfonx |
244 |
import org.opengis.referencing.crs.CoordinateReferenceSystem; |
49 |
|
|
|
50 |
|
|
import schmitzm.geotools.io.GeoImportUtil; |
51 |
|
|
import schmitzm.io.IOUtil; |
52 |
|
|
|
53 |
|
|
/** |
54 |
alfonx |
377 |
* Erweiterungen von Martin's {@link GeoImportUtil} classe fuer die konsequente |
55 |
|
|
* Benutzung mit {@link URL}s. TODO Diese Klasse sollte vielleicht mit der |
56 |
|
|
* {@link GeoImportUtil} zusammengefuegt werden. |
57 |
|
|
* |
58 |
alfonx |
888 |
* @author <a href="mailto:[email protected]">Stefan Alfons Tzeggai</a> |
59 |
alfonx |
377 |
* |
60 |
alfonx |
244 |
*/ |
61 |
|
|
public class GeoImportUtilURL extends GeoImportUtil { |
62 |
alfonx |
377 |
final static private Logger LOGGER = Logger |
63 |
|
|
.getLogger(GeoImportUtilURL.class); |
64 |
alfonx |
244 |
|
65 |
|
|
/** |
66 |
alfonx |
377 |
* Read a {@link GridCoverage2D} from an image file. .prj and .wld files are |
67 |
|
|
* usually expected |
68 |
alfonx |
244 |
*/ |
69 |
|
|
public static GridCoverage2D readGridFromImage(URL url) throws IOException { |
70 |
|
|
return readGridFromImage(url, null); |
71 |
|
|
} |
72 |
|
|
|
73 |
|
|
/** |
74 |
alfonx |
377 |
* Read a {@link GridCoverage2D} from an image file. .wld file is usually |
75 |
|
|
* expected also. The CRS can be given as the crs parameter. null is valid. |
76 |
|
|
* |
77 |
|
|
* @author <a href="mailto:[email protected]">Martin Schmitz</a> |
78 |
|
|
* (University of Bonn/Germany) |
79 |
|
|
* @author <a href="mailto:[email protected]">Stefan Alfons |
80 |
alfonx |
888 |
* Tzeggai</a> |
81 |
alfonx |
244 |
*/ |
82 |
|
|
public static GridCoverage2D readGridFromImage(URL url, |
83 |
|
|
CoordinateReferenceSystem crs) throws IOException { |
84 |
|
|
GridCoverage2D gc = null; |
85 |
|
|
|
86 |
|
|
BufferedImage im = ImageIO.read(url); |
87 |
|
|
if (im == null) |
88 |
|
|
throw new IIOException("No image reader found for this image type!"); |
89 |
|
|
|
90 |
|
|
// World-File einlesen |
91 |
|
|
double[] tfwInfo = null; |
92 |
|
|
|
93 |
alfonx |
377 |
for (WORLD_POSTFIXES pf : WORLD_POSTFIXES.values()) { |
94 |
alfonx |
244 |
if (tfwInfo == null) { |
95 |
|
|
try { |
96 |
alfonx |
377 |
tfwInfo = readWorldFile(IOUtil.changeUrlExt(url, |
97 |
|
|
pf.toString()).openStream()); |
98 |
alfonx |
244 |
} catch (Exception e) { |
99 |
|
|
} |
100 |
|
|
} |
101 |
|
|
} |
102 |
|
|
|
103 |
|
|
if (tfwInfo == null) |
104 |
|
|
throw new IllegalArgumentException( |
105 |
|
|
"No georeferencing information found.\n" |
106 |
alfonx |
377 |
+ "Attach a .wld file to " + url + "please."); |
107 |
alfonx |
244 |
|
108 |
|
|
float w = (float) (im.getWidth() * tfwInfo[0]); // reale Breite = |
109 |
|
|
// RasterSpalten * |
110 |
|
|
// hor. Aufloesung |
111 |
|
|
float h = (float) (im.getHeight() * (-tfwInfo[3])); // reale Hoehe = |
112 |
|
|
// RasterZeilen |
113 |
|
|
// * vert. |
114 |
|
|
// Aufloesung |
115 |
|
|
float x = (float) tfwInfo[4]; // Suedwestliche Ecke! |
116 |
|
|
float y = (float) tfwInfo[5] - h; // Suedwestliche Ecke (im |
117 |
|
|
// tfw-File steht die |
118 |
|
|
// Nordwestliche!) |
119 |
|
|
// ggf. Projektion einlesen |
120 |
|
|
if (crs == null) { |
121 |
alfonx |
377 |
crs = determineProjection(IOUtil.changeUrlExt(url, "prj")); |
122 |
alfonx |
244 |
} |
123 |
|
|
Envelope2D envelope = new Envelope2D(crs, new Rectangle2D.Float(x, y, |
124 |
|
|
w, h)); |
125 |
|
|
// WICHTIG: Name des Rasters sollte Leer-String sein, da ansonsten |
126 |
|
|
// das |
127 |
|
|
// Coloring des Rasters nicht klappt. |
128 |
alfonx |
377 |
// --> Name der Categories und ColorMapEntries-Labels muss (warum auch |
129 |
|
|
// immer) mit dem Namen |
130 |
alfonx |
244 |
// des Rasters uebereinstimmen!!! |
131 |
|
|
gc = new GridCoverageFactory().create("", im, envelope); |
132 |
|
|
|
133 |
|
|
return gc; |
134 |
|
|
} |
135 |
|
|
|
136 |
|
|
/** |
137 |
alfonx |
377 |
* @param input |
138 |
|
|
* URL, File oder InputStream to a GeoTIFF-File |
139 |
alfonx |
244 |
* @param crs |
140 |
|
|
* erzwungenes CoordinateReferenceSystem fuer das Raster (kann |
141 |
|
|
* {@code null} sein) |
142 |
alfonx |
377 |
* |
143 |
alfonx |
244 |
* @author <a href="mailto:[email protected]">Martin Schmitz</a> |
144 |
|
|
* (University of Bonn/Germany) |
145 |
alfonx |
377 |
* @author <a href="mailto:[email protected]">Stefan Alfons |
146 |
alfonx |
888 |
* Tzeggai</a> |
147 |
alfonx |
377 |
* |
148 |
|
|
* @param readParams |
149 |
|
|
* May be {@link NullPointerException}. Otherwise an array of |
150 |
|
|
* {@link GeneralParameterValue}s |
151 |
alfonx |
244 |
*/ |
152 |
alfonx |
377 |
public static GridCoverage2D readGridFromGeoTiff(Object input, |
153 |
|
|
CoordinateReferenceSystem crs, GeneralParameterValue[] readParams) |
154 |
|
|
throws IOException { |
155 |
alfonx |
244 |
GridCoverage2D gc = null; |
156 |
alfonx |
377 |
LOGGER.debug("Loading GeoTiff from URL = " + input + " now."); |
157 |
alfonx |
244 |
|
158 |
|
|
// Versuchen Geo-Information aus Tiff zu lesen |
159 |
|
|
|
160 |
alfonx |
377 |
// Wenn CRS angegeben, dieses durch Hint erzwingen |
161 |
|
|
Hints hints = new Hints(); |
162 |
alfonx |
244 |
|
163 |
alfonx |
377 |
if (crs != null) |
164 |
|
|
hints.put(Hints.DEFAULT_COORDINATE_REFERENCE_SYSTEM, crs); |
165 |
|
|
// Reader (mit Metadaten) erzeugen |
166 |
alfonx |
244 |
|
167 |
alfonx |
377 |
LOGGER.debug("First try to create GeoTiff reader"); |
168 |
alfonx |
244 |
|
169 |
alfonx |
377 |
/** |
170 |
|
|
* Attention, Attention: The GeoTiffReader fails for URLs like: |
171 |
|
|
* |
172 |
|
|
* jar:file:/home/sdsd/ad/atlas/raster_textur00949608497.jar!/ad/data/ |
173 |
|
|
* raster_textur00949608497/textur.tif |
174 |
|
|
* |
175 |
|
|
* It should be converted to something like: |
176 |
|
|
* jar:http:///atlas/raster_textur00949608497 |
177 |
|
|
* .jar!/ad/data/raster_textur00949608497/textur.tif |
178 |
|
|
* jar:http://www.wikisquare |
179 |
|
|
* .de/atlas/raster_textur00949608497.jar!/ad/data |
180 |
|
|
* /raster_textur00949608497/textur.tif |
181 |
|
|
*/ |
182 |
alfonx |
244 |
|
183 |
alfonx |
382 |
try { |
184 |
|
|
|
185 |
|
|
GeoTiffReader reader = new GeoTiffReader(input, hints); |
186 |
|
|
|
187 |
|
|
gc = (GridCoverage2D) reader.read(readParams); |
188 |
|
|
LOGGER.debug("... was successfull!"); |
189 |
|
|
return gc; |
190 |
|
|
} catch (Exception e) { |
191 |
|
|
// keine Geo-Informationen in Tiff |
192 |
|
|
// --> Raster ueber ImageIO einlesen und WorldFile/ProjectionFile |
193 |
|
|
// verwenden |
194 |
|
|
LOGGER.warn("No geo information found in '" + input |
195 |
|
|
+ "'. Using world- and prj-file..."); |
196 |
|
|
|
197 |
|
|
BufferedImage im = null; |
198 |
|
|
double[] tfwInfo = null ; |
199 |
|
|
if (input instanceof File) { |
200 |
|
|
im = ImageIO.read((File) input); |
201 |
|
|
tfwInfo = readWorldFile(IOUtil.changeFileExt((File)input, "tfw")); |
202 |
|
|
if (crs == null) |
203 |
|
|
crs = determineProjection((File)input); |
204 |
|
|
} else if (input instanceof URL) { |
205 |
|
|
tfwInfo = readWorldFile(IOUtil.changeUrlExt((URL)input, "tfw").openStream()); |
206 |
|
|
im = ImageIO.read((URL) input); |
207 |
|
|
if (crs == null) |
208 |
|
|
crs = determineProjection((URL)input); |
209 |
|
|
} |
210 |
|
|
|
211 |
|
|
if (im == null) |
212 |
|
|
throw new IIOException( |
213 |
|
|
"No image reader found for this image type!"); |
214 |
|
|
// World-File einlesen |
215 |
|
|
|
216 |
|
|
float w = (float) (im.getWidth() * tfwInfo[0]); // reale Breite = |
217 |
|
|
// RasterSpalten * hor. |
218 |
|
|
// Aufloesung |
219 |
|
|
float h = (float) (im.getHeight() * (-tfwInfo[3])); // reale Hoehe = |
220 |
|
|
// RasterZeilen * |
221 |
|
|
// vert. Aufloesung |
222 |
|
|
float x = (float) tfwInfo[4]; // Suedwestliche Ecke! |
223 |
|
|
float y = (float) tfwInfo[5] - h; // Suedwestliche Ecke (im tfw-File |
224 |
|
|
// steht die Nordwestliche!) |
225 |
|
|
// ggf. Projektion einlesen |
226 |
|
|
Envelope2D envelope = new Envelope2D(crs, new Rectangle2D.Float(x, |
227 |
|
|
y, w, h)); |
228 |
|
|
// WICHTIG: Name des Rasters sollte Leer-String sein, da ansonsten |
229 |
|
|
// das |
230 |
|
|
// Coloring des Rasters nicht klappt. |
231 |
|
|
// --> Name der Categories muss (warum auch immer) mit dem Namen |
232 |
|
|
// des Rasters uebereinstimmen!!! |
233 |
|
|
gc = new GridCoverageFactory().create("", im.copyData(null), |
234 |
|
|
envelope); |
235 |
|
|
return gc; |
236 |
|
|
} |
237 |
alfonx |
244 |
} |
238 |
|
|
|
239 |
alfonx |
377 |
// |
240 |
|
|
// ArcGridRaster reader = new ArcGridRaster(new BufferedReader( |
241 |
|
|
// new InputStreamReader(url.openStream())), false); |
242 |
|
|
// WritableRaster raster = reader.readRaster(); |
243 |
|
|
// if (crs == null) |
244 |
|
|
// crs = determineProjection(IOUtil.changeUrlExt(url, "prj")); |
245 |
|
|
// |
246 |
|
|
// LOGGER.debug("Position " + reader.getXlCorner() + " / " |
247 |
|
|
// + reader.getYlCorner()); |
248 |
|
|
// LOGGER.debug("Size " + reader.getNCols() + " / " |
249 |
|
|
// + reader.getNRows()); |
250 |
|
|
// LOGGER.debug("Min/Max " + reader.getMinValue() + " / " |
251 |
|
|
// + reader.getMaxValue()); |
252 |
|
|
// LOGGER.debug("NoData " + reader.getNoData()); |
253 |
|
|
// LOGGER.debug("CellSize " + reader.getCellSize()); |
254 |
|
|
// |
255 |
|
|
// Double x = reader.getXlCorner(); // Suedwestliche Ecke! |
256 |
|
|
// Double y = reader.getYlCorner(); // Suedwestliche Ecke! |
257 |
|
|
// if (y.isNaN() || x.isNaN()) { |
258 |
|
|
// throw new IllegalArgumentException( |
259 |
|
|
// "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."); |
260 |
|
|
// } |
261 |
|
|
// |
262 |
|
|
// Double w = (reader.getNCols() * reader.getCellSize()); // reale Breite = |
263 |
|
|
// // RasterSpalten |
264 |
|
|
// // * Aufloesung |
265 |
|
|
// Double h = (reader.getNRows() * reader.getCellSize()); // reale Hoehe = |
266 |
|
|
// // RasterZeilen |
267 |
|
|
// // * Aufloesung |
268 |
|
|
// Envelope2D envelope = new Envelope2D(crs, new Rectangle2D.Double(x, y, |
269 |
|
|
// w, h)); |
270 |
|
|
// |
271 |
|
|
// // WICHTIG: Name des Rasters sollte Leer-String sein, da ansonsten das |
272 |
|
|
// // Coloring des Rasters nicht klappt. |
273 |
|
|
// // --> Name der Categories muss (warum auch immer) mit dem Namen |
274 |
|
|
// // des Rasters uebereinstimmen!!! |
275 |
|
|
// return new GridCoverageFactory().create("", raster, envelope); |
276 |
|
|
// // |
277 |
|
|
// // === OHNE ArcGridRaster === |
278 |
|
|
// // // ArcGridReader reader = new ArcGridReader( new BufferedReader( new |
279 |
|
|
// // InputStreamReader( new FileInputStream(file) ) )); |
280 |
|
|
// // ArcGridReader reader = new ArcGridReader( file ); |
281 |
|
|
// // return (GridCoverage2D)reader.read(null); |
282 |
alfonx |
244 |
|
283 |
alfonx |
1131 |
|
284 |
|
|
|
285 |
alfonx |
244 |
} |