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

History | View | Annotate | Download (4.61 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.geoprocess.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.algorithm.base.core.GeometryOperation;
39
import org.gvsig.geoprocess.lib.sextante.AbstractSextanteGeoProcess;
40
import org.gvsig.geoprocess.lib.sextante.dataObjects.FlyrVectIVectorLayer;
41

    
42
/**
43
 * Merge algorithm
44
 * @author <a href="mailto:nachobrodin@gmail.com">Nacho Brodin</a>
45
 */
46
public class MergeAlgorithm extends AbstractSextanteGeoProcess {
47

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

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

    
126
}