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