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

History | View | Annotate | Download (9.64 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
import org.gvsig.fmap.geom.Geometry;
46
import org.gvsig.tools.namestranslator.NamesTranslator;
47

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

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

    
65
    /*
66
         * (non-Javadoc)
67
         * @see es.unex.sextante.core.GeoAlgorithm#defineCharacteristics()
68
     */
69
        public void defineCharacteristics(){
70
        setName(getTranslation("Dissolve"));
71
        setGroup(getTranslation("basic_vect_algorithms"));
72
        // setGeneratesUserDefinedRasterOutput(false);
73
        try {
74
            m_Parameters.addInputVectorLayer(LAYER, getTranslation("Input_layer"), IVectorLayer.SHAPE_TYPE_WRONG, true);
75
            m_Parameters.addBoolean(SELECTED_GEOM, getTranslation("Selected_geometries"), false);
76
            m_Parameters.addBoolean(DISSOLV_ADJ, getTranslation("Selected_geometries"), false);
77
            m_Parameters.addNumericalValue(FIELD, getTranslation("Field"), 0, AdditionalInfoNumericalValue.NUMERICAL_VALUE_INTEGER);
78
            m_Parameters.addString(FUNCTION_LIST, getTranslation("Function_list"));
79
            addOutputVectorLayer(RESULT, getTranslation("Dissolve"), OutputVectorLayer.SHAPE_TYPE_UNDEFINED);
80
        } catch (RepeatedParameterNameException e) {
81
            Sextante.addErrorToLog(e);
82
        }
83
        //setExternalParameters(new DissolveParametersPanel());
84
    }
85

    
86
    /*
87
         * (non-Javadoc)
88
         * @see es.unex.sextante.core.GeoAlgorithm#processAlgorithm()
89
     */
90
    public boolean processAlgorithm() throws GeoAlgorithmExecutionException {
91
        //long t1 = System.currentTimeMillis();
92
                if(existsOutPutFile(DissolveAlgorithm.RESULT, 0)) {
93
            throw new GeoAlgorithmExecutionException(getTranslation("file_exists"));
94
        }
95
        IVectorLayer layer = m_Parameters.getParameterValueAsVectorLayer(LAYER);
96
        int indexField = m_Parameters.getParameterValueAsInt(FIELD);
97
        boolean selectedGeom = m_Parameters.getParameterValueAsBoolean(SELECTED_GEOM);
98
        boolean dissolvAdj = m_Parameters.getParameterValueAsBoolean(DISSOLV_ADJ);
99
        String functionList = m_Parameters.getParameterValueAsString(FUNCTION_LIST);
100
        loadSummary(functionList);
101

    
102
        FeatureStore storeLayer = null;
103
                if(layer instanceof FlyrVectIVectorLayer)
104
                        storeLayer = ((FlyrVectIVectorLayer)layer).getFeatureStore();
105
                else
106
            return false;
107

    
108
        FeatureSet features = null;
109
        FeatureType featureType = null;
110
        try {
111
            features = storeLayer.getFeatureSet();
112
            featureType = features.getDefaultFeatureType();
113
        } catch (DataException e) {
114
            Sextante.addErrorToLog(e);
115
            return false;
116
        }
117

    
118
        int layerType = layer.getShapeType();
119
        switch (layerType) {
120
            case IVectorLayer.SHAPE_TYPE_POINT:
121
                layerType = IVectorLayer.SHAPE_TYPE_MULTIPOINT;
122
                break;
123
            case IVectorLayer.SHAPE_TYPE_LINE:
124
                layerType = IVectorLayer.SHAPE_TYPE_MULTILINE;
125
                break;
126
            case IVectorLayer.SHAPE_TYPE_POLYGON:
127
                layerType = IVectorLayer.SHAPE_TYPE_MULTIPOLYGON;
128
                break;
129
            default:
130
                break;
131
        }
132
        
133
        FeatureStore outFeatStore = buildDissolvedOutPutStore(featureType, layer.getFieldName(indexField),
134
                        layerType, getTranslation("Dissolve"), RESULT);
135
        IDissolveRule criteria = null;
136
        if (dissolvAdj) {
137
            criteria = new AdjacencyDissolveRule(layer.getFieldName(indexField), funcMap);
138
        } else {
139
            criteria = new DissolveRule(layer.getFieldName(indexField), funcMap);
140
        }
141

    
142
        try {
143
            DissolveOperationFast operation = new DissolveOperationFast(criteria, this);
144
            operation.setTaskStatus(getStatus());
145
            operation.computesGeometryOperation(storeLayer, outFeatStore, attrNames,
146
                    selectedGeom, false, true);
147
        } catch (DataException e) {
148
            Sextante.addErrorToLog(e);
149
            return false;
150
        }
151
        //long t2 = System.currentTimeMillis();
152
        //System.out.println(t2 - t1);
153
                if(getTaskMonitor().isCanceled())
154
            return false;
155
        return true;
156
    }
157

    
158
    /**
159
     * Checks if the parameter is in Summary list
160
     * @param it
161
     * @return the position in the list
162
     */
163
    private int isInList(String it) {
164
        for (int i = 0; i < Summary.length; i++) {
165
                        if(Summary[i].compareTo(it) == 0)
166
                return i;
167
            }
168
        return -1;
169
    }
170

    
171
    /**
172
     * Loads the list of functions to use
173
     * @param functionList
174
     */
175
    private void loadSummary(String functionList) {
176
        String[] attrList = functionList.split(";");
177
        for (int i = 0; i < attrList.length; i++) {
178
            String[] func = attrList[i].split(",");
179
            for (int j = 1; j < func.length; j++) {
180
                int pos = isInList(func[j]);
181
                                if(pos != -1) {
182
                    funcList[pos] = true;
183
                    funcMap.put(Summary[pos], func[0]);
184
                }
185
            }
186
        }
187
    }
188

    
189
    /**
190
     * Builds the output FeatureStore
191
     * @param featureType
192
     * @return FeatureStore
193
     */
194
    @SuppressWarnings("unchecked")
195
    protected FeatureStore buildDissolvedOutPutStore(FeatureType featureType1,
196
            String fieldField,
197
            int shapeType,
198
            String sextanteLayerName,
199
            String sextanteLayerLabel) {
200
        ArrayList<Class> typesList = new ArrayList<Class>();
201
        ArrayList<String> attr = new ArrayList<String>();
202
        attr.add("FID");
203
        typesList.add(Integer.class);
204
        FeatureAttributeDescriptor desc = featureType1.getAttributeDescriptor(fieldField);
205
        attr.add(desc.getName());
206
        typesList.add(desc.getObjectClass());
207
                for (int i = 0; i < funcList.length; i++) 
208
                        if(funcList[i]) {
209
                String fieldName = funcMap.get(Summary[i]);
210
                                if(fieldName.length() >= 6)
211
                    fieldName = fieldName.substring(0, 5);
212
                attr.add(fieldName + "_" + Summary[i]);
213
                typesList.add(Double.class);
214
            }
215

    
216
        attrNames = new String[attr.size()];
217
        attr.toArray(attrNames);
218
        Class[] types = new Class[typesList.size()];
219
        typesList.toArray(types);
220

    
221
        try {
222
            IVectorLayer output = getNewVectorLayer(sextanteLayerLabel,
223
                    sextanteLayerName,
224
                    shapeType, types, attrNames);
225
                        return ((FlyrVectIVectorLayer)output).getFeatureStore();
226
        } catch (UnsupportedOutputChannelException e) {
227
            Sextante.addErrorToLog(e);
228
        } catch (GeoAlgorithmExecutionException e) {
229
            Sextante.addErrorToLog(e);
230
        }
231
        return null;
232
    }
233

    
234
    @Override
235
    public Class<? extends GeoAlgorithmParametersPanel> getCustomParametersPanelClass() {
236
        return DissolveParametersPanel.class;
237
    }
238
}