Statistics
| Revision:

gvsig-geoprocess / org.gvsig.geoprocess / trunk / org.gvsig.geoprocess / org.gvsig.geoprocess.algorithm / org.gvsig.geoprocess.algorithm.dissolve / src / main / java / org / gvsig / geoprocess / algorithm / dissolve / DissolveAlgorithm.java @ 245

History | View | Annotate | Download (8.04 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.dissolve;
25

    
26
import java.util.ArrayList;
27
import java.util.HashMap;
28

    
29
import es.unex.sextante.additionalInfo.AdditionalInfoNumericalValue;
30
import es.unex.sextante.core.Sextante;
31
import es.unex.sextante.dataObjects.IVectorLayer;
32
import es.unex.sextante.exceptions.GeoAlgorithmExecutionException;
33
import es.unex.sextante.exceptions.RepeatedParameterNameException;
34
import es.unex.sextante.exceptions.UnsupportedOutputChannelException;
35
import es.unex.sextante.gui.algorithm.GeoAlgorithmParametersPanel;
36
import es.unex.sextante.outputs.OutputVectorLayer;
37

    
38
import org.gvsig.fmap.dal.exception.DataException;
39
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
40
import org.gvsig.fmap.dal.feature.FeatureSet;
41
import org.gvsig.fmap.dal.feature.FeatureStore;
42
import org.gvsig.fmap.dal.feature.FeatureType;
43
import org.gvsig.geoprocess.lib.sextante.AbstractSextanteGeoProcess;
44
import org.gvsig.geoprocess.lib.sextante.dataObjects.FlyrVectIVectorLayer;
45

    
46
/**
47
 * Dissolve algorithm
48
 * @author <a href="mailto:nachobrodin@gmail.com">Nacho Brodin</a>
49
 */
50
public class DissolveAlgorithm extends AbstractSextanteGeoProcess {
51

    
52
        public static final String        RESULT            = "RESULT";
53
        public static final String        LAYER             = "LAYER";
54
        public static final String        FIELD             = "FIELD";
55
        public static final String        FUNCTIONS         = "FUNCTIONS";
56
        public static final String        SELECTED_GEOM     = "SELECTED_GEOM";
57
        public static final String        DISSOLV_ADJ       = "DISSOLV_ADJ";
58
        public static final String        FUNCTION_LIST     = "FUNCTION_LIST";
59
        public static final String        Summary[]         = {"Min", "Max", "Sum", "Avg"};
60
        private boolean                   funcList[]        = new boolean[Summary.length];
61
        private HashMap<String, String>   funcMap           = new HashMap<String, String>();
62

    
63
        /*
64
         * (non-Javadoc)
65
         * @see es.unex.sextante.core.GeoAlgorithm#defineCharacteristics()
66
         */
67
        public void defineCharacteristics(){
68
        setName(getTranslation("Dissolve"));
69
        setGroup(getTranslation("basic_vect_algorithms"));
70
        // setGeneratesUserDefinedRasterOutput(false);
71
                try {
72
                        m_Parameters.addInputVectorLayer(LAYER, 
73
                getTranslation("Input_layer"),
74
                                                                                                IVectorLayer.SHAPE_TYPE_WRONG, 
75
                                                                                                true);
76
                        m_Parameters.addBoolean(SELECTED_GEOM, 
77
                getTranslation("Selected_geometries"),
78
                                                                        false);
79
                        m_Parameters.addBoolean(DISSOLV_ADJ, 
80
                getTranslation("Selected_geometries"),
81
                                                                        false);
82
                        m_Parameters.addNumericalValue(FIELD, 
83
 getTranslation("Field"),
84
                                                                                        0,
85
                                                                                        AdditionalInfoNumericalValue.NUMERICAL_VALUE_INTEGER);
86
            m_Parameters.addString(FUNCTION_LIST,
87
                getTranslation("Function_list"));
88
                        addOutputVectorLayer(RESULT,
89
 getTranslation("Dissolve"),
90
                                                                OutputVectorLayer.SHAPE_TYPE_POLYGON);
91
                } catch (RepeatedParameterNameException e) {
92
                        Sextante.addErrorToLog(e);
93
                }
94
                //setExternalParameters(new DissolveParametersPanel());
95
        }
96
        
97
        /*
98
         * (non-Javadoc)
99
         * @see es.unex.sextante.core.GeoAlgorithm#processAlgorithm()
100
         */
101
        public boolean processAlgorithm() throws GeoAlgorithmExecutionException {
102
                IVectorLayer layer = m_Parameters.getParameterValueAsVectorLayer(LAYER);
103
                int indexField = m_Parameters.getParameterValueAsInt(FIELD);
104
                boolean selectedGeom = m_Parameters.getParameterValueAsBoolean(SELECTED_GEOM);
105
                boolean dissolvAdj = m_Parameters.getParameterValueAsBoolean(DISSOLV_ADJ);
106
                String functionList = m_Parameters.getParameterValueAsString(FUNCTION_LIST);
107
                loadSummary(functionList);
108
                
109
                FeatureStore storeLayer = null;
110
                if(layer instanceof FlyrVectIVectorLayer)
111
                        storeLayer = ((FlyrVectIVectorLayer)layer).getFeatureStore();
112
                else
113
                        return false;
114
                
115
                FeatureSet features = null;
116
                FeatureType featureType = null;
117
                try {
118
                        features = storeLayer.getFeatureSet();
119
                        featureType = features.getDefaultFeatureType();
120
                } catch (DataException e) {
121
                        Sextante.addErrorToLog(e);
122
                        return false;
123
                }
124
        FeatureStore outFeatStore =
125
            buildDissolvedOutPutStore(featureType, indexField,
126
                layer.getShapeType(), getTranslation("Dissolve"), RESULT);
127
                IDissolveRule criteria = null;
128
                if(dissolvAdj)
129
                        criteria = new AdjacencyDissolveRule(indexField, funcMap);
130
                else
131
                        criteria = new DissolveRule(indexField, funcMap);
132
                
133
                try {
134
                        DissolveOperation operation = new DissolveOperation(criteria);
135
            operation.setTaskStatus(getStatus());
136
                        operation.computesGeometryOperation(storeLayer, outFeatStore, attrNames, selectedGeom, true);
137
                } catch (DataException e) {
138
                        Sextante.addErrorToLog(e);
139
                        return false;
140
                }
141
                return true;
142
        }
143
        
144
        /**
145
         * Checks if the parameter is in Summary list
146
         * @param it
147
         * @return the position in the list
148
         */
149
        private int isInList(String it) {
150
                for (int i = 0; i < Summary.length; i++) {
151
                        if(Summary[i].compareTo(it) == 0)
152
                                return i;
153
                }
154
                return -1;
155
        }
156
        
157
        /**
158
         * Loads the list of functions to use
159
         * @param functionList
160
         */
161
        private void loadSummary(String functionList) {
162
                String[] attrList = functionList.split(";");
163
                for (int i = 0; i < attrList.length; i++) {
164
                        String[] func = attrList[i].split(",");
165
                        for (int j = 1; j < func.length; j++) {
166
                                int pos = isInList(func[j]);
167
                                if(pos != -1) {
168
                                        funcList[pos] = true;
169
                                        funcMap.put(Summary[pos], func[0]);
170
                                }
171
                        }
172
                }
173
        }
174
        
175
        /**
176
         * Builds the output FeatureStore 
177
         * @param featureType
178
         * @return FeatureStore
179
         */
180
        @SuppressWarnings("unchecked")
181
        protected FeatureStore buildDissolvedOutPutStore(FeatureType featureType1,
182
                                                                                        int indexField,
183
                                                                                        int shapeType,
184
                                                                                        String sextanteLayerName, 
185
                                                                                        String sextanteLayerLabel) {
186
                ArrayList<Class> typesList = new ArrayList<Class>();
187
                ArrayList<String> attr = new ArrayList<String>();
188
                attr.add("FID");
189
                typesList.add(Integer.class);
190
                FeatureAttributeDescriptor desc = featureType1.getAttributeDescriptor(indexField);
191
                attr.add(desc.getName());
192
                typesList.add(desc.getObjectClass());
193
                for (int i = 0; i < funcList.length; i++) 
194
                        if(funcList[i]) {
195
                                String fieldName = funcMap.get(Summary[i]);
196
                                if(fieldName.length() >= 6)
197
                                        fieldName = fieldName.substring(0, 7);
198
                                attr.add(fieldName + "_" + Summary[i]);
199
                                typesList.add(Double.class);
200
                        }
201
                
202
                attrNames = new String[attr.size()];
203
                attr.toArray(attrNames);
204
                Class[] types = new Class[typesList.size()];
205
                typesList.toArray(types);
206
                
207
                try {
208
                        IVectorLayer output = getNewVectorLayer(sextanteLayerLabel,
209
                                                                                                        sextanteLayerName,
210
                                                                                                        shapeType, types, attrNames);
211
                        return ((FlyrVectIVectorLayer)output).getFeatureStore();
212
                } catch (UnsupportedOutputChannelException e) {
213
                        Sextante.addErrorToLog(e);
214
        } catch (GeoAlgorithmExecutionException e) {
215
            Sextante.addErrorToLog(e);
216
        }
217
                return null;
218
        }
219

    
220
    @Override
221
    public Class<? extends GeoAlgorithmParametersPanel> getCustomParametersPanelClass() {
222
        return DissolveParametersPanel.class;
223
    }
224
}