Statistics
| Revision:

root / trunk / extensions / extTopology / src / com / iver / cit / gvsig / geoprocess / impl / referencing / fmap / ReferencingGeoprocess.java @ 23163

History | View | Annotate | Download (8.85 KB)

1
/*
2
 * Created on 10-abr-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: 
47
 * $Log: 
48
 */
49
package com.iver.cit.gvsig.geoprocess.impl.referencing.fmap;
50

    
51
import java.rmi.server.UID;
52
import java.util.Map;
53

    
54
import org.apache.log4j.Logger;
55
import org.gvsig.exceptions.BaseException;
56
import org.gvsig.referencing.ReferencingUtil;
57
import org.gvsig.topology.Messages;
58
import org.opengis.referencing.FactoryException;
59
import org.opengis.referencing.operation.MathTransform;
60
import org.opengis.referencing.operation.TransformException;
61

    
62
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
63
import com.hardcode.gdbms.engine.values.Value;
64
import com.iver.andami.PluginServices;
65
import com.iver.cit.gvsig.drivers.featureiterators.FeatureBitsetIterator;
66
import com.iver.cit.gvsig.exceptions.visitors.StartVisitorException;
67
import com.iver.cit.gvsig.exceptions.visitors.StartWriterVisitorException;
68
import com.iver.cit.gvsig.exceptions.visitors.StopVisitorException;
69
import com.iver.cit.gvsig.fmap.MapContext;
70
import com.iver.cit.gvsig.fmap.core.DefaultFeature;
71
import com.iver.cit.gvsig.fmap.core.IFeature;
72
import com.iver.cit.gvsig.fmap.core.IGeometry;
73
import com.iver.cit.gvsig.fmap.drivers.IFeatureIterator;
74
import com.iver.cit.gvsig.fmap.drivers.ILayerDefinition;
75
import com.iver.cit.gvsig.fmap.drivers.LayerDefinition;
76
import com.iver.cit.gvsig.fmap.layers.FBitSet;
77
import com.iver.cit.gvsig.fmap.layers.FLayer;
78
import com.iver.cit.gvsig.fmap.layers.FLayers;
79
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
80
import com.iver.cit.gvsig.geoprocess.core.AbstractMonitorableGeoprocess;
81
import com.iver.cit.gvsig.geoprocess.core.fmap.DefinitionUtils;
82
import com.iver.cit.gvsig.geoprocess.core.fmap.FeaturePersisterProcessor2;
83
import com.iver.cit.gvsig.geoprocess.core.fmap.GeoprocessException;
84
import com.iver.cit.gvsig.project.documents.view.gui.View;
85
import com.iver.utiles.swing.threads.CancellableProgressTask;
86

    
87
/**
88
 * Geoprocess to spatial adjust or reference a vectorial layer
89
 * 
90
 * @author Alvaro Zabala
91
 * 
92
 */
93
public class ReferencingGeoprocess extends AbstractMonitorableGeoprocess {
94
        private static Logger logger = Logger.getLogger(ReferencingGeoprocess.class
95
                        .getName());
96

    
97
        private LayerDefinition resultLayerDefinition;
98

    
99
        private MathTransform mathTransform;
100

    
101
        private boolean hasAuxiliarLyrs = false;
102

    
103
        private FLyrVect[] auxiliarLyrs = null;
104

    
105
        public ReferencingGeoprocess(FLyrVect inputLyr) {
106
                firstLayer = inputLyr;
107
        }
108

    
109
        @Override
110
        public void checkPreconditions() throws GeoprocessException {
111
                try {
112
                        int numShapes = firstLayer.getSource().getShapeCount();
113
                        if (numShapes == 0)
114
                                throw new GeoprocessException("capa de entrada sin features");
115
                } catch (ReadDriverException e) {
116
                        throw new GeoprocessException(
117
                                        "Error de driver al tratar de acceder al numero de elementos",
118
                                        e);
119
                }
120

    
121
                if (mathTransform == null)
122
                        throw new GeoprocessException(
123
                                        "No se ha especificado un metodo de transformacion espacial");
124
        }
125

    
126
        @Override
127
        public ILayerDefinition createLayerDefinition() {
128
                if (resultLayerDefinition == null) {
129
                        try {
130
                                resultLayerDefinition = DefinitionUtils
131
                                                .createLayerDefinition(firstLayer);
132
                        } catch (Exception e) {
133
                                logger.error(e);
134
                        }
135
                }
136
                return resultLayerDefinition;
137
        }
138

    
139
        @Override
140
        public void setParameters(Map params) throws GeoprocessException {
141
                mathTransform = (MathTransform) params.get("mathTransform");
142
                hasAuxiliarLyrs = ((Boolean) params.get("hasAuxiliar")).booleanValue();
143
                if(hasAuxiliarLyrs){
144
                        this.auxiliarLyrs = (FLyrVect[]) params.get("auxiliarLyrs");
145
                }
146
        }
147

    
148
        @Override
149
        public void initialize(CancellableProgressTask progressMonitor)
150
                        throws GeoprocessException {
151
                try {
152
                        progressMonitor.setInitialStep(0);
153
                        int numOfSteps = firstLayer.getSource().getShapeCount();
154
                        progressMonitor.setFinalStep(numOfSteps);
155
                        progressMonitor.setDeterminatedProcess(true);
156
                        progressMonitor.setNote(Messages
157
                                        .getText("referencing_geoprocess_note"));
158
                        progressMonitor.setStatusMessage(Messages
159
                                        .getText("referencing_geoprocess_layer_message"));
160
                } catch (ReadDriverException e) {
161
                        throw new GeoprocessException(
162
                                        "error accediendo al numero de features de una layer", e);
163
                }
164

    
165
        }
166

    
167
        @Override
168
        public void process(CancellableProgressTask progressMonitor)
169
                        throws GeoprocessException {
170

    
171
                /*
172
                 * ++++++++++++++++++++++++++ FIXME: MOVER A CLASE ABSTRACTA
173
                 */
174
                if (progressMonitor != null) {
175
                        initialize(progressMonitor);
176
                }
177

    
178
                // Prepare the result
179
                try {
180
                        writer.preProcess();
181
                } catch (StartWriterVisitorException e) {
182
                        throw new GeoprocessException(e);
183
                }
184
                FeaturePersisterProcessor2 featureProcessor = new FeaturePersisterProcessor2(
185
                                writer);
186
                try {
187
                        featureProcessor.start();
188
                } catch (StartVisitorException e1) {
189
                        throw new GeoprocessException(e1);
190
                }
191

    
192
                /*
193
                 * +++++++++++++++++
194
                 */
195

    
196
                try {
197
                        IFeatureIterator featureIterator = null;
198
                        if (this.operateOnlyWithSelection) {
199
                                FBitSet selection = firstLayer.getRecordset().getSelection();
200
                                featureIterator = new FeatureBitsetIterator(selection,
201
                                                firstLayer.getSource());
202
                        } else {
203
                                featureIterator = firstLayer.getSource().getFeatureIterator();
204
                        }
205

    
206
                        while (featureIterator.hasNext()) {
207
                                IFeature feature = featureIterator.next();
208
                                IGeometry fmapGeo = feature.getGeometry();
209
                                Value[] values = feature.getAttributes();
210
                                IGeometry transformedGeometry = ReferencingUtil.getInstance()
211
                                                .createTransformedGeometry(fmapGeo, null, null,
212
                                                                this.mathTransform);
213
                                if (progressMonitor != null)
214
                                        progressMonitor.reportStep();
215

    
216
                                if (transformedGeometry != null) {
217
                                        DefaultFeature newFeature = new DefaultFeature(
218
                                                        transformedGeometry, values, new UID().toString());
219
                                        featureProcessor.processFeature(newFeature);
220
                                }
221
                        }// while
222
                        featureProcessor.finish();
223
                        if (progressMonitor != null) {
224
                                progressMonitor.finished();
225
                        }
226
                } catch (StopVisitorException e) {
227
                        throw new GeoprocessException(
228
                                        "Error al finalizar el guardado de los resultados del geoproceso",
229
                                        e);
230
                } catch (BaseException e) {
231
                        throw new GeoprocessException(
232
                                        "Error al acceder a la informacion del driver dentro del geoproceso",
233
                                        e);
234
                } catch (TransformException e) {
235
                        throw new GeoprocessException(
236
                                        "Error de transformacion de coordenadas", e);
237
                } catch (FactoryException e) {
238
                        throw new GeoprocessException(
239
                                        "Error al tratar de construir una transformacion de coordenadas",
240
                                        e);
241
                }
242

    
243
        }
244

    
245
        @Override
246
        public FLayer getResult() throws GeoprocessException {
247
                MapContext map = ((View) PluginServices.getMDIManager()
248
                                .getActiveWindow()).getModel().getMapContext();
249

    
250
                // first of all, we look for an existing flayers container
251
                FLayers rootLyrs = map.getLayers();
252
                String folderName = PluginServices.getText(this,
253
                                "SPATIAL_ADJUST_SESSION")
254
                                + " "
255
                                + ReferencingUtil.getInstance()
256
                                                .getNumberOfSpatialAdjustSessions();
257
                FLayers adjustSessionLyrs = (FLayers) rootLyrs.getLayer(folderName);
258
                if (adjustSessionLyrs == null){
259
                        adjustSessionLyrs = new FLayers();
260
                        adjustSessionLyrs.setMapContext(map);
261
                        adjustSessionLyrs.setParentLayer(map.getLayers());
262
                        adjustSessionLyrs.setName(folderName);
263
                        System.err.println("Error, geoproceso ajuste espacial sin capa de links");
264
                }
265
                if (hasAuxiliarLyrs && auxiliarLyrs != null) {
266
                        int position = 0;
267
                        for (int i = 0; i < auxiliarLyrs.length; i++) {
268
                                adjustSessionLyrs.addLayer(position, auxiliarLyrs[i]);
269
                                position++;
270
                        }
271
                } 
272
                
273
                adjustSessionLyrs.addLayer(super.getResult());
274
                
275
                map.invalidate();
276
                return adjustSessionLyrs;
277
        }
278

    
279
}