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 @ 324

History | View | Annotate | Download (5.21 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 org.gvsig.fmap.dal.exception.DataException;
31
import org.gvsig.fmap.dal.feature.FeatureSet;
32
import org.gvsig.fmap.dal.feature.FeatureStore;
33
import org.gvsig.fmap.dal.feature.FeatureType;
34
import org.gvsig.geoprocess.algorithm.base.core.GeometryOperation;
35
import org.gvsig.geoprocess.lib.sextante.AbstractSextanteGeoProcess;
36
import org.gvsig.geoprocess.lib.sextante.dataObjects.FlyrVectIVectorLayer;
37

    
38
import es.unex.sextante.additionalInfo.AdditionalInfoMultipleInput;
39
import es.unex.sextante.core.Sextante;
40
import es.unex.sextante.dataObjects.IVectorLayer;
41
import es.unex.sextante.exceptions.GeoAlgorithmExecutionException;
42
import es.unex.sextante.exceptions.RepeatedParameterNameException;
43
import es.unex.sextante.gui.algorithm.GeoAlgorithmParametersPanel;
44
import es.unex.sextante.outputs.OutputVectorLayer;
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
                if(existsOutPutFile(MergeAlgorithm.RESULT, 0)) {
88
                    throw new GeoAlgorithmExecutionException(getTranslation("file_exists"));
89
            }
90
                IVectorLayer layer = m_Parameters.getParameterValueAsVectorLayer(FIELDLAYER);
91
                ArrayList<IVectorLayer> layers = m_Parameters.getParameterValueAsArrayList(LAYERS);
92
                
93
                FeatureStore storeLayer = null;
94
                if(layer instanceof FlyrVectIVectorLayer)
95
                        storeLayer = ((FlyrVectIVectorLayer)layer).getFeatureStore();
96
                else
97
                        return false;
98
                
99
                try {
100
                        //Gets the list of FeatureStore
101
                        ArrayList<FeatureStore> featureStoreList = new ArrayList<FeatureStore>();
102
                        for (int i = 0; i < layers.size(); i++) {
103
                                IVectorLayer lyr = layers.get(i);
104
                                if(lyr instanceof FlyrVectIVectorLayer && layer.getShapeType() == lyr.getShapeType())
105
                                        featureStoreList.add(((FlyrVectIVectorLayer)lyr).getFeatureStore());
106
                                else {
107
                                        JOptionPane.showMessageDialog(null,
108
                        getTranslation("layers_type_are_different"),
109
                                                        "Error",
110
                                                        JOptionPane.WARNING_MESSAGE);
111
                                        return false;
112
                                }
113
                        }
114
                        
115
                        //Builds the output FeatureStore
116
                        FeatureSet features = null;
117
                        features = storeLayer.getFeatureSet();
118
                        FeatureType featureType = features.getDefaultFeatureType();
119
            FeatureStore outFeatStore =
120
                buildOutPutStore(featureType, layer.getShapeType(),
121
                    getTranslation("Merge"), RESULT);
122

    
123
                        //Calls the operation
124
                        GeometryOperation operation = new MergeOperation(layer, this);
125
            operation.setTaskStatus(getStatus());
126
                        operation.computesGeometryOperationInAList(featureStoreList, outFeatStore, attrNames, false, true);
127
                } catch (DataException e) {
128
                        Sextante.addErrorToLog(e);
129
                        return false;
130
                }
131
                
132
                if(getTaskMonitor().isCanceled())
133
                        return false;
134
                
135
                return true;
136
        }
137

    
138
    @Override
139
    public Class<? extends GeoAlgorithmParametersPanel> getCustomParametersPanelClass() {
140
        return MergeParametersPanel.class;
141
    }
142

    
143
}