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