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