/[xulu]/trunk/src/appl/parallel/data/xulugridfile/XuluGridFileConverter.java
ViewVC logotype

Annotation of /trunk/src/appl/parallel/data/xulugridfile/XuluGridFileConverter.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 114 - (hide annotations)
Mon Jul 11 11:31:25 2011 UTC (13 years, 5 months ago) by mojays
File size: 8705 byte(s)
SCHMITZM library updated to current version (2.6-SNAPSHOT)
Added gt-xsd-filter.jar, gt-xsd-gml2.jar, picocontainer.jar and xsd.jar from Geotools 2.6.5
1 mojays 2 /*
2     * Geotools2 - OpenSource mapping toolkit
3     * http://geotools.org
4     * (C) 2002, Geotools Project Managment Committee (PMC)
5     *
6     * This library is free software; you can redistribute it and/or
7     * modify it under the terms of the GNU Lesser General Public
8     * License as published by the Free Software Foundation;
9     * version 2.1 of the License.
10     *
11     * This library is distributed in the hope that it will be useful,
12     * but WITHOUT ANY WARRANTY; without even the implied warranty of
13     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14     * Lesser General Public License for more details.
15     *
16     */
17     package appl.parallel.data.xulugridfile;
18 alfonx 78 import gtmig.org.geotools.gce.arcgrid.ArcGridRaster;
19 mojays 2
20     import java.awt.image.DataBuffer;
21     import java.io.BufferedReader;
22     import java.io.ByteArrayOutputStream;
23     import java.io.File;
24     import java.io.FileInputStream;
25     import java.io.FileNotFoundException;
26     import java.io.IOException;
27     import java.io.InputStreamReader;
28     import java.io.RandomAccessFile;
29     import java.io.Reader;
30     import java.io.StreamTokenizer;
31    
32 mojays 114 import de.appl.util.RasterMetaData;
33 mojays 2
34 alfonx 78
35 mojays 2 /**
36     * This class converts a ArcGridInfo_ASCII file to a {@link XuluGridFile}. It directly writes from
37     * the the ArcGridInfo to the given file. It does not use the memory!
38     *
39     * It is a modified geotools import class. Buffering was added.
40     *
41     * @author Dominik Appl <br>
42     * For reading this class is based partly on the Geotools class {@link ArcGridRaster}. This was
43     * done to make sure that EVERY ArcInfoASCII can be read. The used {@link ArcGridRaster} was created by <br>
44     * @author <a href="mailto:[email protected]">Christiaan ten Klooster</a>
45     * @author <a href="mailto:[email protected]">Andrea Aime</a>
46     * @author <a href="mailto:[email protected]">Simone Giannecchini
47     * (simboss)</a>
48     * @see XuluGridFile
49     * @see http://svn.geotools.org/geotools/tags/2.2.0/plugin/arcgrid/src/org/geotools/gce/arcgrid/ArcGridRaster.java
50     */
51    
52     public class XuluGridFileConverter extends ArcGridRaster {
53    
54     protected final File inputArcInfoGrid;
55     protected final File outputXuluGridFile;
56    
57     /**
58     * @throws FileNotFoundException
59     */
60     public XuluGridFileConverter(File inputArcInfoGrid, File outputXuluGridFile) throws FileNotFoundException {
61     super(new BufferedReader(new InputStreamReader(new FileInputStream(inputArcInfoGrid))), false);
62     this.inputArcInfoGrid = inputArcInfoGrid;
63     this.outputXuluGridFile = outputXuluGridFile;
64     }
65    
66    
67     /**
68     * partly copied from Geotools. www.geotools.org.
69     *
70     * @param type the type of the values. Use the
71     * {@link DataBuffer} constants to identify the type
72     *
73     * @return the new XuluGridFile
74     *
75     * @throws IOException
76     * @throws XuluGridFileException
77     */
78     public XuluGridFile convertArcInfoToXuluGridFile(int type) throws IOException, XuluGridFileException {
79    
80    
81    
82     // open reader and make tokenizer
83     Reader reader = openReader();
84     StreamTokenizer st = new StreamTokenizer(reader);
85    
86     // parse header
87     parseHeader(st);
88    
89     //create metadata and outputFile
90     RasterMetaData meta = new RasterMetaData(type,getNCols(),getNRows(),0,0,getXlCorner(),getYlCorner(),getCellSize(),null);
91     RandomAccessFile outputFile = new RandomAccessFile(outputXuluGridFile,"rw");
92    
93     // reconfigure tokenizer
94     st.resetSyntax();
95     st.parseNumbers();
96     st.whitespaceChars(' ', ' ');
97     st.whitespaceChars(' ', '\t');
98     st.whitespaceChars('\n', '\n');
99     st.whitespaceChars('\r', '\r'); //linefeed (on windows only?)
100     st.whitespaceChars('\f', '\f'); //form feed (on printers????)
101     st.eolIsSignificant(false);
102     st.ordinaryChars('E', 'E');
103    
104    
105    
106     // Read values from grid and put into raster.
107     // Values must be numbers, which may be simple <num>, or expressed
108     // in scientific notation <num>E<exp>.
109     // The following loop can read both, even if mixed.
110     // The loop expects a token to be read already
111     st.nextToken();
112    
113    
114     for (int y = 0; y < getNRows(); y++) {
115     ByteArrayOutputStream baos;
116     if(type == DataBuffer.TYPE_DOUBLE)
117     baos = new ByteArrayOutputStream(getNCols()*8);
118     else
119     baos = new ByteArrayOutputStream(getNCols()*4);
120    
121     for (int x = 0; x < getNCols(); x++) {
122     // this call always reads the next token
123     double d = readCell(st, x, y);
124    
125     // mask no data values with NaN
126     if (d == getNoData()) {
127     d = Double.NaN;
128     } else {
129     minValue = Math.min(minValue, d);
130     maxValue = Math.max(maxValue, d);
131     }
132     switch(type){
133     case DataBuffer.TYPE_FLOAT:
134     BufferedHelper.writeInt(Float.floatToIntBits((float)d),baos);
135     break;
136     case DataBuffer.TYPE_DOUBLE:
137     BufferedHelper.writeLong(Double.doubleToLongBits(d),baos);
138     case DataBuffer.TYPE_INT:
139     BufferedHelper.writeInt((int)d,baos);
140     break;
141     default:
142     throw new UnsupportedOperationException("Unsupported datatype");
143     }
144     outputFile.write(baos.toByteArray());
145     baos.reset();
146     }
147     }
148     reader.close();
149     try {
150     XuluGridFile.writeWorldFileForMetaData(outputXuluGridFile, meta);
151     } catch (XuluGridFileException e) {
152     throw new IOException ("Could not write WorldFile");
153     }
154     return new XuluGridFile(outputXuluGridFile,"rw");
155     }
156    
157     /**
158     *copied unmodified form superclass (had to be copied, because the geotools method was private)
159     */
160     private double readCell(StreamTokenizer st, int x, int y)
161     throws IOException {
162     double d = 0;
163    
164     // read a token, expected: a number
165     switch (st.ttype) {
166     case StreamTokenizer.TT_NUMBER:
167     d = (float) st.nval;
168    
169     break;
170    
171     case StreamTokenizer.TT_EOF:
172     throw new IOException("Unexpected EOF at " + x + "," + y);
173    
174     default:
175     throw new IOException("Unknown token " + st.ttype);
176     }
177    
178     // read another. May be an exponent of this number.
179     // If its not an exponent, its the next number. Fall through
180     // and token is prefetched for next loop...
181     switch (st.nextToken()) {
182     case 'e':
183     case 'E':
184    
185     // now read the exponent
186     st.nextToken();
187    
188     if (st.ttype != StreamTokenizer.TT_NUMBER) {
189     throw new IOException("Expected exponent at " + x + "," + y);
190     }
191    
192     // calculate
193     d = d * Math.pow(10.0, st.nval);
194    
195     // prefetch for next loop
196     st.nextToken();
197    
198     break;
199    
200     case StreamTokenizer.TT_NUMBER:
201     case StreamTokenizer.TT_EOF:
202     break;
203    
204     default:
205     throw new IOException("Expected Number or EOF");
206     }
207    
208     return d;
209     }
210    
211     /** You can use this method to manually convert datatypes
212     * @param args
213     */
214     public static void main(String[] args) {
215     int type=DataBuffer.TYPE_FLOAT;
216     if((args.length!=2 && args.length!=3)){
217     parameterMessage();
218     return;
219     }
220     if(!args[1].endsWith(".xgrid")){
221     parameterMessage();
222     return;
223     }
224     if(args.length==3){
225     if(args[2].equals("INT"))
226     type=DataBuffer.TYPE_INT;
227     if(args[2].equals("DOUBLE"))
228     type = DataBuffer.TYPE_DOUBLE;
229     }
230     XuluGridFileConverter converter;
231     try {
232     converter = new XuluGridFileConverter(new File(args[0]),new File(args[1]));
233     converter.convertArcInfoToXuluGridFile(type);
234     } catch (FileNotFoundException e) {
235     System.out.println("Could not find file " + args[0]);
236     } catch (IOException e) {
237     System.out.println("IOException while converting: " + e.getMessage());
238     } catch (XuluGridFileException e) {
239     System.out.println("XuluGridFileException while converting: " + e.getMessage());
240     }
241    
242    
243     }
244     public static void parameterMessage(){
245     System.out.println("Syntax: XuluGridFileConverter Source Target [type], where \n" +
246     "Source = a raster in the ArcInfo_ASCII format \n" +
247     "Target = the new XuluGridFile with the extension .xgrid \n" +
248     "[type] = (optional) the type of the XuluGridFile. Possible Values: INT,FLOAT,DOUBLE\n" +
249     " default type is FLOAT");
250     }
251     }
252    

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26