Revision 1260 org.gvsig.geoprocess/trunk/org.gvsig.geoprocess/org.gvsig.geoprocess.lib/org.gvsig.geoprocess.lib.sextante/src/main/java/org/gvsig/geoprocess/lib/sextante/AbstractSextanteGeoProcess.java

View differences:

AbstractSextanteGeoProcess.java
75 75
 * @version $Id$
76 76
 */
77 77
public abstract class AbstractSextanteGeoProcess extends GeoAlgorithm implements GeoProcess {
78
    protected String[]                attrNames   = null;
79
    private SimpleTaskStatusDelegated status      = null;
80
    protected GeometryManager         geomManager = GeometryLocator.getGeometryManager();
81 78

  
79
    protected String[] attrNames = null;
80
    private SimpleTaskStatusDelegated status = null;
81
    protected GeometryManager geomManager = GeometryLocator.getGeometryManager();
82
    protected NamesTranslator namesTranslator = null;
83

  
82 84
    @Override
83 85
    public boolean execute(ITaskMonitor task, OutputFactory outputFactory,
84
        HashMap<String, String> outputMap)
85
        throws GeoAlgorithmExecutionException {
86
            HashMap<String, String> outputMap)
87
            throws GeoAlgorithmExecutionException {
86 88
        status = new SimpleTaskStatusDelegated(m_Task, getGeoProcessName());
87 89
        status.add();
88 90
        boolean result = false;
......
95 97
    }
96 98

  
97 99
    /**
98
     * This method creates a new raster layer and adds it to the set of output objects of the algorithm. Use this when your
99
     * algorithm generates a new raster layer and you have to create it. The grid extent is taken from the algorithm analysis
100
     * extent (@see {@link #getAnalysisExtent()}
100
     * This method creates a new raster layer and adds it to the set of output
101
     * objects of the algorithm. Use this when your algorithm generates a new
102
     * raster layer and you have to create it. The grid extent is taken from the
103
     * algorithm analysis extent (@see {@link #getAnalysisExtent()}
101 104
     *
102
     * @param sName
103
     *                The name of the layer. Has to be the same that you used to define this output in the
105
     * @param sName The name of the layer. Has to be the same that you used to
106
     * define this output in the
104 107
     * @see {@link #defineCharacteristics()} method.
105
     * @param sDescription
106
     *                the long description of the output. This is the one usually used to describe the layer when added to a GIS
107
     *                GUI
108
     * @param iDataType
109
     *                The data type. See
108
     * @param sDescription the long description of the output. This is the one
109
     * usually used to describe the layer when added to a GIS GUI
110
     * @param iDataType The data type. See
110 111
     * @see {@link IRasterLayer} for more info about valid values
111
     * @param iBands
112
     *                the number of bands of the new layer
112
     * @param iBands the number of bands of the new layer
113 113
     * @return a new raster layer
114 114
     * @throws UnsupportedOutputChannelException
115 115
     */
116 116
    protected IRasterLayer getNewRORasterLayer(final String sName,
117
                                             final String sDescription,
118
                                             final int iDataType,
119
                                             final int iBands) throws UnsupportedOutputChannelException {
117
            final String sDescription,
118
            final int iDataType,
119
            final int iBands) throws UnsupportedOutputChannelException {
120 120

  
121
       final IOutputChannel channel = getOutputChannel(sName);
121
        final IOutputChannel channel = getOutputChannel(sName);
122 122

  
123
       final IRasterLayer newLayer = ((DefaultOutputFactory)m_OutputFactory).getNewEmptyRORasterLayer(sName, iDataType,
123
        final IRasterLayer newLayer = ((DefaultOutputFactory) m_OutputFactory).getNewEmptyRORasterLayer(sName, iDataType,
124 124
                m_AnalysisExtent, iBands, channel, m_CRS);
125 125

  
126
       addOutputRasterLayer(sName, sDescription, iBands, channel, newLayer);
126
        addOutputRasterLayer(sName, sDescription, iBands, channel, newLayer);
127 127

  
128
       return newLayer;
128
        return newLayer;
129 129

  
130 130
    }
131 131

  
......
136 136
     * @return FeatureStore
137 137
     */
138 138
    protected FeatureStore buildOutPutStore(FeatureType featureType,
139
        int shapeType, String sextanteLayerName, String sextanteLayerLabel) {
139
            int shapeType, String sextanteLayerName, String sextanteLayerLabel) {
140 140

  
141 141
//        Class<?>[] types = null;
142 142
//        if (featureType.getDefaultGeometryAttribute() != null) {
......
156 156
        Iterator<FeatureAttributeDescriptor> it = featureType.iterator();
157 157
        while (it.hasNext()) {
158 158
            FeatureAttributeDescriptor attribute = it.next();
159
            if(attribute.isComputed()){
159
            if (attribute.isComputed()) {
160 160
                emulateds.add(attribute);
161 161
            } else {
162 162
                String attrName = attribute.getName();
163
                if(attribute.getDataType().getType() != DataTypes.GEOMETRY) {
163
                if (attribute.getDataType().getType() != DataTypes.GEOMETRY) {
164 164
                    theAttrNames.add(attrName);
165 165
                    types.add(attribute.getObjectClass());
166 166
                }
......
170 170
        try {
171 171
            FlyrVectIVectorLayer output
172 172
                    = (FlyrVectIVectorLayer) getNewVectorLayer(
173
                            sextanteLayerLabel, 
173
                            sextanteLayerLabel,
174 174
                            sextanteLayerName,
175
                            shapeType, 
176
                            types.toArray(new Class[types.size()]), 
175
                            shapeType,
176
                            types.toArray(new Class[types.size()]),
177 177
                            theAttrNames.toArray(new String[theAttrNames.size()])
178 178
                    );
179 179
            if (output != null) {
......
194 194
                        if (needStartEditingMode) {
195 195
                            store.finishEditing();
196 196
                        }
197
                        
197

  
198 198
                    } catch (Exception e) {
199 199
                        Sextante.addErrorToLog(e);
200 200
                    }
......
217 217
     * @return FeatureStore
218 218
     */
219 219
    protected FeatureStore buildOutPutStoreFromUnion(FeatureType featureType1,
220
        FeatureType featureType2, int shapeType, String sextanteLayerName,
221
        String sextanteLayerLabel, NamesTranslator nameTranslator) {
220
            FeatureType featureType2, int shapeType, String sextanteLayerName,
221
            String sextanteLayerLabel) {
222 222
        return buildOutPutStoreFromUnion(featureType1, featureType2, shapeType,
223
            sextanteLayerName, sextanteLayerLabel, null, null, nameTranslator);
223
                sextanteLayerName, sextanteLayerLabel, null, null);
224 224
    }
225 225

  
226 226
    /**
......
230 230
     * @return FeatureStore
231 231
     */
232 232
    protected FeatureStore buildOutPutStoreFromUnion(FeatureType featureType1,
233
        FeatureType featureType2, int shapeType, String sextanteLayerName,
234
        String sextanteLayerLabel, String newField, Class<?> newFieldType, NamesTranslator nameTranslator) {
235
        
236
        nameTranslator.clear();
233
            FeatureType featureType2, int shapeType, String sextanteLayerName,
234
            String sextanteLayerLabel, String newField, Class<?> newFieldType) {
237 235

  
236
        NamesTranslator myNamesTranslator = getNamesTranslator();
237

  
238 238
        int sizeAux = 0;
239
        if (newField != null && newFieldType != null)
239
        if (newField != null && newFieldType != null) {
240 240
            sizeAux = 1;
241
        }
241 242

  
242 243
        Class<?>[] types = null;
243 244
        // Tiene campo GEOMETRY. Hay que quitarlo
244 245
        if (featureType1.getDefaultGeometryAttribute() != null) {
245
            attrNames =
246
                new String[featureType1.size() + featureType2.size() - 2
246
            attrNames
247
                    = new String[featureType1.size() + featureType2.size() - 2
247 248
                    + sizeAux];
248
            types =
249
                new Class[featureType1.size() + featureType2.size() - 2
249
            types
250
                    = new Class[featureType1.size() + featureType2.size() - 2
250 251
                    + sizeAux];
251 252
        } else {
252
            attrNames =
253
                new String[featureType1.size() + featureType2.size() + sizeAux];
254
            types =
255
                new Class[featureType1.size() + featureType2.size() + sizeAux];
253
            attrNames
254
                    = new String[featureType1.size() + featureType2.size() + sizeAux];
255
            types
256
                    = new Class[featureType1.size() + featureType2.size() + sizeAux];
256 257
        }
257 258

  
258 259
        int i = 0;
259 260
        @SuppressWarnings("unchecked")
260 261
        Iterator<FeatureAttributeDescriptor> it = featureType1.iterator();
262
        myNamesTranslator.clear();
261 263
        while (it.hasNext()) {
262
            FeatureAttributeDescriptor attribute =
263
                (FeatureAttributeDescriptor) it.next();
264
            if(attribute.getDataType().getType() != DataTypes.GEOMETRY) {
264
            FeatureAttributeDescriptor attribute
265
                    = (FeatureAttributeDescriptor) it.next();
266
            if (attribute.getDataType().getType() != DataTypes.GEOMETRY) {
265 267
                attrNames[i] = attribute.getName();
266
                nameTranslator.addSource(attribute.getName());
268
                myNamesTranslator.addSource(attribute.getName());
267 269
                types[i] = attribute.getObjectClass();
268 270
                i++;
269 271
            }
......
273 275
        Iterator<FeatureAttributeDescriptor> it2 = featureType2.iterator();
274 276
        while (it2.hasNext()) {
275 277
            FeatureAttributeDescriptor attribute = it2.next();
276
            if(attribute.getDataType().getType() != DataTypes.GEOMETRY) {
277
                String attrName =
278
                    checkAttrName(attribute.getName(), featureType1.size() - 1);
278
            if (attribute.getDataType().getType() != DataTypes.GEOMETRY) {
279
                String attrName
280
                        = checkAttrName(attribute.getName(), featureType1.size() - 1);
279 281
                attrNames[i] = attrName;
280
                nameTranslator.addSource(attribute.getName());
282
                myNamesTranslator.addSource(attribute.getName());
281 283
                types[i] = attribute.getObjectClass();
282 284
                i++;
283 285
            }
......
285 287

  
286 288
        if (newField != null && newFieldType != null) {
287 289
            attrNames[attrNames.length - 1] = newField;
288
            nameTranslator.addSource(newField);
290
            myNamesTranslator.addSource(newField);
289 291
            types[types.length - 1] = newFieldType;
290 292
        }
291 293

  
292 294
        try {
293
            IVectorLayer output =
294
                getNewVectorLayer(sextanteLayerLabel, sextanteLayerName,
295
                    shapeType, types, nameTranslator.getTranslatedNamesAsArray());
295
            IVectorLayer output
296
                    = getNewVectorLayer(sextanteLayerLabel, sextanteLayerName,
297
                            shapeType, types, myNamesTranslator.getTranslatedNamesAsArray());
296 298
            if (output != null) {
297 299
                return ((FlyrVectIVectorLayer) output).getFeatureStore();
298 300
            }
......
311 313
     * @return
312 314
     */
313 315
    protected String checkAttrName(String name, int size) {
314
    	return checkAttrName(name, size, attrNames);
316
        return checkAttrName(name, size, attrNames);
315 317
    }
316 318

  
317 319
    /**
318 320
     * Gets the shape type of the selected feature store
319 321
     *
320
     * @param FeatureStore
321
     *            source
322
     * @param FeatureStore source
322 323
     * @return shape type
323 324
     * @throws ReadException
324 325
     */
......
331 332
        }
332 333
        int indexGeom = featureType.getDefaultGeometryAttributeIndex();
333 334
        return featureType.getAttributeDescriptor(indexGeom).getGeomType()
334
            .getType();
335
                .getType();
335 336
    }
336 337

  
337 338
    /**
......
386 387

  
387 388
    @Override
388 389
    protected void setProgressText(String sText) {
389
    	super.setProgressText(sText);
390
        super.setProgressText(sText);
390 391
        getStatus().message(sText);
391 392
    }
392 393

  
393 394
    @Override
394 395
    public boolean setProgress(int iStep, int iTotalNumberOfSteps) {
395
    	boolean cancelled = super.setProgress(iStep, iTotalNumberOfSteps);
396
        boolean cancelled = super.setProgress(iStep, iTotalNumberOfSteps);
396 397
        SimpleTaskStatusDelegated status = getStatus();
397 398
        status.setRangeOfValues(0, iTotalNumberOfSteps);
398 399
        status.setCurValue(iStep);
399
        if(cancelled || status.isCancelled())
400
        	return true;
400
        if (cancelled || status.isCancelled()) {
401
            return true;
402
        }
401 403
        return false;
402 404
    }
403 405

  
......
425 427
    /**
426 428
     * Returns the translation for a label depending on the current locale.
427 429
     *
428
     * @param label
429
     *            to translate
430
     * @param label to translate
430 431
     * @return text in the current locale
431 432
     */
432 433
    protected String getTranslation(String label) {
......
439 440

  
440 441
    /**
441 442
     * Gets a feature query using the analysis bounding box
443
     *
442 444
     * @param e
443 445
     * @param store
444 446
     * @return
......
446 448
     * @throws DataException
447 449
     */
448 450
    public FeatureQuery getQueryFromAnalysisExtent(AnalysisExtent e, FeatureStore store) throws CreateEnvelopeException, DataException {
449
    	Envelope analysisEnvelope = geomManager.createEnvelope(e.getXMin(), e.getYMin(), e.getXMax(), e.getYMax(), SUBTYPES.GEOM2D);
450
    	FeatureQuery query = store.createFeatureQuery();
451
        Envelope analysisEnvelope = geomManager.createEnvelope(e.getXMin(), e.getYMin(), e.getXMax(), e.getYMax(), SUBTYPES.GEOM2D);
452
        FeatureQuery query = store.createFeatureQuery();
451 453
        Evaluator filter = SpatialEvaluatorsFactory.getInstance().intersects(
452 454
                analysisEnvelope,
453 455
                store.getDefaultFeatureType().getDefaultSRS(),
454 456
                store
455 457
        );
456
    	query.setFilter(filter);
457
    	return query;
458
        query.setFilter(filter);
459
        return query;
458 460
    }
459 461

  
460 462
    /**
461 463
     * Returns true if exists the output file and false i
464
     *
462 465
     * @param resultLabel
463 466
     * @param type zero shape and one tif
464 467
     * @return
465 468
     */
466 469
    public boolean existsOutPutFile(String resultLabel, int type) {
467
    	OutputObjectsSet ooSet = getOutputObjects();
468
    	Output out;
469
		try {
470
			out = ooSet.getOutput(resultLabel);
471
		} catch (WrongOutputIDException e) {
472
			return false;
473
		}
474
		String s = out.getOutputChannel()!= null ? out.getOutputChannel().getAsCommandLineParameter() : "";
475
    	if(new File(s).exists())
476
    		return true;
477
    	if(type == 0)
478
    		return (new File(s + ".shp").exists());
479
    	if(type == 1)
480
    		return (new File(s + ".tif").exists());
481
    	return false;
470
        OutputObjectsSet ooSet = getOutputObjects();
471
        Output out;
472
        try {
473
            out = ooSet.getOutput(resultLabel);
474
        } catch (WrongOutputIDException e) {
475
            return false;
476
        }
477
        String s = out.getOutputChannel() != null ? out.getOutputChannel().getAsCommandLineParameter() : "";
478
        if (new File(s).exists()) {
479
            return true;
480
        }
481
        if (type == 0) {
482
            return (new File(s + ".shp").exists());
483
        }
484
        if (type == 1) {
485
            return (new File(s + ".tif").exists());
486
        }
487
        return false;
482 488
    }
483 489

  
484 490
    public String getOutPutFile(String resultLabel) {
485
    	OutputObjectsSet ooSet = getOutputObjects();
486
    	Output out;
487
		try {
488
			out = ooSet.getOutput(resultLabel);
489
		} catch (WrongOutputIDException e) {
490
			return null;
491
		}
492
    	return out.getOutputChannel().getAsCommandLineParameter();
491
        OutputObjectsSet ooSet = getOutputObjects();
492
        Output out;
493
        try {
494
            out = ooSet.getOutput(resultLabel);
495
        } catch (WrongOutputIDException e) {
496
            return null;
497
        }
498
        return out.getOutputChannel().getAsCommandLineParameter();
493 499
    }
494 500

  
495 501
    public ITaskMonitor getTaskMonitor() {
496
    	return m_Task;
502
        return m_Task;
497 503
    }
498 504

  
499 505
    public void setTaskMonitor(ITaskMonitor task) {
500
    	this.m_Task = task;
506
        this.m_Task = task;
501 507
    }
508

  
509
    public NamesTranslator getNamesTranslator() {
510
        if (this.namesTranslator == null) {
511
            NamesTranslator dummy = NamesTranslator.createDummyTranslator();
512
            for (String attrName : attrNames) {
513
                dummy.addSource(attrName);
514
            }
515
            this.namesTranslator = dummy;
516
        }
517
        return this.namesTranslator;
518
    }
502 519
}

Also available in: Unified diff