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 237 cordinyana
/**
2
 * gvSIG. Desktop Geographic Information System.
3 175 cordinyana
 *
4 245 cordinyana
 * Copyright (C) 2007-2012 gvSIG Association.
5 175 cordinyana
 *
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 237 cordinyana
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20 245 cordinyana
 *
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 175 cordinyana
 */
24
package org.gvsig.geoprocess.algorithm.merge;
25
26
import java.util.ArrayList;
27
28
import javax.swing.JOptionPane;
29
30 271 nbrodin
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 175 cordinyana
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 244 cordinyana
import es.unex.sextante.gui.algorithm.GeoAlgorithmParametersPanel;
44 175 cordinyana
import es.unex.sextante.outputs.OutputVectorLayer;
45
46
/**
47
 * Merge algorithm
48
 * @author <a href="mailto:nachobrodin@gmail.com">Nacho Brodin</a>
49
 */
50 191 cordinyana
public class MergeAlgorithm extends AbstractSextanteGeoProcess {
51
52 175 cordinyana
        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 244 cordinyana
        setName(getTranslation("Merge"));
61
        setGroup(getTranslation("basic_vect_algorithms"));
62 175 cordinyana
        // setGeneratesUserDefinedRasterOutput(false);
63
64
                try {
65
                        m_Parameters.addMultipleInput(LAYERS,
66 244 cordinyana
                getTranslation("Input_layers"),
67 175 cordinyana
                                                                                        AdditionalInfoMultipleInput.DATA_TYPE_VECTOR_ANY,
68
                                                                                        true);
69
                        m_Parameters.addInputVectorLayer(FIELDLAYER,
70 244 cordinyana
                getTranslation("Fields"),
71 175 cordinyana
                                                                                        IVectorLayer.SHAPE_TYPE_WRONG,
72
                                                                                        true);
73
                        addOutputVectorLayer(RESULT,
74 244 cordinyana
 getTranslation("Merge"),
75 175 cordinyana
                                                                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 271 nbrodin
                if(existsOutPutFile(MergeAlgorithm.RESULT, 0)) {
88
                    throw new GeoAlgorithmExecutionException(getTranslation("file_exists"));
89
            }
90 175 cordinyana
                IVectorLayer layer = m_Parameters.getParameterValueAsVectorLayer(FIELDLAYER);
91
                ArrayList<IVectorLayer> layers = m_Parameters.getParameterValueAsArrayList(LAYERS);
92
93
                FeatureStore storeLayer = null;
94 225 cordinyana
                if(layer instanceof FlyrVectIVectorLayer)
95
                        storeLayer = ((FlyrVectIVectorLayer)layer).getFeatureStore();
96 175 cordinyana
                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 225 cordinyana
                                if(lyr instanceof FlyrVectIVectorLayer && layer.getShapeType() == lyr.getShapeType())
105
                                        featureStoreList.add(((FlyrVectIVectorLayer)lyr).getFeatureStore());
106 175 cordinyana
                                else {
107
                                        JOptionPane.showMessageDialog(null,
108 244 cordinyana
                        getTranslation("layers_type_are_different"),
109 175 cordinyana
                                                        "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 244 cordinyana
            FeatureStore outFeatStore =
120
                buildOutPutStore(featureType, layer.getShapeType(),
121
                    getTranslation("Merge"), RESULT);
122 175 cordinyana
123
                        //Calls the operation
124 289 nbrodin
                        GeometryOperation operation = new MergeOperation(layer, this);
125 223 cordinyana
            operation.setTaskStatus(getStatus());
126 175 cordinyana
                        operation.computesGeometryOperationInAList(featureStoreList, outFeatStore, attrNames, false, true);
127
                } catch (DataException e) {
128
                        Sextante.addErrorToLog(e);
129
                        return false;
130
                }
131
132 324 nbrodin
                if(getTaskMonitor().isCanceled())
133
                        return false;
134
135 175 cordinyana
                return true;
136
        }
137
138 244 cordinyana
    @Override
139
    public Class<? extends GeoAlgorithmParametersPanel> getCustomParametersPanelClass() {
140
        return MergeParametersPanel.class;
141
    }
142
143 175 cordinyana
}