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