Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extGPE-gvSIG / src / org / gvsig / fmap / drivers / gpe / writer / ExportTask.java @ 27067

History | View | Annotate | Download (8.18 KB)

1
package org.gvsig.fmap.drivers.gpe.writer;
2

    
3
import java.io.File;
4
import java.io.IOException;
5

    
6
import javax.swing.JComponent;
7
import javax.swing.JOptionPane;
8

    
9
import org.gvsig.fmap.drivers.gpe.reader.GPEDriverFactory;
10
import org.gvsig.fmap.drivers.gpe.reader.GPEVectorialDriver;
11
import org.gvsig.gpe.GPEDefaults;
12
import org.gvsig.gpe.GPERegister;
13
import org.gvsig.gpe.exceptions.ParserCreationException;
14
import org.gvsig.gpe.gml.utils.GMLUtilsParser;
15
import org.gvsig.gpe.parser.GPEParser;
16
import org.gvsig.gpe.utils.StringUtils;
17
import org.gvsig.gpe.writer.GPEWriterHandler;
18

    
19
import com.hardcode.gdbms.driver.exceptions.InitializeDriverException;
20
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
21
import com.hardcode.gdbms.engine.data.driver.DriverException;
22
import com.hardcode.gdbms.engine.values.Value;
23
import com.iver.andami.PluginServices;
24
import com.iver.cit.gvsig.fmap.MapContext;
25
import com.iver.cit.gvsig.fmap.core.IGeometry;
26
import com.iver.cit.gvsig.fmap.crs.CRSFactory;
27
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
28
import com.iver.cit.gvsig.fmap.layers.FBitSet;
29
import com.iver.cit.gvsig.fmap.layers.FLayer;
30
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
31
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
32
import com.iver.cit.gvsig.fmap.layers.ReadableVectorial;
33
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
34
import com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection;
35
import com.iver.cit.gvsig.project.documents.view.gui.IView;
36
import com.iver.utiles.swing.threads.AbstractMonitorableTask;
37

    
38
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
39
 *
40
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
41
 *
42
 * This program is free software; you can redistribute it and/or
43
 * modify it under the terms of the GNU General Public License
44
 * as published by the Free Software Foundation; either version 2
45
 * of the License, or (at your option) any later version.
46
 *
47
 * This program is distributed in the hope that it will be useful,
48
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
49
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
50
 * GNU General Public License for more details.
51
 *
52
 * You should have received a copy of the GNU General Public License
53
 * along with this program; if not, write to the Free Software
54
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
55
 *
56
 * For more information, contact:
57
 *
58
 *  Generalitat Valenciana
59
 *   Conselleria d'Infraestructures i Transport
60
 *   Av. Blasco Ib??ez, 50
61
 *   46010 VALENCIA
62
 *   SPAIN
63
 *
64
 *      +34 963862235
65
 *   gvsig@gva.es
66
 *      www.gvsig.gva.es
67
 *
68
 *    or
69
 *
70
 *   IVER T.I. S.A
71
 *   Salamanca 50
72
 *   46005 Valencia
73
 *   Spain
74
 *
75
 *   +34 963163400
76
 *   dac@iver.es
77
 */
78
/* CVS MESSAGES:
79
 *
80
 * $Id$
81
 * $Log$
82
 *
83
 */
84
/**
85
 * This class writes a gvSIG layer and its children
86
 * (if the driver supports a layer with children)
87
 * @author Jorge Piera LLodr? (jorge.piera@iver.es)
88
 */
89
public class ExportTask extends AbstractMonitorableTask{
90
        private FLayer rootLayer = null;
91
        private GPEWriterHandler writer = null;
92
        private MapContext mapContext = null;
93
        private ExportGeometry eGeometry = null;        
94
        private File file = null;
95
        
96
        public ExportTask(FLayer layer, 
97
                        GPEWriterHandler writer,
98
                        MapContext mapContext,
99
                        File file){
100
                this.rootLayer = layer;
101
                this.writer = writer;
102
                this.mapContext = mapContext;
103
                this.eGeometry = new ExportGeometry(writer);
104
                this.file = file;
105
                eGeometry.setProjOrig(mapContext.getProjection());
106
                eGeometry.setProjDest(layer.getProjection());
107
                if (writer.getFormat().equals("KML")){
108
                        eGeometry.setProjDest(CRSFactory.getCRS("EPSG:4326"));                        
109
                }
110
                setInitialStep(0);
111
                setDeterminatedProcess(true);
112
                setStatusMessage(PluginServices.getText(this, "gpe_exporting"));
113
        }
114

    
115
        public void run() throws Exception {
116
                writer.initialize();
117
                exportLayer(rootLayer);
118
                writer.close();
119
                if (isFileLoaded()){
120
                        loadExportedFile();
121
                }
122
        }
123
        
124
        /**
125
         * Load the exported file
126
         * @throws GPEParserCreationException 
127
         * @throws IOException 
128
         */
129
        private void loadExportedFile() throws ParserCreationException, IOException{
130
                GPEParser parser = GPERegister.createParser(file.toURI());
131
                GPEVectorialDriver driver = GPEDriverFactory.createDriver(parser);
132
                driver.open(file);                                
133
                FLayer layer = LayerFactory.createLayer(file.getAbsolutePath(),
134
                                driver, driver.getProjection());
135
                ((IView)PluginServices.getMDIManager().getActiveWindow()).
136
                getMapControl().getMapContext().getLayers().addLayer(layer);
137
        }
138
        
139
        /**
140
         * @return true if the exported files have to be loaded
141
         */
142
        private boolean isFileLoaded(){
143
                int load = JOptionPane.showConfirmDialog(
144
                                (JComponent) PluginServices.getMDIManager().getActiveWindow()
145
                                , PluginServices.getText(this, "insertar_en_la_vista_la_capa_creada"),
146
                                PluginServices.getText(this,"insertar_capa"),
147
                                JOptionPane.YES_NO_OPTION);
148
                if (load == JOptionPane.YES_OPTION){
149
                        return true;
150
                }
151
                return false;
152
        }
153

    
154
        /**
155
         * It writes a layer and its children recursively
156
         * @param layer
157
         * The layer to write
158
         */
159
        private void exportLayer(FLayer layer){
160
                String projection  = null;
161
                if (layer.getProjection() != null){
162
                        projection = layer.getProjection().getAbrev();
163
                }
164
                writer.startLayer(null, layer.getName(), null, 
165
                                projection, null);
166
                //Sets the extent
167
                try {
168
                        writer.startBbox(null,new CoordinatesSequenceBbox(layer.getFullExtent()), mapContext.getProjection().getAbrev());
169
                        writer.endBbox();
170
                } catch (Exception e) {
171
                        writer.getErrorHandler().addWarning(new ExtentExportWarning(layer,e));
172
                } 
173
                //Export the layer information
174
                exportLayerInfo(layer);
175
                //If the layer has children...
176
                if (layer instanceof LayerCollection){
177
                        LayerCollection layers = (LayerCollection)layer;
178
                        for (int i=0 ; i<layers.getLayersCount() ; i++){
179
                                exportLayer(layers.getLayer(i));
180
                        }
181
                }
182
                writer.endLayer();
183
        }
184

    
185
        /**
186
         * It exports the layer information. Geometries if is
187
         * a vectorial layer, images if is a raster layer...
188
         * @param layer
189
         */
190
        private void exportLayerInfo(FLayer layer){
191
                try {
192
                        if (layer instanceof FLyrVect){
193
                                exportVectorialLayer((FLyrVect)layer);
194
                        }
195
                } catch (Exception e) {
196
                        writer.getErrorHandler().addError(e);
197
                }
198
        }        
199

    
200
        /**
201
         * Export the geometries of a vectorial layer
202
         * @param layer
203
         * @throws DriverException 
204
         * @throws DriverIOException 
205
         * @throws ReadDriverException 
206
         * @throws InitializeDriverException 
207
         * @throws ReadDriverException 
208
         */
209
        private void exportVectorialLayer(FLyrVect layer) throws DriverException, DriverIOException, InitializeDriverException, ReadDriverException {
210
                System.out.println(layer.getName());
211
                ReadableVectorial rv = layer.getSource();
212
                SelectableDataSource sds = layer.getRecordset();
213
                rv.start();
214
                //If there is a selection the rows to export have to be 
215
                //the selected rows
216
                FBitSet bitSet = sds.getSelection();
217
                int rowCount;                
218
                if (bitSet.cardinality() == 0){
219
                        rowCount = rv.getShapeCount();
220
                        for (int i = 0; i < rowCount; i++) {
221
                                exportFeature(sds, rv, i, layer);
222
                        }
223
                }else{
224
                        rowCount = bitSet.cardinality();
225
                        for (int i = bitSet.nextSetBit(0); i >= 0; i = bitSet
226
                        .nextSetBit(i + 1)) {
227
                                exportFeature(sds, rv, i, layer);
228
                        }
229
                }        
230
                rv.stop();
231
        }
232

    
233
        /**
234
         * It writes a feature (geometry + attributes)
235
         * @param sds
236
         * The selectable datasource to get the attributes
237
         * @param rv
238
         * The readable vectorial to get the geoemtries
239
         * @param index
240
         * The feature index
241
         * @param layer
242
         * Only to personalize the exceptions
243
         * @throws ReadDriverException
244
         */
245
        private void exportFeature(SelectableDataSource sds, ReadableVectorial rv, int index, FLayer layer){
246
                try {
247
                        writer.startFeature(String.valueOf(index), "FEATURE", null);
248
                        //Add the geoemtry
249
                        IGeometry geom = rv.getShape(index);                        
250
                        eGeometry.writeGeometry(geom);
251
                        //Add the attributes
252
                        Value[] values = sds.getRow(index);
253
                        for (int i=0 ; i<values.length ; i++){                                
254
                                writer.startElement("", 
255
                                                StringUtils.replaceAllString(sds.getFieldName(i), " ", "_"),
256
                                                values[i].toString());
257
                                writer.endElement();
258
                        }
259
                        writer.endFeature();
260
                } catch (Exception e) {
261
                        writer.getErrorHandler().addError(new FeatureExportException(layer,index,e));
262
                }                
263
        }
264

    
265
        /* (non-Javadoc)
266
         * @see com.iver.utiles.swing.threads.IMonitorableTask#finished()
267
         */
268
        public void finished() {                
269

    
270
        }
271
}