001 //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.4_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: 22547 $
065 * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
066 * @author last edited by: $Author: aschmitz $
067 *
068 * @version 1.0. $Revision: 22547 $, $Date: 2010-02-16 10:22:39 +0100 (Di, 16. Feb 2010) $
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 * @return worldfile for the text grid file
106 * @throws IOException
107 */
108 public WorldFile readMetadata()
109 throws IOException {
110 return readMetadata( false );
111 }
112
113 /**
114 * reads metadata from a ArcInfo Grid text file
115 *
116 * @param center
117 *
118 * @return worldfile for the text grid file
119 * @throws IOException
120 */
121 public WorldFile readMetadata( boolean center )
122 throws IOException {
123
124 if ( wf == null ) {
125 BufferedReader br = new BufferedReader( new FileReader( file ) );
126
127 // number of raster columns
128 String line = br.readLine();
129 String[] tmp = StringTools.toArray( line, " \t", false );
130 cols = Integer.parseInt( tmp[1] );
131
132 // number of raster rows
133 line = br.readLine();
134 tmp = StringTools.toArray( line, " \t", false );
135 rows = Integer.parseInt( tmp[1] );
136
137 // x-coordinate of left lower corner
138 line = br.readLine();
139 String[] cornerx = StringTools.toArray( line, " \t", false );
140 minx = Double.parseDouble( cornerx[1].replace( ',', '.' ) );
141
142 // y-coordinate of left lower corner
143 line = br.readLine();
144 String[] cornery = StringTools.toArray( line, " \t", false );
145 miny = Double.parseDouble( cornery[1].replace( ',', '.' ) );
146
147 // raster resolution
148 line = br.readLine();
149 tmp = StringTools.toArray( line, " \t", false );
150 res = Double.parseDouble( tmp[1].replace( ',', '.' ) );
151
152 // raster resolution
153 line = br.readLine();
154 tmp = StringTools.toArray( line, " \t", false );
155 nodata = Double.parseDouble( tmp[1].replace( ',', '.' ) );
156
157 if ( cornerx[0].toUpperCase().indexOf( "XLLCORNER" ) > -1 ) {
158 if ( center ) {
159 minx = minx - res / 2d;
160 } else {
161 minx = minx + res / 2d;
162 }
163 }
164 if ( cornery[0].toUpperCase().indexOf( "YLLCORNER" ) > -1 ) {
165 if ( center ) {
166 miny = miny - res / 2d;
167 } else {
168 miny = miny + res / 2d;
169 }
170 }
171
172 br.close();
173
174 Envelope env = GeometryFactory.createEnvelope( minx, miny, minx + res * ( cols - ( center ? 0 : 1 ) ),
175 miny + res * ( rows - ( center ? 0 : 1 ) ), null );
176 wf = new WorldFile( res, res, 0, 0, env );
177
178 }
179
180 return wf;
181 }
182
183 /**
184 * returns the value used by a ArcInfo grid to indicate no data values
185 *
186 * @return the nodata value
187 * @throws IOException
188 */
189 public double getNoDataValue()
190 throws IOException {
191
192 // ensure that metadata has been read
193 readMetadata();
194
195 return nodata;
196 }
197
198 /**
199 * reads data from a ArcInfo Grid text file
200 *
201 * @return the grid
202 * @throws IOException
203 */
204 public float[][] readData()
205 throws IOException {
206
207 // ensure that metadata has been read
208 readMetadata();
209
210 BufferedReader br = new BufferedReader( new FileReader( file ) );
211 for ( int i = 0; i < 6; i++ ) {
212 // skip first six rows containing metadata
213 br.readLine();
214 }
215
216 float[][] data = new float[rows][];
217 for ( int i = 0; i < data.length; i++ ) {
218 data[i] = StringTools.toArrayFloat( br.readLine(), " \t" );
219 }
220 br.close();
221
222 return data;
223 }
224
225 }