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

History | View | Annotate | Download (9.59 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

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

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

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

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

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

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

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

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

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

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

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

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

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

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