Statistics
| Revision:

root / trunk / extensions / extTopology / src / com / iver / cit / gvsig / geoprocess / impl / voronoi / fmap / VoronoiGeoprocess.java @ 21269

History | View | Annotate | Download (6.77 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.voronoi.fmap;
50

    
51
import java.util.List;
52
import java.util.Map;
53

    
54
import org.apache.log4j.Logger;
55
import org.gvsig.exceptions.BaseException;
56
import org.gvsig.jts.voronoi.VoronoiAndTinInputLyr;
57
import org.gvsig.jts.voronoi.Voronoier;
58
import org.gvsig.topology.Messages;
59

    
60
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
61
import com.iver.andami.PluginServices;
62
import com.iver.cit.gvsig.exceptions.visitors.StartVisitorException;
63
import com.iver.cit.gvsig.exceptions.visitors.StartWriterVisitorException;
64
import com.iver.cit.gvsig.exceptions.visitors.StopVisitorException;
65
import com.iver.cit.gvsig.fmap.core.IFeature;
66
import com.iver.cit.gvsig.fmap.drivers.FieldDescription;
67
import com.iver.cit.gvsig.fmap.drivers.ILayerDefinition;
68
import com.iver.cit.gvsig.fmap.drivers.SHPLayerDefinition;
69
import com.iver.cit.gvsig.geoprocess.core.AbstractMonitorableGeoprocess;
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.fmap.XTypes;
73
import com.iver.utiles.swing.threads.CancellableProgressTask;
74

    
75
public class VoronoiGeoprocess extends AbstractMonitorableGeoprocess {
76

    
77
        private static Logger logger = Logger.getLogger(VoronoiGeoprocess.class
78
                        .getName());
79
        private boolean useTin;
80
        private boolean useThiessen;
81

    
82
        public VoronoiGeoprocess(VoronoiAndTinInputLyr inputLayer) {
83
                super();
84
                this.firstLayer = inputLayer;
85
        }
86

    
87
        public void checkPreconditions() throws GeoprocessException {
88
                if (!(firstLayer instanceof VoronoiAndTinInputLyr))
89
                        throw new GeoprocessException(
90
                                        "Geoproceso Voronoi cuya capa de entrada es incorrecta: VoronoiAndTinInputLyr");
91

    
92
        }
93

    
94
        public ILayerDefinition createLayerDefinition() {
95
                ILayerDefinition resultLayerDefinition = new SHPLayerDefinition();
96
                resultLayerDefinition.setShapeType(XTypes.POLYGON);
97
                FieldDescription[] fields = new FieldDescription[2];
98
                fields[0] = new FieldDescription();
99
                fields[0].setFieldLength(10);
100
                fields[0].setFieldName("fid");
101
                fields[0].setFieldType(XTypes.BIGINT);
102

    
103
                fields[1] = new FieldDescription();
104
                fields[1].setFieldLength(10);
105
                fields[1].setFieldDecimalCount(4);
106
                fields[1].setFieldName(PluginServices.getText(this, "punto_causante"));
107
                fields[1].setFieldType(XTypes.DOUBLE);
108

    
109
                resultLayerDefinition.setFieldsDesc(fields);
110
                return resultLayerDefinition;
111
        }
112

    
113
        
114

    
115
        public void setParameters(Map params) throws GeoprocessException {
116
                Boolean onlySelection = (Boolean) params.get("layer_selection");
117
                if (onlySelection != null)
118
                        this.operateOnlyWithSelection = onlySelection.booleanValue();
119

    
120
                Boolean useTinB = (Boolean) params.get("tin");
121
                if(useTinB != null)
122
                        useTin = useTinB.booleanValue();
123
                
124
                Boolean useThiessenB = (Boolean) params.get("thiessen");
125
                if(useThiessenB != null)
126
                useThiessen = useThiessenB.booleanValue(); 
127
        }
128

    
129

    
130
        public void process(CancellableProgressTask progressMonitor)
131
                        throws GeoprocessException {
132
                if (progressMonitor != null) {
133
                        initialize(progressMonitor);
134
                }
135

    
136
                // Prepare the result
137
                try {
138
                        writer.preProcess();
139
                } catch (StartWriterVisitorException e) {
140
                        throw new GeoprocessException(e);
141
                }
142
                FeaturePersisterProcessor2 featureProcessor = new FeaturePersisterProcessor2(
143
                                writer);
144
                try {
145
                        featureProcessor.start();
146
                } catch (StartVisitorException e1) {
147
                        throw new GeoprocessException(e1);
148
                }
149

    
150
                try {//todo quitar esto y hacer que devuelva un FLyrVect
151
                        List<IFeature> voronoiFeatures = null;
152
                        if(useTin){
153
                                voronoiFeatures = Voronoier.createTIN((VoronoiAndTinInputLyr) firstLayer, operateOnlyWithSelection, "CHEW", progressMonitor);
154
                        }else if(useThiessen){
155
                                voronoiFeatures = Voronoier.createThiessen((VoronoiAndTinInputLyr) firstLayer, operateOnlyWithSelection, "CHEW", progressMonitor);
156
                        }
157
                        
158
                        if(progressMonitor != null){
159
                                progressMonitor.setInitialStep(0);
160
                                int numOfSteps = voronoiFeatures.size();
161
                                progressMonitor.setFinalStep(numOfSteps);
162
                                progressMonitor.setDeterminatedProcess(true);
163
                                progressMonitor.setNote(Messages.getText("saving_results"));
164
                                progressMonitor.
165
                                        setStatusMessage(Messages.getText("voronoi_diagram_layer_message"));
166
                        }
167
                        
168
                        for (int i = 0; i < voronoiFeatures.size(); i++) {
169
                                featureProcessor.processFeature(voronoiFeatures.get(i));
170
                                if (progressMonitor != null) {
171
                                        progressMonitor.reportStep();
172
                                }
173
                                
174
                        }
175
                        featureProcessor.finish();
176
                        if (progressMonitor != null) {
177
                                progressMonitor.finished();
178
                        }
179
                } catch (StopVisitorException e) {
180
                        throw new GeoprocessException(
181
                                        "Error al finalizar el guardado de los resultados del geoproceso",
182
                                        e);
183
                } catch (BaseException e) {
184
                        throw new GeoprocessException(
185
                                        "Error al acceder a la informacion del driver dentro del geoproceso",
186
                                        e);
187
                } 
188
        }
189

    
190
        public void initialize(CancellableProgressTask progressMonitor)
191
                        throws GeoprocessException {
192
                try {
193
                        progressMonitor.setInitialStep(0);
194
                        int numOfSteps = firstLayer.getSource().getShapeCount();
195
                        progressMonitor.setFinalStep(numOfSteps);
196
                        progressMonitor.setDeterminatedProcess(true);
197
                        progressMonitor.setNote(Messages.getText("voronoi_diagram_note"));
198
                        progressMonitor.setStatusMessage(Messages
199
                                        .getText("voronoi_diagram_layer_message"));
200
                } catch (ReadDriverException e) {
201
                        throw new GeoprocessException(
202
                                        "error accediendo al numero de features de una layer", e);
203
                }
204
        }
205

    
206
}