Statistics
| Revision:

gvsig-geoprocess / org.gvsig.geoprocess / trunk / org.gvsig.geoprocess / org.gvsig.geoprocess.algorithm / org.gvsig.geoprocess.algorithm.merge / src / main / java / org / gvsig / geoprocess / algorithm / merge / MergeAlgorithm.java @ 245

History | View | Annotate | Download (5.01 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2012 gvSIG Association.
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., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.geoprocess.algorithm.merge;
25

    
26
import java.util.ArrayList;
27

    
28
import javax.swing.JOptionPane;
29

    
30
import es.unex.sextante.additionalInfo.AdditionalInfoMultipleInput;
31
import es.unex.sextante.core.Sextante;
32
import es.unex.sextante.dataObjects.IVectorLayer;
33
import es.unex.sextante.exceptions.GeoAlgorithmExecutionException;
34
import es.unex.sextante.exceptions.RepeatedParameterNameException;
35
import es.unex.sextante.gui.algorithm.GeoAlgorithmParametersPanel;
36
import es.unex.sextante.outputs.OutputVectorLayer;
37

    
38
import org.gvsig.fmap.dal.exception.DataException;
39
import org.gvsig.fmap.dal.feature.FeatureSet;
40
import org.gvsig.fmap.dal.feature.FeatureStore;
41
import org.gvsig.fmap.dal.feature.FeatureType;
42
import org.gvsig.geoprocess.algorithm.base.core.GeometryOperation;
43
import org.gvsig.geoprocess.lib.sextante.AbstractSextanteGeoProcess;
44
import org.gvsig.geoprocess.lib.sextante.dataObjects.FlyrVectIVectorLayer;
45

    
46
/**
47
 * Merge algorithm
48
 * @author <a href="mailto:nachobrodin@gmail.com">Nacho Brodin</a>
49
 */
50
public class MergeAlgorithm extends AbstractSextanteGeoProcess {
51

    
52
        public static final String        RESULT            = "RESULT";
53
        public static final String        LAYERS            = "LAYERS";
54
        public static final String        FIELDLAYER        = "LAYER";
55
        /*
56
         * (non-Javadoc)
57
         * @see es.unex.sextante.core.GeoAlgorithm#defineCharacteristics()
58
         */
59
        public void defineCharacteristics() {
60
        setName(getTranslation("Merge"));
61
        setGroup(getTranslation("basic_vect_algorithms"));
62
        // setGeneratesUserDefinedRasterOutput(false);
63
                
64
                try {
65
                        m_Parameters.addMultipleInput(LAYERS, 
66
                getTranslation("Input_layers"),
67
                                                                                        AdditionalInfoMultipleInput.DATA_TYPE_VECTOR_ANY, 
68
                                                                                        true);
69
                        m_Parameters.addInputVectorLayer(FIELDLAYER, 
70
                getTranslation("Fields"),
71
                                                                                        IVectorLayer.SHAPE_TYPE_WRONG, 
72
                                                                                        true);
73
                        addOutputVectorLayer(RESULT,
74
 getTranslation("Merge"),
75
                                                                OutputVectorLayer.SHAPE_TYPE_UNDEFINED);
76
                } catch (RepeatedParameterNameException e) {
77
                        Sextante.addErrorToLog(e);
78
                }
79
        }
80
        
81
        /*
82
         * (non-Javadoc)
83
         * @see es.unex.sextante.core.GeoAlgorithm#processAlgorithm()
84
         */
85
        @SuppressWarnings("unchecked")
86
        public boolean processAlgorithm() throws GeoAlgorithmExecutionException {
87
                IVectorLayer layer = m_Parameters.getParameterValueAsVectorLayer(FIELDLAYER);
88
                ArrayList<IVectorLayer> layers = m_Parameters.getParameterValueAsArrayList(LAYERS);
89
                
90
                FeatureStore storeLayer = null;
91
                if(layer instanceof FlyrVectIVectorLayer)
92
                        storeLayer = ((FlyrVectIVectorLayer)layer).getFeatureStore();
93
                else
94
                        return false;
95
                
96
                try {
97
                        //Gets the list of FeatureStore
98
                        ArrayList<FeatureStore> featureStoreList = new ArrayList<FeatureStore>();
99
                        for (int i = 0; i < layers.size(); i++) {
100
                                IVectorLayer lyr = layers.get(i);
101
                                if(lyr instanceof FlyrVectIVectorLayer && layer.getShapeType() == lyr.getShapeType())
102
                                        featureStoreList.add(((FlyrVectIVectorLayer)lyr).getFeatureStore());
103
                                else {
104
                                        JOptionPane.showMessageDialog(null,
105
                        getTranslation("layers_type_are_different"),
106
                                                        "Error",
107
                                                        JOptionPane.WARNING_MESSAGE);
108
                                        return false;
109
                                }
110
                        }
111
                        
112
                        //Builds the output FeatureStore
113
                        FeatureSet features = null;
114
                        features = storeLayer.getFeatureSet();
115
                        FeatureType featureType = features.getDefaultFeatureType();
116
            FeatureStore outFeatStore =
117
                buildOutPutStore(featureType, layer.getShapeType(),
118
                    getTranslation("Merge"), RESULT);
119

    
120
                        //Calls the operation
121
                        GeometryOperation operation = new MergeOperation(layer);
122
            operation.setTaskStatus(getStatus());
123
                        operation.computesGeometryOperationInAList(featureStoreList, outFeatStore, attrNames, false, true);
124
                } catch (DataException e) {
125
                        Sextante.addErrorToLog(e);
126
                        return false;
127
                }
128
                
129
                return true;
130
        }
131

    
132
    @Override
133
    public Class<? extends GeoAlgorithmParametersPanel> getCustomParametersPanelClass() {
134
        return MergeParametersPanel.class;
135
    }
136

    
137
}