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

History | View | Annotate | Download (4.52 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 javax.swing.JOptionPane;
26

    
27
import es.unex.sextante.additionalInfo.AdditionalInfoMultipleInput;
28
import es.unex.sextante.core.Sextante;
29
import es.unex.sextante.dataObjects.IVectorLayer;
30
import es.unex.sextante.exceptions.GeoAlgorithmExecutionException;
31
import es.unex.sextante.exceptions.RepeatedParameterNameException;
32
import es.unex.sextante.outputs.OutputVectorLayer;
33

    
34
import org.gvsig.fmap.dal.exception.DataException;
35
import org.gvsig.fmap.dal.feature.FeatureSet;
36
import org.gvsig.fmap.dal.feature.FeatureStore;
37
import org.gvsig.fmap.dal.feature.FeatureType;
38
import org.gvsig.geoprocess.core.gvGeoAlgorithm;
39
import org.gvsig.geoprocess.core.gvVectorLayer;
40
import org.gvsig.sextante.app.algorithm.base.core.GeometryOperation;
41

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

    
113
                        //Calls the operation
114
                        GeometryOperation operation = new MergeOperation(layer);
115
                        operation.setProgressModel(this);
116
                        operation.computesGeometryOperationInAList(featureStoreList, outFeatStore, attrNames, false, true);
117
                } catch (DataException e) {
118
                        Sextante.addErrorToLog(e);
119
                        return false;
120
                }
121
                
122
                return true;
123
        }
124

    
125
}