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