001    //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.3_testing/src/org/deegree/tools/raster/ArcInfo2xyz.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.raster;
037    
038    import java.io.BufferedReader;
039    import java.io.File;
040    import java.io.FileReader;
041    import java.io.PrintWriter;
042    import java.util.Properties;
043    
044    import org.deegree.framework.util.StringTools;
045    import org.deegree.io.arcinfo_raster.ArcInfoTextRasterReader;
046    import org.deegree.model.coverage.grid.WorldFile;
047    import org.deegree.model.spatialschema.Envelope;
048    
049    /**
050     * converts an ArcInfor raster file (text format):
051     *
052     * <pre>
053     *  ncols         2404
054     *  nrows         2307
055     *  xllcorner     2627130
056     *  yllcorner     5686612
057     *  cellsize      10
058     *  NODATA_value  -9999
059     *  20636 20593 20569 20573 20571 20564 20564 20558 ...
060     *  ...
061     * </pre>
062     *
063     * into its XYZ representation:
064     *
065     * <pre>
066     * 2627130.0 5709682.0 206.36
067     * 2627140.0 5709682.0 205.93
068     * 2627150.0 5709682.0 205.69
069     * 2627160.0 5709682.0 205.73
070     * 2627170.0 5709682.0 205.71
071     * 2627180.0 5709682.0 205.64
072     * 2627190.0 5709682.0 205.64
073     * 2627200.0 5709682.0 205.58
074     * </pre>
075     *
076     *
077     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
078     * @author last edited by: $Author: mschneider $
079     *
080     * @version $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18. Jun 2009) $
081     */
082    public class ArcInfo2xyz {
083    
084        /**
085         * @param args
086         * @throws Exception
087         */
088        public static void main( String[] args )
089                                throws Exception {
090    
091            Properties map = new Properties();
092            for ( int i = 0; i < args.length; i += 2 ) {
093                map.put( args[i], args[i + 1] );
094            }
095            File arcInfo = new File( map.getProperty( "-inFile" ) );
096            File xyz = new File( map.getProperty( "-outFile" ) );
097    
098            ArcInfoTextRasterReader reader = new ArcInfoTextRasterReader( arcInfo );
099            WorldFile wf = reader.readMetadata();
100            Envelope env = wf.getEnvelope();
101            double res = wf.getResx();
102    
103            BufferedReader in = null;
104            PrintWriter pw = null;
105            try {
106    
107                in = new BufferedReader( new FileReader( arcInfo ) );
108                for ( int i = 0; i < 6; i++ ) {
109                    // skip header
110                    in.readLine();
111                }
112    
113                pw = new PrintWriter( xyz );
114    
115                String line = null;
116                double y = env.getMax().getY();
117                while ( ( line = in.readLine() ) != null ) {
118                    System.out.print( y + "\r" );
119                    float[] data = StringTools.toArrayFloat( line, " \t" );
120                    double x = env.getMin().getX();
121                    for ( int i = 0; i < data.length; i++ ) {
122                        pw.print( x );
123                        pw.print( ' ' );
124                        pw.print( y );
125                        pw.print( ' ' );
126                        pw.print( data[i] );
127                        pw.println();
128                        x = x + res;
129                    }
130                    y = y - res;
131                }
132            } catch ( Exception e ) {
133                throw e;
134            } finally {
135                pw.close();
136                in.close();
137            }
138    
139        }
140    
141    }