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 @ 191
History | View | Annotate | Download (7.6 KB)
1 |
/*
|
---|---|
2 |
* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
3 |
*
|
4 |
* Copyright (C) 2010 Generalitat Valenciana.
|
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
19 |
*
|
20 |
*/
|
21 |
package org.gvsig.geoprocess.algorithm.dissolve; |
22 |
|
23 |
import java.util.ArrayList; |
24 |
import java.util.HashMap; |
25 |
|
26 |
import es.unex.sextante.additionalInfo.AdditionalInfoNumericalValue; |
27 |
import es.unex.sextante.core.Sextante; |
28 |
import es.unex.sextante.dataObjects.IVectorLayer; |
29 |
import es.unex.sextante.exceptions.GeoAlgorithmExecutionException; |
30 |
import es.unex.sextante.exceptions.RepeatedParameterNameException; |
31 |
import es.unex.sextante.exceptions.UnsupportedOutputChannelException; |
32 |
import es.unex.sextante.outputs.OutputVectorLayer; |
33 |
|
34 |
import org.gvsig.fmap.dal.exception.DataException; |
35 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
36 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
37 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
38 |
import org.gvsig.fmap.dal.feature.FeatureType; |
39 |
import org.gvsig.geoprocess.core.gvVectorLayer; |
40 |
import org.gvsig.geoprocess.sextante.AbstractSextanteGeoProcess; |
41 |
|
42 |
/**
|
43 |
* Dissolve algorithm
|
44 |
* @author <a href="mailto:nachobrodin@gmail.com">Nacho Brodin</a>
|
45 |
*/
|
46 |
public class DissolveAlgorithm extends AbstractSextanteGeoProcess { |
47 |
|
48 |
public static final String RESULT = "RESULT"; |
49 |
public static final String LAYER = "LAYER"; |
50 |
public static final String FIELD = "FIELD"; |
51 |
public static final String FUNCTIONS = "FUNCTIONS"; |
52 |
public static final String SELECTED_GEOM = "SELECTED_GEOM"; |
53 |
public static final String DISSOLV_ADJ = "DISSOLV_ADJ"; |
54 |
public static final String FUNCTION_LIST = "FUNCTION_LIST"; |
55 |
public static final String Summary[] = {"Min", "Max", "Sum", "Avg"}; |
56 |
private boolean funcList[] = new boolean[Summary.length]; |
57 |
private HashMap<String, String> funcMap = new HashMap<String, String>(); |
58 |
|
59 |
/*
|
60 |
* (non-Javadoc)
|
61 |
* @see es.unex.sextante.core.GeoAlgorithm#defineCharacteristics()
|
62 |
*/
|
63 |
public void defineCharacteristics(){ |
64 |
setName(Sextante.getText("Dissolve"));
|
65 |
setGroup(Sextante.getText("gvSIG_Algorithms"));
|
66 |
// setGeneratesUserDefinedRasterOutput(false);
|
67 |
try {
|
68 |
m_Parameters.addInputVectorLayer(LAYER, |
69 |
Sextante.getText( "Input_layer"),
|
70 |
IVectorLayer.SHAPE_TYPE_WRONG, |
71 |
true);
|
72 |
m_Parameters.addBoolean(SELECTED_GEOM, |
73 |
Sextante.getText("Selected_geometries"),
|
74 |
false);
|
75 |
m_Parameters.addBoolean(DISSOLV_ADJ, |
76 |
Sextante.getText("Selected_geometries"),
|
77 |
false);
|
78 |
m_Parameters.addNumericalValue(FIELD, |
79 |
Sextante.getText("Field"),
|
80 |
0,
|
81 |
AdditionalInfoNumericalValue.NUMERICAL_VALUE_INTEGER); |
82 |
m_Parameters.addString(FUNCTION_LIST, Sextante.getText("Function_list"));
|
83 |
addOutputVectorLayer(RESULT, |
84 |
Sextante.getText( "Dissolve"),
|
85 |
OutputVectorLayer.SHAPE_TYPE_POLYGON); |
86 |
} catch (RepeatedParameterNameException e) {
|
87 |
Sextante.addErrorToLog(e); |
88 |
} |
89 |
//setExternalParameters(new DissolveParametersPanel());
|
90 |
} |
91 |
|
92 |
/*
|
93 |
* (non-Javadoc)
|
94 |
* @see es.unex.sextante.core.GeoAlgorithm#processAlgorithm()
|
95 |
*/
|
96 |
public boolean processAlgorithm() throws GeoAlgorithmExecutionException { |
97 |
IVectorLayer layer = m_Parameters.getParameterValueAsVectorLayer(LAYER); |
98 |
int indexField = m_Parameters.getParameterValueAsInt(FIELD);
|
99 |
boolean selectedGeom = m_Parameters.getParameterValueAsBoolean(SELECTED_GEOM);
|
100 |
boolean dissolvAdj = m_Parameters.getParameterValueAsBoolean(DISSOLV_ADJ);
|
101 |
String functionList = m_Parameters.getParameterValueAsString(FUNCTION_LIST);
|
102 |
loadSummary(functionList); |
103 |
|
104 |
FeatureStore storeLayer = null;
|
105 |
if(layer instanceof gvVectorLayer) |
106 |
storeLayer = ((gvVectorLayer)layer).getFeatureStore(); |
107 |
else
|
108 |
return false; |
109 |
|
110 |
FeatureSet features = null;
|
111 |
FeatureType featureType = null;
|
112 |
try {
|
113 |
features = storeLayer.getFeatureSet(); |
114 |
featureType = features.getDefaultFeatureType(); |
115 |
} catch (DataException e) {
|
116 |
Sextante.addErrorToLog(e); |
117 |
return false; |
118 |
} |
119 |
FeatureStore outFeatStore = buildDissolvedOutPutStore(featureType, indexField, layer.getShapeType(), |
120 |
Sextante.getText("Dissolve"), RESULT);
|
121 |
IDissolveRule criteria = null;
|
122 |
if(dissolvAdj)
|
123 |
criteria = new AdjacencyDissolveRule(indexField, funcMap);
|
124 |
else
|
125 |
criteria = new DissolveRule(indexField, funcMap);
|
126 |
|
127 |
try {
|
128 |
DissolveOperation operation = new DissolveOperation(criteria);
|
129 |
operation.setProgressModel(this);
|
130 |
operation.computesGeometryOperation(storeLayer, outFeatStore, attrNames, selectedGeom, true);
|
131 |
} catch (DataException e) {
|
132 |
Sextante.addErrorToLog(e); |
133 |
return false; |
134 |
} |
135 |
return true; |
136 |
} |
137 |
|
138 |
/**
|
139 |
* Checks if the parameter is in Summary list
|
140 |
* @param it
|
141 |
* @return the position in the list
|
142 |
*/
|
143 |
private int isInList(String it) { |
144 |
for (int i = 0; i < Summary.length; i++) { |
145 |
if(Summary[i].compareTo(it) == 0) |
146 |
return i;
|
147 |
} |
148 |
return -1; |
149 |
} |
150 |
|
151 |
/**
|
152 |
* Loads the list of functions to use
|
153 |
* @param functionList
|
154 |
*/
|
155 |
private void loadSummary(String functionList) { |
156 |
String[] attrList = functionList.split(";"); |
157 |
for (int i = 0; i < attrList.length; i++) { |
158 |
String[] func = attrList[i].split(","); |
159 |
for (int j = 1; j < func.length; j++) { |
160 |
int pos = isInList(func[j]);
|
161 |
if(pos != -1) { |
162 |
funcList[pos] = true;
|
163 |
funcMap.put(Summary[pos], func[0]);
|
164 |
} |
165 |
} |
166 |
} |
167 |
} |
168 |
|
169 |
/**
|
170 |
* Builds the output FeatureStore
|
171 |
* @param featureType
|
172 |
* @return FeatureStore
|
173 |
*/
|
174 |
@SuppressWarnings("unchecked") |
175 |
protected FeatureStore buildDissolvedOutPutStore(FeatureType featureType1,
|
176 |
int indexField,
|
177 |
int shapeType,
|
178 |
String sextanteLayerName,
|
179 |
String sextanteLayerLabel) {
|
180 |
ArrayList<Class> typesList = new ArrayList<Class>(); |
181 |
ArrayList<String> attr = new ArrayList<String>(); |
182 |
attr.add("FID");
|
183 |
typesList.add(Integer.class);
|
184 |
FeatureAttributeDescriptor desc = featureType1.getAttributeDescriptor(indexField); |
185 |
attr.add(desc.getName()); |
186 |
typesList.add(desc.getObjectClass()); |
187 |
for (int i = 0; i < funcList.length; i++) |
188 |
if(funcList[i]) {
|
189 |
String fieldName = funcMap.get(Summary[i]);
|
190 |
if(fieldName.length() >= 6) |
191 |
fieldName = fieldName.substring(0, 7); |
192 |
attr.add(fieldName + "_" + Summary[i]);
|
193 |
typesList.add(Double.class);
|
194 |
} |
195 |
|
196 |
attrNames = new String[attr.size()]; |
197 |
attr.toArray(attrNames); |
198 |
Class[] types = new Class[typesList.size()]; |
199 |
typesList.toArray(types); |
200 |
|
201 |
try {
|
202 |
IVectorLayer output = getNewVectorLayer(sextanteLayerLabel, |
203 |
sextanteLayerName, |
204 |
shapeType, types, attrNames); |
205 |
return ((gvVectorLayer)output).getFeatureStore();
|
206 |
} catch (UnsupportedOutputChannelException e) {
|
207 |
Sextante.addErrorToLog(e); |
208 |
} catch (GeoAlgorithmExecutionException e) {
|
209 |
Sextante.addErrorToLog(e); |
210 |
} |
211 |
return null; |
212 |
} |
213 |
} |