Statistics
| Revision:

root / trunk / extensions / extGeoprocessingExtensions / src / com / iver / cit / gvsig / geoprocess / impl / reproject / fmap / ReprojectGeoprocess.java @ 6179

History | View | Annotate | Download (9.25 KB)

1
/*
2
 * Created on 03-jul-2006
3
 *
4
 * gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
5
 *
6
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
7
 *
8
 * This program is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU General Public License
10
 * as published by the Free Software Foundation; either version 2
11
 * of the License, or (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program; if not, write to the Free Software
20
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
21
 *
22
 * For more information, contact:
23
 *
24
 *  Generalitat Valenciana
25
 *   Conselleria d'Infraestructures i Transport
26
 *   Av. Blasco Ib??ez, 50
27
 *   46010 VALENCIA
28
 *   SPAIN
29
 *
30
 *      +34 963862235
31
 *   gvsig@gva.es
32
 *      www.gvsig.gva.es
33
 *
34
 *    or
35
 *
36
 *   IVER T.I. S.A
37
 *   Salamanca 50
38
 *   46005 Valencia
39
 *   Spain
40
 *
41
 *   +34 963163400
42
 *   dac@iver.es
43
 */
44
/* CVS MESSAGES:
45
 *
46
 * $Id: ReprojectGeoprocess.java 6179 2006-07-04 16:43:18Z azabala $
47
 * $Log$
48
 * Revision 1.2  2006-07-04 16:43:18  azabala
49
 * *** empty log message ***
50
 *
51
 * Revision 1.1  2006/07/03 20:28:38  azabala
52
 * *** empty log message ***
53
 *
54
 *
55
 */
56
package com.iver.cit.gvsig.geoprocess.impl.reproject.fmap;
57

    
58
import java.util.Map;
59

    
60
import org.cresques.cts.ICoordTrans;
61
import org.cresques.cts.IProjection;
62
import org.cresques.cts.gt2.CoordSys;
63
import org.cresques.cts.gt2.CoordTrans;
64

    
65
import com.hardcode.gdbms.engine.data.driver.DriverException;
66
import com.hardcode.gdbms.engine.values.Value;
67
import com.hardcode.gdbms.engine.values.ValueFactory;
68
import com.iver.andami.PluginServices;
69
import com.iver.cit.gvsig.fmap.core.IFeature;
70
import com.iver.cit.gvsig.fmap.core.IGeometry;
71
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
72
import com.iver.cit.gvsig.fmap.drivers.FieldDescription;
73
import com.iver.cit.gvsig.fmap.drivers.ILayerDefinition;
74
import com.iver.cit.gvsig.fmap.edition.EditionException;
75
import com.iver.cit.gvsig.fmap.layers.FBitSet;
76
import com.iver.cit.gvsig.fmap.layers.FLayer;
77
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
78
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
79
import com.iver.cit.gvsig.fmap.layers.layerOperations.AlphanumericData;
80
import com.iver.cit.gvsig.fmap.layers.layerOperations.VectorialData;
81
import com.iver.cit.gvsig.fmap.operations.strategies.FeatureVisitor;
82
import com.iver.cit.gvsig.fmap.operations.strategies.Strategy;
83
import com.iver.cit.gvsig.fmap.operations.strategies.StrategyManager;
84
import com.iver.cit.gvsig.fmap.operations.strategies.VisitException;
85
import com.iver.cit.gvsig.geoprocess.core.fmap.AbstractGeoprocess;
86
import com.iver.cit.gvsig.geoprocess.core.fmap.DefinitionUtils;
87
import com.iver.cit.gvsig.geoprocess.core.fmap.FeatureFactory;
88
import com.iver.cit.gvsig.geoprocess.core.fmap.FeaturePersisterProcessor2;
89
import com.iver.cit.gvsig.geoprocess.core.fmap.GeoprocessException;
90
import com.iver.utiles.swing.threads.AbstractMonitorableTask;
91
import com.iver.utiles.swing.threads.IMonitorableTask;
92

    
93
public class ReprojectGeoprocess extends AbstractGeoprocess {
94

    
95
        /**
96
         * Schema of the result layer
97
         */
98

    
99
        private ILayerDefinition resultLayerDefinition;
100

    
101
        /**
102
         * flag to only clip selection of input layer
103
         */
104
        private boolean onlyInputLayerSelection = false;
105

    
106
        /**
107
         * Processes features (writing them)
108
         */
109
        FeaturePersisterProcessor2 processor;
110

    
111
        /**
112
         * Target projection for features of the input layer
113
         */
114
        private IProjection targetProjection;
115
        
116
        ReprojectTask task = null;
117

    
118
        public ReprojectGeoprocess() {
119
                super();
120
        }
121
        
122
        public void setFirstLayer(FLyrVect inputLayer){
123
                this.firstLayer = inputLayer;
124
        }
125

    
126
        public void setParameters(Map params) throws GeoprocessException {
127
                Boolean firstLayerSelection = (Boolean) params
128
                                .get("firstlayerselection");
129
                if (firstLayerSelection != null)
130
                        this.onlyInputLayerSelection = firstLayerSelection.booleanValue();
131
                this.targetProjection = (IProjection) params.get("targetProjection");
132

    
133
        }
134

    
135
        public void checkPreconditions() throws GeoprocessException {
136
                if (this.writer == null || this.schemaManager == null) {
137
                        throw new GeoprocessException(
138
                                        "Operacion de reproyecci?n sin especificar capa de resultados");
139
                }
140
                if (this.targetProjection == null)
141
                        throw new GeoprocessException(
142
                                        "Geoproceso reproyecci?n sin proyecci?n destino");
143
        }
144

    
145
        public void process() throws GeoprocessException {
146
                try {
147
                        createTask().run();
148
                } catch (Exception e) {
149
                        throw new GeoprocessException(
150
                                        "Error durante ejecuci?n del geoproceso", e);
151
                }
152

    
153
        }
154

    
155
        public ILayerDefinition createLayerDefinition() {
156
                if (resultLayerDefinition == null) {
157
                        try {
158
                                resultLayerDefinition = DefinitionUtils
159
                                                .createLayerDefinition(super.firstLayer);
160
                        } catch (DriverException e) {
161
                                // TODO Auto-generated catch block
162
                                e.printStackTrace();
163
                        } catch (com.iver.cit.gvsig.fmap.DriverException e) {
164
                                // TODO Auto-generated catch block
165
                                e.printStackTrace();
166
                        }
167
                }
168
                return resultLayerDefinition;
169
        }
170

    
171
        class ReprojectVisitor implements FeatureVisitor {
172

    
173
                FeaturePersisterProcessor2 processor;
174

    
175
                SelectableDataSource recordset;
176

    
177
                ILayerDefinition layerDefinition;
178

    
179
                FBitSet selection;
180

    
181
                ICoordTrans ct;
182

    
183
                ReprojectVisitor(ILayerDefinition layerDefinition,
184
                                FeaturePersisterProcessor2 processor, FBitSet selection,
185
                                ICoordTrans coordTrans) {
186

    
187
                        this.layerDefinition = layerDefinition;
188
                        this.processor = processor;
189
                        this.selection = selection;
190
                        this.ct = coordTrans;
191
                }
192

    
193
                public void visit(IGeometry g, int index) throws VisitException {
194
                        if (g == null)
195
                                return;
196
                        if (selection != null) {
197
                                if (!selection.get(index))
198
                                        return;
199
                        }// if
200
                        g.reProject(ct);
201
                        IFeature feature = null;
202
                        try {
203
                                feature = createFeature(g, index);
204
                        } catch (DriverException e) {
205
                                throw new VisitException("Error al crear al reproyectar "+index, e);
206
                        }
207
                        processor.processFeature(feature);
208
                }
209

    
210
                public String getProcessDescription() {
211
                        return "";
212
                }
213

    
214
                public void stop(FLayer layer) {
215
                        processor.finish();
216
                }
217

    
218
                public boolean start(FLayer layer) {
219
                        if (layer instanceof AlphanumericData
220
                                        && layer instanceof VectorialData) {
221
                                try {
222
                                        this.recordset = ((AlphanumericData) layer).getRecordset();
223
                                        this.processor.start();
224
                                } catch (EditionException e) {
225
                                        return false;
226
                                } catch (com.iver.cit.gvsig.fmap.DriverException e) {
227
                                        return false;
228
                                }
229
                                return true;
230
                        }
231
                        return false;
232
                }
233

    
234
                private IFeature createFeature(IGeometry geometry, int layerIndex)
235
                                throws DriverException,
236
                                com.hardcode.gdbms.engine.data.driver.DriverException {
237
                        IFeature solution = null;
238
                        FieldDescription[] fields = layerDefinition.getFieldsDesc();
239
                        Value[] featureAttr = new Value[fields.length];
240
                        int numFields = recordset.getFieldCount();
241
                        for (int indexField = 0; indexField < numFields; indexField++) {
242
                                // for each field of firstRs
243
                                String fieldName = recordset.getFieldName(indexField);
244
                                for (int j = 0; j < fields.length; j++) {
245
                                        if (fieldName.equalsIgnoreCase(fields[j].getFieldName())) {
246
                                                featureAttr[j] = recordset.getFieldValue(layerIndex,
247
                                                                indexField);
248
                                                break;
249
                                        }// if
250
                                }// for
251
                        }// for
252
                        // now we put null values
253
                        for (int i = 0; i < featureAttr.length; i++) {
254
                                if (featureAttr[i] == null)
255
                                        featureAttr[i] = ValueFactory.createNullValue();
256
                        }
257
                        solution = FeatureFactory.createFeature(featureAttr, geometry);
258
                        return solution;
259
                }
260
        }
261

    
262
        class ReprojectTask extends AbstractMonitorableTask {
263

    
264
                private ReprojectTask() {
265
                        setInitialStep(0);
266
                        try {
267
                                if (onlyInputLayerSelection) {
268
                                        int numSelected = firstLayer.getRecordset().getSelection()
269
                                                        .cardinality();
270
                                        setFinalStep(numSelected);
271
                                } else {
272
                                        int numShapes = firstLayer.getSource().getShapeCount();
273
                                        setFinalStep(numShapes);
274
                                }// else
275
                        } catch (com.iver.cit.gvsig.fmap.DriverException e) {
276
                                // TODO Auto-generated catch block
277
                                e.printStackTrace();
278
                        } catch (DriverIOException e) {
279
                                // TODO Auto-generated catch block
280
                                e.printStackTrace();
281
                        }
282
                        setDeterminatedProcess(true);
283
                        setStatusMessage(PluginServices.getText(this,
284
                                        "XYShift._Progress_Message"));
285

    
286
                }
287

    
288
                public void run() throws Exception {
289
                        processor =
290
                                new FeaturePersisterProcessor2(writer);
291
                        FBitSet selection = null;
292
                        if(onlyInputLayerSelection)
293
                                selection = firstLayer.getRecordset().getSelection();
294
                        IProjection from = firstLayer.getProjection();
295
                        IProjection to = targetProjection;
296
                        ICoordTrans ct = new CoordTrans((CoordSys) from, (CoordSys) to);
297
                        ReprojectVisitor visitor = new ReprojectVisitor(resultLayerDefinition, 
298
                                        processor, selection, ct);
299
                        Strategy strategy = StrategyManager.getStrategy(firstLayer);
300
                        try {
301
                                strategy.process(visitor, this);
302
                        } catch (Exception e) {
303
                                e.printStackTrace();
304
                        }
305
                }
306

    
307
                // TODO INTERNACIONALIZAR LOS MENSAJES
308
                public String getNote() {
309
                        return "Reproyectando features..." + " " + getCurrentStep() + " "
310
                                        + "de" + " " + getFinishStep();
311
                }
312

    
313
                public void cancel() {
314
                        setCanceled(true);
315
                        ReprojectGeoprocess.this.cancel();
316
                }
317
        }
318

    
319
        public IMonitorableTask createTask() {
320
                if(task == null)
321
                        task = new ReprojectTask();
322
                return task;
323
        }
324

    
325
}