Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extTopology / src / com / iver / cit / gvsig / geoprocess / impl / fliplines / fmap / FlipLinesGeoprocess.java @ 21293

History | View | Annotate | Download (7.04 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.fliplines.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.fmap.core.FGeometryUtil;
57
import org.gvsig.fmap.core.NewFConverter;
58
import org.gvsig.jts.JtsUtil;
59
import org.gvsig.topology.Messages;
60

    
61
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
62
import com.hardcode.gdbms.engine.values.Value;
63
import com.iver.cit.gvsig.drivers.featureiterators.FeatureBitsetIterator;
64
import com.iver.cit.gvsig.exceptions.visitors.StartVisitorException;
65
import com.iver.cit.gvsig.exceptions.visitors.StartWriterVisitorException;
66
import com.iver.cit.gvsig.exceptions.visitors.StopVisitorException;
67
import com.iver.cit.gvsig.fmap.core.DefaultFeature;
68
import com.iver.cit.gvsig.fmap.core.IFeature;
69
import com.iver.cit.gvsig.fmap.core.IGeometry;
70
import com.iver.cit.gvsig.fmap.drivers.IFeatureIterator;
71
import com.iver.cit.gvsig.fmap.drivers.ILayerDefinition;
72
import com.iver.cit.gvsig.fmap.drivers.LayerDefinition;
73
import com.iver.cit.gvsig.fmap.layers.FBitSet;
74
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
75
import com.iver.cit.gvsig.geoprocess.core.AbstractMonitorableGeoprocess;
76
import com.iver.cit.gvsig.geoprocess.core.fmap.DefinitionUtils;
77
import com.iver.cit.gvsig.geoprocess.core.fmap.FeaturePersisterProcessor2;
78
import com.iver.cit.gvsig.geoprocess.core.fmap.GeoprocessException;
79
import com.iver.utiles.swing.threads.CancellableProgressTask;
80
import com.vividsolutions.jts.geom.Geometry;
81
import com.vividsolutions.jts.geom.GeometryCollection;
82
import com.vividsolutions.jts.geom.LineString;
83

    
84
/**
85
 * Geoprocess to generalize linear or poligonal vectorial layers.
86
 * @author Alvaro Zabala
87
 *
88
 */
89
public class FlipLinesGeoprocess extends AbstractMonitorableGeoprocess {
90

    
91
        private static Logger logger = Logger.getLogger(FlipLinesGeoprocess.class
92
                        .getName());
93
        
94
        private LayerDefinition resultLayerDefinition;
95
        
96
        
97
        
98
        public FlipLinesGeoprocess(FLyrVect inputLayer) {
99
                this.firstLayer = inputLayer;
100
        }
101

    
102
        public void checkPreconditions() throws GeoprocessException {
103
                int lyrDimensions;
104
                try {
105
                        lyrDimensions = FGeometryUtil.getDimensions(firstLayer.getShapeType());
106
                
107
                        if(lyrDimensions != 1)
108
                                throw new GeoprocessException(
109
                                        "Geoproceso invertir lineas con capa que no es de lineas");
110
                
111
                } catch (ReadDriverException e) {
112
                        throw new GeoprocessException("Error intentando acceder al tipo de geometria de capa vectorial");
113
                }
114

    
115
        }
116

    
117
        public ILayerDefinition createLayerDefinition() {
118
                if(resultLayerDefinition == null){
119
                        try {
120
                                resultLayerDefinition = DefinitionUtils
121
                                                .createLayerDefinition(firstLayer);
122
                        } catch (Exception e) {
123
                                logger.error(e);
124
                        }
125
                }
126
                return resultLayerDefinition;
127
        }
128

    
129
        
130

    
131
        public void setParameters(Map params) throws GeoprocessException {
132
                Boolean onlySelection = (Boolean) params.get("layer_selection");
133
                if (onlySelection != null)
134
                        this.operateOnlyWithSelection = onlySelection.booleanValue();
135
        }
136

    
137
        
138

    
139
        public void process(CancellableProgressTask progressMonitor)
140
                        throws GeoprocessException {
141
                if (progressMonitor != null) {
142
                        initialize(progressMonitor);
143
                }
144

    
145
                // Prepare the result
146
                try {
147
                        writer.preProcess();
148
                } catch (StartWriterVisitorException e) {
149
                        throw new GeoprocessException(e);
150
                }
151
                FeaturePersisterProcessor2 featureProcessor = new FeaturePersisterProcessor2(
152
                                writer);
153
                try {
154
                        featureProcessor.start();
155
                } catch (StartVisitorException e1) {
156
                        throw new GeoprocessException(e1);
157
                }
158

    
159
                try {
160
                        IFeatureIterator featureIterator = null;
161
                        if(this.operateOnlyWithSelection){
162
                                FBitSet selection = firstLayer.getRecordset().getSelection();
163
                        featureIterator = new FeatureBitsetIterator(selection, firstLayer.getSource());
164
                        }else{
165
                                featureIterator = firstLayer.getSource().getFeatureIterator();
166
                        }
167
                        
168
                        while(featureIterator.hasNext()){
169
                                IFeature feature = featureIterator.next();
170
                                IGeometry fmapGeo = feature.getGeometry();
171
                                Value[] values = feature.getAttributes();
172
                                Geometry geometry = JtsUtil.toJtsGeometry(fmapGeo);
173
                                Geometry reversedGeometry = null;
174
                                LineString[] lineStrings = JtsUtil.extractLineStrings(geometry);
175
                                GeometryCollection gc = JtsUtil.GEOMETRY_FACTORY.createGeometryCollection(lineStrings);
176
                                reversedGeometry = gc;
177
                                if(progressMonitor != null)
178
                                        progressMonitor.reportStep();
179
                                
180
                                if(reversedGeometry != null){
181
                                        IGeometry simplifiedFMap = NewFConverter.toFMap(reversedGeometry);
182
                                        DefaultFeature newFeature = 
183
                                                new DefaultFeature(simplifiedFMap, 
184
                                                                                                        values, 
185
                                                                                                new UID().toString());
186
                                        featureProcessor.processFeature(newFeature);
187
                                }
188
                        }
189
                        featureProcessor.finish();
190
                        if (progressMonitor != null) {
191
                                progressMonitor.finished();
192
                        }
193
                } catch (StopVisitorException e) {
194
                        throw new GeoprocessException(
195
                                        "Error al finalizar el guardado de los resultados del geoproceso",
196
                                        e);
197
                } catch (BaseException e) {
198
                        throw new GeoprocessException(
199
                                        "Error al acceder a la informacion del driver dentro del geoproceso",
200
                                        e);
201
                } 
202
        }
203
        
204
        
205

    
206
        public void initialize(CancellableProgressTask progressMonitor)
207
                        throws GeoprocessException {
208
                try {
209
                        progressMonitor.setInitialStep(0);
210
                        int numOfSteps = firstLayer.getSource().getShapeCount();
211
                        progressMonitor.setFinalStep(numOfSteps);
212
                        progressMonitor.setDeterminatedProcess(true);
213
                        progressMonitor.setNote(Messages.getText("flip_lines_note"));
214
                        progressMonitor.setStatusMessage(Messages
215
                                        .getText("flip_lines_layer_message"));
216
                } catch (ReadDriverException e) {
217
                        throw new GeoprocessException(
218
                                        "error accediendo al numero de features de una layer", e);
219
                }
220
        }
221

    
222
}