Statistics
| Revision:

root / org.gvsig.toolbox / trunk / org.gvsig.toolbox / org.gvsig.toolbox.algorithm / src / main / java / es / unex / sextante / tin / linearIsolinesFromTin / LinearIsolinesFromTinAlgorithm.java @ 59

History | View | Annotate | Download (7.68 KB)

1
/**
2
 * @author Josef Bezdek, ZCU Plzen
3
 * @version 1.0
4
 * @since JDK1.5
5
 */
6

    
7

    
8
//TODO: This currently returns an empty result.
9
//TODO: Before extracting the isolines, it attempts a Bezier interpolation.
10
//TODO: So I assume the problem(s) here to be identical with smoothTinBezier/SmoothTinBezierAlgorithm.java.
11
//TODO: If we fix the former, the same fixes should be applied here!
12
package es.unex.sextante.tin.linearIsolinesFromTin;
13

    
14
import java.util.ArrayList;
15
import java.util.Iterator;
16
import java.util.LinkedList;
17
import java.util.TreeMap;
18

    
19
import com.vividsolutions.jts.geom.Coordinate;
20
import com.vividsolutions.jts.geom.GeometryFactory;
21
import com.vividsolutions.jts.geom.LineString;
22
import com.vividsolutions.jts.index.strtree.STRtree;
23

    
24
import es.unex.sextante.additionalInfo.AdditionalInfoNumericalValue;
25
import es.unex.sextante.additionalInfo.AdditionalInfoVectorLayer;
26
import es.unex.sextante.core.GeoAlgorithm;
27
import es.unex.sextante.core.Sextante;
28
import es.unex.sextante.dataObjects.IFeature;
29
import es.unex.sextante.dataObjects.IFeatureIterator;
30
import es.unex.sextante.dataObjects.IRecord;
31
import es.unex.sextante.dataObjects.IVectorLayer;
32
import es.unex.sextante.exceptions.GeoAlgorithmExecutionException;
33
import es.unex.sextante.outputs.OutputVectorLayer;
34
import es.unex.sextante.tin.smoothTinBezier.Bezier;
35
import es.unex.sextante.tin.smoothTinBezier.BezierSurface;
36

    
37
public class LinearIsolinesFromTinAlgorithm
38
         extends
39
            GeoAlgorithm {
40

    
41
   public static final String TIN          = "TIN";
42
   public static final String ISOLINES     = "ISOLINES";
43
   public static final String EQUIDISTANCE = "EQUIDISTANCE";
44
   public static final String LoD          = "LoD";
45
   public static final String ClusterTol   = "ClusterTol";
46
   public static final String Smooth       = "Smooth";
47

    
48
   private IVectorLayer       m_Triangles;
49
   private IVectorLayer       m_Isolines;
50
   private double             m_EquiDistance;
51
   private int                m_LoD;
52
   private double             m_ClusterTol;
53
   private double             m_Smooth;
54

    
55
   private STRtree            trianglesIndex;
56
   Coordinate[][]             triangles;
57
   TreeMap                    breakLines   = new TreeMap();
58
   Bezier                     miniBezierTriangles[];
59
   double                     scaleZ;
60

    
61

    
62
   @Override
63
   public void defineCharacteristics() {
64

    
65
      setName(Sextante.getText("Extract_isolines"));
66
      setGroup(Sextante.getText("TIN"));
67
      setUserCanDefineAnalysisExtent(false);
68
      final String[] sDistance = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
69

    
70
      try {
71
         m_Parameters.addInputVectorLayer(TIN, Sextante.getText("TIN"), AdditionalInfoVectorLayer.SHAPE_TYPE_POLYGON, true);
72

    
73
         m_Parameters.addNumericalValue(EQUIDISTANCE, Sextante.getText("Equidistance"),
74
                  AdditionalInfoNumericalValue.NUMERICAL_VALUE_DOUBLE, 10, 0, Double.MAX_VALUE);
75

    
76
         m_Parameters.addSelection(LoD, Sextante.getText("Level_of_detail"), sDistance);
77

    
78
         m_Parameters.addNumericalValue(ClusterTol, Sextante.getText("Cluster_tolerance"),
79
                  AdditionalInfoNumericalValue.NUMERICAL_VALUE_DOUBLE, 0.001, 0, Double.MAX_VALUE);
80
         m_Parameters.addNumericalValue(Smooth, Sextante.getText("Smoothing_coef"),
81
                  AdditionalInfoNumericalValue.NUMERICAL_VALUE_DOUBLE, 1, 0.1, 1);
82

    
83

    
84
         addOutputVectorLayer(ISOLINES, Sextante.getText("Result"), OutputVectorLayer.SHAPE_TYPE_LINE);
85
      }
86
      catch (final Exception e) {
87
         Sextante.addErrorToLog(e);
88
      }
89

    
90
   }
91

    
92

    
93
   @Override
94
   public boolean processAlgorithm() throws GeoAlgorithmExecutionException {
95

    
96
      int i;
97
      int iShapeCount;
98
      final double maxZValue = Double.NEGATIVE_INFINITY;
99
      final double minZValue = Double.POSITIVE_INFINITY;
100

    
101
      m_Triangles = m_Parameters.getParameterValueAsVectorLayer(TIN);
102
      m_EquiDistance = m_Parameters.getParameterValueAsDouble(EQUIDISTANCE);
103
      m_LoD = m_Parameters.getParameterValueAsInt(LoD);
104
      m_ClusterTol = m_Parameters.getParameterValueAsDouble(ClusterTol);
105
      m_Smooth = m_Parameters.getParameterValueAsDouble(Smooth);
106

    
107
      final Class types[] = { Integer.class, Double.class };
108
      final String sNames[] = { "ID", "Value" };
109
      m_Isolines = getNewVectorLayer(ISOLINES, m_Triangles.getName() + "_Isolines", IVectorLayer.SHAPE_TYPE_LINE, types, sNames);
110

    
111
      i = 0;
112
      iShapeCount = m_Triangles.getShapesCount();
113
      triangles = new Coordinate[iShapeCount][3];
114
      IFeatureIterator iter = m_Triangles.iterator();
115
      try {
116
         //                        dd.addField(Integer.class);
117
         //                        PageStore ps = new MemoryPageStore(dd);
118
         trianglesIndex = new STRtree();
119
         while (iter.hasNext()) {
120
            //TODO: We assume the input to be a TIN. So if this feature != triangle, then we need to throw an error here
121
            final IFeature feature = iter.next();
122
            final IRecord record = feature.getRecord();
123
            if (((String) record.getValue(1)) == "Y") {
124
               breakLines.put(i, record.getValue(2));
125
            }
126

    
127
            triangles[i][0] = (Coordinate) feature.getGeometry().getCoordinates()[0].clone();
128
            triangles[i][1] = (Coordinate) feature.getGeometry().getCoordinates()[1].clone();
129
            triangles[i][2] = (Coordinate) feature.getGeometry().getCoordinates()[2].clone();
130

    
131
            //                                data = new Data(dd);
132
            //                                data.addValue(i);
133
            trianglesIndex.insert(feature.getGeometry().getEnvelopeInternal(), new Integer(i));
134

    
135
            for (int k = 0; k < 2; k++) {
136
               final double diffZ = triangles[i][k].z - triangles[i][k + 1].z;
137
               final double diffXY = Math.sqrt(Math.pow((triangles[i][k].x - triangles[i][k + 1].x), 2)
138
                                               + Math.pow((triangles[i][k].y - triangles[i][k + 1].y), 2));
139

    
140
               if (scaleZ < Math.abs(diffZ / diffXY)) {
141
                  scaleZ = Math.abs(diffZ / diffXY);
142
               }
143
            }
144
            setProgress(i, 2 * iShapeCount);
145
            i++;
146
         }
147
         iter.close();
148
      }
149
      catch (final Exception e) {
150
         e.printStackTrace();
151
      }
152
      m_Triangles = null;
153
      iter = null;
154

    
155
      final LinearContourLines isoLineFactory = new LinearContourLines(m_EquiDistance, m_ClusterTol);
156

    
157

    
158
      if (m_LoD != 0) {
159
         final BezierSurface bezierSurface = new BezierSurface(triangles, trianglesIndex, breakLines, scaleZ * m_Smooth, m_LoD);
160
         while (bezierSurface.hasNext()) {
161
            setProgress(i++, 2 * iShapeCount);
162
            final Coordinate newTin[][] = bezierSurface.nextTrinagle();
163
            isoLineFactory.countIsolines(newTin);
164
         }
165
      }
166
      else {
167
         isoLineFactory.countIsolines(triangles);
168
      }
169

    
170
      final ArrayList isolines = isoLineFactory.getIsolines();
171
      final Iterator iterIso = isolines.iterator();
172
      int j = 0;
173
      for (int l = 0; l < isolines.size(); l++) {
174
         final Object o = isolines.get(l);
175
         if (o != null) {
176
            final GeometryFactory gf = new GeometryFactory();
177
            final Iterator isoL = ((LinkedList) o).iterator();
178
            final Coordinate[] coords = new Coordinate[((LinkedList) o).size()];
179
            int k = 0;
180
            while (isoL.hasNext()) {
181
               coords[k] = (Coordinate) isoL.next();
182
               k++;
183
            }
184
            final Object[] record = { new Integer(j), coords[0].z };
185
            final LineString isoline = gf.createLineString(coords);
186
            m_Isolines.addFeature(isoline, record);
187
            j++;
188

    
189
         }
190
      }
191

    
192
      return !m_Task.isCanceled();
193
   }
194
}