001 //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/branches/2.2_testing/src/org/deegree/io/arcinfo_raster/ArcInfoTextRasterReader.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.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: 9342 $
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: 9342 $, $Date: 2007-12-27 13:32:57 +0100 (Do, 27 Dez 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 String[] cornerx = StringTools.toArray( line, " \t", false );
120 minx = Double.parseDouble( cornerx[1].replace( ',', '.' ) );
121
122 // y-coordinate of left lower corner
123 line = br.readLine();
124 String[] cornery = StringTools.toArray( line, " \t", false );
125 miny = Double.parseDouble( cornery[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 if ( cornerx[0].toUpperCase().indexOf( "XLLCORNER" ) > -1 ) {
138 minx = minx + res/2d;
139 }
140 if ( cornery[0].toUpperCase().indexOf( "YLLCORNER" ) > -1 ) {
141 miny = miny + res/2d;
142 }
143
144 br.close();
145
146 Envelope env =
147 GeometryFactory.createEnvelope( minx, miny, minx + res * (cols-1), miny + res * (rows-1), null );
148 wf = new WorldFile( res, res, 0, 0, env );
149
150 }
151
152 return wf;
153 }
154
155 /**
156 * returns the value used by a ArcInfo grid to indicate no data values
157 * @return
158 * @throws IOException
159 */
160 public double getNoDataValue() throws IOException {
161
162 // ensure that metadata has been read
163 readMetadata();
164
165 return nodata;
166 }
167
168 /**
169 * reads data from a ArcInfo Grid text file
170 * @return
171 * @throws IOException
172 */
173 public float[][] readData() throws IOException {
174
175 // ensure that metadata has been read
176 readMetadata();
177
178 BufferedReader br = new BufferedReader( new FileReader( file ) );
179 for ( int i = 0; i < 6; i++ ) {
180 // skip first six rows containing metadata
181 br.readLine();
182 }
183
184 float[][] data = new float[rows][];
185 for ( int i = 0; i < data.length; i++ ) {
186 data[i] = StringTools.toArrayFloat( br.readLine(), " \t" );
187 }
188 br.close();
189
190 return data;
191 }
192
193 }