001: //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/trunk/src/org/deegree/tools/srs/TransformRasterFile.java $
002: /*---------------- FILE HEADER ------------------------------------------
003:
004: This file is part of deegree.
005: Copyright (C) 2001-2008 by:
006: EXSE, Department of Geography, University of Bonn
007: http://www.giub.uni-bonn.de/deegree/
008: lat/lon GmbH
009: http://www.lat-lon.de
010:
011: This library is free software; you can redistribute it and/or
012: modify it under the terms of the GNU Lesser General Public
013: License as published by the Free Software Foundation; either
014: version 2.1 of the License, or (at your option) any later version.
015:
016: This library is distributed in the hope that it will be useful,
017: but WITHOUT ANY WARRANTY; without even the implied warranty of
018: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019: Lesser General Public License for more details.
020:
021: You should have received a copy of the GNU Lesser General Public
022: License along with this library; if not, write to the Free Software
023: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
024:
025: Contact:
026:
027: Andreas Poth
028: lat/lon GmbH
029: Aennchenstr. 19
030: 53177 Bonn
031: Germany
032: E-Mail: poth@lat-lon.de
033:
034: Prof. Dr. Klaus Greve
035: Department of Geography
036: University of Bonn
037: Meckenheimer Allee 166
038: 53115 Bonn
039: Germany
040: E-Mail: greve@giub.uni-bonn.de
041:
042: ---------------------------------------------------------------------------*/
043: package org.deegree.tools.srs;
044:
045: import java.awt.image.BufferedImage;
046: import java.util.Properties;
047:
048: import javax.media.jai.InterpolationNearest;
049:
050: import org.deegree.datatypes.CodeList;
051: import org.deegree.framework.util.ImageUtils;
052: import org.deegree.model.coverage.grid.ImageGridCoverage;
053: import org.deegree.model.coverage.grid.WorldFile;
054: import org.deegree.model.crs.CRSFactory;
055: import org.deegree.model.crs.CoordinateSystem;
056: import org.deegree.model.crs.GeoTransformer;
057: import org.deegree.model.spatialschema.Envelope;
058: import org.deegree.ogcwebservices.SupportedFormats;
059: import org.deegree.ogcwebservices.SupportedSRSs;
060: import org.deegree.ogcwebservices.wcs.describecoverage.CoverageOffering;
061: import org.deegree.ogcwebservices.wcs.describecoverage.DomainSet;
062: import org.deegree.ogcwebservices.wcs.describecoverage.RangeSet;
063: import org.deegree.ogcwebservices.wcs.describecoverage.SpatialDomain;
064:
065: /**
066: * Tool to transform raster files from one CRS to another.
067: *
068: * @author <a href="mailto:tonnhofer@lat-lon.de">Oliver Tonnhofer</a>
069: * @author last edited by: $Author: apoth $
070: *
071: * @version $Revision: 10205 $, $Date: 2008-02-20 08:29:26 -0800 (Wed, 20 Feb 2008) $
072: */
073: public class TransformRasterFile {
074:
075: private final static float DEFAULT_IMAGE_QUALITY = 0.9f;
076:
077: private final static int DEFAULT_PP_GRID_SIZE = 5;
078:
079: private final static int DEFAULT_POLY_ORDER = 3;
080:
081: private static void transformRasterFile(String inFile,
082: String inCRS, String outFile, String outCRS,
083: Float imageQuality, int ppgridsize, int polynomOrder)
084: throws Exception {
085:
086: System.out.println("Loading raster " + inFile);
087: BufferedImage image = ImageUtils.loadImage(inFile);
088:
089: CoordinateSystem sourceCRS = CRSFactory.create(inCRS);
090: CoordinateSystem targetCRS = CRSFactory.create(outCRS);
091:
092: WorldFile worldFile = WorldFile.readWorldFile(inFile,
093: WorldFile.TYPE.CENTER, image);
094: Envelope inEnvelope = worldFile.getEnvelope();
095:
096: // create minimal CoverageOffering for ImageGridCoverage
097: // most parts are not used
098: DomainSet ds = new DomainSet(new SpatialDomain(
099: new Envelope[] { inEnvelope }));
100: RangeSet rs = new RangeSet("", "");
101: CodeList[] dummyCodeList = new CodeList[] { new CodeList("",
102: new String[] {}) };
103: CodeList[] nativeSRSCodeList = new CodeList[] { new CodeList(
104: "", new String[] { inCRS }) };
105:
106: SupportedSRSs supSRSs = new SupportedSRSs(dummyCodeList,
107: dummyCodeList, dummyCodeList, nativeSRSCodeList);
108:
109: SupportedFormats supFormats = new SupportedFormats(
110: dummyCodeList);
111:
112: CoverageOffering coverageOffering = new CoverageOffering("",
113: "", "", null, null, null, ds, rs, supSRSs, supFormats,
114: null, null);
115:
116: ImageGridCoverage igc = new ImageGridCoverage(coverageOffering,
117: inEnvelope, image);
118:
119: GeoTransformer gt = new GeoTransformer(targetCRS);
120:
121: Envelope outEnvelope = gt
122: .transform(inEnvelope, sourceCRS, true);
123:
124: // calculate new output size
125: // use square pixels for output, ie. the aspect ratio changes
126: double deltaX = outEnvelope.getWidth();
127: double deltaY = outEnvelope.getHeight();
128: double diagSize = Math.sqrt(deltaX * deltaX + deltaY * deltaY);
129: // pixelSize for calculation of the new image size
130: double pixelSize = diagSize
131: / Math.sqrt(Math.pow(image.getWidth(), 2)
132: + Math.pow(image.getHeight(), 2));
133: int height = (int) (deltaY / pixelSize + 0.5);
134: int width = (int) (deltaX / pixelSize + 0.5);
135: // realPixelSize for center type world files, etc.
136: double realPixelSize = diagSize
137: / Math.sqrt(Math.pow(image.getWidth() - 1, 2)
138: + Math.pow(image.getHeight() - 1, 2));
139:
140: System.out.println("Transforming raster from " + inCRS + " to "
141: + outCRS);
142: igc = (ImageGridCoverage) gt.transform(igc, outEnvelope, width,
143: height, ppgridsize, polynomOrder,
144: new InterpolationNearest());
145:
146: image = igc.getAsImage(-1, -1);
147:
148: System.out.println("Saving raster " + outFile);
149: ImageUtils.saveImage(image, outFile, imageQuality);
150:
151: // save new WorldFile
152: WorldFile outWorldFile = new WorldFile(realPixelSize,
153: realPixelSize, 0.0f, 0.0f, outEnvelope);
154: String basename = outFile
155: .substring(0, outFile.lastIndexOf("."));
156: WorldFile.writeWorldFile(outWorldFile, basename);
157:
158: }
159:
160: private static void printHelpAndExit() {
161: System.out
162: .println("Usage: java [...] org.deegree.tools.srs.TransformRasterFile ");
163: System.out
164: .println(" -inFile filename -inCRS crs ");
165: System.out
166: .println(" [-outFile filename] -outCRS crs");
167: System.out.println(" [-imageQuality 0.X]");
168: System.out
169: .println(" -passpointGridSize 5 -polynomOrder 3 ");
170: System.exit(1);
171: }
172:
173: /**
174: * @param args
175: */
176: public static void main(String[] args) {
177:
178: if (args.length % 2 != 0)
179: printHelpAndExit();
180:
181: Properties map = new Properties();
182: for (int i = 0; i < args.length; i += 2) {
183: map.put(args[i], args[i + 1]);
184: }
185:
186: String outCRS = (String) map.get("-outCRS");
187: if (outCRS == null)
188: printHelpAndExit();
189:
190: String inCRS = (String) map.get("-inCRS");
191: if (inCRS == null)
192: printHelpAndExit();
193:
194: String inFilename = (String) map.get("-inFile");
195: if (inFilename == null)
196: printHelpAndExit();
197:
198: String outFilename = (String) map.get("-outFile");
199: if (outFilename == null) {
200: String ext = inFilename.substring(inFilename
201: .lastIndexOf("."));
202: outFilename = inFilename.substring(0, inFilename
203: .lastIndexOf("."));
204: outFilename = outFilename + "." + outCRS + ext;
205: }
206:
207: String imageQualityString = (String) map.get("-imageQuality");
208: float imageQuality = DEFAULT_IMAGE_QUALITY;
209: if (imageQualityString != null) {
210: imageQuality = Float.valueOf(imageQualityString);
211: }
212: String ppgridsizeString = (String) map
213: .get("-passpointGridSize");
214: int ppgridsize = DEFAULT_PP_GRID_SIZE;
215: if (ppgridsizeString != null) {
216: ppgridsize = Integer.parseInt(ppgridsizeString);
217: }
218:
219: String polynomOrderString = (String) map.get("-polynomOrder");
220: int polynomOrder = DEFAULT_POLY_ORDER;
221: if (polynomOrderString != null) {
222: polynomOrder = Integer.parseInt(polynomOrderString);
223: }
224:
225: try {
226: transformRasterFile(inFilename, inCRS, outFilename, outCRS,
227: imageQuality, ppgridsize, polynomOrder);
228: } catch (Exception e) {
229: e.printStackTrace();
230: }
231: }
232:
233: }
|