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