Revision 4447 trunk/extensions/extGeoProcessing/src/com/iver/gvsig/geoprocessing/impl/convexhull/ConvexHullGeoprocess.java
ConvexHullGeoprocess.java | ||
---|---|---|
45 | 45 |
* |
46 | 46 |
* $Id$ |
47 | 47 |
* $Log$ |
48 |
* Revision 1.6 2006-03-14 18:32:46 fjp |
|
48 |
* Revision 1.7 2006-03-15 18:31:06 azabala |
|
49 |
* *** empty log message *** |
|
50 |
* |
|
51 |
* Revision 1.6 2006/03/14 18:32:46 fjp |
|
49 | 52 |
* Cambio con LayerDefinition para que sea compatible con la definici?n de tablas tambi?n. |
50 | 53 |
* |
51 | 54 |
* Revision 1.5 2006/03/07 21:01:33 azabala |
... | ... | |
62 | 65 |
* |
63 | 66 |
* Revision 1.1 2006/02/17 16:32:20 azabala |
64 | 67 |
* *** empty log message *** |
65 |
* |
|
66 |
* |
|
67 |
*/ |
|
68 |
* |
|
69 |
* |
|
70 |
*/ |
|
71 |
|
|
68 | 72 |
package com.iver.gvsig.geoprocessing.impl.convexhull; |
69 | 73 |
|
70 | 74 |
import java.util.Map; |
71 | 75 |
|
72 |
|
|
73 | 76 |
import com.iver.cit.gvsig.fmap.DriverException; |
74 | 77 |
import com.iver.cit.gvsig.fmap.core.IFeature; |
75 | 78 |
import com.iver.cit.gvsig.fmap.core.IGeometry; |
79 |
import com.iver.cit.gvsig.fmap.drivers.DriverIOException; |
|
76 | 80 |
import com.iver.cit.gvsig.fmap.drivers.FieldDescription; |
77 | 81 |
import com.iver.cit.gvsig.fmap.drivers.ILayerDefinition; |
78 | 82 |
import com.iver.cit.gvsig.fmap.drivers.LayerDefinition; |
79 | 83 |
import com.iver.cit.gvsig.fmap.drivers.SHPLayerDefinition; |
80 | 84 |
import com.iver.cit.gvsig.fmap.edition.DefaultRowEdited; |
81 | 85 |
import com.iver.cit.gvsig.fmap.edition.EditionException; |
86 |
import com.iver.cit.gvsig.fmap.layers.FBitSet; |
|
82 | 87 |
import com.iver.cit.gvsig.fmap.layers.FLyrVect; |
88 |
import com.iver.cit.gvsig.fmap.operations.CancellableMonitorable; |
|
89 |
import com.iver.cit.gvsig.fmap.operations.DefaultCancellableMonitorable; |
|
83 | 90 |
import com.iver.cit.gvsig.fmap.operations.strategies.Strategy; |
84 | 91 |
import com.iver.cit.gvsig.fmap.operations.strategies.StrategyManager; |
85 | 92 |
import com.iver.cit.gvsig.fmap.operations.strategies.VisitException; |
... | ... | |
88 | 95 |
import com.iver.gvsig.geoprocessing.model.IOneLayerGeoprocess; |
89 | 96 |
import com.iver.gvsig.geoprocessing.schemabuilder.FeatureFactory; |
90 | 97 |
import com.iver.gvsig.geoprocessing.schemabuilder.XTypes; |
98 |
import com.iver.utiles.swing.threads.IMonitorableTask; |
|
99 |
|
|
91 | 100 |
/** |
92 |
* Geoprocess that computes the convex hull of all geometries |
|
93 |
* of the input layer.
|
|
101 |
* Geoprocess that computes the convex hull of all geometries of the input
|
|
102 |
* layer. |
|
94 | 103 |
* |
95 | 104 |
* @author azabala |
96 |
* |
|
105 |
*
|
|
97 | 106 |
*/ |
98 |
public class ConvexHullGeoprocess extends AbstractGeoprocess |
|
99 |
implements IOneLayerGeoprocess {
|
|
100 |
|
|
107 |
public class ConvexHullGeoprocess extends AbstractGeoprocess implements
|
|
108 |
IOneLayerGeoprocess { |
|
109 |
|
|
101 | 110 |
/** |
102 | 111 |
* Schema of the result layer |
103 | 112 |
*/ |
104 | 113 |
private LayerDefinition resultLayerDefinition; |
114 |
|
|
105 | 115 |
/** |
106 |
* Iterates over geometries computing convex |
|
107 |
* hull.
|
|
108 |
* TODO Comparative Memory vs Scalable
|
|
116 |
* Iterates over geometries computing convex hull.
|
|
117 |
* TODO Comparative Memory
|
|
118 |
* vs Scalable |
|
109 | 119 |
*/ |
110 | 120 |
private ScalableConvexHullVisitor visitor; |
121 |
|
|
111 | 122 |
/** |
112 |
* If only consideer selection to compute |
|
113 |
* convex hull |
|
123 |
* If only consideer selection to compute convex hull |
|
114 | 124 |
*/ |
115 | 125 |
private boolean convexSelection; |
116 |
|
|
117 |
|
|
118 |
public ConvexHullGeoprocess(){ |
|
126 |
|
|
127 |
public ConvexHullGeoprocess() { |
|
119 | 128 |
visitor = new ScalableConvexHullVisitor(); |
120 | 129 |
} |
121 |
|
|
122 |
|
|
130 |
|
|
123 | 131 |
public void setParameters(Map params) throws GeoprocessException { |
124 |
Boolean onlySelection = |
|
125 |
(Boolean) params.get("layer_selection"); |
|
126 |
if(onlySelection != null) |
|
132 |
Boolean onlySelection = (Boolean) params.get("layer_selection"); |
|
133 |
if (onlySelection != null) |
|
127 | 134 |
convexSelection = onlySelection.booleanValue(); |
128 |
|
|
135 |
|
|
129 | 136 |
} |
130 | 137 |
|
131 | 138 |
public void checkPreconditions() throws GeoprocessException { |
132 | 139 |
if (firstLayer == null) |
133 |
throw new GeoprocessException("Convex Hull con capa de entrada a null"); |
|
134 |
if(this.writer == null || |
|
135 |
this.schemaManager == null){ |
|
136 |
throw new GeoprocessException("Operacion de convex hull sin especificar capa de resultados"); |
|
140 |
throw new GeoprocessException( |
|
141 |
"Convex Hull con capa de entrada a null"); |
|
142 |
if (this.writer == null || this.schemaManager == null) { |
|
143 |
throw new GeoprocessException( |
|
144 |
"Operacion de convex hull sin especificar capa de resultados"); |
|
137 | 145 |
} |
138 | 146 |
|
139 | 147 |
} |
140 | 148 |
|
141 | 149 |
public void process() throws GeoprocessException { |
142 |
Strategy strategy = StrategyManager. |
|
143 |
getStrategy(firstLayer); |
|
150 |
Strategy strategy = StrategyManager.getStrategy(firstLayer); |
|
144 | 151 |
try { |
145 |
if(convexSelection){
|
|
146 |
strategy.process(visitor, |
|
147 |
firstLayer.getRecordset().getSelection());
|
|
148 |
}else{
|
|
152 |
if (convexSelection) {
|
|
153 |
strategy.process(visitor, firstLayer.getRecordset()
|
|
154 |
.getSelection()); |
|
155 |
} else {
|
|
149 | 156 |
strategy.process(visitor); |
150 | 157 |
} |
151 |
|
|
152 |
IGeometry convexHull = |
|
153 |
visitor.getConvexHull(); |
|
158 |
|
|
159 |
IGeometry convexHull = visitor.getConvexHull(); |
|
154 | 160 |
Object[] attrs = new Object[1]; |
155 | 161 |
attrs[0] = new Long(0); |
156 |
IFeature feature = FeatureFactory.createFeature(attrs, |
|
157 |
convexHull, |
|
162 |
IFeature feature = FeatureFactory.createFeature(attrs, convexHull, |
|
158 | 163 |
resultLayerDefinition); |
159 | 164 |
writer.preProcess(); |
160 |
DefaultRowEdited editedFeature = new |
|
161 |
DefaultRowEdited(feature, |
|
162 |
DefaultRowEdited.STATUS_ADDED, 0); |
|
165 |
DefaultRowEdited editedFeature = new DefaultRowEdited(feature, |
|
166 |
DefaultRowEdited.STATUS_ADDED, 0); |
|
163 | 167 |
writer.process(editedFeature); |
164 | 168 |
writer.postProcess(); |
165 |
|
|
169 |
|
|
166 | 170 |
} catch (DriverException e) { |
167 |
throw new GeoprocessException("Problemas con el driver calculando el convex hull"); |
|
171 |
throw new GeoprocessException( |
|
172 |
"Problemas con el driver calculando el convex hull"); |
|
168 | 173 |
} catch (VisitException e) { |
169 |
throw new GeoprocessException("Problemas durante el proceso de calculo del convex hull"); |
|
170 |
} catch (EditionException e){ |
|
171 |
throw new GeoprocessException("Problemas al guardar la capa resultado del convex hull"); |
|
174 |
throw new GeoprocessException( |
|
175 |
"Problemas durante el proceso de calculo del convex hull"); |
|
176 |
} catch (EditionException e) { |
|
177 |
throw new GeoprocessException( |
|
178 |
"Problemas al guardar la capa resultado del convex hull"); |
|
172 | 179 |
} |
173 | 180 |
} |
174 | 181 |
|
175 | 182 |
public void cancel() { |
176 | 183 |
// TODO Implementar la cancelacion |
184 |
//llamar a ISchemaManager.drop() para borrar la capa |
|
185 |
//resultado |
|
177 | 186 |
|
178 | 187 |
} |
179 | 188 |
|
189 |
|
|
180 | 190 |
public ILayerDefinition createLayerDefinition() { |
181 | 191 |
if(resultLayerDefinition == null){ |
182 | 192 |
resultLayerDefinition = new SHPLayerDefinition(); |
... | ... | |
195 | 205 |
this.firstLayer = firstLayer; |
196 | 206 |
} |
197 | 207 |
|
208 |
public IMonitorableTask createTask() { |
|
209 |
try { |
|
210 |
return new ConvexHullMonitorableTask(); |
|
211 |
} catch (DriverIOException e) { |
|
212 |
return null; |
|
213 |
} |
|
214 |
} |
|
215 |
|
|
216 |
/** |
|
217 |
* IMonitorableTask that allows to run convex hull geoprocess in background, |
|
218 |
* with cancelation requests. |
|
219 |
* |
|
220 |
* @author azabala |
|
221 |
* |
|
222 |
*/ |
|
223 |
class ConvexHullMonitorableTask implements IMonitorableTask { |
|
224 |
private CancellableMonitorable cancelMonitor = null; |
|
225 |
|
|
226 |
// FIXME INTERNACIONALIZAR ESTO |
|
227 |
String HULL_MESSAGE = "Computing convex hull:"; |
|
228 |
|
|
229 |
private boolean finished = false; |
|
230 |
|
|
231 |
ConvexHullMonitorableTask() throws DriverIOException{ |
|
232 |
initialize(); |
|
233 |
} |
|
234 |
void initialize() throws DriverIOException { |
|
235 |
cancelMonitor = createCancelMonitor(); |
|
236 |
} |
|
237 |
|
|
238 |
private CancellableMonitorable createCancelMonitor() |
|
239 |
throws DriverIOException { |
|
240 |
DefaultCancellableMonitorable monitor = new DefaultCancellableMonitorable(); |
|
241 |
monitor.setInitialStep(0); |
|
242 |
monitor.setDeterminatedProcess(true); |
|
243 |
int numSteps = 0; |
|
244 |
if (convexSelection) { |
|
245 |
FBitSet selection = firstLayer.getRecordset().getSelection(); |
|
246 |
numSteps = selection.cardinality(); |
|
247 |
} else { |
|
248 |
numSteps = firstLayer.getSource().getShapeCount(); |
|
249 |
} |
|
250 |
monitor.setFinalStep(numSteps); |
|
251 |
return monitor; |
|
252 |
} |
|
253 |
|
|
254 |
public int getInitialStep() { |
|
255 |
return cancelMonitor.getInitialStep(); |
|
256 |
} |
|
257 |
|
|
258 |
public int getFinishStep() { |
|
259 |
return cancelMonitor.getFinalStep(); |
|
260 |
} |
|
261 |
|
|
262 |
public int getCurrentStep() { |
|
263 |
return cancelMonitor.getCurrentStep(); |
|
264 |
} |
|
265 |
|
|
266 |
public String getStatusMessage() { |
|
267 |
// FIXME Cambiar esto por un mecanismo de eventos, |
|
268 |
// de forma que la tarea lo que tenga sea un escuchador |
|
269 |
// que cambie el mensaje de estado segun los eventos |
|
270 |
// de tareas que se est?n realizando |
|
271 |
return "Convex Hull Geoprocess..."; |
|
272 |
} |
|
273 |
|
|
274 |
public String getNote() { |
|
275 |
// FIXME Cambiar esto por un mecanismo de eventos, |
|
276 |
// de forma que la tarea lo que tenga sea un escuchador |
|
277 |
// que cambie el mensaje de estado segun los eventos |
|
278 |
// de tareas que se est?n realizando |
|
279 |
return HULL_MESSAGE + |
|
280 |
getCurrentStep()+ |
|
281 |
" de " + |
|
282 |
getFinishStep(); |
|
283 |
} |
|
284 |
|
|
285 |
public void cancel() { |
|
286 |
((DefaultCancellableMonitorable) cancelMonitor).setCanceled(true); |
|
287 |
} |
|
288 |
|
|
289 |
public void run() throws GeoprocessException { |
|
290 |
|
|
291 |
Strategy strategy = StrategyManager.getStrategy(firstLayer); |
|
292 |
try { |
|
293 |
if (convexSelection) { |
|
294 |
strategy.process(visitor, firstLayer.getRecordset() |
|
295 |
.getSelection(), cancelMonitor); |
|
296 |
} else { |
|
297 |
strategy.process(visitor, cancelMonitor); |
|
298 |
} |
|
299 |
|
|
300 |
IGeometry convexHull = visitor.getConvexHull(); |
|
301 |
Object[] attrs = new Object[1]; |
|
302 |
attrs[0] = new Long(0); |
|
303 |
IFeature feature = FeatureFactory.createFeature(attrs, |
|
304 |
convexHull, resultLayerDefinition); |
|
305 |
writer.preProcess(); |
|
306 |
DefaultRowEdited editedFeature = new DefaultRowEdited(feature, |
|
307 |
DefaultRowEdited.STATUS_ADDED, 0); |
|
308 |
writer.process(editedFeature); |
|
309 |
writer.postProcess(); |
|
310 |
|
|
311 |
} catch (DriverException e) { |
|
312 |
throw new GeoprocessException( |
|
313 |
"Problemas con el driver calculando el convex hull"); |
|
314 |
} catch (VisitException e) { |
|
315 |
throw new GeoprocessException( |
|
316 |
"Problemas durante el proceso de calculo del convex hull"); |
|
317 |
} catch (EditionException e) { |
|
318 |
throw new GeoprocessException( |
|
319 |
"Problemas al guardar la capa resultado del convex hull"); |
|
320 |
} |
|
321 |
finished = true; |
|
322 |
} |
|
323 |
|
|
324 |
public boolean isDefined() { |
|
325 |
return cancelMonitor.isDeterminatedProcess(); |
|
326 |
} |
|
327 |
|
|
328 |
public boolean isCanceled() { |
|
329 |
return cancelMonitor.isCanceled(); |
|
330 |
} |
|
331 |
|
|
332 |
public boolean isFinished() { |
|
333 |
return finished; |
|
334 |
} |
|
335 |
|
|
336 |
} |
|
337 |
|
|
198 | 338 |
} |
199 |
|
Also available in: Unified diff