001    //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/tags/2.1/src/org/deegree/io/arcinfo_raster/ArcInfoTextRasterReader.java $
002    /*----------------    FILE HEADER  ------------------------------------------
003    
004     This file is part of deegree.
005     Copyright (C) 2001-2007 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.io.arcinfo_raster;
044    
045    import java.io.BufferedReader;
046    import java.io.File;
047    import java.io.FileReader;
048    import java.io.IOException;
049    
050    import org.deegree.framework.util.StringTools;
051    import org.deegree.model.coverage.grid.WorldFile;
052    import org.deegree.model.spatialschema.Envelope;
053    import org.deegree.model.spatialschema.GeometryFactory;
054    
055    /**
056     * reads a raster in ArcInfo text format:<br>
057     * <pre>
058     *  ncols         1600
059     *  nrows         1600
060     *  xllcorner     3540000
061     *  yllcorner     5730000
062     *  cellsize      25
063     *  NODATA_value  -9999
064     *  120.4 132.5 99.9 ... 98.32
065     *  122.5 111.6 110.9 ... 88.77
066     *  ...
067     *  234.23 233.4 265.9 ... 334.7
068     * </pre>
069     *
070     * @version $Revision: 7731 $
071     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
072     * @author last edited by: $Author: apoth $
073     *
074     * @version 1.0. $Revision: 7731 $, $Date: 2007-07-05 21:47:51 +0200 (Do, 05 Jul 2007) $
075     *
076     * @since 2.0
077     */
078    public class ArcInfoTextRasterReader {
079        
080        private File file;
081        private WorldFile wf;
082        private int rows = 0;
083        private int cols = 0;
084        private double minx = 0;
085        private double miny = 0;
086        private double res = 0;
087        private double nodata = 0;
088        
089        public ArcInfoTextRasterReader(File file) {
090            this.file = file;
091        }
092        
093        public ArcInfoTextRasterReader(String fileName) {
094            this.file = new File( fileName );
095        }
096        
097        /**
098         * reads metadata from a ArcInfo Grid text file
099         * @return
100         * @throws IOException
101         */
102        public WorldFile readMetadata() throws IOException {
103            
104            if ( wf == null ) {
105                BufferedReader br = new BufferedReader( new FileReader( file ) );
106                
107                // number of raster columns
108                String line = br.readLine();
109                String[] tmp = StringTools.toArray( line, " \t", false );            
110                cols = Integer.parseInt( tmp[1] );
111                
112                // number of raster rows
113                line = br.readLine();
114                tmp = StringTools.toArray( line, " \t", false );
115                rows = Integer.parseInt( tmp[1] );
116                
117                // x-coordinate of left lower corner 
118                line = br.readLine();
119                tmp = StringTools.toArray( line, " \t", false );
120                minx = Double.parseDouble( tmp[1].replace( ',', '.' ) );
121                
122                // y-coordinate of left lower corner
123                line = br.readLine();
124                tmp = StringTools.toArray( line, " \t", false );
125                miny = Double.parseDouble( tmp[1].replace( ',', '.' ) );
126        
127                // raster resolution
128                line = br.readLine();
129                tmp = StringTools.toArray( line, " \t", false );
130                res = Double.parseDouble( tmp[1].replace( ',', '.' ) );
131                
132                // raster resolution
133                line = br.readLine();
134                tmp = StringTools.toArray( line, " \t", false );
135                nodata = Double.parseDouble( tmp[1].replace( ',', '.' ) );
136                
137                br.close();
138                
139                Envelope env = 
140                    GeometryFactory.createEnvelope( minx, miny, minx + res * cols, miny + res * rows, null );
141                wf = new WorldFile( res, res, 0, 0, env );
142                
143            }
144            
145            return wf;
146        }
147        
148        /**
149         * returns the value used by a ArcInfo grid to indicate no data values
150         * @return
151         * @throws IOException
152         */
153        public double getNoDataValue() throws IOException {
154            
155            // ensure that metadata has been read
156            readMetadata();
157            
158            return nodata;
159        }
160        
161        /**
162         * reads data from a ArcInfo Grid text file
163         * @return
164         * @throws IOException
165         */
166        public float[][] readData() throws IOException {
167    
168            // ensure that metadata has been read
169            readMetadata();
170            
171            BufferedReader br = new BufferedReader( new FileReader( file ) );
172            for ( int i = 0; i < 6; i++ ) {
173                // skip first six rows containing metadata
174                br.readLine();
175            }
176            
177            float[][] data = new float[rows][];
178            for ( int i = 0; i < data.length; i++ ) {
179                data[i] = StringTools.toArrayFloat( br.readLine(), " \t" ); 
180            }
181            br.close();
182            
183            return data;
184        }
185    
186    }