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

History | View | Annotate | Download (8.03 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 org.gvsig.fmap.dal.exception.DataException;
30
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
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.lib.sextante.AbstractSextanteGeoProcess;
35
import org.gvsig.geoprocess.lib.sextante.dataObjects.FlyrVectIVectorLayer;
36

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

    
210
    @Override
211
    public Class<? extends GeoAlgorithmParametersPanel> getCustomParametersPanelClass() {
212
        return DissolveParametersPanel.class;
213
    }
214
}