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