Statistics
| Revision:

gvsig-geoprocess / org.gvsig.sextante / trunk / org.gvsig.sextante.app / org.gvsig.sextante.app.algorithm / org.gvsig.sextante.app.algorithm.merge / src / main / java / org / gvsig / sextante / app / algorithm / merge / MergeAlgorithm.java @ 41

History | View | Annotate | Download (4.28 KB)

1
/*
2
 * gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
3
 *
4
 * Copyright (C) 2010 Generalitat Valenciana.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
19
 *
20
 */
21
package org.gvsig.sextante.app.algorithm.merge;
22

    
23
import java.util.ArrayList;
24

    
25
import org.gvsig.fmap.dal.exception.DataException;
26
import org.gvsig.fmap.dal.feature.FeatureSet;
27
import org.gvsig.fmap.dal.feature.FeatureStore;
28
import org.gvsig.fmap.dal.feature.FeatureType;
29
import org.gvsig.sextante.app.algorithm.base.core.GeometryOperation;
30
import org.gvsig.sextante.app.extension.core.gvGeoAlgorithm;
31
import org.gvsig.sextante.app.extension.core.gvVectorLayer;
32

    
33
import es.unex.sextante.additionalInfo.AdditionalInfoMultipleInput;
34
import es.unex.sextante.core.Sextante;
35
import es.unex.sextante.dataObjects.IVectorLayer;
36
import es.unex.sextante.exceptions.GeoAlgorithmExecutionException;
37
import es.unex.sextante.exceptions.RepeatedParameterNameException;
38
import es.unex.sextante.outputs.OutputVectorLayer;
39

    
40
/**
41
 * Merge algorithm
42
 * @author <a href="mailto:nachobrodin@gmail.com">Nacho Brodin</a>
43
 */
44
public class MergeAlgorithm extends gvGeoAlgorithm {
45
        public static final String        RESULT            = "RESULT";
46
        public static final String        LAYERS            = "LAYERS";
47
        public static final String        FIELDLAYER        = "LAYER";
48
        
49
        /*
50
         * (non-Javadoc)
51
         * @see es.unex.sextante.core.GeoAlgorithm#defineCharacteristics()
52
         */
53
        public void defineCharacteristics() {
54
                setName(Sextante.getText("Merge"));
55
                setGroup(Sextante.getText("gvSIG_Algorithms"));
56
                setGeneratesUserDefinedRasterOutput(false);
57
                
58
                try {
59
                        m_Parameters.addMultipleInput(LAYERS, 
60
                                                                                        Sextante.getText("Input_layers"), 
61
                                                                                        AdditionalInfoMultipleInput.DATA_TYPE_VECTOR_ANY, 
62
                                                                                        true);
63
                        m_Parameters.addInputVectorLayer(FIELDLAYER, 
64
                                                                                        Sextante.getText( "Fields"), 
65
                                                                                        IVectorLayer.SHAPE_TYPE_WRONG, 
66
                                                                                        true);
67
                        addOutputVectorLayer(RESULT,
68
                                                                Sextante.getText("Merge"),
69
                                                                OutputVectorLayer.SHAPE_TYPE_UNDEFINED);
70
                } catch (RepeatedParameterNameException e) {
71
                        Sextante.addErrorToLog(e);
72
                }
73
                setExternalParameters(new MergeParametersPanel());
74
        }
75
        
76
        @SuppressWarnings("unchecked")
77
        public boolean processAlgorithm() throws GeoAlgorithmExecutionException {
78
                IVectorLayer layer = m_Parameters.getParameterValueAsVectorLayer(FIELDLAYER);
79
                ArrayList<IVectorLayer> layers = m_Parameters.getParameterValueAsArrayList(LAYERS);
80
                
81
                FeatureStore storeLayer = null;
82
                if(layer instanceof gvVectorLayer)
83
                        storeLayer = ((gvVectorLayer)layer).getFeatureStore();
84
                else
85
                        return false;
86
                
87
                try {
88
                        //Gets the list of FeatureStore
89
                        ArrayList<FeatureStore> featureStoreList = new ArrayList<FeatureStore>();
90
                        for (int i = 0; i < layers.size(); i++) {
91
                                IVectorLayer lyr = layers.get(i);
92
                                if(lyr instanceof gvVectorLayer && layer.getShapeType() == lyr.getShapeType())
93
                                        featureStoreList.add(((gvVectorLayer)lyr).getFeatureStore());
94
                                else
95
                                        return false;
96
                        }
97
                        
98
                        //Builds the output FeatureStore
99
                        FeatureSet features = null;
100
                        features = storeLayer.getFeatureSet();
101
                        FeatureType featureType = features.getDefaultFeatureType();
102
                        FeatureStore outFeatStore = buildOutPutStore(featureType, layer.getShapeType(), Sextante.getText("Merge"), RESULT);
103

    
104
                        //Calls the operation
105
                        GeometryOperation operation = new MergeOperation(layers, layer);
106
                        operation.computesGeometryOperationInAList(featureStoreList, outFeatStore, attrNames, false, true);
107
                } catch (DataException e) {
108
                        Sextante.addErrorToLog(e);
109
                        return false;
110
                }
111
                
112
                return true;
113
        }
114

    
115
}