Revision 24791
branches/v2_0_0_prep/libraries/libFMap_dalfile/src-test/org/gvsig/fmap/dal/store/dbf/TestDBF.java | ||
---|---|---|
35 | 35 |
import org.gvsig.fmap.dal.DALFileLibrary; |
36 | 36 |
import org.gvsig.fmap.dal.DALLibrary; |
37 | 37 |
import org.gvsig.fmap.dal.DALLocator; |
38 |
import org.gvsig.fmap.dal.DataExplorer; |
|
38 | 39 |
import org.gvsig.fmap.dal.DataManager; |
39 | 40 |
import org.gvsig.fmap.dal.exception.DataException; |
40 | 41 |
import org.gvsig.fmap.dal.feature.Feature; |
... | ... | |
130 | 131 |
|
131 | 132 |
dbfParameters.setFile(dbf_prueba); |
132 | 133 |
|
134 |
this.testSimpleIteration(dbfParameters); |
|
133 | 135 |
|
134 | 136 |
|
137 |
|
|
135 | 138 |
} catch (DataException e3) { |
136 | 139 |
e3.printStackTrace(); |
137 | 140 |
fail(); |
... | ... | |
140 | 143 |
|
141 | 144 |
} |
142 | 145 |
|
146 |
public void test() { |
|
147 |
DBFStoreParameters dbfParameters = null; |
|
148 |
|
|
149 |
try { |
|
150 |
dbfParameters = (DBFStoreParameters) dataManager |
|
151 |
.createStoreParameters(DBFStoreProvider.NAME); |
|
152 |
|
|
153 |
dbfParameters.setFile(dbf_prueba); |
|
154 |
|
|
155 |
FeatureStore store = (FeatureStore) dataManager |
|
156 |
.createStore(dbfParameters); |
|
157 |
FeatureSet set = store.getFeatureSet(); |
|
158 |
|
|
159 |
|
|
160 |
set.dispose(); |
|
161 |
|
|
162 |
} catch (DataException e3) { |
|
163 |
e3.printStackTrace(); |
|
164 |
fail(); |
|
165 |
return; |
|
166 |
} |
|
167 |
|
|
168 |
} |
|
169 |
|
|
143 | 170 |
public void testExplorer() { |
144 | 171 |
FeatureStore store = null; |
145 | 172 |
DBFStoreParameters dbfParameters = null; |
... | ... | |
152 | 179 |
|
153 | 180 |
store = (FeatureStore) dataManager.createStore(dbfParameters); |
154 | 181 |
|
155 |
// TODO por aqui
|
|
182 |
DataExplorer explorer = store.getExplorer();
|
|
156 | 183 |
|
184 |
assertEquals(2, explorer.list().size()); |
|
185 |
|
|
186 |
store.dispose(); |
|
187 |
|
|
157 | 188 |
} catch (DataException e3) { |
158 | 189 |
e3.printStackTrace(); |
159 | 190 |
fail(); |
branches/v2_0_0_prep/libraries/libFMap_dalfile/src/org/gvsig/fmap/dal/store/shp/SHPFileFilter.java | ||
---|---|---|
29 | 29 |
DataManager manager = DALLocator.getDataManager(); |
30 | 30 |
SHPStoreParameters params = (SHPStoreParameters) manager |
31 | 31 |
.createStoreParameters(this.getDataStoreProviderName()); |
32 |
params.setSHPFile(file.getPath()); |
|
32 |
params.setSHPFileName(file.getPath());
|
|
33 | 33 |
return params; |
34 | 34 |
|
35 | 35 |
} |
branches/v2_0_0_prep/libraries/libFMap_dalfile/src/org/gvsig/fmap/dal/store/shp/utils/SHPFile.java | ||
---|---|---|
118 | 118 |
|
119 | 119 |
public void open() throws DataException { |
120 | 120 |
try { |
121 |
fin = new FileInputStream(this.params.getSHPFileObject());
|
|
121 |
fin = new FileInputStream(this.params.getSHPFile()); |
|
122 | 122 |
} catch (java.io.FileNotFoundException e) { |
123 |
throw new FileNotFoundException(this.params.getSHPFile()); |
|
123 |
throw new FileNotFoundException(this.params.getSHPFileName());
|
|
124 | 124 |
} |
125 | 125 |
|
126 | 126 |
|
... | ... | |
134 | 134 |
try { |
135 | 135 |
bb = new BigByteBuffer2(channel, FileChannel.MapMode.READ_ONLY); |
136 | 136 |
} catch (IOException e) { |
137 |
throw new ReadException(this.params.getSHPFile(), e); |
|
137 |
throw new ReadException(this.params.getSHPFileName(), e);
|
|
138 | 138 |
|
139 | 139 |
} |
140 | 140 |
try { |
141 |
finShx = new FileInputStream(this.params.getSHXFileObject());
|
|
141 |
finShx = new FileInputStream(this.params.getSHXFile()); |
|
142 | 142 |
} catch (java.io.FileNotFoundException e) { |
143 |
throw new FileNotFoundException(this.params.getSHXFile()); |
|
143 |
throw new FileNotFoundException(this.params.getSHXFileName());
|
|
144 | 144 |
} |
145 | 145 |
|
146 | 146 |
|
... | ... | |
156 | 156 |
bbShx = new BigByteBuffer2(channelShx, |
157 | 157 |
FileChannel.MapMode.READ_ONLY); |
158 | 158 |
} catch (IOException e) { |
159 |
throw new ReadException(this.params.getSHXFile(), e); |
|
159 |
throw new ReadException(this.params.getSHXFileName(), e);
|
|
160 | 160 |
|
161 | 161 |
} |
162 | 162 |
bbShx.order(ByteOrder.BIG_ENDIAN); |
... | ... | |
202 | 202 |
} |
203 | 203 |
|
204 | 204 |
//TODO: SRS |
205 |
File prjFile = SHP.getPrjFile(this.params.getSHPFileObject());
|
|
205 |
File prjFile = SHP.getPrjFile(this.params.getSHPFile()); |
|
206 | 206 |
if (prjFile.exists()) { |
207 | 207 |
BufferedReader input=null; |
208 | 208 |
try { |
... | ... | |
235 | 235 |
} |
236 | 236 |
|
237 | 237 |
public boolean isEditable(){ |
238 |
return this.params.getSHPFileObject().canWrite()
|
|
239 |
&& this.params.getSHXFileObject().canWrite();
|
|
238 |
return this.params.getSHPFile().canWrite() |
|
239 |
&& this.params.getSHXFile().canWrite(); |
|
240 | 240 |
} |
241 | 241 |
|
242 | 242 |
public int getGeometryType() throws ReadException { |
branches/v2_0_0_prep/libraries/libFMap_dalfile/src/org/gvsig/fmap/dal/store/shp/utils/SHPFileWrite.java | ||
---|---|---|
46 | 46 |
import java.nio.channels.FileChannel; |
47 | 47 |
|
48 | 48 |
import org.gvsig.fmap.dal.exception.WriteException; |
49 |
import org.gvsig.fmap.dal.feature.exception.PerformEditingException; |
|
50 | 49 |
import org.gvsig.fmap.geom.Geometry; |
51 | 50 |
import org.gvsig.fmap.geom.primitive.DefaultEnvelope; |
52 | 51 |
import org.gvsig.fmap.geom.primitive.Envelope; |
... | ... | |
98 | 97 |
* |
99 | 98 |
* @throws IOException DOCUMENT ME! |
100 | 99 |
*/ |
101 |
private void drain() throws PerformEditingException {
|
|
100 |
private void drain() throws WriteException {
|
|
102 | 101 |
m_bb.flip(); |
103 | 102 |
m_indexBuffer.flip(); |
104 | 103 |
try{ |
... | ... | |
110 | 109 |
shxChannel.write(m_indexBuffer); |
111 | 110 |
} |
112 | 111 |
}catch (IOException e) { |
113 |
throw new PerformEditingException("SHP File Write Drain", e);
|
|
112 |
throw new WriteException("SHP File Write Drain", e);
|
|
114 | 113 |
} |
115 | 114 |
m_bb.flip().limit(m_bb.capacity()); |
116 | 115 |
m_indexBuffer.flip().limit(m_indexBuffer.capacity()); |
... | ... | |
127 | 126 |
/** |
128 | 127 |
* Close the underlying Channels. |
129 | 128 |
*/ |
130 |
public void close() throws PerformEditingException {
|
|
129 |
public void close() throws WriteException {
|
|
131 | 130 |
try { |
132 | 131 |
shpChannel.close(); |
133 | 132 |
shxChannel.close(); |
134 | 133 |
} catch (IOException e) { |
135 |
throw new PerformEditingException("SHP File Write Close", e);
|
|
134 |
throw new WriteException("SHP File Write Close", e);
|
|
136 | 135 |
} |
137 | 136 |
shpChannel = null; |
138 | 137 |
shxChannel = null; |
... | ... | |
162 | 161 |
* |
163 | 162 |
*/ |
164 | 163 |
public void write(Geometry[] geometries, int type) |
165 |
throws PerformEditingException {
|
|
164 |
throws WriteException {
|
|
166 | 165 |
m_shape = SHP.create(type); |
167 | 166 |
// m_shape.setFlatness(flatness); |
168 | 167 |
writeHeaders(geometries, type); |
... | ... | |
185 | 184 |
* |
186 | 185 |
*/ |
187 | 186 |
private void writeHeaders(Geometry[] geometries, int type) |
188 |
throws PerformEditingException {
|
|
187 |
throws WriteException {
|
|
189 | 188 |
int fileLength = 100; |
190 | 189 |
Envelope extent = null; |
191 | 190 |
|
... | ... | |
215 | 214 |
*/ |
216 | 215 |
public void writeHeaders(Envelope bounds, int type, |
217 | 216 |
int numberOfGeometries, |
218 |
int fileLength) throws PerformEditingException {
|
|
217 |
int fileLength) throws WriteException {
|
|
219 | 218 |
/*try { |
220 | 219 |
handler = type.getShapeHandler(); |
221 | 220 |
} catch (ShapefileException se) { |
... | ... | |
247 | 246 |
shpChannel.position(0); |
248 | 247 |
shxChannel.position(0); |
249 | 248 |
} catch (IOException e) { |
250 |
throw new PerformEditingException("SHP File Write Headers", e);
|
|
249 |
throw new WriteException("SHP File Write Headers", e);
|
|
251 | 250 |
} |
252 | 251 |
drain(); |
253 | 252 |
} |
254 |
public int writeIGeometry(Geometry g) throws PerformEditingException {
|
|
253 |
public int writeIGeometry(Geometry g) throws WriteException {
|
|
255 | 254 |
int shapeType = getShapeType(g.getType()); |
256 | 255 |
m_shape = SHP.create(shapeType); |
257 | 256 |
// m_shape.setFlatness(flatness); |
... | ... | |
267 | 266 |
* write the file size in the header. |
268 | 267 |
*/ |
269 | 268 |
public synchronized int writeGeometry(Geometry g) |
270 |
throws PerformEditingException {
|
|
269 |
throws WriteException {
|
|
271 | 270 |
if (m_bb == null) { |
272 | 271 |
allocateBuffers(); |
273 | 272 |
m_offset = 50; |
... | ... | |
277 | 276 |
shpChannel.position(0); |
278 | 277 |
shxChannel.position(0); |
279 | 278 |
} catch (IOException e) { |
280 |
throw new PerformEditingException("SHP File Write", e);
|
|
279 |
throw new WriteException("SHP File Write", e);
|
|
281 | 280 |
} |
282 | 281 |
// throw new IOException("Must write headers first"); |
283 | 282 |
} |
branches/v2_0_0_prep/libraries/libFMap_dalfile/src/org/gvsig/fmap/dal/store/shp/SHPStoreProvider.java | ||
---|---|---|
27 | 27 |
|
28 | 28 |
package org.gvsig.fmap.dal.store.shp; |
29 | 29 |
|
30 |
import java.io.File; |
|
31 |
import java.io.IOException; |
|
30 | 32 |
import java.util.Iterator; |
31 | 33 |
|
32 | 34 |
import org.gvsig.fmap.dal.DataTypes; |
35 |
import org.gvsig.fmap.dal.NewDataStoreParameters; |
|
33 | 36 |
import org.gvsig.fmap.dal.exception.CloseException; |
37 |
import org.gvsig.fmap.dal.exception.CreateException; |
|
34 | 38 |
import org.gvsig.fmap.dal.exception.DataException; |
35 | 39 |
import org.gvsig.fmap.dal.exception.FileNotFoundException; |
36 | 40 |
import org.gvsig.fmap.dal.exception.InitializeException; |
... | ... | |
41 | 45 |
import org.gvsig.fmap.dal.feature.EditableFeatureType; |
42 | 46 |
import org.gvsig.fmap.dal.feature.Feature; |
43 | 47 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
48 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
|
44 | 49 |
import org.gvsig.fmap.dal.feature.FeatureType; |
45 | 50 |
import org.gvsig.fmap.dal.feature.exception.PerformEditingException; |
46 | 51 |
import org.gvsig.fmap.dal.feature.impl.DefaultFeatureType; |
... | ... | |
61 | 66 |
private SHPFile shpFile; |
62 | 67 |
private ResourceProvider shpResource; |
63 | 68 |
private SHPStoreParameters shpParams; |
69 |
protected static final String GEOMETRY_ATTIBUTE_NAME = "GEOMETRY"; |
|
64 | 70 |
|
65 | 71 |
public SHPStoreProvider(SHPStoreParameters params) |
66 | 72 |
throws InitializeException { |
... | ... | |
68 | 74 |
this.shpParams = params; |
69 | 75 |
|
70 | 76 |
shpResource = this.createResource(FileResource.NAME, |
71 |
new Object[] { this.shpParams.getSHPFile() }); |
|
77 |
new Object[] { this.shpParams.getSHPFileName() });
|
|
72 | 78 |
shpResource.addConsumer(this); |
73 | 79 |
|
74 | 80 |
//DBFResource tmpResource = new DBFResource(dbfParameters); |
... | ... | |
82 | 88 |
this.shpFile = new SHPFile(this.shpParams); |
83 | 89 |
} |
84 | 90 |
|
91 |
private EditableFeatureAttributeDescriptor addGeometryColumn( |
|
92 |
EditableFeatureType fType) { |
|
93 |
fType.setDefaultGeometryAttributeName(GEOMETRY_ATTIBUTE_NAME); |
|
94 |
return fType.add(GEOMETRY_ATTIBUTE_NAME, DataTypes.GEOMETRY); |
|
95 |
|
|
96 |
} |
|
97 |
|
|
98 |
private void removeGeometryColumn( |
|
99 |
EditableFeatureType fType) { |
|
100 |
fType.setDefaultGeometryAttributeName(null); |
|
101 |
fType.remove(GEOMETRY_ATTIBUTE_NAME); |
|
102 |
} |
|
103 |
|
|
85 | 104 |
protected EditableFeatureType getTheFeatureType() |
86 | 105 |
throws InitializeException { |
87 | 106 |
EditableFeatureType fType = super.getTheFeatureType(); |
... | ... | |
93 | 112 |
} |
94 | 113 |
try { |
95 | 114 |
|
96 |
EditableFeatureAttributeDescriptor attr = fType.add("GEOMETRY",
|
|
97 |
DataTypes.GEOMETRY).setGeometryType(
|
|
115 |
EditableFeatureAttributeDescriptor attr = this.addGeometryColumn(
|
|
116 |
fType).setGeometryType(
|
|
98 | 117 |
this.shpFile.getGeometryType()); |
99 | 118 |
String srs = this.getSRS(this.shpFile.getSRSParameters()); |
100 | 119 |
if (srs == null){ |
... | ... | |
102 | 121 |
} |
103 | 122 |
attr.setSRS(srs); |
104 | 123 |
|
105 |
fType.setDefaultGeometryAttributeName(attr.getName()); |
|
106 | 124 |
|
107 | 125 |
return fType; |
108 | 126 |
} catch (ReadException e) { |
... | ... | |
265 | 283 |
|
266 | 284 |
public void performEditing(Iterator deleteds, Iterator inserteds, |
267 | 285 |
Iterator updateds) throws PerformEditingException { |
268 |
// TODO Auto-generated method stub |
|
269 |
super.performEditing(deleteds, inserteds, updateds); |
|
286 |
FeatureType fType; |
|
287 |
try { |
|
288 |
fType = this.store.getDefaultFeatureType(); |
|
289 |
} catch (DataException e) { |
|
290 |
throw new PerformEditingException(this.getName(), e); |
|
291 |
} |
|
292 |
// TODO Comprobar el campo de geometria |
|
293 |
|
|
294 |
EditableFeatureType dbfFtype = fType.getEditable(); |
|
295 |
|
|
296 |
this.removeGeometryColumn(dbfFtype); |
|
297 |
|
|
298 |
SHPFeatureWriter writer = null; |
|
299 |
try { |
|
300 |
this.shpResource.begin(); |
|
301 |
|
|
302 |
FeatureSet set = this.store.getFeatureSet(); |
|
303 |
|
|
304 |
writer = new SHPFeatureWriter(this.getName()); |
|
305 |
|
|
306 |
writer.begin(this.shpParams, fType, dbfFtype, set.getSize()); |
|
307 |
|
|
308 |
Iterator iter = set.iterator(); |
|
309 |
while (iter.hasNext()) { |
|
310 |
writer.write((Feature) iter.next()); |
|
311 |
} |
|
312 |
|
|
313 |
writer.end(); |
|
314 |
|
|
315 |
shpResource.notifyChanges(); |
|
316 |
} catch (Exception e) { |
|
317 |
throw new PerformEditingException(this.getName(), e); |
|
318 |
} finally { |
|
319 |
|
|
320 |
shpResource.end(); |
|
321 |
} |
|
322 |
|
|
323 |
|
|
270 | 324 |
} |
271 | 325 |
|
326 |
public void create(NewDataStoreParameters parameters, boolean overwrite) |
|
327 |
throws CreateException { |
|
328 |
|
|
329 |
SHPNewStoreParameters params = (SHPNewStoreParameters) parameters; |
|
330 |
FeatureType fType = params.getDefaultFeatureType(); |
|
331 |
// TODO Comprobar que el campo de geometria |
|
332 |
EditableFeatureType dbfFtype = fType.getEditable(); |
|
333 |
|
|
334 |
this.removeGeometryColumn(dbfFtype); |
|
335 |
File shpFile = params.getSHPFile(); |
|
336 |
File shxFile = params.getSHXFile(); |
|
337 |
|
|
338 |
if (shpFile.exists()) { |
|
339 |
if (overwrite) { |
|
340 |
if (!shpFile.delete()) { |
|
341 |
throw new CreateException(this.getName(), new IOException( |
|
342 |
"cannot delete file: " + shpFile.getPath())); |
|
343 |
} |
|
344 |
if (shxFile.exists()) { |
|
345 |
if (!shxFile.delete()) { |
|
346 |
throw new CreateException(this.getName(), |
|
347 |
new IOException("cannot delete file: " |
|
348 |
+ shxFile.getPath())); |
|
349 |
} |
|
350 |
|
|
351 |
} |
|
352 |
} else { |
|
353 |
throw new CreateException(this.getName(), new IOException( |
|
354 |
"file already exist")); |
|
355 |
} |
|
356 |
} |
|
357 |
|
|
358 |
FileResource resource; |
|
359 |
try { |
|
360 |
resource = (FileResource) this.createResource(FileResource.NAME, |
|
361 |
new Object[] { shpFile.getAbsolutePath() }); |
|
362 |
} catch (InitializeException e1) { |
|
363 |
throw new CreateException(shpFile.getPath(), e1); |
|
364 |
} |
|
365 |
resource.addConsumer(this); |
|
366 |
|
|
367 |
SHPFeatureWriter writer = null; |
|
368 |
try { |
|
369 |
resource.begin(); |
|
370 |
|
|
371 |
|
|
372 |
writer = new SHPFeatureWriter(this.getName()); |
|
373 |
|
|
374 |
writer.begin(params, fType, dbfFtype, 0); |
|
375 |
|
|
376 |
writer.end(); |
|
377 |
|
|
378 |
resource.notifyChanges(); |
|
379 |
} catch (Exception e) { |
|
380 |
throw new CreateException(this.getName(), e); |
|
381 |
} finally { |
|
382 |
resource.end(); |
|
383 |
resource.removeConsumer(this); |
|
384 |
} |
|
385 |
} |
|
386 |
|
|
387 |
protected NewDataStoreParameters createInstanceNewDataStoreParameters() { |
|
388 |
return new SHPNewStoreParameters(); |
|
389 |
} |
|
390 |
|
|
391 |
public NewDataStoreParameters getCreateParameters() throws DataException { |
|
392 |
SHPNewStoreParameters params = (SHPNewStoreParameters) super |
|
393 |
.getCreateParameters(); |
|
394 |
|
|
395 |
EditableFeatureType fType = params.getDefaultFeatureType().getEditable(); |
|
396 |
this.addGeometryColumn(fType); |
|
397 |
params.setDefaultFeatureType(fType); |
|
398 |
return params; |
|
399 |
} |
|
400 |
|
|
401 |
|
|
272 | 402 |
public void setState(PersistentState state) throws PersistenceException { |
273 | 403 |
// TODO Auto-generated method stub |
274 | 404 |
super.setState(state); |
... | ... | |
299 | 429 |
super.endAppend(); |
300 | 430 |
} |
301 | 431 |
|
302 |
|
|
303 |
|
|
304 |
|
|
305 |
|
|
306 | 432 |
} |
branches/v2_0_0_prep/libraries/libFMap_dalfile/src/org/gvsig/fmap/dal/store/shp/SHPFeatureWriter.java | ||
---|---|---|
1 |
/* gvSIG. Geographic Information System of the Valencian Government |
|
2 |
* |
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department |
|
4 |
* of the Valencian Government (CIT) |
|
5 |
* |
|
6 |
* This program is free software; you can redistribute it and/or |
|
7 |
* modify it under the terms of the GNU General Public License |
|
8 |
* as published by the Free Software Foundation; either version 2 |
|
9 |
* of the License, or (at your option) any later version. |
|
10 |
* |
|
11 |
* This program is distributed in the hope that it will be useful, |
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 |
* GNU General Public License for more details. |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License |
|
17 |
* along with this program; if not, write to the Free Software |
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
19 |
* MA 02110-1301, USA. |
|
20 |
* |
|
21 |
*/ |
|
22 |
|
|
23 |
/* |
|
24 |
* AUTHORS (In addition to CIT): |
|
25 |
* 2008 IVER T.I. S.A. {{Task}} |
|
26 |
*/ |
|
27 |
|
|
28 |
package org.gvsig.fmap.dal.store.shp; |
|
29 |
|
|
30 |
import java.io.File; |
|
31 |
import java.io.IOException; |
|
32 |
import java.nio.channels.FileChannel; |
|
33 |
|
|
34 |
import org.gvsig.fmap.dal.exception.DataException; |
|
35 |
import org.gvsig.fmap.dal.exception.WriteException; |
|
36 |
import org.gvsig.fmap.dal.feature.Feature; |
|
37 |
import org.gvsig.fmap.dal.feature.FeatureType; |
|
38 |
import org.gvsig.fmap.dal.store.dbf.DBFFeatureWriter; |
|
39 |
import org.gvsig.fmap.dal.store.dbf.DBFStoreParameters; |
|
40 |
import org.gvsig.fmap.dal.store.shp.utils.SHPFileWrite; |
|
41 |
import org.gvsig.fmap.geom.Geometry; |
|
42 |
import org.gvsig.fmap.geom.primitive.DefaultEnvelope; |
|
43 |
import org.gvsig.fmap.geom.primitive.Envelope; |
|
44 |
import org.gvsig.fmap.geom.primitive.NullGeometry; |
|
45 |
|
|
46 |
public class SHPFeatureWriter extends DBFFeatureWriter { |
|
47 |
|
|
48 |
private SHPFileWrite shpWrite; |
|
49 |
private Envelope envelope; |
|
50 |
private long fileSize; |
|
51 |
private int[] supportedGeometryTypes; |
|
52 |
private Geometry defaultGeometry; |
|
53 |
|
|
54 |
protected SHPFeatureWriter(String name) { |
|
55 |
super(name); |
|
56 |
} |
|
57 |
|
|
58 |
public void begin(DBFStoreParameters dbfParameters, |
|
59 |
FeatureType featureType, FeatureType dbfFeatureType, long numRows) |
|
60 |
throws DataException { |
|
61 |
super.begin(dbfParameters, featureType, numRows); |
|
62 |
|
|
63 |
SHPStoreParameters shpParameters = (SHPStoreParameters) dbfParameters; |
|
64 |
File shpFile = shpParameters.getSHPFile(); |
|
65 |
File shxFile = shpParameters.getSHXFile(); |
|
66 |
|
|
67 |
FileChannel shpChannel = null; |
|
68 |
FileChannel shxChannel = null; |
|
69 |
|
|
70 |
try { |
|
71 |
shpChannel = (FileChannel) getWriteChannel(shpFile |
|
72 |
.getAbsolutePath()); |
|
73 |
shxChannel = (FileChannel) getWriteChannel(shxFile |
|
74 |
.getAbsolutePath()); |
|
75 |
} catch (IOException e) { |
|
76 |
throw new WriteException(this.name, e); |
|
77 |
} |
|
78 |
|
|
79 |
shpWrite = new SHPFileWrite(shpChannel, shxChannel); |
|
80 |
|
|
81 |
int gvSIG_geometryType =featureType.getAttributeDescriptor( |
|
82 |
featureType.getDefaultGeometryAttributeIndex()) |
|
83 |
.getGeometryType(); |
|
84 |
this.setSupportedGeometryTypes(gvSIG_geometryType); |
|
85 |
int shapeType = 0; |
|
86 |
shapeType = shpWrite |
|
87 |
.getShapeType(gvSIG_geometryType); |
|
88 |
|
|
89 |
defaultGeometry= (Geometry) featureType.getAttributeDescriptor( |
|
90 |
featureType.getDefaultGeometryAttributeIndex()).getDefaultValue(); |
|
91 |
if (defaultGeometry == null){ |
|
92 |
defaultGeometry = new NullGeometry(); |
|
93 |
} |
|
94 |
envelope = new DefaultEnvelope(2); |
|
95 |
shpWrite.writeHeaders(envelope, shapeType,(int) numRows, 0); //FIXME long!!! |
|
96 |
|
|
97 |
} |
|
98 |
|
|
99 |
public void dispose() { |
|
100 |
super.dispose(); |
|
101 |
this.envelope = null; |
|
102 |
this.shpWrite = null; |
|
103 |
} |
|
104 |
|
|
105 |
public void end() throws DataException { |
|
106 |
super.end(); |
|
107 |
shpWrite.close(); |
|
108 |
} |
|
109 |
|
|
110 |
public void write(Feature feature) throws DataException { |
|
111 |
|
|
112 |
Geometry theGeom = feature.getDefaultGeometry(); |
|
113 |
if (theGeom==null){ |
|
114 |
theGeom=this.defaultGeometry; |
|
115 |
} |
|
116 |
if (!canWriteGeometry(theGeom.getType())){ |
|
117 |
throw new WriteException(this.name, // FIXME Excepcion correcta |
|
118 |
new RuntimeException("UnsupportedGeometryType: "+ theGeom.getGeometryType().getName())); |
|
119 |
} |
|
120 |
|
|
121 |
super.write(feature); |
|
122 |
|
|
123 |
fileSize = shpWrite.writeIGeometry(theGeom); |
|
124 |
Envelope envelope = theGeom.getEnvelope(); |
|
125 |
if (envelope!=null){ |
|
126 |
this.envelope.add(envelope); |
|
127 |
} |
|
128 |
} |
|
129 |
|
|
130 |
private void setSupportedGeometryTypes(int gvSIG_geometryType) { |
|
131 |
switch (gvSIG_geometryType % Geometry.TYPES.Z) |
|
132 |
{ |
|
133 |
case Geometry.TYPES.POINT: |
|
134 |
supportedGeometryTypes = new int[] {Geometry.TYPES.POINT, Geometry.TYPES.NULL }; |
|
135 |
break; |
|
136 |
case Geometry.TYPES.MULTIPOINT: |
|
137 |
supportedGeometryTypes = new int[] {Geometry.TYPES.MULTIPOINT, Geometry.TYPES.NULL }; |
|
138 |
break; |
|
139 |
case Geometry.TYPES.CURVE: |
|
140 |
supportedGeometryTypes = new int[] {Geometry.TYPES.CURVE, Geometry.TYPES.ELLIPSE, |
|
141 |
Geometry.TYPES.ARC, Geometry.TYPES.CIRCLE, Geometry.TYPES.SURFACE, Geometry.TYPES.NULL }; |
|
142 |
break; |
|
143 |
case Geometry.TYPES.SURFACE: |
|
144 |
supportedGeometryTypes = new int[] {Geometry.TYPES.ELLIPSE, |
|
145 |
Geometry.TYPES.CIRCLE, Geometry.TYPES.SURFACE, Geometry.TYPES.NULL }; |
|
146 |
break; |
|
147 |
|
|
148 |
default: |
|
149 |
supportedGeometryTypes = new int[] {}; |
|
150 |
} |
|
151 |
} |
|
152 |
public boolean canWriteGeometry(int gvSIGgeometryType) { |
|
153 |
for (int i=0; i < supportedGeometryTypes.length; i++) |
|
154 |
{ |
|
155 |
if (gvSIGgeometryType == supportedGeometryTypes[i] || |
|
156 |
gvSIGgeometryType == (supportedGeometryTypes[i] | Geometry.TYPES.Z)) { |
|
157 |
return true; |
|
158 |
} |
|
159 |
} |
|
160 |
return false; |
|
161 |
} |
|
162 |
|
|
163 |
public void begin(DBFStoreParameters storeParameters, |
|
164 |
FeatureType featureType, long numRows) throws DataException { |
|
165 |
throw new UnsupportedOperationException(); |
|
166 |
} |
|
167 |
|
|
168 |
} |
|
0 | 169 |
branches/v2_0_0_prep/libraries/libFMap_dalfile/src/org/gvsig/fmap/dal/store/shp/SHPNewStoreParameters.java | ||
---|---|---|
1 |
/* gvSIG. Geographic Information System of the Valencian Government |
|
2 |
* |
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department |
|
4 |
* of the Valencian Government (CIT) |
|
5 |
* |
|
6 |
* This program is free software; you can redistribute it and/or |
|
7 |
* modify it under the terms of the GNU General Public License |
|
8 |
* as published by the Free Software Foundation; either version 2 |
|
9 |
* of the License, or (at your option) any later version. |
|
10 |
* |
|
11 |
* This program is distributed in the hope that it will be useful, |
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 |
* GNU General Public License for more details. |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License |
|
17 |
* along with this program; if not, write to the Free Software |
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
19 |
* MA 02110-1301, USA. |
|
20 |
* |
|
21 |
*/ |
|
22 |
|
|
23 |
/* |
|
24 |
* AUTHORS (In addition to CIT): |
|
25 |
* 2008 IVER T.I. S.A. {{Task}} |
|
26 |
*/ |
|
27 |
|
|
28 |
package org.gvsig.fmap.dal.store.shp; |
|
29 |
|
|
30 |
import java.io.File; |
|
31 |
|
|
32 |
import org.gvsig.fmap.dal.DataStoreParameters; |
|
33 |
import org.gvsig.fmap.dal.DataTypes; |
|
34 |
import org.gvsig.fmap.dal.store.dbf.DBFNewStoreParameters; |
|
35 |
import org.gvsig.fmap.dal.store.shp.utils.SHP; |
|
36 |
import org.gvsig.tools.exception.NotYetImplemented; |
|
37 |
import org.gvsig.tools.persistence.PersistenceException; |
|
38 |
import org.gvsig.tools.persistence.PersistentState; |
|
39 |
|
|
40 |
public class SHPNewStoreParameters extends DBFNewStoreParameters { |
|
41 |
public DataStoreParameters initialize() { |
|
42 |
super.initialize(); |
|
43 |
this.addParameter("shpfilename", DataTypes.STRING, |
|
44 |
"Ruta al fichero shp", |
|
45 |
null); |
|
46 |
this.addAttributeAlias("shpfilename", "filename"); |
|
47 |
this.addParameter("shxfilename", DataTypes.STRING, |
|
48 |
"Ruta al fichero shx", |
|
49 |
null); |
|
50 |
this.addParameter("srs", DataTypes.STRING, "SRS", null); |
|
51 |
return this; |
|
52 |
} |
|
53 |
|
|
54 |
public String getDataStoreName() { |
|
55 |
return SHPStoreProvider.NAME; |
|
56 |
} |
|
57 |
|
|
58 |
public boolean isValid() { |
|
59 |
return super.isValid() && (this.getSHPFileName() != null); |
|
60 |
} |
|
61 |
|
|
62 |
public File getFile() { |
|
63 |
return new File(this.getSHPFileName()); |
|
64 |
} |
|
65 |
|
|
66 |
public void setFile(File aFile) { |
|
67 |
this.setSHPFile(aFile); |
|
68 |
} |
|
69 |
|
|
70 |
public String getSHPFileName() { |
|
71 |
return (String) this.getAttribute("shpfilename"); |
|
72 |
} |
|
73 |
|
|
74 |
public File getSHPFile() { |
|
75 |
return new File((String) this.getAttribute("shpfilename")); |
|
76 |
} |
|
77 |
|
|
78 |
public void setSHPFile(File aFile) { |
|
79 |
this.setAttribute("shpfilename", aFile.getPath()); |
|
80 |
if (this.getDBFFileName() == null) { |
|
81 |
this.setDBFFile(SHP.getDbfFile(aFile)); |
|
82 |
} |
|
83 |
if (this.getSHXFileName() == null) { |
|
84 |
this.setSHXFile(SHP.getShxFile(aFile)); |
|
85 |
} |
|
86 |
} |
|
87 |
|
|
88 |
public void setSHPFileName(String aFileName) { |
|
89 |
this.setAttribute("shpfilename", aFileName); |
|
90 |
if (this.getDBFFileName() == null) { |
|
91 |
this.setDBFFile(SHP.getDbfFile(new File(aFileName))); |
|
92 |
} |
|
93 |
if (this.getSHXFileName() == null) { |
|
94 |
this.setSHXFile(SHP.getShxFile(new File(aFileName))); |
|
95 |
} |
|
96 |
} |
|
97 |
|
|
98 |
public String getSHXFileName() { |
|
99 |
return (String) this.getAttribute("shxfilename"); |
|
100 |
} |
|
101 |
|
|
102 |
public File getSHXFile() { |
|
103 |
return new File((String) this.getAttribute("shxfilename")); |
|
104 |
} |
|
105 |
public void setSHXFile(File aFile) { |
|
106 |
this.setAttribute("shxfilename", aFile.getPath()); |
|
107 |
} |
|
108 |
|
|
109 |
public void setSHXFileName(String aFileName) { |
|
110 |
this.setAttribute("shxfilename", aFileName); |
|
111 |
} |
|
112 |
|
|
113 |
public String getDescription() { |
|
114 |
return SHPStoreProvider.DESCRIPTION; |
|
115 |
} |
|
116 |
|
|
117 |
public void getState(PersistentState arg0) throws PersistenceException { |
|
118 |
// TODO Auto-generated method stub |
|
119 |
throw new NotYetImplemented(); |
|
120 |
} |
|
121 |
|
|
122 |
public void setState(PersistentState state) throws PersistenceException { |
|
123 |
// TODO Auto-generated method stub |
|
124 |
super.setState(state); |
|
125 |
throw new NotYetImplemented(); |
|
126 |
} |
|
127 |
|
|
128 |
} |
|
0 | 129 |
branches/v2_0_0_prep/libraries/libFMap_dalfile/src/org/gvsig/fmap/dal/store/shp/SHPStoreParameters.java | ||
---|---|---|
41 | 41 |
|
42 | 42 |
public DataStoreParameters initialize() { |
43 | 43 |
super.initialize(); |
44 |
this.addParameter("shpFile", DataTypes.STRING, "Ruta al fichero shp", |
|
44 |
this.addParameter("shpfilename", DataTypes.STRING, |
|
45 |
"Ruta al fichero shp", |
|
45 | 46 |
null); |
46 |
this.addAttributeAlias("shpFile", "filename"); |
|
47 |
this.addParameter("shxFile", DataTypes.STRING, "Ruta al fichero shx", |
|
47 |
this.addAttributeAlias("shpfilename", "filename"); |
|
48 |
this.addParameter("shxfilename", DataTypes.STRING, |
|
49 |
"Ruta al fichero shx", |
|
48 | 50 |
null); |
49 | 51 |
this.addParameter("srs", DataTypes.STRING, "SRS", null); |
50 | 52 |
return this; |
... | ... | |
55 | 57 |
} |
56 | 58 |
|
57 | 59 |
public boolean isValid() { |
58 |
return super.isValid() && (this.getSHPFile() != null); |
|
60 |
return super.isValid() && (this.getSHPFileName() != null);
|
|
59 | 61 |
} |
60 | 62 |
|
61 | 63 |
public File getFile() { |
62 |
return new File(this.getSHPFile()); |
|
64 |
return new File(this.getSHPFileName());
|
|
63 | 65 |
} |
64 | 66 |
|
65 | 67 |
public void setFile(File aFile) { |
66 | 68 |
this.setSHPFile(aFile); |
67 | 69 |
} |
68 | 70 |
|
69 |
public String getSHPFile() { |
|
70 |
return (String) this.getAttribute("shpFile");
|
|
71 |
public String getSHPFileName() {
|
|
72 |
return (String) this.getAttribute("shpfilename");
|
|
71 | 73 |
} |
72 | 74 |
|
73 |
public File getSHPFileObject() {
|
|
74 |
return new File((String) this.getAttribute("shpFile"));
|
|
75 |
public File getSHPFile() { |
|
76 |
return new File((String) this.getAttribute("shpfilename"));
|
|
75 | 77 |
} |
76 | 78 |
|
77 | 79 |
public void setSHPFile(File aFile) { |
78 |
this.setAttribute("shpFile", aFile.getPath());
|
|
80 |
this.setAttribute("shpfilename", aFile.getPath());
|
|
79 | 81 |
if (this.getDBFFileName() == null){ |
80 | 82 |
this.setDBFFile(SHP.getDbfFile(aFile)); |
81 | 83 |
} |
82 |
if (this.getSHXFile() == null) { |
|
84 |
if (this.getSHXFileName() == null) {
|
|
83 | 85 |
this.setSHXFile(SHP.getShxFile(aFile)); |
84 | 86 |
} |
85 | 87 |
} |
86 | 88 |
|
87 |
public void setSHPFile(String aFileName) { |
|
88 |
this.setAttribute("shpFile", aFileName);
|
|
89 |
public void setSHPFileName(String aFileName) {
|
|
90 |
this.setAttribute("shpfilename", aFileName);
|
|
89 | 91 |
if (this.getDBFFileName() == null) { |
90 | 92 |
this.setDBFFile(SHP.getDbfFile(new File(aFileName))); |
91 | 93 |
} |
92 |
if (this.getSHXFile() == null) { |
|
94 |
if (this.getSHXFileName() == null) {
|
|
93 | 95 |
this.setSHXFile(SHP.getShxFile(new File(aFileName))); |
94 | 96 |
} |
95 | 97 |
} |
96 | 98 |
|
97 |
public String getSHXFile() { |
|
98 |
return (String) this.getAttribute("shxFile");
|
|
99 |
public String getSHXFileName() {
|
|
100 |
return (String) this.getAttribute("shxfilename");
|
|
99 | 101 |
} |
100 | 102 |
|
101 |
public File getSHXFileObject() {
|
|
102 |
return new File((String) this.getAttribute("shxFile"));
|
|
103 |
public File getSHXFile() { |
|
104 |
return new File((String) this.getAttribute("shxfilename"));
|
|
103 | 105 |
} |
104 | 106 |
public void setSHXFile(File aFile) { |
105 |
this.setAttribute("shxFile", aFile.getPath());
|
|
107 |
this.setAttribute("shxfilename", aFile.getPath());
|
|
106 | 108 |
} |
107 | 109 |
|
108 |
public void setSHXFile(String aFileName) { |
|
109 |
this.setAttribute("shxFile", aFileName);
|
|
110 |
public void setSHXFileName(String aFileName) {
|
|
111 |
this.setAttribute("shxfilename", aFileName);
|
|
110 | 112 |
} |
111 | 113 |
|
112 | 114 |
|
branches/v2_0_0_prep/libraries/libFMap_dalfile/src/org/gvsig/fmap/dal/store/dbf/DBFStoreProvider.java | ||
---|---|---|
2 | 2 |
|
3 | 3 |
import java.io.File; |
4 | 4 |
import java.io.IOException; |
5 |
import java.io.RandomAccessFile; |
|
6 |
import java.nio.channels.FileChannel; |
|
7 |
import java.nio.channels.WritableByteChannel; |
|
8 |
import java.nio.charset.Charset; |
|
9 | 5 |
import java.text.DateFormat; |
10 | 6 |
import java.text.ParseException; |
11 | 7 |
import java.util.ArrayList; |
... | ... | |
55 | 51 |
import org.gvsig.fmap.dal.resource.spi.ResourceConsumer; |
56 | 52 |
import org.gvsig.fmap.dal.resource.spi.ResourceProvider; |
57 | 53 |
import org.gvsig.fmap.dal.store.dbf.utils.DbaseFile; |
58 |
import org.gvsig.fmap.dal.store.dbf.utils.DbaseFileHeader; |
|
59 |
import org.gvsig.fmap.dal.store.dbf.utils.DbaseFileWriter; |
|
60 | 54 |
import org.gvsig.metadata.Metadata; |
61 | 55 |
import org.gvsig.tools.exception.NotYetImplemented; |
62 | 56 |
import org.gvsig.tools.persistence.PersistenceException; |
... | ... | |
146 | 140 |
.getDefaultFeatureType()); |
147 | 141 |
} |
148 | 142 |
|
149 |
public void performEditing(Iterator deleteds, Iterator inserteds, Iterator updateds) throws PerformEditingException { |
|
143 |
public void performEditing(Iterator deleteds, Iterator inserteds, |
|
144 |
Iterator updateds) |
|
145 |
throws PerformEditingException { |
|
150 | 146 |
try { |
151 | 147 |
this.close(); |
152 | 148 |
} catch (CloseException e1) { |
153 | 149 |
throw new PerformEditingException(this.getName(), e1); |
154 | 150 |
} |
155 |
File file = this.getDBFParameters().getFile(); |
|
156 | 151 |
|
157 |
|
|
158 |
// TODO sacar copia o grabar en nuevo? |
|
159 |
|
|
160 | 152 |
try { |
161 | 153 |
this.dbfResource.begin(); |
162 |
|
|
163 |
|
|
164 |
DbaseFileHeader myHeader = DbaseFileHeader.createDbaseHeader(this.store.getDefaultFeatureType()); |
|
165 |
|
|
154 |
DBFFeatureWriter writer = new DBFFeatureWriter(this.getName()); |
|
166 | 155 |
FeatureSet set = this.store.getFeatureSet(); |
167 |
myHeader.setNumRecords((int) set.getSize()); //FIXME long !!! |
|
168 |
DbaseFileWriter dbfWrite; |
|
169 |
try { |
|
170 |
dbfWrite = new DbaseFileWriter( |
|
171 |
myHeader, |
|
172 |
(FileChannel) getWriteChannel(file.getAbsolutePath())); |
|
173 |
dbfWrite.setCharset(Charset.forName("ISO-8859-1")); // FIXME !!!! y actualizar codePage |
|
174 |
} catch (IOException e) { |
|
175 |
throw new CreateException(this.getName(), e); |
|
176 |
} |
|
156 |
writer.begin(this.getDBFParameters(), this.store |
|
157 |
.getDefaultFeatureType(), set.getSize()); |
|
177 | 158 |
|
178 | 159 |
Iterator iter = set.iterator(); |
179 | 160 |
while (iter.hasNext()) { |
180 |
dbfWrite.write((Feature) updateds.next());
|
|
161 |
writer.write((Feature) iter.next());
|
|
181 | 162 |
} |
182 |
dbfWrite.close(); |
|
183 | 163 |
|
164 |
writer.end(); |
|
165 |
|
|
184 | 166 |
this.dbfResource.notifyChanges(); |
185 | 167 |
} catch (Exception e) { |
186 | 168 |
throw new PerformEditingException(this.getName(), e); |
... | ... | |
592 | 574 |
throws CreateException { |
593 | 575 |
|
594 | 576 |
DBFNewStoreParameters params = (DBFNewStoreParameters) parameters; |
595 |
File file = params.getFile(); |
|
577 |
File file = params.getDBFFile();
|
|
596 | 578 |
|
597 | 579 |
if (file.exists()) { |
598 | 580 |
if (overwrite) { |
... | ... | |
620 | 602 |
try { |
621 | 603 |
resource.begin(); |
622 | 604 |
|
605 |
DBFFeatureWriter writer = new DBFFeatureWriter(this.getName()); |
|
623 | 606 |
|
624 | 607 |
|
625 |
DbaseFileHeader myHeader = DbaseFileHeader.createDbaseHeader(params |
|
626 |
.getDefaultFeatureType()); |
|
608 |
writer.begin(params, params.getDefaultFeatureType(), 0); |
|
609 |
writer.end(); |
|
610 |
writer.dispose(); |
|
627 | 611 |
|
628 |
myHeader.setNumRecords(0); |
|
629 |
try { |
|
630 | 612 |
|
631 |
FileChannel filechannel = (FileChannel) getWriteChannel(file |
|
632 |
.getAbsolutePath()); |
|
633 |
new DbaseFileWriter(myHeader, filechannel); |
|
634 |
filechannel.close(); |
|
635 |
} catch (IOException e) { |
|
636 |
throw new CreateException(this.getName(), e); |
|
637 |
} |
|
638 |
|
|
639 | 613 |
resource.notifyChanges(); |
640 | 614 |
} catch (Exception e) { |
641 | 615 |
throw new CreateException(this.getName(), e); |
... | ... | |
645 | 619 |
} |
646 | 620 |
} |
647 | 621 |
|
622 |
protected NewDataStoreParameters createInstanceNewDataStoreParameters() { |
|
623 |
return new DBFNewStoreParameters(); |
|
624 |
} |
|
648 | 625 |
public NewDataStoreParameters getCreateParameters() throws DataException { |
649 |
DBFNewStoreParameters params = new DBFNewStoreParameters(); |
|
626 |
DBFNewStoreParameters params = (DBFNewStoreParameters) this |
|
627 |
.createInstanceNewDataStoreParameters(); |
|
650 | 628 |
// FIXME Como crear un FeatureType ?????????? |
651 | 629 |
// params.setDefaultFeatureType(this.store.createFeatureType()); |
652 | 630 |
return params; |
653 | 631 |
} |
654 | 632 |
|
655 | 633 |
|
656 |
protected static WritableByteChannel getWriteChannel(String path) |
|
657 |
throws IOException { |
|
658 |
WritableByteChannel channel; |
|
659 |
|
|
660 |
File f = new File(path); |
|
661 |
|
|
662 |
if (!f.exists()) { |
|
663 |
// System.out.println("Creando fichero " + f.getAbsolutePath()); |
|
664 |
|
|
665 |
if (!f.createNewFile()) { |
|
666 |
System.err.print("Error al crear el fichero " |
|
667 |
+ f.getAbsolutePath()); |
|
668 |
throw new IOException("Cannot create file " + f); |
|
669 |
} |
|
670 |
} |
|
671 |
|
|
672 |
RandomAccessFile raf = new RandomAccessFile(f, "rw"); |
|
673 |
channel = raf.getChannel(); |
|
674 |
|
|
675 |
return channel; |
|
676 |
} |
|
677 |
|
|
678 |
|
|
679 | 634 |
public boolean supportsAppendMode() { |
680 | 635 |
return false; |
681 | 636 |
} |
... | ... | |
688 | 643 |
} |
689 | 644 |
|
690 | 645 |
public void beginAppend() { |
691 |
// TODO Auto-generated method stub |
|
692 |
throw new NotYetImplemented(); |
|
693 | 646 |
|
694 | 647 |
} |
695 | 648 |
|
branches/v2_0_0_prep/libraries/libFMap_dalfile/src/org/gvsig/fmap/dal/store/dbf/DBFFeatureWriter.java | ||
---|---|---|
1 |
/* gvSIG. Geographic Information System of the Valencian Government |
|
2 |
* |
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department |
|
4 |
* of the Valencian Government (CIT) |
|
5 |
* |
|
6 |
* This program is free software; you can redistribute it and/or |
|
7 |
* modify it under the terms of the GNU General Public License |
|
8 |
* as published by the Free Software Foundation; either version 2 |
|
9 |
* of the License, or (at your option) any later version. |
|
10 |
* |
|
11 |
* This program is distributed in the hope that it will be useful, |
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 |
* GNU General Public License for more details. |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License |
|
17 |
* along with this program; if not, write to the Free Software |
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
19 |
* MA 02110-1301, USA. |
|
20 |
* |
|
21 |
*/ |
|
22 |
|
|
23 |
/* |
|
24 |
* AUTHORS (In addition to CIT): |
|
25 |
* 2008 IVER T.I. S.A. {{Task}} |
|
26 |
*/ |
|
27 |
|
|
28 |
package org.gvsig.fmap.dal.store.dbf; |
|
29 |
|
|
30 |
import java.io.File; |
|
31 |
import java.io.IOException; |
|
32 |
import java.io.RandomAccessFile; |
|
33 |
import java.nio.channels.FileChannel; |
|
34 |
import java.nio.channels.WritableByteChannel; |
|
35 |
import java.nio.charset.Charset; |
|
36 |
|
|
37 |
import org.gvsig.fmap.dal.exception.DataException; |
|
38 |
import org.gvsig.fmap.dal.exception.InitializeException; |
|
39 |
import org.gvsig.fmap.dal.exception.WriteException; |
|
40 |
import org.gvsig.fmap.dal.feature.Feature; |
|
41 |
import org.gvsig.fmap.dal.feature.FeatureType; |
|
42 |
import org.gvsig.fmap.dal.feature.exception.AttributeFeatureTypeNotSuportedException; |
|
43 |
import org.gvsig.fmap.dal.store.dbf.utils.DbaseFileHeader; |
|
44 |
import org.gvsig.fmap.dal.store.dbf.utils.DbaseFileWriter; |
|
45 |
|
|
46 |
public class DBFFeatureWriter { |
|
47 |
|
|
48 |
private DbaseFileWriter dbfWriter = null; |
|
49 |
protected String name; |
|
50 |
|
|
51 |
protected DBFFeatureWriter(String name) { |
|
52 |
this.name= name; |
|
53 |
} |
|
54 |
|
|
55 |
public void begin(DBFStoreParameters storeParameters, |
|
56 |
FeatureType featureType, long numRows) throws DataException { |
|
57 |
|
|
58 |
File file = storeParameters.getDBFFile(); |
|
59 |
|
|
60 |
DbaseFileHeader myHeader; |
|
61 |
try { |
|
62 |
myHeader = DbaseFileHeader.createDbaseHeader(featureType); |
|
63 |
} catch (AttributeFeatureTypeNotSuportedException e1) { |
|
64 |
throw new WriteException(this.name, e1); |
|
65 |
} |
|
66 |
|
|
67 |
|
|
68 |
myHeader.setNumRecords((int) numRows); //FIXME long !!! |
|
69 |
try { |
|
70 |
this.dbfWriter = new DbaseFileWriter(myHeader, |
|
71 |
(FileChannel) getWriteChannel(file.getAbsolutePath())); |
|
72 |
dbfWriter.setCharset(Charset.forName("ISO-8859-1")); // FIXME !!!! y actualizar codePage |
|
73 |
} catch (IOException e) { |
|
74 |
throw new WriteException(this.name, e); |
|
75 |
} catch (InitializeException e) { |
|
76 |
throw new WriteException(this.name, e); |
|
77 |
} |
|
78 |
|
|
79 |
} |
|
80 |
|
|
81 |
public void write(Feature feature) throws DataException { |
|
82 |
dbfWriter.append(feature); |
|
83 |
} |
|
84 |
|
|
85 |
public void end() throws DataException { |
|
86 |
dbfWriter.close(); |
|
87 |
} |
|
88 |
|
|
89 |
public void dispose() { |
|
90 |
dbfWriter = null; |
|
91 |
|
|
92 |
} |
|
93 |
|
|
94 |
protected static WritableByteChannel getWriteChannel(String path) |
|
95 |
throws IOException { |
|
96 |
WritableByteChannel channel; |
|
97 |
|
|
98 |
File f = new File(path); |
|
99 |
|
|
100 |
if (!f.exists()) { |
|
101 |
// System.out.println("Creando fichero " + f.getAbsolutePath()); |
|
102 |
|
|
103 |
if (!f.createNewFile()) { |
|
104 |
System.err.print("Error al crear el fichero " |
|
105 |
+ f.getAbsolutePath()); |
|
106 |
throw new IOException("Cannot create file " + f); |
|
107 |
} |
|
108 |
} |
|
109 |
|
|
110 |
RandomAccessFile raf = new RandomAccessFile(f, "rw"); |
|
111 |
channel = raf.getChannel(); |
|
112 |
|
|
113 |
return channel; |
|
114 |
} |
|
115 |
|
|
116 |
|
|
117 |
} |
|
0 | 118 |
branches/v2_0_0_prep/libraries/libFMap_dalfile/src/org/gvsig/fmap/dal/store/dbf/utils/DbaseFileWriter.java | ||
---|---|---|
66 | 66 |
import java.nio.ByteBuffer; |
67 | 67 |
import java.nio.MappedByteBuffer; |
68 | 68 |
import java.nio.channels.FileChannel; |
69 |
import java.nio.channels.WritableByteChannel; |
|
70 | 69 |
import java.nio.charset.Charset; |
71 | 70 |
import java.text.FieldPosition; |
72 | 71 |
import java.text.NumberFormat; |
... | ... | |
84 | 83 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
85 | 84 |
import org.gvsig.fmap.dal.feature.FeatureType; |
86 | 85 |
|
87 |
|
|
88 |
/** A DbaseFileReader is used to read a dbase III format file. |
|
89 |
* The general use of this class is: |
|
90 |
* <CODE><PRE> |
|
86 |
/** |
|
87 |
* A DbaseFileReader is used to read a dbase III format file. The general use of |
|
88 |
* this class is: <CODE><PRE> |
|
91 | 89 |
* DbaseFileHeader header = ... |
92 | 90 |
* WritableFileChannel out = new FileOutputStream("thefile.dbf").getChannel(); |
93 | 91 |
* DbaseFileWriter w = new DbaseFileWriter(header,out); |
... | ... | |
95 | 93 |
* w.write( getMyRecord() ); |
96 | 94 |
* } |
97 | 95 |
* w.close(); |
98 |
* </PRE></CODE> |
|
99 |
* You must supply the <CODE>moreRecords</CODE> and <CODE>getMyRecord()</CODE>
|
|
100 |
* logic...
|
|
96 |
* </PRE></CODE> You must supply the <CODE>moreRecords</CODE> and
|
|
97 |
* <CODE>getMyRecord()</CODE> logic...
|
|
98 |
* |
|
101 | 99 |
* @author Ian Schneider |
102 | 100 |
*/ |
103 |
public class DbaseFileWriter {
|
|
101 |
public class DbaseFileWriter { |
|
104 | 102 |
|
105 |
private DbaseFileHeader header; |
|
106 |
private DbaseFileWriter.FieldFormatter formatter = new DbaseFileWriter.FieldFormatter(); |
|
107 |
WritableByteChannel channel; |
|
108 |
private ByteBuffer buffer; |
|
109 |
private final Number NULL_NUMBER = new Integer(0); |
|
110 |
private final String NULL_STRING = ""; |
|
111 |
private final String NULL_DATE = " "; |
|
103 |
private DbaseFileHeader header; |
|
104 |
private DbaseFileWriter.FieldFormatter formatter = new DbaseFileWriter.FieldFormatter(); |
|
105 |
FileChannel channel; |
|
106 |
private ByteBuffer buffer; |
|
107 |
private final Number NULL_NUMBER = new Integer(0); |
|
108 |
private final String NULL_STRING = ""; |
|
109 |
private final String NULL_DATE = " "; |
|
110 |
private boolean headDrity = false; |
|
112 | 111 |
|
113 |
// TODO: READ HEADER AND STABLIST THE RIGHT CHARSET
|
|
114 |
private Charset charset = Charset.forName("ISO-8859-1");
|
|
112 |
// TODO: READ HEADER AND STABLIST THE RIGHT CHARSET
|
|
113 |
private Charset charset = Charset.forName("ISO-8859-1");
|
|
115 | 114 |
|
116 |
/** Create a DbaseFileWriter using the specified header and writing to the given |
|
117 |
* channel. |
|
118 |
* @param header The DbaseFileHeader to write. |
|
119 |
* @param out The Channel to write to. |
|
120 |
* @throws InitializeWriterException |
|
121 |
* @throws IOException If errors occur while initializing. |
|
122 |
*/ |
|
123 |
public DbaseFileWriter(DbaseFileHeader header, FileChannel out) |
|
115 |
/** |
|
116 |
* Create a DbaseFileWriter using the specified header and writing to the |
|
117 |
* given channel. |
|
118 |
* |
|
119 |
* @param header |
|
120 |
* The DbaseFileHeader to write. |
|
121 |
* @param out |
|
122 |
* The Channel to write to. |
|
123 |
* @throws InitializeWriterException |
|
124 |
* @throws IOException |
|
125 |
* If errors occur while initializing. |
|
126 |
*/ |
|
127 |
public DbaseFileWriter(DbaseFileHeader header, FileChannel out) |
|
124 | 128 |
throws InitializeException { |
125 |
try {
|
|
126 |
header.writeHeader(out);
|
|
127 |
} catch (IOException e) { |
|
128 |
throw new InitializeException(e);
|
|
129 |
this.header = header;
|
|
130 |
this.channel = out;
|
|
131 |
|
|
132 |
init();
|
|
129 | 133 |
} |
130 |
this.header = header; |
|
131 |
this.channel = out; |
|
132 | 134 |
|
133 |
init(); |
|
134 |
} |
|
135 |
private void init() throws InitializeException { |
|
136 |
try { |
|
137 |
if (this.channel.size() < this.header.getHeaderLength()) { |
|
138 |
this.writeHeader(); |
|
139 |
} |
|
140 |
buffer = ByteBuffer.allocateDirect(header.getRecordLength()); |
|
141 |
} catch (Exception e) { |
|
142 |
throw new InitializeException("DBF Writer", e); |
|
143 |
} |
|
144 |
} |
|
135 | 145 |
|
136 |
private void init() throws InitializeException { |
|
137 |
try{ |
|
138 |
buffer = ByteBuffer.allocateDirect(header.getRecordLength()); |
|
139 |
}catch (Exception e) { |
|
140 |
throw new InitializeException("DBF Writer", e); |
|
146 |
private void write() throws WriteException { |
|
147 |
buffer.position(0); |
|
148 |
int r = buffer.remaining(); |
|
149 |
try { |
|
150 |
while ((r -= channel.write(buffer)) > 0) { |
|
151 |
; // do nothing |
|
152 |
} |
|
153 |
} catch (IOException e) { |
|
154 |
throw new WriteException("DBF Writer", e); |
|
155 |
} |
|
141 | 156 |
} |
142 |
} |
|
143 | 157 |
|
144 |
private void write() throws WriteException {
|
|
145 |
buffer.position(0);
|
|
146 |
int r = buffer.remaining();
|
|
147 |
try {
|
|
148 |
while ( (r -= channel.write(buffer)) > 0) {
|
|
149 |
; // do nothing
|
|
158 |
private void writeHeader() throws WriteException {
|
|
159 |
try {
|
|
160 |
channel.position(0);
|
|
161 |
header.writeHeader(channel);
|
|
162 |
} catch (IOException e) {
|
|
163 |
throw new WriteException("DBF Writer", e);
|
|
150 | 164 |
} |
151 |
} catch (IOException e) { |
|
152 |
throw new WriteException("DBF Writer",e); |
|
153 | 165 |
} |
154 |
} |
|
155 | 166 |
|
167 |
/** |
|
168 |
* Write a single dbase record. |
|
169 |
* |
|
170 |
* @param record |
|
171 |
* The entries to write. |
|
172 |
* @throws UnsupportedEncodingException |
|
173 |
* @throws WriteException |
|
174 |
*/ |
|
175 |
public void append(Feature feature) throws WriteException, |
|
176 |
UnsupportedEncodingException { |
|
177 |
this.fillBuffer(feature); |
|
178 |
try { |
|
179 |
this.moveToEOF(); |
|
180 |
} catch (IOException e) { |
|
181 |
throw new WriteException("DbaseFileWriter", e); |
|
182 |
} |
|
183 |
write(); |
|
184 |
this.header.setNumRecords(this.header.getNumRecords() + 1); |
|
185 |
this.headDrity = true; |
|
186 |
} |
|
156 | 187 |
|
157 |
/** Write a single dbase record. |
|
158 |
* @param record The entries to write. |
|
159 |
* @throws UnsupportedEncodingException |
|
160 |
* @throws WriteException |
|
161 |
* @throws UnsupportedEncodingDriverException |
|
162 |
* @throws IOException If IO error occurs. |
|
163 |
* @throws DbaseFileException If the entry doesn't comply to the header. |
|
164 |
*/ |
|
165 |
public void write(Feature feature) throws WriteException, UnsupportedEncodingException{ |
|
166 |
FeatureType featureType=feature.getType(); |
|
188 |
private void fillBuffer(Feature feature) |
|
189 |
throws UnsupportedEncodingException { |
|
190 |
FeatureType featureType = feature.getType(); |
|
167 | 191 |
|
168 |
buffer.position(0);
|
|
192 |
buffer.position(0);
|
|
169 | 193 |
|
170 |
// put the 'not-deleted' marker
|
|
171 |
buffer.put( (byte) ' ');
|
|
194 |
// put the 'not-deleted' marker
|
|
195 |
buffer.put((byte) ' ');
|
|
172 | 196 |
|
173 |
Iterator iterator=featureType.iterator(); |
|
174 |
while (iterator.hasNext()) { |
|
175 |
FeatureAttributeDescriptor fad = (FeatureAttributeDescriptor) iterator.next(); |
|
176 |
int type = fad.getDataType(); |
|
177 |
int i = fad.getIndex(); // TODO comprobar |
|
178 |
// int i=fad.ordinal(); |
|
179 |
if (featureType.getDefaultGeometryAttributeName()!=null && featureType.getDefaultGeometryAttributeName().equals(fad.getName())){ |
|
180 |
continue; |
|
181 |
} |
|
182 |
String fieldString = fieldString(type, feature, i); |
|
183 |
if (fieldString==null){ |
|
184 |
if (type == DataTypes.STRING) { |
|
185 |
fieldString = NULL_STRING; |
|
186 |
} else if (type == DataTypes.DATE) { |
|
187 |
fieldString = NULL_DATE; |
|
188 |
}else{ |
|
189 |
fieldString = "0"; |
|
190 |
} |
|
191 |
} |
|
192 |
try { |
|
193 |
buffer.put(fieldString.getBytes(charset.name())); |
|
194 |
} catch (java.io.UnsupportedEncodingException e) { |
|
195 |
throw new UnsupportedEncodingException(e); |
|
196 |
} |
|
197 |
} |
|
198 |
write(); |
|
199 |
} |
|
197 |
Iterator iterator = featureType.iterator(); |
|
198 |
while (iterator.hasNext()) { |
|
199 |
FeatureAttributeDescriptor fad = (FeatureAttributeDescriptor) iterator |
|
200 |
.next(); |
|
201 |
int type = fad.getDataType(); |
|
202 |
int i = fad.getIndex(); // TODO comprobar |
|
203 |
// int i=fad.ordinal(); |
|
204 |
String fieldString = fieldString(type, feature, i); |
|
205 |
if (fieldString == null) { |
|
206 |
if (type == DataTypes.STRING) { |
|
207 |
fieldString = NULL_STRING; |
|
208 |
} else if (type == DataTypes.DATE) { |
|
209 |
fieldString = NULL_DATE; |
|
210 |
} else { |
|
211 |
fieldString = "0"; |
|
212 |
} |
|
213 |
} |
|
214 |
try { |
|
215 |
buffer.put(fieldString.getBytes(charset.name())); |
|
216 |
} catch (java.io.UnsupportedEncodingException e) { |
|
217 |
throw new UnsupportedEncodingException(e); |
|
218 |
} |
|
219 |
} |
|
200 | 220 |
|
201 |
/** Write a single dbase record. Useful to update a dbf. |
|
202 |
* @param record The entries to write. |
|
203 |
* @throws WriteException |
|
204 |
* @throws UnsupportedEncodingException |
|
205 |
* @throws |
|
206 |
* @throws IOException If IO error occurs. |
|
207 |
* @throws DbaseFileException If the entry doesn't comply to the header. |
|
208 |
*/ |
|
209 |
public void writeRecord(Feature feature, int numReg) throws WriteException, UnsupportedEncodingException{ |
|
210 |
FeatureType featureType=feature.getType(); |
|
211 |
if (!(channel instanceof FileChannel)) { |
|
212 |
throw new WriteException("DBF Writer",new IOException("DbaseFileWriterNIO: channel is not a FileChannel. Cannot position properly")); |
|
213 | 221 |
} |
214 | 222 |
|
215 |
if (featureType.size() != header.getNumFields()) { |
|
216 |
throw new WriteException("DBF Writer",new IOException("Wrong number of fields " + featureType.size() + |
|
217 |
" expected " + header.getNumFields())); |
|
218 |
} |
|
223 |
private void moveToEOF() throws IOException { |
|
224 |
this.moveTo(this.header.getNumRecords() + 1); |
|
225 |
} |
|
219 | 226 |
|
220 |
FileChannel fileChannel = (FileChannel) channel; |
|
221 |
long newPos = header.getHeaderLength() + numReg*header.getRecordLength(); |
|
227 |
private void moveTo(long numReg) throws IOException { |
|
228 |
// if (!(channel instanceof FileChannel)) { |
|
229 |
// throw new IOException( |
|
230 |
// "DbaseFileWriterNIO: channel is not a FileChannel. Cannot position properly"); |
|
231 |
// } |
|
222 | 232 |
|
223 |
buffer.position(0); |
|
233 |
long newPos = header.getHeaderLength() + numReg |
|
234 |
* header.getRecordLength(); |
|
235 |
if (this.channel.position() != newPos) { |
|
236 |
this.channel.position(newPos); |
|
237 |
} |
|
238 |
} |
|
224 | 239 |
|
225 |
// put the 'not-deleted' marker |
|
226 |
buffer.put( (byte) ' '); |
|
240 |
/** |
|
241 |
* Write a single dbase record. Useful to update a dbf. |
|
242 |
* |
|
243 |
* @param record |
|
244 |
* The entries to write. |
|
245 |
* @throws WriteException |
|
246 |
* @throws UnsupportedEncodingException |
|
247 |
*/ |
|
248 |
public void update(Feature feature, long numReg) throws WriteException, |
|
249 |
UnsupportedEncodingException { |
|
250 |
this.fillBuffer(feature); |
|
227 | 251 |
|
228 |
Iterator iterator=featureType.iterator(); |
|
229 |
while (iterator.hasNext()) { |
|
230 |
FeatureAttributeDescriptor fad = (FeatureAttributeDescriptor) iterator.next(); |
|
231 |
int i = fad.getIndex(); // TODO comporbar originalPosition(); |
|
232 |
int type = fad.getDataType(); |
|
233 |
String fieldString = fieldString(type, feature, i); |
|
234 |
try { |
|
235 |
buffer.put(fieldString.getBytes(charset.name())); |
|
236 |
} catch (java.io.UnsupportedEncodingException e) { |
|
237 |
throw new UnsupportedEncodingException(e); |
|
238 |
} |
|
239 |
} |
|
252 |
try { |
|
253 |
this.moveTo(numReg); |
|
254 |
} catch (IOException e) { |
|
255 |
throw new WriteException("DbaseFileWriter", e); |
|
256 |
} |
|
240 | 257 |
|
241 |
try { |
|
242 |
fileChannel.write(buffer, newPos); |
|
243 |
} catch (IOException e) { |
|
244 |
throw new WriteException("DBF Writer",e); |
|
258 |
write(); |
|
245 | 259 |
} |
246 |
} |
|
247 | 260 |
|
248 |
private String fieldString(int type, Feature feature, int i) { |
|
249 |
final int fieldLen = header.getFieldLength(i); |
|
250 |
String fieldString=""; |
|
251 |
if (DataTypes.BOOLEAN == type) { |
|
252 |
boolean b=feature.getBoolean(i); |
|
253 |
if (b) { |
|
254 |
fieldString="T"; |
|
255 |
} else { |
|
256 |
fieldString="F"; |
|
261 |
private String fieldString(int type, Feature feature, int i) { |
|
262 |
final int fieldLen = header.getFieldLength(i); |
|
263 |
String fieldString = ""; |
|
264 |
if (DataTypes.BOOLEAN == type) { |
|
265 |
boolean b = feature.getBoolean(i); |
Also available in: Unified diff