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 @ 335
History | View | Annotate | Download (8.23 KB)
1 | 237 | cordinyana | /**
|
---|---|---|---|
2 | * gvSIG. Desktop Geographic Information System.
|
||
3 | 175 | cordinyana | *
|
4 | 245 | cordinyana | * Copyright (C) 2007-2012 gvSIG Association.
|
5 | 175 | cordinyana | *
|
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 | 237 | cordinyana | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 | * MA 02110-1301, USA.
|
||
20 | 245 | cordinyana | *
|
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 | 175 | cordinyana | */
|
24 | package org.gvsig.geoprocess.algorithm.dissolve; |
||
25 | |||
26 | import java.util.ArrayList; |
||
27 | import java.util.HashMap; |
||
28 | |||
29 | 271 | nbrodin | 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 | 175 | cordinyana | 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 | 244 | cordinyana | import es.unex.sextante.gui.algorithm.GeoAlgorithmParametersPanel; |
44 | 175 | cordinyana | import es.unex.sextante.outputs.OutputVectorLayer; |
45 | |||
46 | /**
|
||
47 | * Dissolve algorithm
|
||
48 | * @author <a href="mailto:nachobrodin@gmail.com">Nacho Brodin</a>
|
||
49 | */
|
||
50 | 191 | cordinyana | public class DissolveAlgorithm extends AbstractSextanteGeoProcess { |
51 | |||
52 | 175 | cordinyana | 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 | 244 | cordinyana | setName(getTranslation("Dissolve"));
|
69 | setGroup(getTranslation("basic_vect_algorithms"));
|
||
70 | 175 | cordinyana | // setGeneratesUserDefinedRasterOutput(false);
|
71 | try {
|
||
72 | 259 | nbrodin | 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 | 260 | nbrodin | addOutputVectorLayer(RESULT, getTranslation("Dissolve"), OutputVectorLayer.SHAPE_TYPE_UNDEFINED);
|
78 | 175 | cordinyana | } 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 | 319 | nbrodin | //long t1 = System.currentTimeMillis();
|
90 | 271 | nbrodin | if(existsOutPutFile(DissolveAlgorithm.RESULT, 0)) { |
91 | throw new GeoAlgorithmExecutionException(getTranslation("file_exists")); |
||
92 | } |
||
93 | 175 | cordinyana | IVectorLayer layer = m_Parameters.getParameterValueAsVectorLayer(LAYER); |
94 | int indexField = m_Parameters.getParameterValueAsInt(FIELD);
|
||
95 | boolean selectedGeom = m_Parameters.getParameterValueAsBoolean(SELECTED_GEOM);
|
||
96 | boolean dissolvAdj = m_Parameters.getParameterValueAsBoolean(DISSOLV_ADJ);
|
||
97 | String functionList = m_Parameters.getParameterValueAsString(FUNCTION_LIST);
|
||
98 | loadSummary(functionList); |
||
99 | |||
100 | FeatureStore storeLayer = null;
|
||
101 | 225 | cordinyana | if(layer instanceof FlyrVectIVectorLayer) |
102 | storeLayer = ((FlyrVectIVectorLayer)layer).getFeatureStore(); |
||
103 | 175 | cordinyana | else
|
104 | return false; |
||
105 | |||
106 | FeatureSet features = null;
|
||
107 | FeatureType featureType = null;
|
||
108 | try {
|
||
109 | features = storeLayer.getFeatureSet(); |
||
110 | featureType = features.getDefaultFeatureType(); |
||
111 | } catch (DataException e) {
|
||
112 | Sextante.addErrorToLog(e); |
||
113 | return false; |
||
114 | } |
||
115 | 244 | cordinyana | FeatureStore outFeatStore = |
116 | buildDissolvedOutPutStore(featureType, indexField, |
||
117 | layer.getShapeType(), getTranslation("Dissolve"), RESULT);
|
||
118 | 175 | cordinyana | IDissolveRule criteria = null;
|
119 | if(dissolvAdj)
|
||
120 | criteria = new AdjacencyDissolveRule(indexField, funcMap);
|
||
121 | else
|
||
122 | criteria = new DissolveRule(indexField, funcMap);
|
||
123 | |||
124 | try {
|
||
125 | 297 | nbrodin | DissolveOperationFast operation = new DissolveOperationFast(criteria, this); |
126 | 223 | cordinyana | operation.setTaskStatus(getStatus()); |
127 | 335 | nbrodin | operation.computesGeometryOperation(storeLayer, outFeatStore, attrNames, |
128 | selectedGeom, false, true); |
||
129 | 175 | cordinyana | } catch (DataException e) {
|
130 | Sextante.addErrorToLog(e); |
||
131 | return false; |
||
132 | } |
||
133 | 319 | nbrodin | //long t2 = System.currentTimeMillis();
|
134 | //System.out.println(t2 - t1);
|
||
135 | if(getTaskMonitor().isCanceled())
|
||
136 | return false; |
||
137 | 175 | cordinyana | return true; |
138 | } |
||
139 | |||
140 | /**
|
||
141 | * Checks if the parameter is in Summary list
|
||
142 | * @param it
|
||
143 | * @return the position in the list
|
||
144 | */
|
||
145 | private int isInList(String it) { |
||
146 | for (int i = 0; i < Summary.length; i++) { |
||
147 | if(Summary[i].compareTo(it) == 0) |
||
148 | return i;
|
||
149 | } |
||
150 | return -1; |
||
151 | } |
||
152 | |||
153 | /**
|
||
154 | * Loads the list of functions to use
|
||
155 | * @param functionList
|
||
156 | */
|
||
157 | private void loadSummary(String functionList) { |
||
158 | String[] attrList = functionList.split(";"); |
||
159 | for (int i = 0; i < attrList.length; i++) { |
||
160 | String[] func = attrList[i].split(","); |
||
161 | for (int j = 1; j < func.length; j++) { |
||
162 | int pos = isInList(func[j]);
|
||
163 | if(pos != -1) { |
||
164 | funcList[pos] = true;
|
||
165 | funcMap.put(Summary[pos], func[0]);
|
||
166 | } |
||
167 | } |
||
168 | } |
||
169 | } |
||
170 | |||
171 | /**
|
||
172 | * Builds the output FeatureStore
|
||
173 | * @param featureType
|
||
174 | * @return FeatureStore
|
||
175 | */
|
||
176 | @SuppressWarnings("unchecked") |
||
177 | protected FeatureStore buildDissolvedOutPutStore(FeatureType featureType1,
|
||
178 | int indexField,
|
||
179 | int shapeType,
|
||
180 | String sextanteLayerName,
|
||
181 | String sextanteLayerLabel) {
|
||
182 | ArrayList<Class> typesList = new ArrayList<Class>(); |
||
183 | ArrayList<String> attr = new ArrayList<String>(); |
||
184 | attr.add("FID");
|
||
185 | typesList.add(Integer.class);
|
||
186 | FeatureAttributeDescriptor desc = featureType1.getAttributeDescriptor(indexField); |
||
187 | attr.add(desc.getName()); |
||
188 | typesList.add(desc.getObjectClass()); |
||
189 | for (int i = 0; i < funcList.length; i++) |
||
190 | if(funcList[i]) {
|
||
191 | String fieldName = funcMap.get(Summary[i]);
|
||
192 | if(fieldName.length() >= 6) |
||
193 | fieldName = fieldName.substring(0, 7); |
||
194 | attr.add(fieldName + "_" + Summary[i]);
|
||
195 | typesList.add(Double.class);
|
||
196 | } |
||
197 | |||
198 | attrNames = new String[attr.size()]; |
||
199 | attr.toArray(attrNames); |
||
200 | Class[] types = new Class[typesList.size()]; |
||
201 | typesList.toArray(types); |
||
202 | |||
203 | try {
|
||
204 | IVectorLayer output = getNewVectorLayer(sextanteLayerLabel, |
||
205 | sextanteLayerName, |
||
206 | shapeType, types, attrNames); |
||
207 | 225 | cordinyana | return ((FlyrVectIVectorLayer)output).getFeatureStore();
|
208 | 175 | cordinyana | } catch (UnsupportedOutputChannelException e) {
|
209 | Sextante.addErrorToLog(e); |
||
210 | } catch (GeoAlgorithmExecutionException e) {
|
||
211 | Sextante.addErrorToLog(e); |
||
212 | } |
||
213 | return null; |
||
214 | } |
||
215 | 244 | cordinyana | |
216 | @Override
|
||
217 | public Class<? extends GeoAlgorithmParametersPanel> getCustomParametersPanelClass() { |
||
218 | return DissolveParametersPanel.class;
|
||
219 | } |
||
220 | 175 | cordinyana | } |