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

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

132
                        //Calls the operation
133
                        GeometryOperation operation = new MergeOperation(layer, this);
134
            operation.setTaskStatus(getStatus());
135
                        operation.computesGeometryOperationInAList(featureStoreList, outFeatStore, attrNames, false, true);
136
                        */
137
                        GeometryOperation operation = new MergeOperation(layer, this);
138
            operation.setTaskStatus(getStatus());
139
            
140
                        if (isPolygon(storeLayer) || isUndefined(storeLayer)) {
141
                                FeatureStore outFeatStore =
142
                                        buildOutPutStore(featureType, IVectorLayer.SHAPE_TYPE_POLYGON,
143
                                                        getTranslation("Merge_polygon"), RESULT_POL);
144

    
145
                                operation.computesGeometryOperationInAList(featureStoreList, outFeatStore, attrNames, 
146
                                                false, false, true);
147
                        } else {
148
                                buildOutPutStore(featureType, IVectorLayer.SHAPE_TYPE_POLYGON,
149
                                                getTranslation("Null_polygon"), RESULT_POL);
150
                        }
151
                        
152
                        if (isLine(storeLayer) || isUndefined(storeLayer)) {
153
                                FeatureStore outFeatStore =
154
                                        buildOutPutStore(featureType, IVectorLayer.SHAPE_TYPE_LINE,
155
                                                        getTranslation("Merge_line"), RESULT_LINE);
156

    
157
                                operation.computesGeometryOperationInAList(featureStoreList, outFeatStore, attrNames, 
158
                                                false, false, true);
159
                        } else {
160
                                buildOutPutStore(featureType.getCopy(), IVectorLayer.SHAPE_TYPE_LINE,
161
                                                getTranslation("Null_line"), RESULT_LINE);
162
                        }
163
                        
164
                        if (isPoint(storeLayer) || isUndefined(storeLayer)) {
165
                                FeatureStore outFeatStore =
166
                                        buildOutPutStore(featureType.getCopy(), IVectorLayer.SHAPE_TYPE_POINT,
167
                                                        getTranslation("Merge_point"), RESULT_POINT);
168

    
169
                                operation.computesGeometryOperationInAList(featureStoreList, outFeatStore, attrNames, 
170
                                                false, false, true);
171
                        } else {
172
                                buildOutPutStore(featureType.getCopy(), IVectorLayer.SHAPE_TYPE_POINT,
173
                                                getTranslation("Null_point"), RESULT_POINT);
174
                        }
175
                        
176
                } catch (DataException e) {
177
                        Sextante.addErrorToLog(e);
178
                        return false;
179
                }
180
                
181
                if(getTaskMonitor().isCanceled())
182
                        return false;
183
                
184
                return true;
185
        }
186

    
187
    @Override
188
    public Class<? extends GeoAlgorithmParametersPanel> getCustomParametersPanelClass() {
189
        return MergeParametersPanel.class;
190
    }
191

    
192
}