root / trunk / extensions / extGeoProcessing / src / com / iver / gvsig / geoprocessing / impl / dissolve / DissolveGeoprocess.java @ 4571
History | View | Annotate | Download (10.4 KB)
1 |
/*
|
---|---|
2 |
* Created on 23-feb-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: DissolveGeoprocess.java 4571 2006-03-23 21:07:32Z azabala $
|
47 |
* $Log$
|
48 |
* Revision 1.8 2006-03-23 21:03:45 azabala
|
49 |
* *** empty log message ***
|
50 |
*
|
51 |
* Revision 1.7 2006/03/17 19:53:14 azabala
|
52 |
* *** empty log message ***
|
53 |
*
|
54 |
* Revision 1.6 2006/03/15 18:33:24 azabala
|
55 |
* *** empty log message ***
|
56 |
*
|
57 |
* Revision 1.5 2006/03/14 18:32:46 fjp
|
58 |
* Cambio con LayerDefinition para que sea compatible con la definici?n de tablas tambi?n.
|
59 |
*
|
60 |
* Revision 1.4 2006/03/07 21:01:33 azabala
|
61 |
* *** empty log message ***
|
62 |
*
|
63 |
* Revision 1.3 2006/03/06 19:48:39 azabala
|
64 |
* *** empty log message ***
|
65 |
*
|
66 |
* Revision 1.2 2006/03/05 19:58:20 azabala
|
67 |
* *** empty log message ***
|
68 |
*
|
69 |
* Revision 1.1 2006/02/26 20:53:44 azabala
|
70 |
* *** empty log message ***
|
71 |
*
|
72 |
*
|
73 |
*/
|
74 |
package com.iver.gvsig.geoprocessing.impl.dissolve; |
75 |
|
76 |
import java.util.ArrayList; |
77 |
import java.util.Iterator; |
78 |
import java.util.Map; |
79 |
|
80 |
import com.iver.andami.PluginServices; |
81 |
import com.iver.cit.gvsig.fmap.DriverException; |
82 |
import com.iver.cit.gvsig.fmap.drivers.DriverIOException; |
83 |
import com.iver.cit.gvsig.fmap.drivers.FieldDescription; |
84 |
import com.iver.cit.gvsig.fmap.drivers.ILayerDefinition; |
85 |
import com.iver.cit.gvsig.fmap.drivers.LayerDefinition; |
86 |
import com.iver.cit.gvsig.fmap.drivers.SHPLayerDefinition; |
87 |
import com.iver.cit.gvsig.fmap.edition.EditionException; |
88 |
import com.iver.cit.gvsig.fmap.layers.FLyrVect; |
89 |
import com.iver.cit.gvsig.fmap.operations.CancellableMonitorable; |
90 |
import com.iver.cit.gvsig.fmap.operations.DefaultCancellableMonitorable; |
91 |
import com.iver.cit.gvsig.fmap.operations.strategies.Strategy; |
92 |
import com.iver.cit.gvsig.fmap.operations.strategies.StrategyManager; |
93 |
import com.iver.cit.gvsig.fmap.operations.strategies.VisitException; |
94 |
import com.iver.gvsig.geoprocessing.impl.AbstractGeoprocess; |
95 |
import com.iver.gvsig.geoprocessing.impl.jtsprocessors.FeaturePersisterProcessor2; |
96 |
import com.iver.gvsig.geoprocessing.model.GeoprocessException; |
97 |
import com.iver.gvsig.geoprocessing.model.IOneLayerGeoprocess; |
98 |
import com.iver.gvsig.geoprocessing.model.SummarizationFunction; |
99 |
import com.iver.gvsig.geoprocessing.schemabuilder.XTypes; |
100 |
import com.iver.utiles.swing.threads.IMonitorableTask; |
101 |
/**
|
102 |
* Processes each geometry of a polygonal vectorial layer, looking for
|
103 |
* its adjacent polygons. If this adjacent polygons has the same specified
|
104 |
* field value as processed geometry, this polygons will be dissolved.
|
105 |
*
|
106 |
*
|
107 |
* @author azabala
|
108 |
*
|
109 |
*/
|
110 |
public class DissolveGeoprocess extends AbstractGeoprocess |
111 |
implements IOneLayerGeoprocess {
|
112 |
|
113 |
private String dissolveField; |
114 |
private boolean dissolveOnlySelection; |
115 |
private LayerDefinition resultLayerDefinition;
|
116 |
private Map fields_functions; |
117 |
private DissolveVisitor visitor;
|
118 |
|
119 |
|
120 |
/**
|
121 |
* Constructor.
|
122 |
*
|
123 |
* @param inputLayer Layer whose geometries we are
|
124 |
* going to dissolve
|
125 |
*/
|
126 |
public DissolveGeoprocess(FLyrVect inputLayer, String dissolveField) { |
127 |
setFirstOperand(inputLayer); |
128 |
this.dissolveField = dissolveField;
|
129 |
} |
130 |
|
131 |
public void setFieldsFunctions(Map fieldsFunctions){ |
132 |
this.fields_functions = fieldsFunctions;
|
133 |
} |
134 |
|
135 |
public void setFirstOperand(FLyrVect inputLayer) { |
136 |
this.firstLayer = inputLayer;
|
137 |
|
138 |
} |
139 |
|
140 |
public void setParameters(Map params) throws GeoprocessException { |
141 |
Boolean onlySelection =
|
142 |
(Boolean) params.get("layer_selection"); |
143 |
if(onlySelection != null) |
144 |
dissolveOnlySelection = onlySelection.booleanValue(); |
145 |
|
146 |
} |
147 |
|
148 |
public void checkPreconditions() throws GeoprocessException { |
149 |
if (firstLayer == null) |
150 |
throw new GeoprocessException("Buffer con capa de entrada a null"); |
151 |
if(this.writer == null || |
152 |
this.schemaManager == null){ |
153 |
throw new GeoprocessException("Operacion de dissolve sin especificar capa de resultados"); |
154 |
} |
155 |
try {
|
156 |
if(firstLayer.getShapeType() != XTypes.POLYGON)
|
157 |
throw new GeoprocessException("La capa a disolver debe ser de pol?gonos"); |
158 |
} catch (DriverException e) {
|
159 |
throw new GeoprocessException("Error intentando verificar el tipo de geometria de la capa a disolver"); |
160 |
} |
161 |
if(this.dissolveField == null) |
162 |
throw new GeoprocessException("No se ha proporcionado el campo para dissolver"); |
163 |
|
164 |
} |
165 |
|
166 |
public void process() throws GeoprocessException { |
167 |
try {
|
168 |
FeaturePersisterProcessor2 processor = |
169 |
new FeaturePersisterProcessor2(writer);
|
170 |
visitor = new DissolveVisitor(this.dissolveField, processor); |
171 |
visitor.setDissolvedAttributesInfo(fields_functions); |
172 |
Strategy strategy = |
173 |
StrategyManager.getStrategy(firstLayer); |
174 |
visitor.setStrategy(strategy); |
175 |
if(this.dissolveOnlySelection){ |
176 |
strategy.process(visitor, firstLayer. |
177 |
getRecordset(). |
178 |
getSelection()); |
179 |
}else{
|
180 |
strategy.process(visitor); |
181 |
} |
182 |
} catch (DriverException e) {
|
183 |
throw new GeoprocessException("Error accediendo a los datos durante un dissolve"); |
184 |
} catch (VisitException e) {
|
185 |
throw new GeoprocessException("Error al procesar los elementos de la capa de entrada durante un dissolve"); |
186 |
} |
187 |
} |
188 |
|
189 |
|
190 |
|
191 |
public void cancel() { |
192 |
try {
|
193 |
schemaManager.drop(); |
194 |
} catch (EditionException e) {
|
195 |
// TODO Auto-generated catch block
|
196 |
e.printStackTrace(); |
197 |
} |
198 |
} |
199 |
|
200 |
public ILayerDefinition createLayerDefinition() {
|
201 |
if(resultLayerDefinition == null){ |
202 |
ArrayList fields = new ArrayList(); |
203 |
resultLayerDefinition = new SHPLayerDefinition();
|
204 |
resultLayerDefinition.setShapeType(XTypes.POLYGON); |
205 |
|
206 |
Iterator fieldsIt = fields_functions.keySet().iterator();
|
207 |
while(fieldsIt.hasNext()){
|
208 |
String field = (String) fieldsIt.next(); |
209 |
SummarizationFunction[] functions =
|
210 |
(SummarizationFunction[]) fields_functions.get(field);
|
211 |
for(int i = 0; i < functions.length; i++){ |
212 |
FieldDescription description = |
213 |
new FieldDescription();
|
214 |
description.setFieldLength(10);
|
215 |
description.setFieldDecimalCount(4);
|
216 |
//to avoid truncation of field names (f.example shp)
|
217 |
//we only catch three first letters
|
218 |
String shortName = null; |
219 |
if(field.length() > 3) |
220 |
shortName = field.substring(0,3); |
221 |
else
|
222 |
shortName = field; |
223 |
description.setFieldName( |
224 |
shortName + "_" + functions[i].toString());
|
225 |
description.setFieldType(XTypes.DOUBLE); |
226 |
|
227 |
fields.add(description); |
228 |
}//for
|
229 |
}//while
|
230 |
FieldDescription[] fieldsDesc = null; |
231 |
if(fields.size() == 0){ |
232 |
fieldsDesc = new FieldDescription[0]; |
233 |
}else{
|
234 |
fieldsDesc = new FieldDescription[fields.size()];
|
235 |
fields.toArray(fieldsDesc); |
236 |
} |
237 |
resultLayerDefinition.setFieldsDesc(fieldsDesc); |
238 |
} |
239 |
return resultLayerDefinition;
|
240 |
} |
241 |
|
242 |
public IMonitorableTask createTask() {
|
243 |
try {
|
244 |
return new DissolveMonitorableTask(); |
245 |
} catch (DriverIOException e) {
|
246 |
return null; |
247 |
} |
248 |
} |
249 |
|
250 |
/**
|
251 |
* IMonitorableTask that allows to run diff geoprocess in background,
|
252 |
* with cancelation requests.
|
253 |
*
|
254 |
* @author azabala
|
255 |
*
|
256 |
*/
|
257 |
class DissolveMonitorableTask implements IMonitorableTask { |
258 |
private CancellableMonitorable cancelMonitor = null; |
259 |
|
260 |
String DISSOLVE_MESSAGE = PluginServices.getText(this, "Mensaje_dissolve"); |
261 |
String DISSOLVE_NOTE = PluginServices.getText(this, "Mensaje_procesando_dissolves"); |
262 |
String OF = PluginServices.getText(this, "De"); |
263 |
private boolean finished = false; |
264 |
|
265 |
DissolveMonitorableTask() throws DriverIOException {
|
266 |
initialize(); |
267 |
} |
268 |
void initialize() throws DriverIOException{ |
269 |
cancelMonitor = createCancelMonitor(); |
270 |
} |
271 |
|
272 |
private CancellableMonitorable createCancelMonitor() throws DriverIOException{ |
273 |
DefaultCancellableMonitorable monitor = new
|
274 |
DefaultCancellableMonitorable(); |
275 |
monitor.setInitialStep(0);
|
276 |
//monitor.setDeterminatedProcess(false);
|
277 |
monitor.setFinalStep(firstLayer.getSource().getShapeCount()); |
278 |
monitor.setDeterminatedProcess(true);
|
279 |
return monitor;
|
280 |
} |
281 |
|
282 |
public int getInitialStep() { |
283 |
return cancelMonitor.getInitialStep();
|
284 |
} |
285 |
|
286 |
public int getFinishStep() { |
287 |
return cancelMonitor.getFinalStep();
|
288 |
} |
289 |
|
290 |
public int getCurrentStep() { |
291 |
//return cancelMonitor.getCurrentStep();
|
292 |
if(visitor == null) |
293 |
return getInitialStep();
|
294 |
else
|
295 |
return visitor.getNumProcessedGeometries();
|
296 |
} |
297 |
|
298 |
public String getStatusMessage() { |
299 |
return DISSOLVE_MESSAGE;
|
300 |
} |
301 |
|
302 |
public String getNote() { |
303 |
return DISSOLVE_NOTE + " "+ |
304 |
getCurrentStep() + " " +
|
305 |
OF + " " +
|
306 |
getFinishStep(); |
307 |
} |
308 |
|
309 |
public void cancel() { |
310 |
((DefaultCancellableMonitorable) cancelMonitor).setCanceled(true);
|
311 |
} |
312 |
|
313 |
public void run() throws GeoprocessException { |
314 |
|
315 |
try {
|
316 |
|
317 |
FeaturePersisterProcessor2 processor = |
318 |
new FeaturePersisterProcessor2(writer);
|
319 |
visitor = new DissolveVisitor(dissolveField, processor);
|
320 |
visitor.setDissolvedAttributesInfo(fields_functions); |
321 |
Strategy strategy = |
322 |
StrategyManager.getStrategy(firstLayer); |
323 |
visitor.setStrategy(strategy); |
324 |
if(dissolveOnlySelection){
|
325 |
strategy.process(visitor, firstLayer. |
326 |
getRecordset(). |
327 |
getSelection(), |
328 |
cancelMonitor); |
329 |
}else{
|
330 |
strategy.process(visitor, cancelMonitor); |
331 |
} |
332 |
} catch (DriverException e) {
|
333 |
throw new GeoprocessException("Error accediendo a los datos durante un dissolve"); |
334 |
} catch (VisitException e) {
|
335 |
throw new GeoprocessException("Error al procesar los elementos de la capa de entrada durante un dissolve"); |
336 |
} finally{
|
337 |
finished = true;
|
338 |
} |
339 |
} |
340 |
|
341 |
public boolean isDefined() { |
342 |
return cancelMonitor.isDeterminatedProcess();
|
343 |
} |
344 |
|
345 |
public boolean isCanceled() { |
346 |
return cancelMonitor.isCanceled();
|
347 |
} |
348 |
|
349 |
public boolean isFinished() { |
350 |
return finished;
|
351 |
} |
352 |
} |
353 |
|
354 |
} |
355 |
|