gvsig-geoprocess / org.gvsig.geoprocess / trunk / org.gvsig.geoprocess / org.gvsig.geoprocess.algorithm / org.gvsig.geoprocess.algorithm.groupby / src / main / java / org / gvsig / geoprocess / algorithm / groupby / GroupByAlgorithm.java @ 385
History | View | Annotate | Download (18.9 KB)
1 | 357 | nbrodin | /**
|
---|---|---|---|
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.groupby; |
||
25 | |||
26 | import java.util.ArrayList; |
||
27 | 379 | nbrodin | import java.util.Date; |
28 | 357 | nbrodin | import java.util.HashMap; |
29 | import java.util.Iterator; |
||
30 | import java.util.List; |
||
31 | import java.util.Set; |
||
32 | |||
33 | import org.gvsig.fmap.dal.DALLocator; |
||
34 | import org.gvsig.fmap.dal.DataManager; |
||
35 | import org.gvsig.fmap.dal.exception.DataException; |
||
36 | import org.gvsig.fmap.dal.feature.EditableFeature; |
||
37 | import org.gvsig.fmap.dal.feature.Feature; |
||
38 | import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
||
39 | import org.gvsig.fmap.dal.feature.FeatureQuery; |
||
40 | import org.gvsig.fmap.dal.feature.FeatureSet; |
||
41 | import org.gvsig.fmap.dal.feature.FeatureStore; |
||
42 | import org.gvsig.fmap.dal.feature.FeatureType; |
||
43 | import org.gvsig.fmap.geom.Geometry; |
||
44 | import org.gvsig.fmap.geom.Geometry.SUBTYPES; |
||
45 | import org.gvsig.fmap.geom.Geometry.TYPES; |
||
46 | import org.gvsig.fmap.geom.aggregate.MultiCurve; |
||
47 | import org.gvsig.fmap.geom.aggregate.MultiPoint; |
||
48 | 385 | nbrodin | import org.gvsig.fmap.geom.aggregate.MultiPrimitive; |
49 | 357 | nbrodin | import org.gvsig.fmap.geom.aggregate.MultiSurface; |
50 | import org.gvsig.fmap.geom.exception.CreateGeometryException; |
||
51 | import org.gvsig.fmap.geom.primitive.Curve; |
||
52 | import org.gvsig.fmap.geom.primitive.Point; |
||
53 | import org.gvsig.fmap.geom.primitive.Surface; |
||
54 | 362 | nbrodin | import org.gvsig.geoprocess.algorithm.base.util.GeometryUtil; |
55 | import org.gvsig.geoprocess.algorithm.base.util.JTSFacade; |
||
56 | 357 | nbrodin | import org.gvsig.geoprocess.lib.sextante.AbstractSextanteGeoProcess; |
57 | import org.gvsig.geoprocess.lib.sextante.dataObjects.FlyrVectIVectorLayer; |
||
58 | import org.gvsig.timesupport.Instant; |
||
59 | import org.gvsig.tools.dispose.DisposableIterator; |
||
60 | import org.gvsig.tools.evaluator.Evaluator; |
||
61 | |||
62 | import es.unex.sextante.additionalInfo.AdditionalInfoNumericalValue; |
||
63 | import es.unex.sextante.core.Sextante; |
||
64 | import es.unex.sextante.dataObjects.IVectorLayer; |
||
65 | import es.unex.sextante.exceptions.GeoAlgorithmExecutionException; |
||
66 | import es.unex.sextante.exceptions.RepeatedParameterNameException; |
||
67 | import es.unex.sextante.exceptions.UnsupportedOutputChannelException; |
||
68 | import es.unex.sextante.gui.algorithm.GeoAlgorithmParametersPanel; |
||
69 | import es.unex.sextante.outputs.OutputVectorLayer; |
||
70 | |||
71 | /**
|
||
72 | * @author <a href="mailto:nachobrodin@gmail.com">Nacho Brodin</a>
|
||
73 | */
|
||
74 | public class GroupByAlgorithm extends AbstractSextanteGeoProcess { |
||
75 | |||
76 | public static final String RESULT = "RESULT"; |
||
77 | public static final String LAYER = "LAYER"; |
||
78 | public static final String FIELD = "FIELD"; |
||
79 | public static final String FIELD_DATE = "FIELD_DATE"; |
||
80 | public static final String FIELD_LIST = "FIELD_LIST"; |
||
81 | public static final String LAYER_NAME = "LAYER_NAME"; |
||
82 | /**
|
||
83 | * <P>Possible values</P>
|
||
84 | * <UL>
|
||
85 | * <LI>-1: Without geometry</LI>
|
||
86 | * <LI>0: First geometry </LI>
|
||
87 | * <LI>1: Creates a multigeometry </LI>
|
||
88 | 363 | nbrodin | * <LI>2: Fusion of polygons </LI>
|
89 | 357 | nbrodin | * </UL>
|
90 | **/
|
||
91 | public static final String GEOM_OPTION = "GEOM_OPTION"; |
||
92 | public static final String FUNCTION_LIST = "FUNCTION_LIST"; |
||
93 | public static final String Summary[] = {"Min", "Max", "Sum", "Avg", "Last", "Any"}; |
||
94 | |||
95 | /*
|
||
96 | * (non-Javadoc)
|
||
97 | * @see es.unex.sextante.core.GeoAlgorithm#defineCharacteristics()
|
||
98 | */
|
||
99 | public void defineCharacteristics(){ |
||
100 | setName(getTranslation("groupby"));
|
||
101 | setGroup(getTranslation("basic_vect_algorithms"));
|
||
102 | // setGeneratesUserDefinedRasterOutput(false);
|
||
103 | try {
|
||
104 | m_Parameters.addInputVectorLayer(LAYER, getTranslation("Input_layer"), IVectorLayer.SHAPE_TYPE_WRONG, true); |
||
105 | 385 | nbrodin | m_Parameters.addString(FIELD, getTranslation("Field"));
|
106 | m_Parameters.addString(FIELD_DATE, getTranslation("Field_date"));
|
||
107 | 357 | nbrodin | m_Parameters.addString(FIELD_LIST, getTranslation("Field_list"));
|
108 | m_Parameters.addString(FUNCTION_LIST, getTranslation("Function_list"));
|
||
109 | m_Parameters.addString(LAYER_NAME, getTranslation("Layer_name"));
|
||
110 | m_Parameters.addNumericalValue(GEOM_OPTION, getTranslation("Geom"), -1, AdditionalInfoNumericalValue.NUMERICAL_VALUE_INTEGER); |
||
111 | addOutputVectorLayer(RESULT, getTranslation("group_by"), OutputVectorLayer.SHAPE_TYPE_UNDEFINED);
|
||
112 | } catch (RepeatedParameterNameException e) {
|
||
113 | Sextante.addErrorToLog(e); |
||
114 | } |
||
115 | } |
||
116 | |||
117 | /*
|
||
118 | * (non-Javadoc)
|
||
119 | * @see es.unex.sextante.core.GeoAlgorithm#processAlgorithm()
|
||
120 | */
|
||
121 | public boolean processAlgorithm() throws GeoAlgorithmExecutionException { |
||
122 | if(existsOutPutFile(GroupByAlgorithm.RESULT, 0)) { |
||
123 | throw new GeoAlgorithmExecutionException(getTranslation("file_exists")); |
||
124 | } |
||
125 | IVectorLayer layer = m_Parameters.getParameterValueAsVectorLayer(LAYER); |
||
126 | 385 | nbrodin | String groupByField = m_Parameters.getParameterValueAsString(FIELD);
|
127 | String fieldDate = m_Parameters.getParameterValueAsString(FIELD_DATE);
|
||
128 | 357 | nbrodin | String fieldsString = m_Parameters.getParameterValueAsString(FIELD_LIST);
|
129 | String functionsString = m_Parameters.getParameterValueAsString(FUNCTION_LIST);
|
||
130 | String layerName = m_Parameters.getParameterValueAsString(LAYER_NAME);
|
||
131 | int geomOption = m_Parameters.getParameterValueAsInt(GEOM_OPTION);
|
||
132 | 365 | nbrodin | int inputShapeType = layer.getShapeType();
|
133 | 357 | nbrodin | |
134 | String[] funcList = functionsString.split(";"); |
||
135 | 385 | nbrodin | String[] fieldList = fieldsString.split(";"); |
136 | 357 | nbrodin | |
137 | if(fieldList != null && funcList != null) { |
||
138 | if(fieldList.length != funcList.length) {
|
||
139 | Sextante.addErrorToLog("invalid_input_parameters");
|
||
140 | } |
||
141 | } |
||
142 | |||
143 | FeatureStore featureStore = null;
|
||
144 | if(layer instanceof FlyrVectIVectorLayer) |
||
145 | featureStore = ((FlyrVectIVectorLayer)layer).getFeatureStore(); |
||
146 | else
|
||
147 | return false; |
||
148 | |||
149 | try {
|
||
150 | FeatureType featureType = featureStore.getDefaultFeatureType(); |
||
151 | FeatureAttributeDescriptor desc = featureType.getAttributeDescriptor(groupByField); |
||
152 | String fieldNameGrouped = desc.getName();
|
||
153 | FeatureStore outFeatStore = buildOutPutStore(featureType, |
||
154 | layerName, |
||
155 | groupByField, |
||
156 | fieldList, |
||
157 | funcList, |
||
158 | 365 | nbrodin | geomOption, |
159 | inputShapeType); |
||
160 | 357 | nbrodin | |
161 | 379 | nbrodin | Set<String> list = getGroupedValues(featureStore, groupByField); |
162 | 365 | nbrodin | int numberOfFeatures = list.size();
|
163 | if (getStatus() != null) { |
||
164 | getStatus().setRangeOfValues(0, numberOfFeatures);
|
||
165 | } |
||
166 | int cont = 0; |
||
167 | 379 | nbrodin | Iterator<String> it = list.iterator(); |
168 | 357 | nbrodin | while( it.hasNext() ) {
|
169 | 365 | nbrodin | if (getStatus() != null) { |
170 | getStatus().setCurValue(cont); |
||
171 | } |
||
172 | setProgress(cont, numberOfFeatures); |
||
173 | 379 | nbrodin | String groupedValue = it.next();
|
174 | 357 | nbrodin | loadFeature(featureStore, |
175 | outFeatStore, |
||
176 | fieldNameGrouped, |
||
177 | 385 | nbrodin | fieldDate, |
178 | 357 | nbrodin | groupedValue, |
179 | fieldList, |
||
180 | funcList, |
||
181 | geomOption); |
||
182 | 365 | nbrodin | cont++; |
183 | 357 | nbrodin | } |
184 | |||
185 | outFeatStore.finishEditing(); |
||
186 | } catch (DataException e) {
|
||
187 | Sextante.addErrorToLog(e); |
||
188 | } |
||
189 | |||
190 | return true; |
||
191 | } |
||
192 | |||
193 | private void loadFeature(FeatureStore inStore, |
||
194 | FeatureStore outStore, |
||
195 | String fieldNameGrouped,
|
||
196 | 385 | nbrodin | String fieldDate,
|
197 | 379 | nbrodin | String groupedValue,
|
198 | 385 | nbrodin | String[] fieldList, |
199 | 357 | nbrodin | String[] funcList, |
200 | int geomOption) throws DataException { |
||
201 | DataManager manager = DALLocator.getDataManager(); |
||
202 | |||
203 | EditableFeature feat = outStore.createNewFeature(outStore.getDefaultFeatureType(), true);
|
||
204 | |||
205 | FeatureQuery query = inStore.createFeatureQuery(); |
||
206 | Evaluator filter = manager.createExpresion(fieldNameGrouped + " = '" + groupedValue + "'"); |
||
207 | query.setFilter(filter); |
||
208 | FeatureSet featSet = inStore.getFeatureSet(query); |
||
209 | 379 | nbrodin | DisposableIterator it = featSet.iterator(); |
210 | 357 | nbrodin | List<Geometry> geomList = new ArrayList<Geometry>(); |
211 | |||
212 | 379 | nbrodin | Object[] values = null; |
213 | 357 | nbrodin | if(fieldList != null) |
214 | 379 | nbrodin | values = new Object[fieldList.length]; |
215 | int nFeaturesByGroup = 0; |
||
216 | 357 | nbrodin | Instant lastDate = null;
|
217 | |||
218 | while(it.hasNext()) {
|
||
219 | Feature feature = (Feature)it.next(); |
||
220 | |||
221 | if(fieldList != null) { |
||
222 | for (int i = 0; i < fieldList.length; i++) { |
||
223 | Object v = feature.get(fieldList[i]);
|
||
224 | //Class<?> c = inStore.getDefaultFeatureType().getAttributeDescriptor(funcList[i]).getClassOfValue();
|
||
225 | 380 | nbrodin | if(v instanceof Number) { |
226 | if(funcList[i].compareToIgnoreCase("Min") == 0) { |
||
227 | if(values[i] == null) |
||
228 | values[i] = Double.POSITIVE_INFINITY;
|
||
229 | values[i] = updateMinimum((Double)values[i], v);
|
||
230 | } |
||
231 | if(funcList[i].compareToIgnoreCase("Max") == 0) { |
||
232 | if(values[i] == null) |
||
233 | values[i] = Double.NEGATIVE_INFINITY;
|
||
234 | values[i] = updateMaximum((Double)values[i], v);
|
||
235 | } |
||
236 | if(funcList[i].compareToIgnoreCase("Sum") == 0 || funcList[i].compareToIgnoreCase("Average") == 0) { |
||
237 | if(values[i] == null) |
||
238 | values[i] = new Double(0D); |
||
239 | values[i] = sum((Double)values[i], v);
|
||
240 | } |
||
241 | 357 | nbrodin | } |
242 | 379 | nbrodin | if(funcList[i].compareToIgnoreCase("First") == 0) { |
243 | 357 | nbrodin | if(values[i] == null) |
244 | 379 | nbrodin | values[i] = v; |
245 | 357 | nbrodin | } |
246 | 385 | nbrodin | if(funcList[i].compareToIgnoreCase("Last") == 0 && fieldDate != null) { |
247 | 357 | nbrodin | if(lastDate == null || |
248 | values[i] == null ||
|
||
249 | 385 | nbrodin | lastDate.isAfter(feature.getInstant(fieldDate))) { |
250 | lastDate = feature.getInstant(fieldDate); |
||
251 | values[i] = v; |
||
252 | 357 | nbrodin | } |
253 | } |
||
254 | } |
||
255 | } |
||
256 | |||
257 | if(geomOption >= 0) { |
||
258 | switch (geomOption) {
|
||
259 | 362 | nbrodin | case 0: //Adds only the first geometry |
260 | 357 | nbrodin | if(nFeaturesByGroup == 0) { //If is the first feature |
261 | 385 | nbrodin | Geometry g = feature.getDefaultGeometry(); |
262 | if(!(g instanceof MultiPrimitive)) { |
||
263 | List<Geometry> gList = new ArrayList<Geometry>(); |
||
264 | gList.add(g); |
||
265 | g = buildMultiGeometry(gList); |
||
266 | } |
||
267 | feat.set(outStore.getDefaultFeatureType().getDefaultGeometryAttributeName(), g); |
||
268 | 357 | nbrodin | } |
269 | break;
|
||
270 | 362 | nbrodin | case 1: //Multigeometry with all geometries |
271 | case 2: //Union among all geometries |
||
272 | geomList.add(feature.getDefaultGeometry().cloneGeometry()); |
||
273 | 357 | nbrodin | break;
|
274 | } |
||
275 | } |
||
276 | nFeaturesByGroup ++; |
||
277 | } |
||
278 | |||
279 | it.dispose(); |
||
280 | featSet.dispose(); |
||
281 | feat.set(fieldNameGrouped, groupedValue); |
||
282 | |||
283 | //Se asignan los campos calculados
|
||
284 | if(fieldList != null) { |
||
285 | for (int i = 0; i < values.length; i++) { |
||
286 | 379 | nbrodin | if(funcList[i].compareToIgnoreCase("Average") == 0) { |
287 | values[i] = ((Double)values[i]).doubleValue() / nFeaturesByGroup;
|
||
288 | 357 | nbrodin | } |
289 | feat.set(i + 1, values[i]);
|
||
290 | } |
||
291 | } |
||
292 | |||
293 | //Si hay que convertir a multigeometr?a
|
||
294 | 362 | nbrodin | if(geomOption == 1 && geomList.size() > 0) |
295 | 357 | nbrodin | feat.set(outStore.getDefaultFeatureType().getDefaultGeometryAttributeName(), buildMultiGeometry(geomList)); |
296 | 362 | nbrodin | if(geomOption == 2 && geomList.size() > 0) |
297 | feat.set(outStore.getDefaultFeatureType().getDefaultGeometryAttributeName(), computesUnion(geomList)); |
||
298 | 357 | nbrodin | outStore.update(feat); |
299 | } |
||
300 | 362 | nbrodin | |
301 | /**
|
||
302 | * Builds a unique geometry with all geometries in the list
|
||
303 | * @param geomList
|
||
304 | * @return
|
||
305 | */
|
||
306 | 357 | nbrodin | private Geometry buildMultiGeometry(List<Geometry> geomList) { |
307 | 362 | nbrodin | Geometry newGeom = null;
|
308 | for (int i = 0; i < geomList.size(); i++) { |
||
309 | if(i == 0) { |
||
310 | try {
|
||
311 | if(geomList.get(0).getType() == TYPES.POINT || geomList.get(0).getType() == TYPES.MULTIPOINT) |
||
312 | newGeom = (MultiPoint)geomManager.create(TYPES.MULTIPOINT, SUBTYPES.GEOM2D); |
||
313 | if(geomList.get(0).getType() == TYPES.CURVE || geomList.get(0).getType() == TYPES.MULTICURVE) |
||
314 | newGeom = (MultiCurve)geomManager.create(TYPES.MULTICURVE, SUBTYPES.GEOM2D); |
||
315 | if(geomList.get(0).getType() == TYPES.SURFACE || geomList.get(0).getType() == TYPES.MULTISURFACE) |
||
316 | newGeom = (MultiSurface)geomManager.create(TYPES.MULTISURFACE, SUBTYPES.GEOM2D); |
||
317 | } catch (CreateGeometryException e) {
|
||
318 | } |
||
319 | 357 | nbrodin | } |
320 | |||
321 | 362 | nbrodin | if(geomList.get(i) instanceof Surface) { |
322 | ((MultiSurface)newGeom).addPrimitive((Surface)geomList.get(i)); |
||
323 | } |
||
324 | if(geomList.get(i) instanceof MultiSurface) { |
||
325 | int nPrimitives = ((MultiSurface)geomList.get(i)).getPrimitivesNumber();
|
||
326 | for (int iPrimitive = 0; iPrimitive < nPrimitives; iPrimitive++) { |
||
327 | ((MultiSurface)newGeom).addPrimitive(((MultiSurface)geomList.get(i)).getSurfaceAt(iPrimitive)); |
||
328 | 357 | nbrodin | } |
329 | } |
||
330 | 362 | nbrodin | if(geomList.get(i) instanceof Curve) { |
331 | ((MultiCurve)newGeom).addPrimitive((Curve)geomList.get(i)); |
||
332 | } |
||
333 | if(geomList.get(i) instanceof MultiCurve) { |
||
334 | int nPrimitives = ((MultiSurface)geomList.get(i)).getPrimitivesNumber();
|
||
335 | for (int iPrimitive = 0; iPrimitive < nPrimitives; iPrimitive++) { |
||
336 | ((MultiCurve)newGeom).addPrimitive(((MultiCurve)geomList.get(i)).getCurveAt(iPrimitive)); |
||
337 | 357 | nbrodin | } |
338 | } |
||
339 | 362 | nbrodin | if(geomList.get(i) instanceof Point) { |
340 | ((MultiPoint)newGeom).addPrimitive((Point)geomList.get(i));
|
||
341 | } |
||
342 | if(geomList.get(i) instanceof MultiPoint) { |
||
343 | int nPrimitives = ((MultiPoint)geomList.get(i)).getPrimitivesNumber();
|
||
344 | for (int iPrimitive = 0; iPrimitive < nPrimitives; iPrimitive++) { |
||
345 | ((MultiPoint)newGeom).addPrimitive(((MultiPoint)geomList.get(i)).getPointAt(iPrimitive)); |
||
346 | 357 | nbrodin | } |
347 | } |
||
348 | |||
349 | } |
||
350 | |||
351 | 362 | nbrodin | return newGeom;
|
352 | } |
||
353 | 357 | nbrodin | |
354 | 362 | nbrodin | /**
|
355 | * Computes the union of gemetries in the list
|
||
356 | * @param listInput
|
||
357 | * @return
|
||
358 | */
|
||
359 | private Geometry computesUnion(List<Geometry> listInput) { |
||
360 | List<com.vividsolutions.jts.geom.Geometry> listResult = new ArrayList<com.vividsolutions.jts.geom.Geometry>(); |
||
361 | for (int i = listInput.size() - 1; i >= 0; i--) { |
||
362 | listResult.add(GeometryUtil.geomToJTS(listInput.get(i))); |
||
363 | listInput.remove(i); |
||
364 | 357 | nbrodin | } |
365 | 362 | nbrodin | com.vividsolutions.jts.geom.Geometry newGeom = null;
|
366 | while(listResult.size() > 1) { |
||
367 | List<com.vividsolutions.jts.geom.Geometry> list = new ArrayList<com.vividsolutions.jts.geom.Geometry>(); |
||
368 | for (int i = 0; i < listResult.size(); i = i + 2) { |
||
369 | if(i == (listResult.size() - 1)) |
||
370 | list.add(listResult.get(i)); |
||
371 | else {
|
||
372 | newGeom = JTSFacade.union(listResult.get(i), listResult.get(i + 1));
|
||
373 | list.add(newGeom); |
||
374 | } |
||
375 | 357 | nbrodin | } |
376 | 362 | nbrodin | listResult = list; |
377 | 357 | nbrodin | } |
378 | 362 | nbrodin | return GeometryUtil.jtsToGeom(newGeom);
|
379 | 357 | nbrodin | } |
380 | |||
381 | private Double updateAny(Double min, Object value) { |
||
382 | if(value instanceof Double) |
||
383 | return (Double)value; |
||
384 | else if(value instanceof Integer) |
||
385 | return ((Integer)value).doubleValue(); |
||
386 | return min;
|
||
387 | } |
||
388 | |||
389 | private Double updateMinimum(Double min, Object value) { |
||
390 | if(value instanceof Double && ((Double)value).doubleValue() < min) |
||
391 | return (Double)value; |
||
392 | else if(value instanceof Integer && ((Integer)value).intValue() < min) |
||
393 | return ((Integer)value).doubleValue(); |
||
394 | return min;
|
||
395 | } |
||
396 | |||
397 | private Double updateMaximum(Double max, Object value) { |
||
398 | if(value instanceof Double && ((Double)value).doubleValue() > max) |
||
399 | return (Double)value; |
||
400 | else if(value instanceof Integer && ((Integer)value).intValue() > max) |
||
401 | return ((Integer)value).doubleValue(); |
||
402 | return max;
|
||
403 | } |
||
404 | |||
405 | private Double sum(Double current, Object value) { |
||
406 | if(value instanceof Double) |
||
407 | return ((Double)value) + current; |
||
408 | else if(value instanceof Integer) |
||
409 | return ((Integer)value) + current; |
||
410 | return current;
|
||
411 | } |
||
412 | |||
413 | /**
|
||
414 | * Gets the list of grouped values
|
||
415 | * @param featureStore
|
||
416 | * @param groupByField
|
||
417 | * @return
|
||
418 | * @throws DataException
|
||
419 | */
|
||
420 | 385 | nbrodin | private Set<String> getGroupedValues(FeatureStore featureStore, String groupByField) throws DataException { |
421 | 379 | nbrodin | HashMap<String, String> valueGrouped = new HashMap<String, String>(); |
422 | 357 | nbrodin | FeatureSet featureSet = featureStore.getFeatureSet(); |
423 | 379 | nbrodin | DisposableIterator it = featureSet.iterator(); |
424 | 357 | nbrodin | while( it.hasNext() ) {
|
425 | Feature feature = (Feature)it.next(); |
||
426 | Object obj = feature.get(groupByField);
|
||
427 | 379 | nbrodin | String s = null; |
428 | if(obj instanceof String) |
||
429 | s = new String(((String)obj)); |
||
430 | else if(obj instanceof Double) |
||
431 | s = String.valueOf((Double)obj); |
||
432 | else if(obj instanceof Integer) |
||
433 | s = String.valueOf((Integer)obj); |
||
434 | else if(obj instanceof Float) |
||
435 | s = String.valueOf((Float)obj); |
||
436 | else if(obj instanceof Boolean) |
||
437 | s = String.valueOf((Boolean)obj); |
||
438 | else if(obj instanceof Long) |
||
439 | s = String.valueOf((Long)obj); |
||
440 | else if(obj instanceof Byte) |
||
441 | s = String.valueOf((Byte)obj); |
||
442 | else if(obj instanceof Date) |
||
443 | s = ((Date)obj).toString();
|
||
444 | if(valueGrouped.get(s) == null) { |
||
445 | valueGrouped.put(s, s); |
||
446 | 357 | nbrodin | } |
447 | } |
||
448 | it.dispose(); |
||
449 | featureSet.dispose(); |
||
450 | return valueGrouped.keySet();
|
||
451 | } |
||
452 | |||
453 | /**
|
||
454 | * Builds the output FeatureStore
|
||
455 | *
|
||
456 | * @param featureType
|
||
457 | * @return FeatureStore
|
||
458 | */
|
||
459 | private FeatureStore buildOutPutStore(FeatureType featureType,
|
||
460 | String sextanteLayerName,
|
||
461 | 385 | nbrodin | String groupByField,
|
462 | String[] fieldList, |
||
463 | 357 | nbrodin | String[] funcList, |
464 | 365 | nbrodin | int geomOption,
|
465 | int inputShapeType) {
|
||
466 | 357 | nbrodin | int len = fieldList != null ? fieldList.length + 2 : 2; |
467 | |||
468 | Class<?>[] types = new Class[len]; |
||
469 | attrNames = new String[len]; |
||
470 | |||
471 | FeatureAttributeDescriptor desc = featureType.getAttributeDescriptor(groupByField); |
||
472 | attrNames[0] = desc.getName();
|
||
473 | types[0] = desc.getObjectClass();
|
||
474 | |||
475 | if(fieldList != null) { |
||
476 | for (int i = 1; i < attrNames.length - 1; i++) { |
||
477 | desc = featureType.getAttributeDescriptor(fieldList[i - 1]);
|
||
478 | attrNames[i] = desc.getName(); |
||
479 | if(attrNames[i].length() >= 6) |
||
480 | attrNames[i] = attrNames[i].substring(0, 5); |
||
481 | attrNames[i] += "_" + funcList[i - 1]; |
||
482 | types[i] = desc.getObjectClass(); |
||
483 | } |
||
484 | } |
||
485 | |||
486 | 363 | nbrodin | attrNames[attrNames.length - 1] = featureType.getDefaultGeometryAttributeName();
|
487 | types[types.length - 1] = featureType.getDefaultGeometryAttribute().getObjectClass();
|
||
488 | 357 | nbrodin | |
489 | try {
|
||
490 | 363 | nbrodin | IVectorLayer output = getNewVectorLayer(RESULT, sextanteLayerName, |
491 | 365 | nbrodin | inputShapeType, types, attrNames); |
492 | 357 | nbrodin | return ((FlyrVectIVectorLayer) output).getFeatureStore();
|
493 | } catch (UnsupportedOutputChannelException e) {
|
||
494 | Sextante.addErrorToLog(e); |
||
495 | } catch (GeoAlgorithmExecutionException e) {
|
||
496 | Sextante.addErrorToLog(e); |
||
497 | } |
||
498 | return null; |
||
499 | } |
||
500 | |||
501 | /**
|
||
502 | * Gets the list of fields index to add to the table.
|
||
503 | * @param fieldsString
|
||
504 | * @return
|
||
505 | */
|
||
506 | private int[] getFieldList(String fieldsString) { |
||
507 | String[] list = fieldsString.split(";"); |
||
508 | int[] l = new int[list.length]; |
||
509 | for (int i = 0; i < list.length; i++) { |
||
510 | try {
|
||
511 | l[i] = new Integer(list[i]); |
||
512 | } catch (NumberFormatException e) { |
||
513 | return null; |
||
514 | } |
||
515 | } |
||
516 | return l;
|
||
517 | } |
||
518 | |||
519 | @Override
|
||
520 | public Class<? extends GeoAlgorithmParametersPanel> getCustomParametersPanelClass() { |
||
521 | return GroupByParametersPanel.class;
|
||
522 | } |
||
523 | |||
524 | } |