Statistics
| Revision:

root / org.gvsig.toolbox / trunk / org.gvsig.toolbox / org.gvsig.toolbox.algorithm / src / main / java / es / unex / sextante / vectorTools / addEventTheme / AddEventThemeAlgorithm.java @ 59

History | View | Annotate | Download (3.45 KB)

1
package es.unex.sextante.vectorTools.addEventTheme;
2

    
3
import com.vividsolutions.jts.geom.Coordinate;
4
import com.vividsolutions.jts.geom.GeometryFactory;
5
import com.vividsolutions.jts.geom.Point;
6

    
7
import es.unex.sextante.core.GeoAlgorithm;
8
import es.unex.sextante.core.Sextante;
9
import es.unex.sextante.dataObjects.IRecord;
10
import es.unex.sextante.dataObjects.IRecordsetIterator;
11
import es.unex.sextante.dataObjects.ITable;
12
import es.unex.sextante.dataObjects.IVectorLayer;
13
import es.unex.sextante.exceptions.GeoAlgorithmExecutionException;
14
import es.unex.sextante.exceptions.OptionalParentParameterException;
15
import es.unex.sextante.exceptions.RepeatedParameterNameException;
16
import es.unex.sextante.exceptions.UndefinedParentParameterNameException;
17
import es.unex.sextante.outputs.OutputVectorLayer;
18

    
19
public class AddEventThemeAlgorithm
20
         extends
21
            GeoAlgorithm {
22

    
23
   public static final String TABLE  = "TABLE";
24
   public static final String XFIELD = "XFIELD";
25
   public static final String RESULT = "RESULT";
26
   public static final String YFIELD = "YFIELD";
27

    
28

    
29
   @Override
30
   public boolean processAlgorithm() throws GeoAlgorithmExecutionException {
31

    
32
      int j;
33
      int iFieldX, iFieldY;
34
      ITable table;
35
      String[] sFields;
36
      Class[] types;
37

    
38
      table = m_Parameters.getParameterValueAsTable(TABLE);
39
      iFieldX = m_Parameters.getParameterValueAsInt(XFIELD);
40
      iFieldY = m_Parameters.getParameterValueAsInt(YFIELD);
41

    
42
      sFields = new String[table.getFieldCount()];
43
      types = new Class[table.getFieldCount()];
44

    
45
      for (j = 0; j < table.getFieldCount(); j++) {
46
         sFields[j] = table.getFieldName(j);
47
         types[j] = table.getFieldType(j);
48
      }
49

    
50
      final IVectorLayer output = getNewVectorLayer(RESULT, Sextante.getText("Layer_from_table"), IVectorLayer.SHAPE_TYPE_POINT,
51
               types, sFields);
52
      final IRecordsetIterator iter = table.iterator();
53
      final GeometryFactory geomFac = new GeometryFactory();
54
      while (iter.hasNext()) {
55
         try {
56
            final IRecord record = iter.next();
57
            final String sX = record.getValue(iFieldX).toString();
58
            final String sY = record.getValue(iFieldY).toString();
59
            final double dX = Double.parseDouble(sX);
60
            final double dY = Double.parseDouble(sY);
61
            final Point pt = geomFac.createPoint(new Coordinate(dX, dY));
62
            output.addFeature(pt, record.getValues());
63
         }
64
         catch (final NumberFormatException e) {}
65
      }
66
      iter.close();
67

    
68
      return !m_Task.isCanceled();
69

    
70
   }
71

    
72

    
73
   @Override
74
   public void defineCharacteristics() {
75

    
76
      this.setName(Sextante.getText("Points_layer_from_table"));
77
      setGroup(Sextante.getText("Tools_for_point_layers"));
78

    
79
      try {
80
         m_Parameters.addInputTable(TABLE, Sextante.getText("Table"), true);
81
         m_Parameters.addTableField(XFIELD, Sextante.getText("X"), TABLE);
82
         m_Parameters.addTableField(YFIELD, Sextante.getText("Y"), TABLE);
83
         addOutputVectorLayer(RESULT, Sextante.getText("Result"), OutputVectorLayer.SHAPE_TYPE_POINT);
84
      }
85
      catch (final RepeatedParameterNameException e) {
86
         Sextante.addErrorToLog(e);
87
      }
88
      catch (final UndefinedParentParameterNameException e) {
89
         Sextante.addErrorToLog(e);
90
      }
91
      catch (final OptionalParentParameterException e) {
92
         Sextante.addErrorToLog(e);
93
      }
94

    
95
   }
96

    
97
}