root / org.gvsig.toolbox / trunk / org.gvsig.toolbox / org.gvsig.toolbox.algorithm / src / main / java / es / unex / sextante / tridimensional / line3DFromTable / Line3DFromTableAlgorithm.java @ 59
History | View | Annotate | Download (4.04 KB)
1 | 59 | nbrodin | |
---|---|---|---|
2 | |||
3 | package es.unex.sextante.tridimensional.line3DFromTable; |
||
4 | |||
5 | import java.util.ArrayList; |
||
6 | |||
7 | import com.vividsolutions.jts.geom.Coordinate; |
||
8 | import com.vividsolutions.jts.geom.GeometryFactory; |
||
9 | import com.vividsolutions.jts.geom.LineString; |
||
10 | |||
11 | import es.unex.sextante.core.GeoAlgorithm; |
||
12 | import es.unex.sextante.core.Sextante; |
||
13 | import es.unex.sextante.dataObjects.IRecord; |
||
14 | import es.unex.sextante.dataObjects.IRecordsetIterator; |
||
15 | import es.unex.sextante.dataObjects.ITable; |
||
16 | import es.unex.sextante.dataObjects.IVectorLayer; |
||
17 | import es.unex.sextante.exceptions.GeoAlgorithmExecutionException; |
||
18 | import es.unex.sextante.exceptions.OptionalParentParameterException; |
||
19 | import es.unex.sextante.exceptions.RepeatedParameterNameException; |
||
20 | import es.unex.sextante.exceptions.UndefinedParentParameterNameException; |
||
21 | import es.unex.sextante.outputs.OutputVectorLayer; |
||
22 | |||
23 | |||
24 | public class Line3DFromTableAlgorithm |
||
25 | extends
|
||
26 | GeoAlgorithm { |
||
27 | |||
28 | public static final String TABLE = "TABLE"; |
||
29 | public static final String XFIELD = "XFIELD"; |
||
30 | public static final String YFIELD = "YFIELD"; |
||
31 | public static final String ZFIELD = "ZFIELD"; |
||
32 | public static final String RESULT = "RESULT"; |
||
33 | |||
34 | |||
35 | @Override
|
||
36 | public boolean processAlgorithm() throws GeoAlgorithmExecutionException { |
||
37 | |||
38 | final int j; |
||
39 | int iFieldX, iFieldY, iFieldZ;
|
||
40 | ITable table; |
||
41 | String[] sFields; |
||
42 | Class[] types; |
||
43 | |||
44 | table = m_Parameters.getParameterValueAsTable(TABLE); |
||
45 | iFieldX = m_Parameters.getParameterValueAsInt(XFIELD); |
||
46 | iFieldY = m_Parameters.getParameterValueAsInt(YFIELD); |
||
47 | iFieldZ = m_Parameters.getParameterValueAsInt(ZFIELD); |
||
48 | |||
49 | sFields = new String[] { "ID" }; |
||
50 | types = new Class[] { Integer.class }; |
||
51 | |||
52 | final IVectorLayer output = getNewVectorLayer(RESULT, Sextante.getText("3DLineFromTable"), IVectorLayer.SHAPE_TYPE_LINE, |
||
53 | types, sFields); |
||
54 | final ArrayList<Coordinate> coordsArray = new ArrayList<Coordinate>(); |
||
55 | final IRecordsetIterator iter = table.iterator();
|
||
56 | final GeometryFactory geomFac = new GeometryFactory(); |
||
57 | while (iter.hasNext()) {
|
||
58 | try {
|
||
59 | final IRecord record = iter.next();
|
||
60 | final String sX = record.getValue(iFieldX).toString(); |
||
61 | final String sY = record.getValue(iFieldY).toString(); |
||
62 | final String sZ = record.getValue(iFieldZ).toString(); |
||
63 | final double dX = Double.parseDouble(sX); |
||
64 | final double dY = Double.parseDouble(sY); |
||
65 | final double dZ = Double.parseDouble(sZ); |
||
66 | coordsArray.add(new Coordinate(dX, dY, dZ));
|
||
67 | } |
||
68 | catch (final NumberFormatException e) { |
||
69 | } |
||
70 | } |
||
71 | iter.close(); |
||
72 | |||
73 | if (coordsArray.size() > 1) { |
||
74 | final LineString line = geomFac.createLineString(coordsArray.toArray(new Coordinate[0])); |
||
75 | output.addFeature(line, new Object[] { new Integer(1) }); |
||
76 | } |
||
77 | else {
|
||
78 | throw new GeoAlgorithmExecutionException(Sextante.getText("LessThanTwoPointsInTable")); |
||
79 | } |
||
80 | |||
81 | return !m_Task.isCanceled();
|
||
82 | |||
83 | } |
||
84 | |||
85 | |||
86 | @Override
|
||
87 | public void defineCharacteristics() { |
||
88 | |||
89 | this.setName(Sextante.getText("3DLineFromTable")); |
||
90 | setGroup(Sextante.getText("3D"));
|
||
91 | |||
92 | try {
|
||
93 | m_Parameters.addInputTable(TABLE, Sextante.getText("Table"), true); |
||
94 | m_Parameters.addTableField(XFIELD, Sextante.getText("X"), TABLE);
|
||
95 | m_Parameters.addTableField(YFIELD, Sextante.getText("Y"), TABLE);
|
||
96 | m_Parameters.addTableField(ZFIELD, Sextante.getText("Z"), TABLE);
|
||
97 | addOutputVectorLayer(RESULT, Sextante.getText("Result"), OutputVectorLayer.SHAPE_TYPE_LINE);
|
||
98 | } |
||
99 | catch (final RepeatedParameterNameException e) { |
||
100 | Sextante.addErrorToLog(e); |
||
101 | } |
||
102 | catch (final UndefinedParentParameterNameException e) { |
||
103 | Sextante.addErrorToLog(e); |
||
104 | } |
||
105 | catch (final OptionalParentParameterException e) { |
||
106 | Sextante.addErrorToLog(e); |
||
107 | } |
||
108 | |||
109 | } |
||
110 | |||
111 | |||
112 | @Override
|
||
113 | public boolean isActive() { |
||
114 | |||
115 | return false; |
||
116 | |||
117 | } |
||
118 | |||
119 | |||
120 | } |