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

History | View | Annotate | Download (7.89 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, 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_POLYGON);
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
                IVectorLayer layer = m_Parameters.getParameterValueAsVectorLayer(LAYER);
90
                int indexField = m_Parameters.getParameterValueAsInt(FIELD);
91
                boolean selectedGeom = m_Parameters.getParameterValueAsBoolean(SELECTED_GEOM);
92
                boolean dissolvAdj = m_Parameters.getParameterValueAsBoolean(DISSOLV_ADJ);
93
                String functionList = m_Parameters.getParameterValueAsString(FUNCTION_LIST);
94
                loadSummary(functionList);
95
                
96
                FeatureStore storeLayer = null;
97
                if(layer instanceof FlyrVectIVectorLayer)
98
                        storeLayer = ((FlyrVectIVectorLayer)layer).getFeatureStore();
99
                else
100
                        return false;
101
                
102
                FeatureSet features = null;
103
                FeatureType featureType = null;
104
                try {
105
                        features = storeLayer.getFeatureSet();
106
                        featureType = features.getDefaultFeatureType();
107
                } catch (DataException e) {
108
                        Sextante.addErrorToLog(e);
109
                        return false;
110
                }
111
        FeatureStore outFeatStore =
112
            buildDissolvedOutPutStore(featureType, indexField,
113
                layer.getShapeType(), getTranslation("Dissolve"), RESULT);
114
                IDissolveRule criteria = null;
115
                if(dissolvAdj)
116
                        criteria = new AdjacencyDissolveRule(indexField, funcMap);
117
                else
118
                        criteria = new DissolveRule(indexField, funcMap);
119
                
120
                try {
121
                        DissolveOperation operation = new DissolveOperation(criteria);
122
            operation.setTaskStatus(getStatus());
123
                        operation.computesGeometryOperation(storeLayer, outFeatStore, attrNames, selectedGeom, true);
124
                } catch (DataException e) {
125
                        Sextante.addErrorToLog(e);
126
                        return false;
127
                }
128
                return true;
129
        }
130
        
131
        /**
132
         * Checks if the parameter is in Summary list
133
         * @param it
134
         * @return the position in the list
135
         */
136
        private int isInList(String it) {
137
                for (int i = 0; i < Summary.length; i++) {
138
                        if(Summary[i].compareTo(it) == 0)
139
                                return i;
140
                }
141
                return -1;
142
        }
143
        
144
        /**
145
         * Loads the list of functions to use
146
         * @param functionList
147
         */
148
        private void loadSummary(String functionList) {
149
                String[] attrList = functionList.split(";");
150
                for (int i = 0; i < attrList.length; i++) {
151
                        String[] func = attrList[i].split(",");
152
                        for (int j = 1; j < func.length; j++) {
153
                                int pos = isInList(func[j]);
154
                                if(pos != -1) {
155
                                        funcList[pos] = true;
156
                                        funcMap.put(Summary[pos], func[0]);
157
                                }
158
                        }
159
                }
160
        }
161
        
162
        /**
163
         * Builds the output FeatureStore 
164
         * @param featureType
165
         * @return FeatureStore
166
         */
167
        @SuppressWarnings("unchecked")
168
        protected FeatureStore buildDissolvedOutPutStore(FeatureType featureType1,
169
                                                                                        int indexField,
170
                                                                                        int shapeType,
171
                                                                                        String sextanteLayerName, 
172
                                                                                        String sextanteLayerLabel) {
173
                ArrayList<Class> typesList = new ArrayList<Class>();
174
                ArrayList<String> attr = new ArrayList<String>();
175
                attr.add("FID");
176
                typesList.add(Integer.class);
177
                FeatureAttributeDescriptor desc = featureType1.getAttributeDescriptor(indexField);
178
                attr.add(desc.getName());
179
                typesList.add(desc.getObjectClass());
180
                for (int i = 0; i < funcList.length; i++) 
181
                        if(funcList[i]) {
182
                                String fieldName = funcMap.get(Summary[i]);
183
                                if(fieldName.length() >= 6)
184
                                        fieldName = fieldName.substring(0, 7);
185
                                attr.add(fieldName + "_" + Summary[i]);
186
                                typesList.add(Double.class);
187
                        }
188
                
189
                attrNames = new String[attr.size()];
190
                attr.toArray(attrNames);
191
                Class[] types = new Class[typesList.size()];
192
                typesList.toArray(types);
193
                
194
                try {
195
                        IVectorLayer output = getNewVectorLayer(sextanteLayerLabel,
196
                                                                                                        sextanteLayerName,
197
                                                                                                        shapeType, types, attrNames);
198
                        return ((FlyrVectIVectorLayer)output).getFeatureStore();
199
                } catch (UnsupportedOutputChannelException e) {
200
                        Sextante.addErrorToLog(e);
201
        } catch (GeoAlgorithmExecutionException e) {
202
            Sextante.addErrorToLog(e);
203
        }
204
                return null;
205
        }
206

    
207
    @Override
208
    public Class<? extends GeoAlgorithmParametersPanel> getCustomParametersPanelClass() {
209
        return DissolveParametersPanel.class;
210
    }
211
}