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