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