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