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