Statistics
| Revision:

root / org.gvsig.toolbox / trunk / org.gvsig.toolbox / org.gvsig.toolbox.algorithm / src / main / java / es / unex / sextante / tables / vectorHistogram / VectorHistogramAlgorithm.java @ 59

History | View | Annotate | Download (3.86 KB)

1

    
2

    
3
package es.unex.sextante.tables.vectorHistogram;
4

    
5
import java.util.ArrayList;
6

    
7
import org.jfree.chart.ChartFactory;
8
import org.jfree.chart.ChartPanel;
9
import org.jfree.chart.JFreeChart;
10
import org.jfree.chart.plot.PlotOrientation;
11
import org.jfree.data.statistics.HistogramDataset;
12

    
13
import es.unex.sextante.additionalInfo.AdditionalInfoVectorLayer;
14
import es.unex.sextante.core.GeoAlgorithm;
15
import es.unex.sextante.core.Sextante;
16
import es.unex.sextante.dataObjects.IFeature;
17
import es.unex.sextante.dataObjects.IFeatureIterator;
18
import es.unex.sextante.dataObjects.IVectorLayer;
19
import es.unex.sextante.dataObjects.vectorFilters.BoundingBoxFilter;
20
import es.unex.sextante.exceptions.GeoAlgorithmExecutionException;
21
import es.unex.sextante.exceptions.OptionalParentParameterException;
22
import es.unex.sextante.exceptions.RepeatedParameterNameException;
23
import es.unex.sextante.exceptions.UndefinedParentParameterNameException;
24

    
25

    
26
public class VectorHistogramAlgorithm
27
         extends
28
            GeoAlgorithm {
29

    
30
   public static final String HISTOGRAM = "HISTOGRAM";
31
   public static final String FIELD     = "FIELD";
32
   public static final String LAYER     = "LAYER";
33

    
34

    
35
   @Override
36
   public boolean processAlgorithm() throws GeoAlgorithmExecutionException {
37

    
38
      int i;
39
      int iField;
40
      int iShapesCount;
41
      double dValue;
42
      IVectorLayer layer;
43

    
44
      layer = m_Parameters.getParameterValueAsVectorLayer(LAYER);
45
      if (!m_bIsAutoExtent) {
46
         layer.addFilter(new BoundingBoxFilter(m_AnalysisExtent));
47
      }
48
      iField = m_Parameters.getParameterValueAsInt(FIELD);
49

    
50
      i = 0;
51
      final ArrayList<Double> array = new ArrayList<Double>();
52
      iShapesCount = layer.getShapesCount();
53
      final IFeatureIterator iter = layer.iterator();
54
      while (iter.hasNext() && setProgress(i, iShapesCount)) {
55
         final IFeature feature = iter.next();
56
         try {
57
            dValue = Double.parseDouble(feature.getRecord().getValue(iField).toString());
58
            array.add(new Double(dValue));
59
         }
60
         catch (final Exception e) {
61
         }
62
         i++;
63
      }
64
      iter.close();
65

    
66
      if (m_Task.isCanceled()) {
67
         return false;
68
      }
69

    
70
      else {
71
         final HistogramDataset dataset = new HistogramDataset();
72
         final double[] values = new double[array.size()];
73
         for (int j = 0; j < array.size(); j++) {
74
            values[j] = array.get(j).doubleValue();
75
         }
76
         dataset.addSeries("Histogram", values, 100);
77
         final JFreeChart chart = ChartFactory.createHistogram("", null, null, dataset, PlotOrientation.VERTICAL, true, false,
78
                  false);
79
         final ChartPanel jPanelChart = new ChartPanel(chart);
80
         jPanelChart.setMouseZoomable(true, true);
81
         jPanelChart.setPreferredSize(new java.awt.Dimension(500, 300));
82
         jPanelChart.setPreferredSize(new java.awt.Dimension(500, 300));
83
         jPanelChart.setBorder(javax.swing.BorderFactory.createLineBorder(java.awt.Color.gray, 1));
84

    
85
         addOutputChart(HISTOGRAM, Sextante.getText("Histogram"), jPanelChart);
86
         return true;
87
      }
88

    
89
   }
90

    
91

    
92
   @Override
93
   public void defineCharacteristics() {
94

    
95
      setName(Sextante.getText("Histogram"));
96
      setGroup(Sextante.getText("Tools_for_vector_layers"));
97
      setUserCanDefineAnalysisExtent(true);
98
      try {
99
         m_Parameters.addInputVectorLayer(LAYER, Sextante.getText("Layer"), AdditionalInfoVectorLayer.SHAPE_TYPE_ANY, true);
100
         m_Parameters.addTableField(FIELD, Sextante.getText("Field"), "LAYER");
101
         addOutputChart(HISTOGRAM, Sextante.getText("Histogram"));
102
      }
103
      catch (final RepeatedParameterNameException e) {
104
         Sextante.addErrorToLog(e);
105
      }
106
      catch (final UndefinedParentParameterNameException e) {
107
         Sextante.addErrorToLog(e);
108
      }
109
      catch (final OptionalParentParameterException e) {
110
         Sextante.addErrorToLog(e);
111
      }
112

    
113
   }
114

    
115
}