svn-gvsig-desktop / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / drivers / shp / DbaseFileNIO.java @ 6458
History | View | Annotate | Download (10.8 KB)
1 | 1100 | fjp | /* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|---|---|
2 | *
|
||
3 | * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
||
4 | *
|
||
5 | * This program is free software; you can redistribute it and/or
|
||
6 | * modify it under the terms of the GNU General Public License
|
||
7 | * as published by the Free Software Foundation; either version 2
|
||
8 | * of the License, or (at your option) any later version.
|
||
9 | *
|
||
10 | * This program is distributed in the hope that it will be useful,
|
||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
13 | * GNU General Public License for more details.
|
||
14 | *
|
||
15 | * You should have received a copy of the GNU General Public License
|
||
16 | * along with this program; if not, write to the Free Software
|
||
17 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
||
18 | *
|
||
19 | * For more information, contact:
|
||
20 | *
|
||
21 | * Generalitat Valenciana
|
||
22 | * Conselleria d'Infraestructures i Transport
|
||
23 | * Av. Blasco Ib??ez, 50
|
||
24 | * 46010 VALENCIA
|
||
25 | * SPAIN
|
||
26 | *
|
||
27 | * +34 963862235
|
||
28 | * gvsig@gva.es
|
||
29 | * www.gvsig.gva.es
|
||
30 | *
|
||
31 | * or
|
||
32 | *
|
||
33 | * IVER T.I. S.A
|
||
34 | * Salamanca 50
|
||
35 | * 46005 Valencia
|
||
36 | * Spain
|
||
37 | *
|
||
38 | * +34 963163400
|
||
39 | * dac@iver.es
|
||
40 | */
|
||
41 | 377 | fjp | package com.iver.cit.gvsig.fmap.drivers.shp; |
42 | |||
43 | import java.io.File; |
||
44 | import java.io.FileInputStream; |
||
45 | import java.io.IOException; |
||
46 | import java.nio.ByteBuffer; |
||
47 | import java.nio.channels.FileChannel; |
||
48 | |||
49 | 2632 | fjp | import com.iver.utiles.bigfile.BigByteBuffer; |
50 | 4430 | fjp | import com.iver.utiles.bigfile.BigByteBuffer2; |
51 | 377 | fjp | |
52 | 2632 | fjp | |
53 | 377 | fjp | /**
|
54 | * Class to read and write data to a dbase III format file. Creation date:
|
||
55 | * (5/15/2001 5:15:13 PM)
|
||
56 | */
|
||
57 | public class DbaseFileNIO { |
||
58 | 1005 | vcaballero | // Header information for the DBase File
|
59 | private DbaseFileHeaderNIO myHeader;
|
||
60 | private FileInputStream fin; |
||
61 | private FileChannel channel; |
||
62 | 4430 | fjp | private BigByteBuffer2 buffer;
|
63 | 377 | fjp | |
64 | 1005 | vcaballero | /**
|
65 | * Retrieve number of records in the DbaseFile
|
||
66 | *
|
||
67 | * @return N?mero de registros.
|
||
68 | */
|
||
69 | public int getRecordCount() { |
||
70 | return myHeader.getNumRecords();
|
||
71 | } |
||
72 | 377 | fjp | |
73 | 1005 | vcaballero | /**
|
74 | * Devuelve el n?mero de fields.
|
||
75 | *
|
||
76 | * @return N?mero de fields.
|
||
77 | */
|
||
78 | public int getFieldCount() { |
||
79 | return myHeader.getNumFields();
|
||
80 | } |
||
81 | 377 | fjp | |
82 | 1005 | vcaballero | /**
|
83 | * Devuelve el valor de un boolean a partir de su n?mero de fila y de
|
||
84 | * field.
|
||
85 | *
|
||
86 | * @param rowIndex N?mero de fila.
|
||
87 | * @param fieldId N?mero columna.
|
||
88 | *
|
||
89 | * @return boolean.
|
||
90 | */
|
||
91 | public boolean getBooleanFieldValue(int rowIndex, int fieldId) { |
||
92 | int recordOffset = (myHeader.getRecordLength() * rowIndex) +
|
||
93 | myHeader.getHeaderLength() + 1;
|
||
94 | 377 | fjp | |
95 | //Se calcula el offset del campo
|
||
96 | int fieldOffset = 0; |
||
97 | 1005 | vcaballero | |
98 | for (int i = 0; i < (fieldId - 1); i++) { |
||
99 | 377 | fjp | fieldOffset += myHeader.getFieldLength(i); |
100 | } |
||
101 | 1005 | vcaballero | |
102 | 377 | fjp | buffer.position(recordOffset + fieldOffset); |
103 | 1005 | vcaballero | |
104 | 377 | fjp | char bool = (char) buffer.get(); |
105 | 1005 | vcaballero | |
106 | return ((bool == 't') || (bool == 'T') || (bool == 'Y') || |
||
107 | (bool == 'y'));
|
||
108 | 377 | fjp | } |
109 | |||
110 | 1005 | vcaballero | /**
|
111 | * Devuelve el String a partir del n?mero de fila y columna.
|
||
112 | *
|
||
113 | * @param rowIndex N?mero de fila.
|
||
114 | * @param fieldId N?mero de columna.
|
||
115 | *
|
||
116 | * @return String.
|
||
117 | */
|
||
118 | public String getStringFieldValue(int rowIndex, int fieldId) { |
||
119 | 377 | fjp | int recordOffset = (myHeader.getRecordLength() * rowIndex) +
|
120 | 1005 | vcaballero | myHeader.getHeaderLength() + 1;
|
121 | |||
122 | 377 | fjp | //Se calcula el offset del campo
|
123 | int fieldOffset = 0; |
||
124 | 1005 | vcaballero | |
125 | for (int i = 0; i < fieldId; i++) { |
||
126 | 377 | fjp | fieldOffset += myHeader.getFieldLength(i); |
127 | } |
||
128 | 1005 | vcaballero | |
129 | 377 | fjp | buffer.position(recordOffset + fieldOffset); |
130 | 1005 | vcaballero | |
131 | 377 | fjp | byte[] data = new byte[myHeader.getFieldLength(fieldId)]; |
132 | buffer.get(data); |
||
133 | 1005 | vcaballero | |
134 | 377 | fjp | return new String(data); |
135 | } |
||
136 | 1005 | vcaballero | |
137 | /**
|
||
138 | * Devuelve el Number a partir de una fila y columna.
|
||
139 | *
|
||
140 | * @param rowIndex N?mero fila.
|
||
141 | * @param fieldId N?mero columna.
|
||
142 | *
|
||
143 | * @return Number.
|
||
144 | */
|
||
145 | public Number getNumberFieldValue(int rowIndex, int fieldId) { |
||
146 | 377 | fjp | //System.out.println("rowIndex = "+rowIndex+ " , "+"fieldId = "+fieldId);
|
147 | int recordOffset = (myHeader.getRecordLength() * rowIndex) +
|
||
148 | 1005 | vcaballero | myHeader.getHeaderLength() + 1;
|
149 | |||
150 | 377 | fjp | //Se calcula el offset del campo
|
151 | int fieldOffset = 0; |
||
152 | 1005 | vcaballero | |
153 | for (int i = 0; i < fieldId; i++) { |
||
154 | 377 | fjp | fieldOffset += myHeader.getFieldLength(i); |
155 | } |
||
156 | 1005 | vcaballero | |
157 | 377 | fjp | buffer.position(recordOffset + fieldOffset); |
158 | 1005 | vcaballero | |
159 | 377 | fjp | byte[] data = new byte[myHeader.getFieldLength(fieldId)]; |
160 | buffer.get(data); |
||
161 | 1005 | vcaballero | |
162 | String s = new String(data); |
||
163 | s = s.trim(); |
||
164 | |||
165 | if (getFieldType(fieldId) == 'N') { |
||
166 | 377 | fjp | Object tempObject = Double.valueOf(s); |
167 | 1005 | vcaballero | |
168 | return new Double(tempObject.toString()); |
||
169 | } else {
|
||
170 | 377 | fjp | Object tempObject = Integer.valueOf(s); |
171 | 1005 | vcaballero | |
172 | return new Integer(tempObject.toString()); |
||
173 | 377 | fjp | } |
174 | 1005 | vcaballero | |
175 | 377 | fjp | //return 0;
|
176 | } |
||
177 | |||
178 | 1005 | vcaballero | // Retrieve the record at the given index
|
179 | 377 | fjp | |
180 | 1005 | vcaballero | /* public Object[] getRecord(long inIndex) throws IOException {
|
181 | long nRecordOffset = (myHeader.getRecordLength() * inIndex) +
|
||
182 | myHeader.getHeaderLength();
|
||
183 | // retrieve the record length
|
||
184 | int tempNumFields = myHeader.getNumFields();
|
||
185 | // storage for the actual values
|
||
186 | Object[] tempRow = new Object[tempNumFields];
|
||
187 | buffer.position((int) nRecordOffset);
|
||
188 | // read the deleted flag
|
||
189 | char tempDeleted = (char) buffer.get();
|
||
190 | // read the record length
|
||
191 | int tempRecordLength = 1; // for the deleted character just read.
|
||
192 | // read the Fields
|
||
193 | for (int j = 0; j < tempNumFields; j++) {
|
||
194 | // find the length of the field.
|
||
195 | int tempFieldLength = myHeader.getFieldLength(j);
|
||
196 | tempRecordLength = tempRecordLength + tempFieldLength;
|
||
197 | // find the field type
|
||
198 | char tempFieldType = myHeader.getFieldType(j);
|
||
199 | //System.out.print("Reading Name="+myHeader.getFieldName(j)+" Type="+tempFieldType +" Length="+tempFieldLength);
|
||
200 | // read the data.
|
||
201 | Object tempObject = null;
|
||
202 | switch (tempFieldType) {
|
||
203 | case 'L': // logical data type, one character (T,t,F,f,Y,y,N,n)
|
||
204 | char tempChar = (char) buffer.get();
|
||
205 | if ((tempChar == 'T') || (tempChar == 't') ||
|
||
206 | (tempChar == 'Y') || (tempChar == 'y')) {
|
||
207 | tempObject = new Boolean(true);
|
||
208 | } else {
|
||
209 | tempObject = new Boolean(false);
|
||
210 | }
|
||
211 | break;
|
||
212 | case 'C': // character record.
|
||
213 | byte[] sbuffer = new byte[tempFieldLength];
|
||
214 | buffer.get(sbuffer);
|
||
215 | tempObject = new String(sbuffer, "ISO-8859-1").trim();
|
||
216 | break;
|
||
217 | case 'D': // date data type.
|
||
218 | byte[] dbuffer = new byte[8];
|
||
219 | buffer.get(dbuffer);
|
||
220 | String tempString = new String(dbuffer, 0, 4);
|
||
221 | try {
|
||
222 | int tempYear = Integer.parseInt(tempString);
|
||
223 | tempString = new String(dbuffer, 4, 2);
|
||
224 | int tempMonth = Integer.parseInt(tempString) - 1;
|
||
225 | tempString = new String(dbuffer, 6, 2);
|
||
226 | int tempDay = Integer.parseInt(tempString);
|
||
227 | Calendar c = Calendar.getInstance();
|
||
228 | c.set(Calendar.YEAR, tempYear);
|
||
229 | c.set(Calendar.MONTH, tempMonth);
|
||
230 | c.set(Calendar.DAY_OF_MONTH, tempDay);
|
||
231 | tempObject = c.getTime();
|
||
232 | } catch (NumberFormatException e) {
|
||
233 | }
|
||
234 | break;
|
||
235 | case 'M': // memo field.
|
||
236 | byte[] mbuffer = new byte[10];
|
||
237 | buffer.get(mbuffer);
|
||
238 | break;
|
||
239 | case 'N': // number
|
||
240 | case 'F': // floating point number
|
||
241 | byte[] fbuffer = new byte[tempFieldLength];
|
||
242 | buffer.get(fbuffer);
|
||
243 | try {
|
||
244 | tempString = new String(fbuffer);
|
||
245 | tempObject = Double.valueOf(tempString.trim());
|
||
246 | } catch (NumberFormatException e) {
|
||
247 | }
|
||
248 | break;
|
||
249 | default:
|
||
250 | byte[] defbuffer = new byte[tempFieldLength];
|
||
251 | buffer.get(defbuffer);
|
||
252 | System.out.println("Do not know how to parse Field type " +
|
||
253 | tempFieldType);
|
||
254 | }
|
||
255 | tempRow[j] = tempObject;
|
||
256 | // System.out.println(" Data="+tempObject);
|
||
257 | }
|
||
258 | // ensure that the full record has been read.
|
||
259 | if (tempRecordLength < myHeader.getRecordLength()) {
|
||
260 | byte[] tempbuff = new byte[myHeader.getRecordLength() -
|
||
261 | tempRecordLength];
|
||
262 | buffer.get(tempbuff);
|
||
263 | /* if (tempTelling){
|
||
264 | System.out.println("DBF File has "+(myHeader.getRecordLength()-tempRecordLength)+" extra bytes per record");
|
||
265 | tempTelling = false;
|
||
266 | } */
|
||
267 | /* }
|
||
268 | return tempRow;
|
||
269 | }
|
||
270 | */
|
||
271 | 377 | fjp | |
272 | 1005 | vcaballero | /**
|
273 | * Retrieve the name of the given column.
|
||
274 | *
|
||
275 | * @param inIndex ?ndice.
|
||
276 | *
|
||
277 | * @return nombre del campo.
|
||
278 | */
|
||
279 | public String getFieldName(int inIndex) { |
||
280 | return myHeader.getFieldName(inIndex).trim();
|
||
281 | } |
||
282 | 377 | fjp | |
283 | 1005 | vcaballero | /**
|
284 | * Retrieve the type of the given column.
|
||
285 | *
|
||
286 | * @param inIndex ?ndice.
|
||
287 | *
|
||
288 | * @return tipo de campo.
|
||
289 | */
|
||
290 | public char getFieldType(int inIndex) { |
||
291 | return myHeader.getFieldType(inIndex);
|
||
292 | } |
||
293 | 377 | fjp | |
294 | 1005 | vcaballero | /**
|
295 | * Retrieve the length of the given column.
|
||
296 | *
|
||
297 | * @param inIndex indice.
|
||
298 | *
|
||
299 | * @return longitud del field.
|
||
300 | */
|
||
301 | public int getFieldLength(int inIndex) { |
||
302 | return myHeader.getFieldLength(inIndex);
|
||
303 | } |
||
304 | 377 | fjp | |
305 | 1005 | vcaballero | /*
|
306 | * Retrieve the value of the given column as string.
|
||
307 | *
|
||
308 | * @param idField DOCUMENT ME!
|
||
309 | * @param idRecord DOCUMENT ME!
|
||
310 | *
|
||
311 | * @return DOCUMENT ME!
|
||
312 | *
|
||
313 | public Object getFieldValue(int idField, long idRecord) throws IOException {
|
||
314 | Object[] tmpReg = getRecord(idRecord);
|
||
315 | return tmpReg[idField];
|
||
316 | }
|
||
317 | */
|
||
318 | /*
|
||
319 | * DOCUMENT ME!
|
||
320 | *
|
||
321 | * @param idField DOCUMENT ME!
|
||
322 | * @param idRecord DOCUMENT ME!
|
||
323 | *
|
||
324 | * @return DOCUMENT ME!
|
||
325 | *
|
||
326 | public double getFieldValueAsDouble(int idField, int idRecord) throws IOException {
|
||
327 | Object[] tmpReg = getRecord(idRecord);
|
||
328 | return (double) Double.parseDouble(tmpReg[idField].toString());
|
||
329 | }
|
||
330 | */
|
||
331 | 377 | fjp | |
332 | 1005 | vcaballero | /**
|
333 | * Retrieve the location of the decimal point.
|
||
334 | *
|
||
335 | * @param inIndex ?ndice.
|
||
336 | *
|
||
337 | * @return localizaci?n.
|
||
338 | */
|
||
339 | public int getFieldDecimalLength(int inIndex) { |
||
340 | return myHeader.getFieldDecimalCount(inIndex);
|
||
341 | } |
||
342 | 377 | fjp | |
343 | 1005 | vcaballero | /**
|
344 | * read the DBF file into memory.
|
||
345 | *
|
||
346 | * @param file Fichero.
|
||
347 | *
|
||
348 | * @throws IOException
|
||
349 | */
|
||
350 | public void open(File file) throws IOException { |
||
351 | fin = new FileInputStream(file); |
||
352 | channel = fin.getChannel(); |
||
353 | 377 | fjp | |
354 | 2632 | fjp | // buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
|
355 | 4430 | fjp | buffer = new BigByteBuffer2(channel, FileChannel.MapMode.READ_ONLY); |
356 | 377 | fjp | |
357 | 1005 | vcaballero | // create the header to contain the header information.
|
358 | myHeader = new DbaseFileHeaderNIO();
|
||
359 | myHeader.readHeader(buffer); |
||
360 | } |
||
361 | 377 | fjp | |
362 | 1005 | vcaballero | /**
|
363 | * Removes all data from the dataset
|
||
364 | *
|
||
365 | * @throws IOException .
|
||
366 | */
|
||
367 | public void close() throws IOException { |
||
368 | fin.close(); |
||
369 | 377 | fjp | channel.close(); |
370 | 1005 | vcaballero | } |
371 | 4430 | fjp | |
372 | /**
|
||
373 | * @return Returns the DbaseFileHeaderNIO.
|
||
374 | */
|
||
375 | public DbaseFileHeaderNIO getDBaseHeader() {
|
||
376 | return myHeader;
|
||
377 | } |
||
378 | 377 | fjp | } |