svn-gvsig-desktop / tags / J2ME_compat_v1_2_Build_1209 / libraries / libFMap / src / com / iver / cit / gvsig / fmap / drivers / shp / DBFFromGeometries.java @ 19509
History | View | Annotate | Download (4.9 KB)
1 |
/* 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 |
package com.iver.cit.gvsig.fmap.drivers.shp; |
42 |
|
43 |
import java.io.File; |
44 |
import java.io.IOException; |
45 |
import java.io.RandomAccessFile; |
46 |
import java.nio.channels.FileChannel; |
47 |
import java.nio.channels.WritableByteChannel; |
48 |
import java.util.BitSet; |
49 |
|
50 |
import com.hardcode.gdbms.driver.exceptions.ReadDriverException; |
51 |
import com.hardcode.gdbms.engine.values.Value; |
52 |
import com.hardcode.gdbms.engine.values.ValueFactory; |
53 |
import com.iver.cit.gvsig.fmap.core.IGeometry; |
54 |
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource; |
55 |
|
56 |
|
57 |
/**
|
58 |
* DOCUMENT ME!
|
59 |
*
|
60 |
* @author Vicente Caballero Navarro
|
61 |
*/
|
62 |
public class DBFFromGeometries { |
63 |
private IGeometry[] geometries = null; |
64 |
private String dbfPath; |
65 |
//private int temp = 0;
|
66 |
private DbaseFileWriterNIO dbfWrite;
|
67 |
private Value[] enteros; |
68 |
private Object[] record; |
69 |
|
70 |
//private DbaseFileNIO m_FichDbf = new DbaseFileNIO();
|
71 |
public DBFFromGeometries(IGeometry[] geometries, File f) { |
72 |
this.geometries = geometries;
|
73 |
setFile(f); |
74 |
} |
75 |
|
76 |
/**
|
77 |
* Inserta el fichero.
|
78 |
*
|
79 |
* @param f Fichero.
|
80 |
*/
|
81 |
private void setFile(File f) { |
82 |
String strFichDbf = f.getAbsolutePath().replaceAll("\\.shp", ".dbf"); |
83 |
dbfPath = strFichDbf.replaceAll("\\.SHP", ".DBF"); |
84 |
} |
85 |
|
86 |
/**
|
87 |
* Inicializa.
|
88 |
*
|
89 |
* @param sds Capa.
|
90 |
*/
|
91 |
public void create(SelectableDataSource sds,BitSet bitset) { |
92 |
//if (layer instanceof AlphanumericData) {
|
93 |
try {
|
94 |
if (sds==null) { |
95 |
DbaseFileHeaderNIO myHeader = DbaseFileHeaderNIO.createNewDbaseHeader(); |
96 |
myHeader.setNumRecords(geometries.length); |
97 |
dbfWrite = new DbaseFileWriterNIO(myHeader,
|
98 |
(FileChannel) getWriteChannel(dbfPath));
|
99 |
enteros = new Value[1]; |
100 |
} else {
|
101 |
//VectorialFileAdapter vfa=(VectorialFileAdapter)((SingleLayer)lv).getSource();
|
102 |
DbaseFileHeaderNIO myHeader; |
103 |
|
104 |
myHeader = DbaseFileHeaderNIO.createDbaseHeader(sds); |
105 |
|
106 |
myHeader.setNumRecords(geometries.length); |
107 |
dbfWrite = new DbaseFileWriterNIO(myHeader,
|
108 |
(FileChannel) getWriteChannel(dbfPath));
|
109 |
record = new Object[sds.getFieldCount()]; |
110 |
} |
111 |
|
112 |
createdbf(sds,bitset); |
113 |
System.out.println("Acabado DBF"); |
114 |
} catch (IOException e) { |
115 |
e.printStackTrace(); |
116 |
|
117 |
///} catch (DriverException e1) {
|
118 |
// e1.printStackTrace();
|
119 |
} catch (ReadDriverException e2) {
|
120 |
e2.printStackTrace(); |
121 |
} |
122 |
|
123 |
//return true;
|
124 |
//}
|
125 |
//return false;
|
126 |
} |
127 |
|
128 |
/**
|
129 |
* Rellena los registros del dbf.
|
130 |
*
|
131 |
* @param sds DOCUMENT ME!
|
132 |
*/
|
133 |
private void createdbf(SelectableDataSource sds,BitSet bitset) { |
134 |
int i = 0; |
135 |
|
136 |
|
137 |
//for (int i = 0; i < fgs.length; i++) {
|
138 |
try {
|
139 |
if (sds==null) { |
140 |
for (int j = 0; j < geometries.length; j++) { |
141 |
enteros[0] = ValueFactory.createValue((double)i); |
142 |
dbfWrite.write(enteros); |
143 |
i++; |
144 |
} |
145 |
} else {
|
146 |
for (int j = bitset.nextSetBit(0); |
147 |
j >= 0;
|
148 |
j = bitset.nextSetBit(j + 1)){
|
149 |
for (int r = 0; r < sds.getFieldCount(); r++) { |
150 |
record[r] = sds.getFieldValue(j, r); |
151 |
} |
152 |
|
153 |
dbfWrite.write(record); |
154 |
} |
155 |
} |
156 |
} catch (IOException e1) { |
157 |
e1.printStackTrace(); |
158 |
} catch (ReadDriverException e) {
|
159 |
e.printStackTrace(); |
160 |
} |
161 |
} |
162 |
|
163 |
|
164 |
/**
|
165 |
* DOCUMENT ME!
|
166 |
*
|
167 |
* @param path DOCUMENT ME!
|
168 |
*
|
169 |
* @return DOCUMENT ME!
|
170 |
*
|
171 |
* @throws IOException DOCUMENT ME!
|
172 |
*/
|
173 |
private WritableByteChannel getWriteChannel(String path) |
174 |
throws IOException { |
175 |
WritableByteChannel channel;
|
176 |
|
177 |
File f = new File(path); |
178 |
|
179 |
if (!f.exists()) {
|
180 |
System.out.println("Creando fichero " + f.getAbsolutePath()); |
181 |
|
182 |
if (!f.createNewFile()) {
|
183 |
throw new IOException("Cannot create file " + f); |
184 |
} |
185 |
} |
186 |
RandomAccessFile raf = new RandomAccessFile(f, "rw"); |
187 |
channel = raf.getChannel(); |
188 |
|
189 |
return channel;
|
190 |
} |
191 |
} |