svn-gvsig-desktop / trunk / extensions / extCAD / src / com / iver / cit / gvsig / ExportTo.java @ 6880
History | View | Annotate | Download (23.5 KB)
1 |
package com.iver.cit.gvsig; |
---|---|
2 |
|
3 |
import java.awt.Component; |
4 |
import java.awt.geom.Point2D; |
5 |
import java.io.File; |
6 |
import java.io.IOException; |
7 |
import java.sql.Connection; |
8 |
import java.sql.DriverManager; |
9 |
import java.sql.SQLException; |
10 |
import java.sql.Types; |
11 |
import java.util.Vector; |
12 |
|
13 |
import javax.swing.JComponent; |
14 |
import javax.swing.JFileChooser; |
15 |
import javax.swing.JOptionPane; |
16 |
import javax.swing.ProgressMonitor; |
17 |
|
18 |
import com.hardcode.driverManager.Driver; |
19 |
import com.hardcode.driverManager.DriverLoadException; |
20 |
import com.hardcode.gdbms.engine.values.Value; |
21 |
import com.iver.andami.PluginServices; |
22 |
import com.iver.andami.messages.NotificationManager; |
23 |
import com.iver.andami.plugins.Extension; |
24 |
import com.iver.cit.gvsig.fmap.DriverException; |
25 |
import com.iver.cit.gvsig.fmap.MapContext; |
26 |
import com.iver.cit.gvsig.fmap.core.DefaultFeature; |
27 |
import com.iver.cit.gvsig.fmap.core.FShape; |
28 |
import com.iver.cit.gvsig.fmap.core.IFeature; |
29 |
import com.iver.cit.gvsig.fmap.core.IGeometry; |
30 |
import com.iver.cit.gvsig.fmap.core.ShapeFactory; |
31 |
import com.iver.cit.gvsig.fmap.core.v02.FLabel; |
32 |
import com.iver.cit.gvsig.fmap.drivers.DBLayerDefinition; |
33 |
import com.iver.cit.gvsig.fmap.drivers.DriverIOException; |
34 |
import com.iver.cit.gvsig.fmap.drivers.FieldDescription; |
35 |
import com.iver.cit.gvsig.fmap.drivers.ILayerDefinition; |
36 |
import com.iver.cit.gvsig.fmap.drivers.SHPLayerDefinition; |
37 |
import com.iver.cit.gvsig.fmap.drivers.VectorialDriver; |
38 |
import com.iver.cit.gvsig.fmap.drivers.dxf.DXFMemoryDriver; |
39 |
import com.iver.cit.gvsig.fmap.drivers.gml.GMLDriver; |
40 |
import com.iver.cit.gvsig.fmap.drivers.jdbc.postgis.PostGISWriter; |
41 |
import com.iver.cit.gvsig.fmap.drivers.jdbc.postgis.PostGisDriver; |
42 |
import com.iver.cit.gvsig.fmap.drivers.shp.IndexedShpDriver; |
43 |
import com.iver.cit.gvsig.fmap.edition.DefaultRowEdited; |
44 |
import com.iver.cit.gvsig.fmap.edition.EditionException; |
45 |
import com.iver.cit.gvsig.fmap.edition.IWriter; |
46 |
import com.iver.cit.gvsig.fmap.edition.writers.dxf.DxfFieldsMapping; |
47 |
import com.iver.cit.gvsig.fmap.edition.writers.dxf.DxfWriter; |
48 |
import com.iver.cit.gvsig.fmap.edition.writers.gml.GMLWriter; |
49 |
import com.iver.cit.gvsig.fmap.edition.writers.shp.ShpWriter; |
50 |
import com.iver.cit.gvsig.fmap.layers.FBitSet; |
51 |
import com.iver.cit.gvsig.fmap.layers.FLayer; |
52 |
import com.iver.cit.gvsig.fmap.layers.FLayers; |
53 |
import com.iver.cit.gvsig.fmap.layers.FLyrAnnotation; |
54 |
import com.iver.cit.gvsig.fmap.layers.FLyrVect; |
55 |
import com.iver.cit.gvsig.fmap.layers.LayerFactory; |
56 |
import com.iver.cit.gvsig.fmap.layers.ReadableVectorial; |
57 |
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource; |
58 |
import com.iver.cit.gvsig.gui.View; |
59 |
import com.iver.cit.gvsig.jdbc_spatial.DlgConnection; |
60 |
import com.iver.cit.gvsig.jdbc_spatial.gui.jdbcwizard.ConnectionSettings; |
61 |
import com.iver.cit.gvsig.project.ProjectView; |
62 |
import com.iver.utiles.PostProcessSupport; |
63 |
import com.iver.utiles.SimpleFileFilter; |
64 |
import com.iver.utiles.swing.threads.AbstractMonitorableTask; |
65 |
|
66 |
public class ExportTo extends Extension { |
67 |
|
68 |
private class WriterTask extends AbstractMonitorableTask |
69 |
{ |
70 |
FLyrVect lyrVect; |
71 |
IWriter writer; |
72 |
int rowCount;
|
73 |
ReadableVectorial va; |
74 |
SelectableDataSource sds; |
75 |
FBitSet bitSet; |
76 |
MapContext mapContext; |
77 |
VectorialDriver reader; |
78 |
|
79 |
public WriterTask(MapContext mapContext, FLyrVect lyr, IWriter writer, Driver reader) throws DriverException, DriverIOException |
80 |
{ |
81 |
this.mapContext = mapContext;
|
82 |
this.lyrVect = lyr;
|
83 |
this.writer = writer;
|
84 |
this.reader = (VectorialDriver) reader;
|
85 |
|
86 |
setInitialStep(0);
|
87 |
setDeterminatedProcess(true);
|
88 |
setStatusMessage(PluginServices.getText(this, "exportando_features")); |
89 |
|
90 |
va = lyrVect.getSource(); |
91 |
sds = lyrVect.getRecordset(); |
92 |
|
93 |
bitSet = sds.getSelection(); |
94 |
|
95 |
if (bitSet.cardinality() == 0) |
96 |
rowCount = va.getShapeCount(); |
97 |
else
|
98 |
rowCount = bitSet.cardinality(); |
99 |
|
100 |
setFinalStep(rowCount); |
101 |
|
102 |
} |
103 |
public void run() throws Exception { |
104 |
|
105 |
if (lyrVect instanceof FLyrAnnotation && lyrVect.getShapeType()!=FShape.POINT) { |
106 |
SHPLayerDefinition lyrDef=(SHPLayerDefinition)writer.getTableDefinition(); |
107 |
lyrDef.setShapeType(FShape.POINT); |
108 |
writer.initialize(lyrDef); |
109 |
} |
110 |
|
111 |
// Creamos la tabla.
|
112 |
writer.preProcess(); |
113 |
|
114 |
if (bitSet.cardinality() == 0) { |
115 |
rowCount = va.getShapeCount(); |
116 |
for (int i = 0; i < rowCount; i++) { |
117 |
IGeometry geom = va.getShape(i); |
118 |
if (lyrVect instanceof FLyrAnnotation && geom.getGeometryType()!=FShape.POINT) { |
119 |
Point2D p=FLabel.createLabelPoint((FShape)geom.getInternalShape());
|
120 |
geom=ShapeFactory.createPoint2D(p.getX(),p.getY()); |
121 |
} |
122 |
reportStep(); |
123 |
setNote(PluginServices.getText(this, "exporting_") + i); |
124 |
if (isCanceled())
|
125 |
break;
|
126 |
|
127 |
if (geom != null) { |
128 |
Value[] values = sds.getRow(i);
|
129 |
IFeature feat = new DefaultFeature(geom, values, "" + i); |
130 |
DefaultRowEdited edRow = new DefaultRowEdited(feat,
|
131 |
DefaultRowEdited.STATUS_ADDED, i); |
132 |
writer.process(edRow); |
133 |
} |
134 |
} |
135 |
} else {
|
136 |
int counter = 0; |
137 |
for (int i = bitSet.nextSetBit(0); i >= 0; i = bitSet |
138 |
.nextSetBit(i + 1)) {
|
139 |
IGeometry geom = va.getShape(i); |
140 |
if (lyrVect instanceof FLyrAnnotation && geom.getGeometryType()!=FShape.POINT) { |
141 |
Point2D p=FLabel.createLabelPoint((FShape)geom.getInternalShape());
|
142 |
geom=ShapeFactory.createPoint2D(p.getX(),p.getY()); |
143 |
} |
144 |
reportStep(); |
145 |
setNote(PluginServices.getText(this, "exporting_") + counter); |
146 |
if (isCanceled())
|
147 |
break;
|
148 |
|
149 |
if (geom != null) { |
150 |
Value[] values = sds.getRow(i);
|
151 |
IFeature feat = new DefaultFeature(geom, values, "" + i); |
152 |
DefaultRowEdited edRow = new DefaultRowEdited(feat,
|
153 |
DefaultRowEdited.STATUS_ADDED, i); |
154 |
|
155 |
writer.process(edRow); |
156 |
} |
157 |
} |
158 |
|
159 |
} |
160 |
|
161 |
writer.postProcess(); |
162 |
if (reader != null){ |
163 |
int res = JOptionPane.showConfirmDialog( |
164 |
(JComponent) PluginServices.getMDIManager().getActiveWindow()
|
165 |
, PluginServices.getText(this, "insertar_en_la_vista_la_capa_creada"), |
166 |
PluginServices.getText(this,"insertar_capa"), |
167 |
JOptionPane.YES_NO_OPTION);
|
168 |
|
169 |
if (res == JOptionPane.YES_OPTION) |
170 |
{ |
171 |
PostProcessSupport.executeCalls(); |
172 |
ILayerDefinition lyrDef = (ILayerDefinition) writer.getTableDefinition(); |
173 |
FLayer newLayer = LayerFactory.createLayer( |
174 |
lyrDef.getName(), reader, mapContext.getProjection()); |
175 |
mapContext.getLayers().addLayer(newLayer); |
176 |
} |
177 |
} |
178 |
|
179 |
} |
180 |
|
181 |
} |
182 |
private class MultiWriterTask extends AbstractMonitorableTask{ |
183 |
Vector tasks=new Vector(); |
184 |
public void addTask(WriterTask wt) { |
185 |
tasks.add(wt); |
186 |
} |
187 |
public void run() throws Exception { |
188 |
for (int i = 0; i < tasks.size(); i++) { |
189 |
((WriterTask)tasks.get(i)).run(); |
190 |
} |
191 |
tasks.clear(); |
192 |
} |
193 |
|
194 |
|
195 |
} |
196 |
/**
|
197 |
* @see com.iver.andami.plugins.IExtension#initialize()
|
198 |
*/
|
199 |
public void initialize() { |
200 |
} |
201 |
|
202 |
/**
|
203 |
* @see com.iver.andami.plugins.IExtension#execute(java.lang.String)
|
204 |
*/
|
205 |
public void execute(String actionCommand) { |
206 |
com.iver.andami.ui.mdiManager.IWindow f = PluginServices.getMDIManager() |
207 |
.getActiveWindow(); |
208 |
|
209 |
if (f instanceof View) { |
210 |
View vista = (View) f; |
211 |
ProjectView model = vista.getModel(); |
212 |
MapContext mapa = model.getMapContext(); |
213 |
FLayers layers = mapa.getLayers(); |
214 |
FLayer[] actives = layers.getActives();
|
215 |
try {
|
216 |
// NOTA: SI HAY UNA SELECCI?N, SOLO SE SALVAN LOS SELECCIONADOS
|
217 |
for (int i = 0; i < actives.length; i++) { |
218 |
if (actives[i] instanceof FLyrVect) { |
219 |
FLyrVect lv = (FLyrVect) actives[i]; |
220 |
int numSelec = lv.getRecordset().getSelection()
|
221 |
.cardinality(); |
222 |
if (numSelec > 0) { |
223 |
int resp = JOptionPane.showConfirmDialog( |
224 |
(Component) PluginServices.getMainFrame(),
|
225 |
PluginServices.getText(this,"se_van_a_guardar_") + numSelec |
226 |
+ PluginServices.getText(this,"features_desea_continuar"), |
227 |
PluginServices.getText(this,"export_to"), JOptionPane.YES_NO_OPTION); |
228 |
if (resp != JOptionPane.YES_OPTION) { |
229 |
continue;
|
230 |
} |
231 |
} // if numSelec > 0
|
232 |
if (actionCommand.equals("SHP")) { |
233 |
saveToShp(mapa, lv); |
234 |
} |
235 |
if (actionCommand.equals("DXF")) { |
236 |
saveToDxf(mapa, lv); |
237 |
} |
238 |
if (actionCommand.equals("POSTGIS")) { |
239 |
saveToPostGIS(mapa, lv); |
240 |
} |
241 |
if (actionCommand.equals("GML")) { |
242 |
saveToGml(mapa, lv); |
243 |
} |
244 |
} // actives[i]
|
245 |
} // for
|
246 |
} catch (EditionException e) {
|
247 |
e.printStackTrace(); |
248 |
NotificationManager.addError(e.getMessage(), e); |
249 |
} catch (DriverException e) {
|
250 |
e.printStackTrace(); |
251 |
NotificationManager.addError(e.getMessage(), e); |
252 |
} catch (DriverIOException e) {
|
253 |
e.printStackTrace(); |
254 |
NotificationManager.addError(e.getMessage(), e); |
255 |
} |
256 |
|
257 |
} |
258 |
} |
259 |
|
260 |
public void saveToPostGIS(MapContext mapContext, FLyrVect layer) throws EditionException, DriverIOException { |
261 |
try {
|
262 |
String tableName = JOptionPane.showInputDialog(PluginServices |
263 |
.getText(this, "intro_tablename")); |
264 |
if (tableName == null) |
265 |
return;
|
266 |
tableName = tableName.toLowerCase(); |
267 |
DlgConnection dlg = new DlgConnection();
|
268 |
dlg.setModal(true);
|
269 |
dlg.setVisible(true);
|
270 |
ConnectionSettings cs = dlg.getConnSettings(); |
271 |
if (cs == null) |
272 |
return;
|
273 |
Connection conex = DriverManager.getConnection(cs |
274 |
.getConnectionString(), cs.getUser(), cs.getPassw()); |
275 |
|
276 |
DBLayerDefinition dbLayerDef = new DBLayerDefinition();
|
277 |
dbLayerDef.setCatalogName(cs.getDb()); |
278 |
dbLayerDef.setTableName(tableName); |
279 |
dbLayerDef.setName(tableName); |
280 |
dbLayerDef.setShapeType(layer.getShapeType()); |
281 |
SelectableDataSource sds = layer.getRecordset(); |
282 |
FieldDescription[] fieldsDescrip = sds.getFieldsDescription();
|
283 |
dbLayerDef.setFieldsDesc(fieldsDescrip); |
284 |
// Creamos el driver. OJO: Hay que a?adir el campo ID a la
|
285 |
// definici?n de campos.
|
286 |
|
287 |
boolean bFound = false; |
288 |
for (int i=0; i < fieldsDescrip.length; i++) |
289 |
{ |
290 |
FieldDescription f = fieldsDescrip[i]; |
291 |
if (f.getFieldName().equalsIgnoreCase("gid")) |
292 |
{ |
293 |
bFound = true;
|
294 |
break;
|
295 |
} |
296 |
} |
297 |
// Si no est?, lo a?adimos
|
298 |
if (!bFound)
|
299 |
{ |
300 |
int numFieldsAnt = fieldsDescrip.length;
|
301 |
FieldDescription[] newFields = new FieldDescription[dbLayerDef.getFieldsDesc().length + 1]; |
302 |
for (int i=0; i < numFieldsAnt; i++) |
303 |
{ |
304 |
newFields[i] = fieldsDescrip[i]; |
305 |
} |
306 |
newFields[numFieldsAnt] = new FieldDescription();
|
307 |
newFields[numFieldsAnt].setFieldDecimalCount(0);
|
308 |
newFields[numFieldsAnt].setFieldType(Types.INTEGER);
|
309 |
newFields[numFieldsAnt].setFieldLength(7);
|
310 |
newFields[numFieldsAnt].setFieldName("gid");
|
311 |
dbLayerDef.setFieldsDesc(newFields); |
312 |
|
313 |
} |
314 |
|
315 |
|
316 |
|
317 |
dbLayerDef.setFieldGeometry("the_geom");
|
318 |
dbLayerDef.setFieldID("gid");
|
319 |
|
320 |
dbLayerDef.setWhereClause("");
|
321 |
String strSRID = layer.getProjection().getAbrev().substring(5); |
322 |
dbLayerDef.setSRID_EPSG(strSRID); |
323 |
dbLayerDef.setConnection(conex); |
324 |
|
325 |
PostGISWriter writer = new PostGISWriter(); // (PostGISWriter)LayerFactory.getWM().getWriter("PostGIS |
326 |
// Writer");
|
327 |
writer.setWriteAll(true);
|
328 |
writer.setCreateTable(true);
|
329 |
writer.initialize(dbLayerDef); |
330 |
PostGisDriver postGISDriver=new PostGisDriver();
|
331 |
postGISDriver.setLyrDef(dbLayerDef); |
332 |
postGISDriver.open(); |
333 |
PostProcessSupport.clearList(); |
334 |
Object[] params = new Object[2]; |
335 |
params[0] = conex;
|
336 |
params[1] = dbLayerDef;
|
337 |
PostProcessSupport.addToPostProcess(postGISDriver, "setData",
|
338 |
params, 1);
|
339 |
|
340 |
writeFeatures(mapContext, layer, writer, postGISDriver); |
341 |
|
342 |
} catch (DriverException e) {
|
343 |
e.printStackTrace(); |
344 |
throw new EditionException(e); |
345 |
} catch (DriverLoadException e) {
|
346 |
throw new EditionException(e); |
347 |
} catch (SQLException e) { |
348 |
throw new EditionException(e); |
349 |
} catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
|
350 |
e.printStackTrace(); |
351 |
throw new EditionException(e); |
352 |
} |
353 |
|
354 |
} |
355 |
|
356 |
/**
|
357 |
* Lanza un thread en background que escribe las features. Cuando termina, pregunta al usuario si quiere
|
358 |
* a?adir la nueva capa a la vista. Para eso necesita un driver de lectura ya configurado.
|
359 |
* @param mapContext
|
360 |
* @param layer
|
361 |
* @param writer
|
362 |
* @param reader
|
363 |
* @throws DriverException
|
364 |
* @throws DriverIOException
|
365 |
*/
|
366 |
private void writeFeatures(MapContext mapContext, FLyrVect layer, IWriter writer, Driver reader) throws DriverException, DriverIOException |
367 |
{ |
368 |
PluginServices.cancelableBackgroundExecution(new WriterTask(mapContext, layer, writer, reader));
|
369 |
} |
370 |
private void writeMultiFeatures(MapContext mapContext, FLyrVect layers, IWriter[] writers, Driver[] readers) throws DriverException, DriverIOException{ |
371 |
MultiWriterTask mwt=new MultiWriterTask();
|
372 |
for (int i=0;i<writers.length;i++) { |
373 |
mwt.addTask(new WriterTask(mapContext, layers, writers[i], readers[i]));
|
374 |
} |
375 |
PluginServices.cancelableBackgroundExecution(mwt); |
376 |
} |
377 |
/**
|
378 |
* @param layer
|
379 |
* FLyrVect to obtain features. If selection, only selected
|
380 |
* features will be precessed.
|
381 |
* @param writer
|
382 |
* (Must be already initialized)
|
383 |
* @throws EditionException
|
384 |
* @throws DriverException
|
385 |
* @throws DriverIOException
|
386 |
* @throws com.hardcode.gdbms.engine.data.driver.DriverException
|
387 |
*/
|
388 |
public void writeFeaturesNoThread(FLyrVect layer, IWriter writer) |
389 |
throws EditionException, DriverException, DriverIOException,
|
390 |
com.hardcode.gdbms.engine.data.driver.DriverException { |
391 |
ReadableVectorial va = layer.getSource(); |
392 |
SelectableDataSource sds = layer.getRecordset(); |
393 |
|
394 |
// Creamos la tabla.
|
395 |
writer.preProcess(); |
396 |
|
397 |
int rowCount;
|
398 |
FBitSet bitSet = layer.getRecordset().getSelection(); |
399 |
|
400 |
if (bitSet.cardinality() == 0) |
401 |
rowCount = va.getShapeCount(); |
402 |
else
|
403 |
rowCount = bitSet.cardinality(); |
404 |
|
405 |
ProgressMonitor progress = new ProgressMonitor( |
406 |
(JComponent) PluginServices.getMDIManager().getActiveWindow(),
|
407 |
PluginServices.getText(this, "exportando_features"), |
408 |
PluginServices.getText(this, "exportando_features"), 0, |
409 |
rowCount); |
410 |
|
411 |
progress.setMillisToDecideToPopup(200);
|
412 |
progress.setMillisToPopup(500);
|
413 |
|
414 |
if (bitSet.cardinality() == 0) { |
415 |
rowCount = va.getShapeCount(); |
416 |
for (int i = 0; i < rowCount; i++) { |
417 |
IGeometry geom = va.getShape(i); |
418 |
|
419 |
progress.setProgress(i); |
420 |
if (progress.isCanceled())
|
421 |
break;
|
422 |
|
423 |
if (geom != null) { |
424 |
Value[] values = sds.getRow(i);
|
425 |
IFeature feat = new DefaultFeature(geom, values, "" + i); |
426 |
DefaultRowEdited edRow = new DefaultRowEdited(feat,
|
427 |
DefaultRowEdited.STATUS_ADDED, i); |
428 |
writer.process(edRow); |
429 |
} |
430 |
} |
431 |
} else {
|
432 |
int counter = 0; |
433 |
for (int i = bitSet.nextSetBit(0); i >= 0; i = bitSet |
434 |
.nextSetBit(i + 1)) {
|
435 |
IGeometry geom = va.getShape(i); |
436 |
|
437 |
progress.setProgress(counter++); |
438 |
if (progress.isCanceled())
|
439 |
break;
|
440 |
|
441 |
if (geom != null) { |
442 |
Value[] values = sds.getRow(i);
|
443 |
IFeature feat = new DefaultFeature(geom, values, "" + i); |
444 |
DefaultRowEdited edRow = new DefaultRowEdited(feat,
|
445 |
DefaultRowEdited.STATUS_ADDED, i); |
446 |
|
447 |
writer.process(edRow); |
448 |
} |
449 |
} |
450 |
|
451 |
} |
452 |
|
453 |
writer.postProcess(); |
454 |
progress.close(); |
455 |
} |
456 |
|
457 |
public void saveToDxf(MapContext mapContext, FLyrVect layer) throws EditionException, DriverIOException { |
458 |
try {
|
459 |
JFileChooser jfc = new JFileChooser(); |
460 |
SimpleFileFilter filterShp = new SimpleFileFilter("dxf", |
461 |
PluginServices.getText(this, "dxf_files")); |
462 |
jfc.setFileFilter(filterShp); |
463 |
if (jfc.showSaveDialog((Component) PluginServices.getMainFrame()) == JFileChooser.APPROVE_OPTION) { |
464 |
File newFile = jfc.getSelectedFile();
|
465 |
String path = newFile.getAbsolutePath();
|
466 |
if (!(path.toLowerCase().endsWith(".dxf"))) { |
467 |
path = path + ".dxf";
|
468 |
} |
469 |
newFile = new File(path); |
470 |
|
471 |
DxfWriter writer = (DxfWriter) LayerFactory.getWM().getWriter( |
472 |
"DXF Writer");
|
473 |
SHPLayerDefinition lyrDef = new SHPLayerDefinition();
|
474 |
SelectableDataSource sds = layer.getRecordset(); |
475 |
FieldDescription[] fieldsDescrip = sds.getFieldsDescription();
|
476 |
lyrDef.setFieldsDesc(fieldsDescrip); |
477 |
lyrDef.setFile(newFile); |
478 |
lyrDef.setName(newFile.getName()); |
479 |
lyrDef.setShapeType(layer.getShapeType()); |
480 |
writer.setFile(newFile); |
481 |
writer.initialize(lyrDef); |
482 |
writer.setProjection(layer.getProjection()); |
483 |
DxfFieldsMapping fieldsMapping = new DxfFieldsMapping();
|
484 |
// TODO: Recuperar aqu? los campos del cuadro de di?logo.
|
485 |
writer.setFieldMapping(fieldsMapping); |
486 |
DXFMemoryDriver dxfDriver=new DXFMemoryDriver();
|
487 |
dxfDriver.open(newFile); |
488 |
writeFeatures(mapContext, layer, writer, dxfDriver); |
489 |
} |
490 |
|
491 |
} catch (DriverException e) {
|
492 |
e.printStackTrace(); |
493 |
throw new EditionException(e); |
494 |
} catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
|
495 |
e.printStackTrace(); |
496 |
throw new EditionException(e); |
497 |
} catch (IOException e) { |
498 |
// TODO Auto-generated catch block
|
499 |
e.printStackTrace(); |
500 |
} |
501 |
|
502 |
} |
503 |
|
504 |
public void saveToShp(MapContext mapContext, FLyrVect layer) throws EditionException, DriverIOException { |
505 |
try {
|
506 |
JFileChooser jfc = new JFileChooser(); |
507 |
SimpleFileFilter filterShp = new SimpleFileFilter("shp", |
508 |
PluginServices.getText(this, "shp_files")); |
509 |
jfc.setFileFilter(filterShp); |
510 |
if (jfc.showSaveDialog((Component) PluginServices.getMainFrame()) == JFileChooser.APPROVE_OPTION) { |
511 |
File newFile = jfc.getSelectedFile();
|
512 |
String path = newFile.getAbsolutePath();
|
513 |
if( newFile.exists()){
|
514 |
int resp = JOptionPane.showConfirmDialog( |
515 |
(Component) PluginServices.getMainFrame(),PluginServices.getText(this,"fichero_ya_existe_seguro_desea_guardarlo"), |
516 |
PluginServices.getText(this,"guardar"), JOptionPane.YES_NO_OPTION); |
517 |
if (resp != JOptionPane.YES_OPTION) { |
518 |
return;
|
519 |
} |
520 |
} |
521 |
if (!(path.toLowerCase().endsWith(".shp"))) { |
522 |
path = path + ".shp";
|
523 |
} |
524 |
newFile = new File(path); |
525 |
|
526 |
|
527 |
|
528 |
SelectableDataSource sds = layer.getRecordset(); |
529 |
FieldDescription[] fieldsDescrip = sds.getFieldsDescription();
|
530 |
|
531 |
if (layer.getShapeType() == FShape.MULTI) // Exportamos a 3 |
532 |
// ficheros
|
533 |
{ |
534 |
ShpWriter writer1 = (ShpWriter) LayerFactory.getWM().getWriter( |
535 |
"Shape Writer");
|
536 |
Driver[] drivers=new Driver[3]; |
537 |
ShpWriter[] writers=new ShpWriter[3]; |
538 |
|
539 |
// puntos
|
540 |
String auxPoint = path.replaceFirst(".shp", "_points.shp"); |
541 |
|
542 |
SHPLayerDefinition lyrDefPoint = new SHPLayerDefinition();
|
543 |
lyrDefPoint.setFieldsDesc(fieldsDescrip); |
544 |
File filePoints = new File(auxPoint); |
545 |
lyrDefPoint.setFile(filePoints); |
546 |
lyrDefPoint.setName(filePoints.getName()); |
547 |
lyrDefPoint.setShapeType(FShape.POINT); |
548 |
writer1.setFile(filePoints); |
549 |
writer1.initialize(lyrDefPoint); |
550 |
writers[0]=writer1;
|
551 |
drivers[0]=getOpenShpDriver(filePoints);
|
552 |
//drivers[0]=null;
|
553 |
|
554 |
|
555 |
|
556 |
ShpWriter writer2 = (ShpWriter) LayerFactory.getWM().getWriter( |
557 |
"Shape Writer");
|
558 |
// Lineas
|
559 |
String auxLine = path.replaceFirst(".shp", "_line.shp"); |
560 |
SHPLayerDefinition lyrDefLine = new SHPLayerDefinition();
|
561 |
lyrDefLine.setFieldsDesc(fieldsDescrip); |
562 |
|
563 |
File fileLines = new File(auxLine); |
564 |
lyrDefLine.setFile(fileLines); |
565 |
lyrDefLine.setName(fileLines.getName()); |
566 |
lyrDefLine.setShapeType(FShape.LINE); |
567 |
writer2.setFile(fileLines); |
568 |
writer2.initialize(lyrDefLine); |
569 |
writers[1]=writer2;
|
570 |
drivers[1]=getOpenShpDriver(fileLines);
|
571 |
//drivers[1]=null;
|
572 |
|
573 |
ShpWriter writer3 = (ShpWriter) LayerFactory.getWM().getWriter( |
574 |
"Shape Writer");
|
575 |
// Pol?gonos
|
576 |
String auxPolygon = path.replaceFirst(".shp", "_polygons.shp"); |
577 |
SHPLayerDefinition lyrDefPolygon = new SHPLayerDefinition();
|
578 |
lyrDefPolygon.setFieldsDesc(fieldsDescrip); |
579 |
File filePolygons = new File(auxPolygon); |
580 |
lyrDefPolygon.setFile(filePolygons); |
581 |
lyrDefPolygon.setName(filePolygons.getName()); |
582 |
lyrDefPolygon.setShapeType(FShape.POLYGON); |
583 |
writer3.setFile(filePolygons); |
584 |
writer3.initialize(lyrDefPolygon); |
585 |
writers[2]=writer3;
|
586 |
drivers[2]=getOpenShpDriver(filePolygons);
|
587 |
//drivers[2]=null;
|
588 |
|
589 |
writeMultiFeatures(mapContext,layer, writers, drivers); |
590 |
} else {
|
591 |
ShpWriter writer = (ShpWriter) LayerFactory.getWM().getWriter( |
592 |
"Shape Writer");
|
593 |
IndexedShpDriver drv = getOpenShpDriver(newFile); |
594 |
SHPLayerDefinition lyrDef = new SHPLayerDefinition();
|
595 |
lyrDef.setFieldsDesc(fieldsDescrip); |
596 |
lyrDef.setFile(newFile); |
597 |
lyrDef.setName(newFile.getName()); |
598 |
lyrDef.setShapeType(layer.getShapeType()); |
599 |
writer.setFile(newFile); |
600 |
writer.initialize(lyrDef); |
601 |
writeFeatures(mapContext, layer, writer, drv); |
602 |
|
603 |
} |
604 |
} |
605 |
} catch (DriverException e) {
|
606 |
e.printStackTrace(); |
607 |
throw new EditionException(e); |
608 |
} catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
|
609 |
e.printStackTrace(); |
610 |
throw new EditionException(e); |
611 |
} |
612 |
// catch (IOException e) {
|
613 |
// e.printStackTrace();
|
614 |
// throw new EditionException(e);
|
615 |
// }
|
616 |
catch (IOException e) { |
617 |
// TODO Auto-generated catch block
|
618 |
e.printStackTrace(); |
619 |
} |
620 |
|
621 |
} |
622 |
/**
|
623 |
* This method saves a layer to GML
|
624 |
* @param mapContext
|
625 |
* @param layer
|
626 |
* @throws EditionException
|
627 |
* @throws DriverIOException
|
628 |
*/
|
629 |
public void saveToGml(MapContext mapContext, FLyrVect layer) throws EditionException, DriverIOException { |
630 |
try {
|
631 |
JFileChooser jfc = new JFileChooser(); |
632 |
SimpleFileFilter filterShp = new SimpleFileFilter("gml", |
633 |
PluginServices.getText(this, "gml_files")); |
634 |
jfc.setFileFilter(filterShp); |
635 |
if (jfc.showSaveDialog((Component) PluginServices.getMainFrame()) == JFileChooser.APPROVE_OPTION) { |
636 |
File newFile = jfc.getSelectedFile();
|
637 |
String path = newFile.getAbsolutePath();
|
638 |
if (!(path.toLowerCase().endsWith(".gml"))) { |
639 |
path = path + ".gml";
|
640 |
} |
641 |
newFile = new File(path); |
642 |
|
643 |
GMLWriter writer = (GMLWriter)LayerFactory.getWM().getWriter("GML Writer");
|
644 |
|
645 |
SHPLayerDefinition lyrDef = new SHPLayerDefinition();
|
646 |
SelectableDataSource sds = layer.getRecordset(); |
647 |
FieldDescription[] fieldsDescrip = sds.getFieldsDescription();
|
648 |
lyrDef.setFieldsDesc(fieldsDescrip); |
649 |
lyrDef.setName(newFile.getName()); |
650 |
lyrDef.setShapeType(layer.getShapeType()); |
651 |
|
652 |
writer.setFile(newFile); |
653 |
writer.setSchema(lyrDef); |
654 |
writer.setBoundedBy(layer.getFullExtent(),layer.getProjection()); |
655 |
writer.initialize(lyrDef); |
656 |
GMLDriver gmlDriver=new GMLDriver();
|
657 |
gmlDriver.open(newFile); |
658 |
writeFeatures(mapContext, layer, writer, gmlDriver); |
659 |
} |
660 |
|
661 |
} catch (DriverException e) {
|
662 |
e.printStackTrace(); |
663 |
throw new EditionException(e); |
664 |
} catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
|
665 |
e.printStackTrace(); |
666 |
throw new EditionException(e); |
667 |
} |
668 |
// catch (IOException e) {
|
669 |
// e.printStackTrace();
|
670 |
// throw new EditionException(e);
|
671 |
// }
|
672 |
catch (IOException e) { |
673 |
e.printStackTrace(); |
674 |
} |
675 |
|
676 |
} |
677 |
private IndexedShpDriver getOpenShpDriver(File fileShp) throws IOException { |
678 |
IndexedShpDriver drv = new IndexedShpDriver();
|
679 |
if (!fileShp.exists()) {
|
680 |
fileShp.createNewFile(); |
681 |
File newFileSHX=new File(fileShp.getAbsolutePath().replaceAll(".shp",".shx")); |
682 |
newFileSHX.createNewFile(); |
683 |
File newFileDBF=new File(fileShp.getAbsolutePath().replaceAll(".shp",".dbf")); |
684 |
newFileDBF.createNewFile(); |
685 |
} |
686 |
drv.open(fileShp); |
687 |
return drv;
|
688 |
} |
689 |
/**
|
690 |
* @see com.iver.andami.plugins.IExtension#isEnabled()
|
691 |
*/
|
692 |
public boolean isEnabled() { |
693 |
int status = EditionUtilities.getEditionStatus();
|
694 |
if (( status == EditionUtilities.EDITION_STATUS_ONE_VECTORIAL_LAYER_ACTIVE || status == EditionUtilities.EDITION_STATUS_ONE_VECTORIAL_LAYER_ACTIVE_AND_EDITABLE)
|
695 |
|| (status == EditionUtilities.EDITION_STATUS_MULTIPLE_VECTORIAL_LAYER_ACTIVE)|| (status == EditionUtilities.EDITION_STATUS_MULTIPLE_VECTORIAL_LAYER_ACTIVE_AND_EDITABLE)) |
696 |
{ |
697 |
return true; |
698 |
} |
699 |
return false; |
700 |
} |
701 |
|
702 |
/**
|
703 |
* @see com.iver.andami.plugins.IExtension#isVisible()
|
704 |
*/
|
705 |
public boolean isVisible() { |
706 |
com.iver.andami.ui.mdiManager.IWindow f = PluginServices.getMDIManager() |
707 |
.getActiveWindow(); |
708 |
|
709 |
if (f == null) { |
710 |
return false; |
711 |
} |
712 |
|
713 |
if (f instanceof View) |
714 |
return true; |
715 |
return false; |
716 |
} |
717 |
|
718 |
} |