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

History | View | Annotate | Download (9.71 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
        this.namesTranslator = NamesTranslator.createTrimTranslator(11);
111
        try {
112
            features = storeLayer.getFeatureSet();
113
            featureType = features.getDefaultFeatureType();
114
        } catch (DataException e) {
115
            Sextante.addErrorToLog(e);
116
            return false;
117
        }
118

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

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

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

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

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

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

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

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