Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extGeoprocessingExtensions / src / com / iver / cit / gvsig / geoprocess / impl / xyshift / fmap / XYShiftGeoprocess.java @ 6111

History | View | Annotate | Download (7.3 KB)

1
/*
2
 * Created on 27-jun-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: XYShiftGeoprocess.java 6111 2006-06-29 17:58:31Z azabala $
47
* $Log$
48
* Revision 1.2  2006-06-29 17:58:31  azabala
49
* *** empty log message ***
50
*
51
* Revision 1.1  2006/06/28 18:17:21  azabala
52
* first version in cvs
53
*
54
*
55
*/
56
package com.iver.cit.gvsig.geoprocess.impl.xyshift.fmap;
57

    
58
import java.util.Map;
59

    
60
import com.hardcode.gdbms.engine.data.driver.DriverException;
61
import com.iver.andami.PluginServices;
62
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
63
import com.iver.cit.gvsig.fmap.drivers.ILayerDefinition;
64
import com.iver.cit.gvsig.fmap.layers.FBitSet;
65
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
66
import com.iver.cit.gvsig.fmap.operations.strategies.Strategy;
67
import com.iver.cit.gvsig.fmap.operations.strategies.StrategyManager;
68
import com.iver.cit.gvsig.geoprocess.core.fmap.AbstractGeoprocess;
69
import com.iver.cit.gvsig.geoprocess.core.fmap.DefinitionUtils;
70
import com.iver.cit.gvsig.geoprocess.core.fmap.FeaturePersisterProcessor2;
71
import com.iver.cit.gvsig.geoprocess.core.fmap.GeoprocessException;
72
import com.iver.cit.gvsig.geoprocess.core.util.UnitUtils;
73
import com.iver.utiles.swing.threads.AbstractMonitorableTask;
74
import com.iver.utiles.swing.threads.DefaultCancellableMonitorable;
75
import com.iver.utiles.swing.threads.IMonitorableTask;
76
/**
77
 * This geoprocess apply an offset in x and y directions producing
78
 * a new FLyrVect whose coordinates has been shifted.
79
 * @author azabala
80
 *
81
 */
82
public class XYShiftGeoprocess  extends AbstractGeoprocess {
83

    
84
        /**
85
         * Schema of the result layer
86
         */
87

    
88
        private ILayerDefinition resultLayerDefinition;
89

    
90
        /**
91
         * flag to only clip selection of input layer
92
         */
93
        private boolean onlyInputLayerSelection = false;
94
        
95
        /**
96
         * feature visitor to apply an xy shift
97
         */
98
        private XYShifterFeatureVisitor visitor;
99
        
100
        /**
101
         * Processes shifted features (writing them)
102
         */
103
        FeaturePersisterProcessor2 processor;
104
        
105
        /**
106
         * x offset
107
         */
108
        private double offsetX;
109
        /**
110
         * y offset
111
         */
112
        private double offsetY;
113
        
114
        
115
        public XYShiftGeoprocess(FLyrVect inputLayer){
116
                setFirstOperand(inputLayer);
117
        }
118
        
119
        
120
        public void setParameters(Map params) throws GeoprocessException {
121
                Boolean firstLayerSelection = (Boolean) 
122
                        params.get("firstlayerselection");
123
                if (firstLayerSelection != null)
124
                        this.onlyInputLayerSelection = 
125
                                firstLayerSelection.booleanValue();
126
                Double xShift = (Double)
127
                        params.get("xshift");
128
                if(xShift != null)
129
                        this.offsetX = UnitUtils.
130
                                getInInternalUnits(xShift.doubleValue());
131
                Double yShift = (Double)
132
                        params.get("yshift");
133
                if(yShift != null)
134
                        this.offsetY = UnitUtils.
135
                                getInInternalUnits(yShift.doubleValue());
136
        }
137

    
138
        public void checkPreconditions() throws GeoprocessException {
139
                //TODO llevar a un  metodo verifyWriter de AbstractGeoprocess
140
                if (this.writer == null || this.schemaManager == null) {
141
                        throw new GeoprocessException(
142
                                        "Operacion de xyshift sin especificar capa de resultados");
143
                }
144
                if(offsetX == 0 && offsetY == 0)
145
                        throw new GeoprocessException("Geoproceso XYShift inicializado con un offset de 0,0");
146

    
147
        }
148

    
149
        public void process() throws GeoprocessException {
150
                try {
151
                        new XYShiftTask().run();
152
                } catch (Exception e) {
153
                        throw new GeoprocessException("Error al ejecutar el geoproceso XYShift", e);
154
                }
155

    
156
        }
157

    
158
        /**
159
         * ITask (cancelable and progres-monitorable task) which makes all 
160
         * xyshift computations
161
         * @author azabala
162
         *
163
         */
164
        class XYShiftTask extends AbstractMonitorableTask{
165

    
166
                private XYShiftTask(){
167
                        setInitialStep(0);
168
                        try {
169
                                if(onlyInputLayerSelection){
170
                                        int numSelected = firstLayer.
171
                                                                                getRecordset().
172
                                                                                getSelection().
173
                                                                                cardinality();
174
                                        setFinalStep(numSelected);
175
                                }else{
176
                                        int numShapes = firstLayer.getSource().getShapeCount();
177
                                        setFinalStep(numShapes);
178
                                }//else
179
                        } catch (com.iver.cit.gvsig.fmap.DriverException e) {
180
                                // TODO Auto-generated catch block
181
                                e.printStackTrace();
182
                        } catch (DriverIOException e) {
183
                                // TODO Auto-generated catch block
184
                                e.printStackTrace();
185
                        }
186
                        setDeterminatedProcess(true);
187
                        setStatusMessage(PluginServices.getText(this, "XYShift._Progress_Message"));
188
                        
189
                }
190
                
191
                public void run() throws Exception {
192
                        processor =
193
                                new FeaturePersisterProcessor2(writer);
194
                        visitor = new XYShifterFeatureVisitor(processor,
195
                                        createLayerDefinition(), offsetX, offsetY );
196
                        if(onlyInputLayerSelection)
197
                                visitor.setSelection(firstLayer.getRecordset().getSelection());
198
                        Strategy strategy = StrategyManager.getStrategy(firstLayer);
199
                        try {
200
                                //AbstractMonitorableTask is a cancel monitor too
201
                                strategy.process(visitor, this);
202
                        } catch (Exception e) {
203
                                e.printStackTrace();
204
                        }
205
                }
206
                
207
                //TODO INTERNACIONALIZAR LOS MENSAJES
208
                public String getNote() {
209
                        return "Desplazando features..."  + 
210
                        " " +
211
                        getCurrentStep()+ 
212
                        " "+
213
                        "de"+
214
                        " "+
215
                        getFinishStep();
216
                }
217

    
218
                public void cancel() {
219
                        setCanceled(true);
220
                        XYShiftGeoprocess.this.cancel();
221
                }
222
        }
223
        
224
        
225
        public IMonitorableTask createTask() {
226
                return new XYShiftTask();
227
        }
228

    
229
        public void setFirstOperand(FLyrVect firstLayer) {
230
                super.firstLayer = firstLayer;
231
        }
232

    
233

    
234
        public ILayerDefinition createLayerDefinition() {
235
                if(resultLayerDefinition == null){
236
                        try {
237
                                resultLayerDefinition = DefinitionUtils.
238
                                        createLayerDefinition(super.firstLayer);
239
                        } catch (DriverException e) {
240
                                // TODO Auto-generated catch block
241
                                e.printStackTrace();
242
                        } catch (com.iver.cit.gvsig.fmap.DriverException e) {
243
                                // TODO Auto-generated catch block
244
                                e.printStackTrace();
245
                        }
246
                }
247
                return resultLayerDefinition;
248
        }
249
        
250
        private DefaultCancellableMonitorable createCancelMonitor() {
251
                DefaultCancellableMonitorable monitor = new DefaultCancellableMonitorable();
252
                monitor.setInitialStep(0);
253
                monitor.setDeterminatedProcess(true);
254
                int numSteps = 0;
255
                try {
256
                        if (onlyInputLayerSelection){
257
                                FBitSet selection = firstLayer.getRecordset().getSelection();
258
                                numSteps = selection.cardinality();
259
                        }else{
260
                                        numSteps = firstLayer.getSource().getShapeCount();
261
                        }
262
                        monitor.setFinalStep(numSteps);
263
                } catch (DriverIOException e) {
264
                        // TODO Auto-generated catch block
265
                        e.printStackTrace();
266
                } catch (com.iver.cit.gvsig.fmap.DriverException e) {
267
                        // TODO Auto-generated catch block
268
                        e.printStackTrace();
269
                }
270
                return monitor;
271
        }
272

    
273

    
274
}
275