svn-gvsig-desktop / branches / v2_0_0_prep / extensions / extEditing / src / com / iver / cit / gvsig / ExportTo.java @ 27723
History | View | Annotate | Download (28.6 KB)
1 |
package com.iver.cit.gvsig; |
---|---|
2 |
|
3 |
import java.awt.Component; |
4 |
import java.io.File; |
5 |
|
6 |
import javax.swing.JComponent; |
7 |
import javax.swing.JFileChooser; |
8 |
import javax.swing.JOptionPane; |
9 |
|
10 |
import org.gvsig.fmap.dal.DALLocator; |
11 |
import org.gvsig.fmap.dal.DataManager; |
12 |
import org.gvsig.fmap.dal.exception.DataException; |
13 |
import org.gvsig.fmap.dal.exception.ReadException; |
14 |
import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
15 |
import org.gvsig.fmap.dal.feature.DisposableIterator; |
16 |
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor; |
17 |
import org.gvsig.fmap.dal.feature.EditableFeatureType; |
18 |
import org.gvsig.fmap.dal.feature.Feature; |
19 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
20 |
import org.gvsig.fmap.dal.feature.FeatureQuery; |
21 |
import org.gvsig.fmap.dal.feature.FeatureSelection; |
22 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
23 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
24 |
import org.gvsig.fmap.dal.feature.FeatureType; |
25 |
import org.gvsig.fmap.dal.feature.NewFeatureStoreParameters; |
26 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorer; |
27 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorerParameters; |
28 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemStoreParameters; |
29 |
import org.gvsig.fmap.dal.store.dxf.DXFStoreParameters; |
30 |
import org.gvsig.fmap.dal.store.shp.SHPNewStoreParameters; |
31 |
import org.gvsig.fmap.geom.Geometry; |
32 |
import org.gvsig.fmap.mapcontext.MapContext; |
33 |
import org.gvsig.fmap.mapcontext.exceptions.LoadLayerException; |
34 |
import org.gvsig.fmap.mapcontext.layers.FLayer; |
35 |
import org.gvsig.fmap.mapcontext.layers.FLayers; |
36 |
import org.gvsig.fmap.mapcontext.layers.LayerFactory; |
37 |
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
38 |
import org.gvsig.tools.evaluator.AbstractEvaluator; |
39 |
import org.gvsig.tools.evaluator.Evaluator; |
40 |
import org.gvsig.tools.evaluator.EvaluatorData; |
41 |
import org.gvsig.tools.evaluator.EvaluatorException; |
42 |
|
43 |
import com.iver.andami.PluginServices; |
44 |
import com.iver.andami.messages.NotificationManager; |
45 |
import com.iver.andami.plugins.Extension; |
46 |
import com.iver.cit.gvsig.project.documents.view.IProjectView; |
47 |
import com.iver.cit.gvsig.project.documents.view.gui.View; |
48 |
import com.iver.utiles.PostProcessSupport; |
49 |
import com.iver.utiles.SimpleFileFilter; |
50 |
|
51 |
public class ExportTo extends Extension { |
52 |
private String lastPath = null; |
53 |
// private class WriterTask extends AbstractMonitorableTask
|
54 |
// {
|
55 |
// FLyrVect lyrVect;
|
56 |
// IWriter writer;
|
57 |
// int rowCount;
|
58 |
// ReadableVectorial va;
|
59 |
// SelectableDataSource sds;
|
60 |
// FBitSet bitSet;
|
61 |
// MapContext mapContext;
|
62 |
// VectorialDriver reader;
|
63 |
//
|
64 |
// public WriterTask(MapContext mapContext, FLyrVect lyr, IWriter writer, Driver reader) throws ReadDriverException
|
65 |
// {
|
66 |
// this.mapContext = mapContext;
|
67 |
// this.lyrVect = lyr;
|
68 |
// this.writer = writer;
|
69 |
// this.reader = (VectorialDriver) reader;
|
70 |
//
|
71 |
// setInitialStep(0);
|
72 |
// setDeterminatedProcess(true);
|
73 |
// setStatusMessage(PluginServices.getText(this, "exportando_features"));
|
74 |
//
|
75 |
// va = lyrVect.getSource();
|
76 |
// sds = lyrVect.getRecordset();
|
77 |
//
|
78 |
// bitSet = sds.getSelection();
|
79 |
//
|
80 |
// if (bitSet.cardinality() == 0)
|
81 |
// rowCount = va.getShapeCount();
|
82 |
// else
|
83 |
// rowCount = bitSet.cardinality();
|
84 |
//
|
85 |
// setFinalStep(rowCount);
|
86 |
//
|
87 |
// }
|
88 |
// public void run() throws Exception {
|
89 |
// va.start();
|
90 |
// ICoordTrans ct = lyrVect.getCoordTrans();
|
91 |
// DriverAttributes attr = va.getDriverAttributes();
|
92 |
// boolean bMustClone = false;
|
93 |
// if (attr != null) {
|
94 |
// if (attr.isLoadedInMemory()) {
|
95 |
// bMustClone = attr.isLoadedInMemory();
|
96 |
// }
|
97 |
// }
|
98 |
// if (lyrVect instanceof FLyrAnnotation && lyrVect.getShapeType()!=FShape.POINT) {
|
99 |
// SHPLayerDefinition lyrDef=(SHPLayerDefinition)writer.getTableDefinition();
|
100 |
// lyrDef.setShapeType(FShape.POINT);
|
101 |
// writer.initialize(lyrDef);
|
102 |
// }
|
103 |
//
|
104 |
// // Creamos la tabla.
|
105 |
// writer.preProcess();
|
106 |
//
|
107 |
// if (bitSet.cardinality() == 0) {
|
108 |
// rowCount = va.getShapeCount();
|
109 |
// for (int i = 0; i < rowCount; i++) {
|
110 |
// IGeometry geom = va.getShape(i);
|
111 |
// if (geom == null) {
|
112 |
// continue;
|
113 |
// }
|
114 |
// if (lyrVect instanceof FLyrAnnotation && geom.getGeometryType()!=FShape.POINT) {
|
115 |
// Point2D p=FLabel.createLabelPoint((FShape)geom.getInternalShape());
|
116 |
// geom=ShapeFactory.createPoint2D(p.getX(),p.getY());
|
117 |
// }
|
118 |
// if (ct != null) {
|
119 |
// if (bMustClone)
|
120 |
// geom = geom.cloneGeometry();
|
121 |
// geom.reProject(ct);
|
122 |
// }
|
123 |
// reportStep();
|
124 |
// setNote(PluginServices.getText(this, "exporting_") + i);
|
125 |
// if (isCanceled())
|
126 |
// break;
|
127 |
//
|
128 |
// if (geom != null) {
|
129 |
// Value[] values = sds.getRow(i);
|
130 |
// IFeature feat = new DefaultFeature(geom, values, "" + i);
|
131 |
// DefaultRowEdited edRow = new DefaultRowEdited(feat,
|
132 |
// DefaultRowEdited.STATUS_ADDED, i);
|
133 |
// writer.process(edRow);
|
134 |
// }
|
135 |
// }
|
136 |
// } else {
|
137 |
// int counter = 0;
|
138 |
// for (int i = bitSet.nextSetBit(0); i >= 0; i = bitSet
|
139 |
// .nextSetBit(i + 1)) {
|
140 |
// IGeometry geom = va.getShape(i);
|
141 |
// if (geom == null) {
|
142 |
// continue;
|
143 |
// }
|
144 |
// if (lyrVect instanceof FLyrAnnotation && geom.getGeometryType()!=FShape.POINT) {
|
145 |
// Point2D p=FLabel.createLabelPoint((FShape)geom.getInternalShape());
|
146 |
// geom=ShapeFactory.createPoint2D(p.getX(),p.getY());
|
147 |
// }
|
148 |
// if (ct != null) {
|
149 |
// if (bMustClone)
|
150 |
// geom = geom.cloneGeometry();
|
151 |
// geom.reProject(ct);
|
152 |
// }
|
153 |
// reportStep();
|
154 |
// setNote(PluginServices.getText(this, "exporting_") + counter);
|
155 |
// if (isCanceled())
|
156 |
// break;
|
157 |
//
|
158 |
// if (geom != null) {
|
159 |
// Value[] values = sds.getRow(i);
|
160 |
// IFeature feat = new DefaultFeature(geom, values, "" + i);
|
161 |
// DefaultRowEdited edRow = new DefaultRowEdited(feat,
|
162 |
// DefaultRowEdited.STATUS_ADDED, i);
|
163 |
//
|
164 |
// writer.process(edRow);
|
165 |
// }
|
166 |
// }
|
167 |
//
|
168 |
// }
|
169 |
//
|
170 |
// writer.postProcess();
|
171 |
// va.stop();
|
172 |
// if (reader != null){
|
173 |
// int res = JOptionPane.showConfirmDialog(
|
174 |
// (JComponent) PluginServices.getMDIManager().getActiveWindow()
|
175 |
// , PluginServices.getText(this, "insertar_en_la_vista_la_capa_creada"),
|
176 |
// PluginServices.getText(this,"insertar_capa"),
|
177 |
// JOptionPane.YES_NO_OPTION);
|
178 |
//
|
179 |
// if (res == JOptionPane.YES_OPTION)
|
180 |
// {
|
181 |
// PostProcessSupport.executeCalls();
|
182 |
// ILayerDefinition lyrDef = (ILayerDefinition) writer.getTableDefinition();
|
183 |
// FLayer newLayer = LayerFactory.createLayer(
|
184 |
// lyrDef.getName(), reader, mapContext.getProjection());
|
185 |
// mapContext.getLayers().addLayer(newLayer);
|
186 |
// }
|
187 |
// }
|
188 |
//
|
189 |
// }
|
190 |
// /* (non-Javadoc)
|
191 |
// * @see com.iver.utiles.swing.threads.IMonitorableTask#finished()
|
192 |
// */
|
193 |
// public void finished() {
|
194 |
// // TODO Auto-generated method stub
|
195 |
//
|
196 |
// }
|
197 |
//
|
198 |
// }
|
199 |
// private class MultiWriterTask extends AbstractMonitorableTask{
|
200 |
// Vector tasks=new Vector();
|
201 |
// public void addTask(WriterTask wt) {
|
202 |
// tasks.add(wt);
|
203 |
// }
|
204 |
// public void run() throws Exception {
|
205 |
// for (int i = 0; i < tasks.size(); i++) {
|
206 |
// ((WriterTask)tasks.get(i)).run();
|
207 |
// }
|
208 |
// tasks.clear();
|
209 |
// }
|
210 |
// /* (non-Javadoc)
|
211 |
// * @see com.iver.utiles.swing.threads.IMonitorableTask#finished()
|
212 |
// */
|
213 |
// public void finished() {
|
214 |
// // TODO Auto-generated method stub
|
215 |
//
|
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 |
// saveToDXF(mapa,lv);
|
245 |
|
246 |
|
247 |
|
248 |
/////////
|
249 |
|
250 |
long numSelec = ((FeatureSelection)lv.getFeatureStore().getSelection()).getSize();
|
251 |
if (numSelec > 0) { |
252 |
int resp = JOptionPane.showConfirmDialog( |
253 |
(Component) PluginServices.getMainFrame(),
|
254 |
PluginServices.getText(this,"se_van_a_guardar_") + numSelec |
255 |
+ PluginServices.getText(this,"features_desea_continuar"), |
256 |
PluginServices.getText(this,"export_to"), JOptionPane.YES_NO_OPTION); |
257 |
if (resp != JOptionPane.YES_OPTION) { |
258 |
continue;
|
259 |
} |
260 |
} // if numSelec > 0
|
261 |
if (actionCommand.equals("SHP")) { |
262 |
saveToSHP(mapa, lv); |
263 |
} |
264 |
if (actionCommand.equals("DXF")) { |
265 |
saveToDXF(mapa, lv); |
266 |
} |
267 |
// if (actionCommand.equals("POSTGIS")) {
|
268 |
// saveToPostGIS(mapa, lv);
|
269 |
// }
|
270 |
// if (actionCommand.equals("GML")) {
|
271 |
// saveToGml(mapa, lv);
|
272 |
// }
|
273 |
} // actives[i]
|
274 |
} // for
|
275 |
} catch (Exception e) { |
276 |
NotificationManager.showMessageError(e.getMessage(),e); |
277 |
} |
278 |
} |
279 |
} |
280 |
|
281 |
// public void saveToPostGIS(MapContext mapContext, FLyrVect layer){
|
282 |
// try {
|
283 |
// String tableName = JOptionPane.showInputDialog(PluginServices
|
284 |
// .getText(this, "intro_tablename"));
|
285 |
// if (tableName == null)
|
286 |
// return;
|
287 |
// tableName = tableName.toLowerCase();
|
288 |
// DlgConnection dlg = new DlgConnection();
|
289 |
// dlg.setModal(true);
|
290 |
// dlg.setVisible(true);
|
291 |
// ConnectionSettings cs = dlg.getConnSettings();
|
292 |
// if (cs == null)
|
293 |
// return;
|
294 |
// IConnection conex = ConnectionFactory.createConnection(cs
|
295 |
// .getConnectionString(), cs.getUser(), cs.getPassw());
|
296 |
//
|
297 |
//
|
298 |
// DBLayerDefinition originalDef = null;
|
299 |
// if (layer.getSource().getDriver() instanceof IVectorialDatabaseDriver) {
|
300 |
// originalDef=((IVectorialDatabaseDriver) layer.getSource().getDriver()).getLyrDef();
|
301 |
// }
|
302 |
//
|
303 |
//
|
304 |
// DBLayerDefinition dbLayerDef = new DBLayerDefinition();
|
305 |
// // Fjp:
|
306 |
// // Cambio: En Postgis, el nombre de cat?logo est? siempre vac?o. Es algo heredado de Oracle, que no se usa.
|
307 |
// // dbLayerDef.setCatalogName(cs.getDb());
|
308 |
// dbLayerDef.setCatalogName("");
|
309 |
//
|
310 |
// // A?adimos el schema dentro del layer definition para poder tenerlo en cuenta.
|
311 |
// dbLayerDef.setSchema(cs.getSchema());
|
312 |
//
|
313 |
// dbLayerDef.setTableName(tableName);
|
314 |
// dbLayerDef.setName(tableName);
|
315 |
// dbLayerDef.setShapeType(layer.getShapeType());
|
316 |
// SelectableDataSource sds = layer.getRecordset();
|
317 |
//
|
318 |
// FieldDescription[] fieldsDescrip = sds.getFieldsDescription();
|
319 |
// dbLayerDef.setFieldsDesc(fieldsDescrip);
|
320 |
// // Creamos el driver. OJO: Hay que a?adir el campo ID a la
|
321 |
// // definici?n de campos.
|
322 |
//
|
323 |
// if (originalDef != null){
|
324 |
// dbLayerDef.setFieldID(originalDef.getFieldID());
|
325 |
// dbLayerDef.setFieldGeometry(originalDef.getFieldGeometry());
|
326 |
//
|
327 |
// }else{
|
328 |
// // Search for id field name
|
329 |
// int index=0;
|
330 |
// String fieldName="gid";
|
331 |
// while (findFileByName(fieldsDescrip,fieldName) != -1){
|
332 |
// index++;
|
333 |
// fieldName="gid"+index;
|
334 |
// }
|
335 |
// dbLayerDef.setFieldID(fieldName);
|
336 |
//
|
337 |
// // search for geom field name
|
338 |
// index=0;
|
339 |
// fieldName="the_geom";
|
340 |
// while (findFileByName(fieldsDescrip,fieldName) != -1){
|
341 |
// index++;
|
342 |
// fieldName="the_geom"+index;
|
343 |
// }
|
344 |
// dbLayerDef.setFieldGeometry(fieldName);
|
345 |
//
|
346 |
// }
|
347 |
//
|
348 |
// // if id field dosen't exist we add it
|
349 |
// if (findFileByName(fieldsDescrip,dbLayerDef.getFieldID()) == -1)
|
350 |
// {
|
351 |
// int numFieldsAnt = fieldsDescrip.length;
|
352 |
// FieldDescription[] newFields = new FieldDescription[dbLayerDef.getFieldsDesc().length + 1];
|
353 |
// for (int i=0; i < numFieldsAnt; i++)
|
354 |
// {
|
355 |
// newFields[i] = fieldsDescrip[i];
|
356 |
// }
|
357 |
// newFields[numFieldsAnt] = new FieldDescription();
|
358 |
// newFields[numFieldsAnt].setFieldDecimalCount(0);
|
359 |
// newFields[numFieldsAnt].setFieldType(Types.INTEGER);
|
360 |
// newFields[numFieldsAnt].setFieldLength(7);
|
361 |
// newFields[numFieldsAnt].setFieldName("gid");
|
362 |
// dbLayerDef.setFieldsDesc(newFields);
|
363 |
//
|
364 |
// }
|
365 |
//
|
366 |
//
|
367 |
// dbLayerDef.setWhereClause("");
|
368 |
// String strSRID = layer.getProjection().getAbrev();
|
369 |
// dbLayerDef.setSRID_EPSG(strSRID);
|
370 |
// dbLayerDef.setConnection(conex);
|
371 |
//
|
372 |
// PostGISWriter writer=(PostGISWriter)LayerFactory.getWM().getWriter("PostGIS Writer");
|
373 |
// writer.setWriteAll(true);
|
374 |
// writer.setCreateTable(true);
|
375 |
// writer.initialize(dbLayerDef);
|
376 |
// PostGisDriver postGISDriver=new PostGisDriver();
|
377 |
// postGISDriver.setLyrDef(dbLayerDef);
|
378 |
// postGISDriver.open();
|
379 |
// PostProcessSupport.clearList();
|
380 |
// Object[] params = new Object[2];
|
381 |
// params[0] = conex;
|
382 |
// params[1] = dbLayerDef;
|
383 |
// PostProcessSupport.addToPostProcess(postGISDriver, "setData",
|
384 |
// params, 1);
|
385 |
//
|
386 |
// writeFeatures(mapContext, layer, writer, postGISDriver);
|
387 |
//
|
388 |
// } catch (DriverLoadException e) {
|
389 |
// NotificationManager.addError(e.getMessage(),e);
|
390 |
// } catch (DBException e) {
|
391 |
// NotificationManager.addError(e.getMessage(),e);
|
392 |
// } catch (InitializeWriterException e) {
|
393 |
// NotificationManager.addError(e.getMessage(),e);
|
394 |
// } catch (ReadDriverException e) {
|
395 |
// NotificationManager.addError(e.getMessage(),e);
|
396 |
// }
|
397 |
//
|
398 |
// }
|
399 |
|
400 |
/**
|
401 |
* Lanza un thread en background que escribe las features. Cuando termina, pregunta al usuario si quiere
|
402 |
* a?adir la nueva capa a la vista. Para eso necesita un driver de lectura ya configurado.
|
403 |
* @param mapContext
|
404 |
* @param layer
|
405 |
* @param writer
|
406 |
* @param reader
|
407 |
*/
|
408 |
// private void writeFeatures(MapContext mapContext, FLyrVect layer, IWriter writer, Driver reader) throws ReadDriverException
|
409 |
// {
|
410 |
// PluginServices.cancelableBackgroundExecution(new WriterTask(mapContext, layer, writer, reader));
|
411 |
// }
|
412 |
// private void writeMultiFeatures(MapContext mapContext, FLyrVect layers, IWriter[] writers, Driver[] readers) throws ReadDriverException{
|
413 |
// MultiWriterTask mwt=new MultiWriterTask();
|
414 |
// for (int i=0;i<writers.length;i++) {
|
415 |
// mwt.addTask(new WriterTask(mapContext, layers, writers[i], readers[i]));
|
416 |
// }
|
417 |
// PluginServices.cancelableBackgroundExecution(mwt);
|
418 |
// }
|
419 |
/**
|
420 |
* @param layer
|
421 |
* FLyrVect to obtain features. If selection, only selected
|
422 |
* features will be precessed.
|
423 |
* @param writer
|
424 |
* (Must be already initialized)
|
425 |
* @throws DataException
|
426 |
* @throws ReadException
|
427 |
* @throws ProcessWriterException
|
428 |
* @throws ExpansionFileReadException
|
429 |
* @throws EditionException
|
430 |
*/
|
431 |
// public void writeFeaturesNoThread(FLyrVect layer, IWriter writer) throws ReadDriverException, VisitorException, ExpansionFileReadException{
|
432 |
// ReadableVectorial va = layer.getSource();
|
433 |
// SelectableDataSource sds = layer.getRecordset();
|
434 |
//
|
435 |
// // Creamos la tabla.
|
436 |
// writer.preProcess();
|
437 |
//
|
438 |
// int rowCount;
|
439 |
// FBitSet bitSet = layer.getRecordset().getSelection();
|
440 |
//
|
441 |
// if (bitSet.cardinality() == 0)
|
442 |
// rowCount = va.getShapeCount();
|
443 |
// else
|
444 |
// rowCount = bitSet.cardinality();
|
445 |
//
|
446 |
// ProgressMonitor progress = new ProgressMonitor(
|
447 |
// (JComponent) PluginServices.getMDIManager().getActiveWindow(),
|
448 |
// PluginServices.getText(this, "exportando_features"),
|
449 |
// PluginServices.getText(this, "exportando_features"), 0,
|
450 |
// rowCount);
|
451 |
//
|
452 |
// progress.setMillisToDecideToPopup(200);
|
453 |
// progress.setMillisToPopup(500);
|
454 |
//
|
455 |
// if (bitSet.cardinality() == 0) {
|
456 |
// rowCount = va.getShapeCount();
|
457 |
// for (int i = 0; i < rowCount; i++) {
|
458 |
// IGeometry geom = va.getShape(i);
|
459 |
//
|
460 |
// progress.setProgress(i);
|
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 |
// IRowEdited.STATUS_ADDED, i);
|
469 |
// writer.process(edRow);
|
470 |
// }
|
471 |
// }
|
472 |
// } else {
|
473 |
// int counter = 0;
|
474 |
// for (int i = bitSet.nextSetBit(0); i >= 0; i = bitSet
|
475 |
// .nextSetBit(i + 1)) {
|
476 |
// IGeometry geom = va.getShape(i);
|
477 |
//
|
478 |
// progress.setProgress(counter++);
|
479 |
// if (progress.isCanceled())
|
480 |
// break;
|
481 |
//
|
482 |
// if (geom != null) {
|
483 |
// Value[] values = sds.getRow(i);
|
484 |
// IFeature feat = new DefaultFeature(geom, values, "" + i);
|
485 |
// DefaultRowEdited edRow = new DefaultRowEdited(feat,
|
486 |
// IRowEdited.STATUS_ADDED, i);
|
487 |
//
|
488 |
// writer.process(edRow);
|
489 |
// }
|
490 |
// }
|
491 |
//
|
492 |
// }
|
493 |
//
|
494 |
// writer.postProcess();
|
495 |
// progress.close();
|
496 |
// }
|
497 |
|
498 |
|
499 |
|
500 |
public void saveToDXF(MapContext mapContext, FLyrVect lv) |
501 |
throws DataException, ValidateDataParametersException {
|
502 |
JFileChooser jfc = new JFileChooser(lastPath); |
503 |
SimpleFileFilter filterShp = new SimpleFileFilter("dxf", |
504 |
PluginServices.getText(this, "dxf_files")); |
505 |
jfc.setFileFilter(filterShp); |
506 |
if (jfc.showSaveDialog((Component) PluginServices.getMainFrame()) == JFileChooser.APPROVE_OPTION) { |
507 |
File newFile = jfc.getSelectedFile();
|
508 |
String path = newFile.getAbsolutePath();
|
509 |
if( newFile.exists()){
|
510 |
int resp = JOptionPane.showConfirmDialog( |
511 |
(Component) PluginServices.getMainFrame(),PluginServices.getText(this,"fichero_ya_existe_seguro_desea_guardarlo"), |
512 |
PluginServices.getText(this,"guardar"), JOptionPane.YES_NO_OPTION); |
513 |
if (resp != JOptionPane.YES_OPTION) { |
514 |
return;
|
515 |
} |
516 |
} |
517 |
if (!(path.toLowerCase().endsWith(".dxf"))){ |
518 |
path = path + ".dxf";
|
519 |
} |
520 |
newFile = new File(path); |
521 |
|
522 |
DataManager datamanager=DALLocator.getDataManager(); |
523 |
|
524 |
FilesystemServerExplorerParameters explorerParams=(FilesystemServerExplorerParameters) datamanager.createServerExplorerParameters(FilesystemServerExplorer.NAME); |
525 |
explorerParams.setRoot(newFile.getParent()); |
526 |
|
527 |
FilesystemServerExplorer explorer=(FilesystemServerExplorer) datamanager.createServerExplorer(explorerParams); |
528 |
|
529 |
NewFeatureStoreParameters newParams = (NewFeatureStoreParameters) explorer.getAddParameters(newFile); |
530 |
((DXFStoreParameters)newParams).setSRS(mapContext.getProjection()); |
531 |
|
532 |
lv.getFeatureStore().export(explorer, newParams); |
533 |
|
534 |
openLayer(newParams, newFile.getName(), mapContext); |
535 |
} |
536 |
} |
537 |
|
538 |
class GeometryTypeEvaluator extends AbstractEvaluator{ |
539 |
private FeatureType featureType=null; |
540 |
private int type=0; |
541 |
private FeatureSelection selection;
|
542 |
private boolean hasSelection=false; |
543 |
|
544 |
public GeometryTypeEvaluator(FeatureType ft, int t, FeatureSelection selection) { |
545 |
featureType=ft; |
546 |
type=t; |
547 |
this.selection=selection;
|
548 |
hasSelection=selection.getSelectedCount()>0;
|
549 |
this.getFieldsInfo().addFieldValue(
|
550 |
ft.getDefaultGeometryAttributeName()); |
551 |
} |
552 |
public Object evaluate(EvaluatorData data) throws EvaluatorException { |
553 |
if (hasSelection){
|
554 |
if (!selection.isSelected((Feature)data)){
|
555 |
return false; |
556 |
} |
557 |
} |
558 |
Geometry geometry=(Geometry) data.getDataValue(featureType.getDefaultGeometryAttributeName()); |
559 |
return new Boolean(geometry.getType()==type); |
560 |
} |
561 |
|
562 |
public String getName() { |
563 |
return "GeometryTypeEvaluator"; |
564 |
} |
565 |
|
566 |
} |
567 |
|
568 |
public void saveToSHP(MapContext mapContext, FLyrVect lv) |
569 |
throws DataException, ValidateDataParametersException {
|
570 |
JFileChooser jfc = new JFileChooser(lastPath); |
571 |
SimpleFileFilter filterShp = new SimpleFileFilter("shp", |
572 |
PluginServices.getText(this, "shp_files")); |
573 |
jfc.setFileFilter(filterShp); |
574 |
if (jfc.showSaveDialog((Component) PluginServices.getMainFrame()) == JFileChooser.APPROVE_OPTION) { |
575 |
File newFile = jfc.getSelectedFile();
|
576 |
String path = newFile.getAbsolutePath();
|
577 |
if( newFile.exists()){
|
578 |
int resp = JOptionPane.showConfirmDialog( |
579 |
(Component) PluginServices.getMainFrame(),PluginServices.getText(this,"fichero_ya_existe_seguro_desea_guardarlo"), |
580 |
PluginServices.getText(this,"guardar"), JOptionPane.YES_NO_OPTION); |
581 |
if (resp != JOptionPane.YES_OPTION) { |
582 |
return;
|
583 |
} |
584 |
} |
585 |
if (!(path.toLowerCase().endsWith(".shp"))){ |
586 |
path = path + ".shp";
|
587 |
} |
588 |
newFile = new File(path); |
589 |
|
590 |
DataManager datamanager=DALLocator.getDataManager(); |
591 |
|
592 |
FilesystemServerExplorerParameters explorerParams=(FilesystemServerExplorerParameters) datamanager.createServerExplorerParameters(FilesystemServerExplorer.NAME); |
593 |
explorerParams.setRoot(newFile.getParent()); |
594 |
|
595 |
FilesystemServerExplorer explorer=(FilesystemServerExplorer) datamanager.createServerExplorer(explorerParams); |
596 |
|
597 |
NewFeatureStoreParameters newParams = (NewFeatureStoreParameters) explorer.getAddParameters(newFile); |
598 |
|
599 |
|
600 |
FeatureStore store=lv.getFeatureStore(); |
601 |
FeatureQuery featureQuery = store.createFeatureQuery(); |
602 |
((SHPNewStoreParameters) newParams).setSRS(mapContext.getProjection()); |
603 |
|
604 |
if (lv.getShapeType()==Geometry.TYPES.GEOMETRY){
|
605 |
//POINT
|
606 |
Evaluator evaluator=new GeometryTypeEvaluator(store.getDefaultFeatureType(),Geometry.TYPES.POINT,store.getFeatureSelection());
|
607 |
featureQuery.setFilter(evaluator); |
608 |
FeatureSet set=store.getFeatureSet(featureQuery); |
609 |
String pathFile=((FilesystemStoreParameters) newParams).getFile().getAbsolutePath();
|
610 |
String withoutShp=pathFile.replaceAll("\\.shp", ""); |
611 |
((SHPNewStoreParameters) newParams).setSHPFile(new File(withoutShp + "_point" + ".shp")); |
612 |
((SHPNewStoreParameters) newParams).setDBFFile(new File(withoutShp + "_point" + ".dbf")); |
613 |
((SHPNewStoreParameters) newParams).setSHXFile(new File(withoutShp + "_point" + ".shx")); |
614 |
export(explorer, newParams, set, store,Geometry.TYPES.POINT);
|
615 |
openLayer(newParams,((SHPNewStoreParameters) newParams).getFile().getName(),mapContext); |
616 |
|
617 |
//CURVE
|
618 |
evaluator=new GeometryTypeEvaluator(store.getDefaultFeatureType(),Geometry.TYPES.CURVE,store.getFeatureSelection());
|
619 |
featureQuery.setFilter(evaluator); |
620 |
set=store.getFeatureSet(featureQuery); |
621 |
((SHPNewStoreParameters) newParams).setSHPFile(new File(withoutShp + "_curve" + ".shp")); |
622 |
((SHPNewStoreParameters) newParams).setDBFFile(new File(withoutShp + "_curve" + ".dbf")); |
623 |
((SHPNewStoreParameters) newParams).setSHXFile(new File(withoutShp + "_curve" + ".shx")); |
624 |
export(explorer, newParams, set, lv.getFeatureStore(),Geometry.TYPES.CURVE);
|
625 |
openLayer(newParams,((SHPNewStoreParameters) newParams).getFile().getName(),mapContext); |
626 |
|
627 |
//SURFACE
|
628 |
evaluator=new GeometryTypeEvaluator(store.getDefaultFeatureType(),Geometry.TYPES.SURFACE,store.getFeatureSelection());
|
629 |
featureQuery.setFilter(evaluator); |
630 |
set=store.getFeatureSet(featureQuery); |
631 |
((SHPNewStoreParameters) newParams).setSHPFile(new File(withoutShp + "_surface" + ".shp")); |
632 |
((SHPNewStoreParameters) newParams).setDBFFile(new File(withoutShp + "_surface" + ".dbf")); |
633 |
((SHPNewStoreParameters) newParams).setSHXFile(new File(withoutShp + "_surface" + ".shx")); |
634 |
export(explorer, newParams, set, lv.getFeatureStore(),Geometry.TYPES.SURFACE);
|
635 |
openLayer(newParams,((SHPNewStoreParameters) newParams).getFile().getName(),mapContext); |
636 |
}else{
|
637 |
store.export(explorer, newParams); |
638 |
openLayer(newParams,newFile.getName(),mapContext); |
639 |
} |
640 |
} |
641 |
} |
642 |
|
643 |
|
644 |
private void openLayer(NewFeatureStoreParameters newParams, String name, MapContext mapContext) throws ReadException { |
645 |
int res = JOptionPane.showConfirmDialog( |
646 |
(JComponent) PluginServices.getMDIManager().getActiveWindow()
|
647 |
, PluginServices.getText(this, "insertar_en_la_vista_la_capa_creada"), |
648 |
PluginServices.getText(this,"insertar_capa"), |
649 |
JOptionPane.YES_NO_OPTION);
|
650 |
|
651 |
if (res == JOptionPane.YES_OPTION){ |
652 |
PostProcessSupport.executeCalls(); |
653 |
LayerFactory layerFactory=LayerFactory.getInstance(); |
654 |
try {
|
655 |
FLayer newLayer = layerFactory.getInstance().createLayer( |
656 |
name,newParams); |
657 |
|
658 |
mapContext.getLayers().addLayer(newLayer); |
659 |
} catch (LoadLayerException e) {
|
660 |
throw new ReadException("Load layer",e); |
661 |
} |
662 |
} |
663 |
|
664 |
} |
665 |
|
666 |
private void export(FilesystemServerExplorer explorer, |
667 |
NewFeatureStoreParameters params, FeatureSet set, FeatureStore featureStore, int geometryType) {
|
668 |
// if (this.getFeatureTypes().size() != 1) {
|
669 |
// throw new NotYetImplemented(
|
670 |
// "export whith more than one type not yet implemented");
|
671 |
// }
|
672 |
// FeatureSelection featureSelection=(FeatureSelection)getSelection();
|
673 |
DisposableIterator it1 = null;
|
674 |
try {
|
675 |
EditableFeatureType type = featureStore.getDefaultFeatureType().getEditable(); |
676 |
FeatureAttributeDescriptor fad=(FeatureAttributeDescriptor)type.get(type.getDefaultGeometryAttributeName()); |
677 |
type.remove(fad.getName()); |
678 |
EditableFeatureAttributeDescriptor efad=type.add(fad.getName(), fad.getDataType(), fad.getSize()); |
679 |
efad.setDefaultValue(fad.getDefaultValue()); |
680 |
efad.setGeometryType(geometryType); |
681 |
efad.setPrecision(fad.getPrecision()); |
682 |
type.setDefaultGeometryAttributeName(fad.getName()); |
683 |
// if (params.getDefaultFeatureType() == null
|
684 |
// || params.getDefaultFeatureType().size() == 0) {
|
685 |
params.setDefaultFeatureType(type); |
686 |
// }
|
687 |
explorer.add(params, true);
|
688 |
|
689 |
DataManager manager = DALLocator.getDataManager(); |
690 |
FeatureStore target = (FeatureStore) manager |
691 |
.createStore(params); |
692 |
FeatureType targetType = target.getDefaultFeatureType(); |
693 |
|
694 |
target.edit(FeatureStore.MODE_APPEND); |
695 |
// FeatureSet features=null;
|
696 |
// if (featureSelection.getSize()>0){
|
697 |
// features = this.getFeatureSelection();
|
698 |
// }else{
|
699 |
// features = this.getFeatureSet();
|
700 |
// }
|
701 |
it1 = set.iterator(); |
702 |
while (it1.hasNext()) {
|
703 |
Feature feature = (Feature) it1.next(); |
704 |
target.insert(target.createNewFeature(targetType, feature)); |
705 |
} |
706 |
set.dispose(); |
707 |
target.finishEditing(); |
708 |
target.dispose(); |
709 |
} catch (Exception e) { |
710 |
e.printStackTrace(); |
711 |
// throw new DataExportException(e, params.toString());
|
712 |
} finally {
|
713 |
if (it1 != null) { |
714 |
it1.dispose(); |
715 |
} |
716 |
} |
717 |
|
718 |
} |
719 |
/**
|
720 |
* This method saves a layer to GML
|
721 |
* @param mapContext
|
722 |
* @param layer
|
723 |
* @throws EditionException
|
724 |
* @throws DriverIOException
|
725 |
*/
|
726 |
// public void saveToGml(MapContext mapContext, FLyrVect layer) {
|
727 |
// try {
|
728 |
// JFileChooser jfc = new JFileChooser();
|
729 |
// SimpleFileFilter filterShp = new SimpleFileFilter("gml",
|
730 |
// PluginServices.getText(this, "gml_files"));
|
731 |
// jfc.setFileFilter(filterShp);
|
732 |
// if (jfc.showSaveDialog((Component) PluginServices.getMainFrame()) == JFileChooser.APPROVE_OPTION) {
|
733 |
// File newFile = jfc.getSelectedFile();
|
734 |
// String path = newFile.getAbsolutePath();
|
735 |
// if (!(path.toLowerCase().endsWith(".gml"))) {
|
736 |
// path = path + ".gml";
|
737 |
// }
|
738 |
// newFile = new File(path);
|
739 |
//
|
740 |
// GMLWriter writer = (GMLWriter)LayerFactory.getWM().getWriter("GML Writer");
|
741 |
//
|
742 |
// SHPLayerDefinition lyrDef = new SHPLayerDefinition();
|
743 |
// SelectableDataSource sds = layer.getRecordset();
|
744 |
// FieldDescription[] fieldsDescrip = sds.getFieldsDescription();
|
745 |
// lyrDef.setFieldsDesc(fieldsDescrip);
|
746 |
// lyrDef.setName(newFile.getName());
|
747 |
// lyrDef.setShapeType(layer.getShapeType());
|
748 |
//
|
749 |
// writer.setFile(newFile);
|
750 |
// writer.setSchema(lyrDef);
|
751 |
// writer.setBoundedBy(layer.getFullExtent(),layer.getProjection());
|
752 |
// writer.initialize(lyrDef);
|
753 |
// GMLDriver gmlDriver=new GMLDriver();
|
754 |
// gmlDriver.open(newFile);
|
755 |
// writeFeatures(mapContext, layer, writer, gmlDriver);
|
756 |
// }
|
757 |
//
|
758 |
// } catch (SchemaEditionException e) {
|
759 |
// NotificationManager.addError(e.getMessage(),e);
|
760 |
// } catch (InitializeWriterException e) {
|
761 |
// NotificationManager.addError(e.getMessage(),e);
|
762 |
// } catch (ReadDriverException e) {
|
763 |
// NotificationManager.addError(e.getMessage(),e);
|
764 |
// } catch (DriverLoadException e) {
|
765 |
// NotificationManager.addError(e.getMessage(),e);
|
766 |
// }
|
767 |
//
|
768 |
// }
|
769 |
// private IndexedShpDriver getOpenShpDriver(File fileShp) throws OpenDriverException {
|
770 |
// IndexedShpDriver drv = new IndexedShpDriver();
|
771 |
// if (!fileShp.exists()) {
|
772 |
// try {
|
773 |
// fileShp.createNewFile();
|
774 |
// File newFileSHX=new File(fileShp.getAbsolutePath().replaceAll("[.]shp",".shx"));
|
775 |
// newFileSHX.createNewFile();
|
776 |
// File newFileDBF=new File(fileShp.getAbsolutePath().replaceAll("[.]shp",".dbf"));
|
777 |
// newFileDBF.createNewFile();
|
778 |
// } catch (IOException e) {
|
779 |
// throw new FileNotFoundDriverException("SHP",e,fileShp.getAbsolutePath());
|
780 |
// }
|
781 |
// }
|
782 |
// drv.open(fileShp);
|
783 |
// return drv;
|
784 |
// }
|
785 |
/**
|
786 |
* @see com.iver.andami.plugins.IExtension#isEnabled()
|
787 |
*/
|
788 |
public boolean isEnabled() { |
789 |
int status = EditionUtilities.getEditionStatus();
|
790 |
if (( status == EditionUtilities.EDITION_STATUS_ONE_VECTORIAL_LAYER_ACTIVE || status == EditionUtilities.EDITION_STATUS_ONE_VECTORIAL_LAYER_ACTIVE_AND_EDITABLE)
|
791 |
|| (status == EditionUtilities.EDITION_STATUS_MULTIPLE_VECTORIAL_LAYER_ACTIVE)|| (status == EditionUtilities.EDITION_STATUS_MULTIPLE_VECTORIAL_LAYER_ACTIVE_AND_EDITABLE)) |
792 |
{ |
793 |
return true; |
794 |
} |
795 |
return false; |
796 |
} |
797 |
|
798 |
/**
|
799 |
* @see com.iver.andami.plugins.IExtension#isVisible()
|
800 |
*/
|
801 |
public boolean isVisible() { |
802 |
com.iver.andami.ui.mdiManager.IWindow f = PluginServices.getMDIManager() |
803 |
.getActiveWindow(); |
804 |
|
805 |
if (f == null) { |
806 |
return false; |
807 |
} |
808 |
|
809 |
if (f instanceof View) { |
810 |
return true; |
811 |
} |
812 |
return false; |
813 |
} |
814 |
|
815 |
// private int findFileByName(FieldDescription[] fields, String fieldName){
|
816 |
// for (int i=0; i < fields.length; i++)
|
817 |
// {
|
818 |
// FieldDescription f = fields[i];
|
819 |
// if (f.getFieldName().equalsIgnoreCase(fieldName))
|
820 |
// {
|
821 |
// return i;
|
822 |
// }
|
823 |
// }
|
824 |
//
|
825 |
// return -1;
|
826 |
//
|
827 |
// }
|
828 |
|
829 |
} |