root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / drivers / gvs / GVSDriver.java @ 2183
History | View | Annotate | Download (7.22 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.gvs; |
42 |
|
43 |
import java.awt.geom.Rectangle2D; |
44 |
import java.io.ByteArrayInputStream; |
45 |
import java.io.File; |
46 |
import java.io.FileInputStream; |
47 |
import java.io.IOException; |
48 |
import java.io.ObjectInputStream; |
49 |
import java.nio.ByteBuffer; |
50 |
import java.nio.channels.FileChannel; |
51 |
|
52 |
import com.hardcode.gdbms.engine.data.driver.DriverException; |
53 |
import com.hardcode.gdbms.engine.data.driver.ObjectDriver; |
54 |
import com.hardcode.gdbms.engine.values.Value; |
55 |
import com.iver.cit.gvsig.fmap.core.FShape; |
56 |
import com.iver.cit.gvsig.fmap.core.IGeometry; |
57 |
import com.iver.cit.gvsig.fmap.drivers.DriverAttributes; |
58 |
import com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver; |
59 |
|
60 |
public class GVSDriver implements VectorialFileDriver, ObjectDriver { |
61 |
|
62 |
private File file; |
63 |
private FileChannel channel; |
64 |
|
65 |
private File indexFile; |
66 |
private FileChannel indexChannel; |
67 |
private ByteBuffer byteCountBuffer = ByteBuffer.allocate(4); |
68 |
|
69 |
private int fieldCount; |
70 |
private int rowCount; |
71 |
|
72 |
private Rectangle2D extent; |
73 |
|
74 |
/**
|
75 |
* @see com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver#open(java.io.File)
|
76 |
*/
|
77 |
public void open(File f) throws IOException { |
78 |
|
79 |
//Abrimos el fichero de ?ndice
|
80 |
String name = f.getName().substring(0, f.getName().length() - 4) + ".gvi"; |
81 |
indexFile = new File(name); |
82 |
FileInputStream fis = new FileInputStream(indexFile); |
83 |
indexChannel = fis.getChannel(); |
84 |
|
85 |
//leemos el n?mero de geometr?as y de campos
|
86 |
ByteBuffer indexBuffer = ByteBuffer.allocate(4); |
87 |
indexChannel.read(indexBuffer); |
88 |
fieldCount = indexBuffer.getInt(); |
89 |
indexChannel.read(indexBuffer); |
90 |
rowCount = indexBuffer.getInt(); |
91 |
|
92 |
//Leemos el extent
|
93 |
ByteBuffer extentBuffer = ByteBuffer.allocate(8 * 4); |
94 |
indexChannel.read(extentBuffer); |
95 |
extent = new Rectangle2D.Double(extentBuffer.getDouble(), |
96 |
extentBuffer.getDouble(), |
97 |
extentBuffer.getDouble(), |
98 |
extentBuffer.getDouble()); |
99 |
|
100 |
//Abrimos el fichero de datos
|
101 |
name = f.getName(); |
102 |
file = new File(name); |
103 |
fis = new FileInputStream(file); |
104 |
channel = fis.getChannel(); |
105 |
} |
106 |
|
107 |
/**
|
108 |
* @see com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver#close()
|
109 |
*/
|
110 |
public void close() throws IOException { |
111 |
channel.close(); |
112 |
indexChannel.close(); |
113 |
} |
114 |
|
115 |
/**
|
116 |
* @see com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver#initialize()
|
117 |
*/
|
118 |
public void initialize() throws IOException { |
119 |
} |
120 |
|
121 |
/**
|
122 |
* @see com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver#accept(java.io.File)
|
123 |
*/
|
124 |
public boolean accept(File f) { |
125 |
return (f.getName().toLowerCase().endsWith("gvs")); |
126 |
} |
127 |
|
128 |
/**
|
129 |
* @see com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver#getShape(int)
|
130 |
*/
|
131 |
public IGeometry getShape(int index) throws IOException { |
132 |
//Obtenemos el ?ndice del fichero de ?ndices
|
133 |
int fileIndex = getFileIndex(index, -1); |
134 |
|
135 |
//Leemos la geometr?a
|
136 |
channel.position((long)fileIndex);
|
137 |
|
138 |
//Leemos el tama?o en bytes
|
139 |
channel.read(byteCountBuffer); |
140 |
int geomBytes = byteCountBuffer.getInt();
|
141 |
|
142 |
//Leemos la geometr?a
|
143 |
ByteBuffer byteBuffer = ByteBuffer.allocate(geomBytes); |
144 |
channel.read(byteBuffer); |
145 |
byte[] byteArray = new byte[geomBytes]; |
146 |
byteBuffer.get(byteArray); |
147 |
ByteArrayInputStream bais = new ByteArrayInputStream(byteArray); |
148 |
ObjectInputStream ois = new ObjectInputStream(bais); |
149 |
try {
|
150 |
return (IGeometry) ois.readObject();
|
151 |
} catch (ClassNotFoundException e) { |
152 |
throw new IOException(e.getMessage()); |
153 |
} |
154 |
} |
155 |
|
156 |
/**
|
157 |
* @see com.iver.cit.gvsig.fmap.drivers.VectorialDriver#getShapeType()
|
158 |
*/
|
159 |
public int getShapeType() { |
160 |
return FShape.MULTI;
|
161 |
} |
162 |
|
163 |
/**
|
164 |
* @see com.iver.cit.gvsig.fmap.drivers.VectorialDriver#getShapeCount()
|
165 |
*/
|
166 |
public int getShapeCount() throws IOException { |
167 |
return rowCount;
|
168 |
} |
169 |
|
170 |
/**
|
171 |
* @see com.iver.cit.gvsig.fmap.drivers.VectorialDriver#getDriverAttributes()
|
172 |
*/
|
173 |
public DriverAttributes getDriverAttributes() {
|
174 |
return null; |
175 |
} |
176 |
|
177 |
/**
|
178 |
* @see com.iver.cit.gvsig.fmap.drivers.VectorialDriver#getFullExtent()
|
179 |
*/
|
180 |
public Rectangle2D getFullExtent() throws IOException { |
181 |
return extent;
|
182 |
} |
183 |
|
184 |
/**
|
185 |
* @see com.hardcode.driverManager.Driver#getName()
|
186 |
*/
|
187 |
public String getName() { |
188 |
return "gvs driver"; |
189 |
} |
190 |
|
191 |
private int getFileIndex(int rowIndex, int columnIndex){ |
192 |
//Obtenemos la fila en el fichero de ?ndices
|
193 |
int indexFileIndex = 2*4 + 8 * 4; |
194 |
indexFileIndex += (fieldCount + 1) * rowIndex;
|
195 |
|
196 |
columnIndex++; |
197 |
indexFileIndex += columnIndex * 4;
|
198 |
|
199 |
return indexFileIndex;
|
200 |
} |
201 |
|
202 |
/**
|
203 |
* @see com.hardcode.gdbms.engine.data.driver.ReadAccess#getFieldValue(long, int)
|
204 |
*/
|
205 |
public Value getFieldValue(long rowIndex, int fieldIndex) throws DriverException { |
206 |
int fileIndex = getFileIndex((int) rowIndex, fieldIndex); |
207 |
|
208 |
try {
|
209 |
//Leemos el tama?o en bytes
|
210 |
channel.position((long)fileIndex);
|
211 |
channel.read(byteCountBuffer); |
212 |
int geomBytes = byteCountBuffer.getInt();
|
213 |
|
214 |
//Leemos el value
|
215 |
ByteBuffer byteBuffer = ByteBuffer.allocate(geomBytes); |
216 |
channel.read(byteBuffer); |
217 |
byte[] byteArray = new byte[geomBytes]; |
218 |
byteBuffer.get(byteArray); |
219 |
ByteArrayInputStream bais = new ByteArrayInputStream(byteArray); |
220 |
ObjectInputStream ois = new ObjectInputStream(bais); |
221 |
return (Value) ois.readObject();
|
222 |
} catch (IOException e1) { |
223 |
throw new DriverException(e1); |
224 |
} catch (ClassNotFoundException e) { |
225 |
throw new DriverException(e); |
226 |
} |
227 |
} |
228 |
|
229 |
/**
|
230 |
* @see com.hardcode.gdbms.engine.data.driver.ReadAccess#getFieldCount()
|
231 |
*/
|
232 |
public int getFieldCount() throws DriverException { |
233 |
return fieldCount;
|
234 |
} |
235 |
|
236 |
/**
|
237 |
* @see com.hardcode.gdbms.engine.data.driver.ReadAccess#getFieldName(int)
|
238 |
*/
|
239 |
public String getFieldName(int arg0) throws DriverException { |
240 |
return "geom"; |
241 |
} |
242 |
|
243 |
/**
|
244 |
* @see com.hardcode.gdbms.engine.data.driver.ReadAccess#getRowCount()
|
245 |
*/
|
246 |
public long getRowCount() throws DriverException { |
247 |
return rowCount;
|
248 |
} |
249 |
|
250 |
/**
|
251 |
* @see com.hardcode.gdbms.engine.data.driver.ReadAccess#getFieldType(int)
|
252 |
*/
|
253 |
public int getFieldType(int arg0) throws DriverException { |
254 |
// TODO Auto-generated method stub
|
255 |
return 0; |
256 |
} |
257 |
|
258 |
/**
|
259 |
* @see com.hardcode.gdbms.engine.data.driver.ObjectDriver#getPrimaryKeys()
|
260 |
*/
|
261 |
public int[] getPrimaryKeys() throws DriverException { |
262 |
// TODO Auto-generated method stub
|
263 |
return null; |
264 |
} |
265 |
} |