Statistics
| Revision:

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

History | View | Annotate | Download (7.23 KB)

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

    
3
import com.vividsolutions.jts.geom.Coordinate;
4
import com.vividsolutions.jts.geom.Geometry;
5
import com.vividsolutions.jts.geom.GeometryFactory;
6
import com.vividsolutions.jts.geom.LinearRing;
7

    
8
import es.unex.sextante.additionalInfo.AdditionalInfoNumericalValue;
9
import es.unex.sextante.core.GeoAlgorithm;
10
import es.unex.sextante.core.Sextante;
11
import es.unex.sextante.dataObjects.IVectorLayer;
12
import es.unex.sextante.exceptions.GeoAlgorithmExecutionException;
13
import es.unex.sextante.exceptions.RepeatedParameterNameException;
14

    
15
public class GraticuleBuilderAlgorithm
16
         extends
17
            GeoAlgorithm {
18

    
19
   private static final int   TYPE_RECTANGLES = 0;
20
   private static final int   TYPE_LINES      = 1;
21
   private static final int   TYPE_POINTS     = 2;
22

    
23
   public static final String GRATICULE       = "GRATICULE";
24
   public static final String TYPE            = "TYPE";
25
   public static final String INTERVALY       = "INTERVALY";
26
   public static final String INTERVALX       = "INTERVALX";
27

    
28

    
29
   @Override
30
   public void defineCharacteristics() {
31

    
32
      setName(Sextante.getText("Create_graticule"));
33
      setGroup(Sextante.getText("Tools_for_vector_layers"));
34
      setUserCanDefineAnalysisExtent(true);
35

    
36
      final String[] sOptions = { Sextante.getText("Rectangles"), Sextante.getText("Lines"), Sextante.getText("Points") };
37

    
38
      try {
39
         m_Parameters.addNumericalValue(INTERVALX, Sextante.getText("X_interval"), 1,
40
                  AdditionalInfoNumericalValue.NUMERICAL_VALUE_DOUBLE);
41
         m_Parameters.addNumericalValue(INTERVALY, Sextante.getText("Y_interval"), 1,
42
                  AdditionalInfoNumericalValue.NUMERICAL_VALUE_DOUBLE);
43
         m_Parameters.addSelection(TYPE, Sextante.getText("Type"), sOptions);
44
         addOutputVectorLayer(GRATICULE, Sextante.getText("Graticule"));
45
      }
46
      catch (final RepeatedParameterNameException e) {
47
         Sextante.addErrorToLog(e);
48
      }
49

    
50
   }
51

    
52

    
53
   @Override
54
   public boolean processAlgorithm() throws GeoAlgorithmExecutionException {
55

    
56
      double x, y;
57
      int i = 0;
58
      int iCountX, iCountY;
59
      int iID = 0;
60
      int iShapeType;
61
      final String[] sNames = { "ID", "X", "Y", "BORDER" };
62
      final Class[] types = { Integer.class, Double.class, Double.class, Integer.class };
63

    
64
      final double dXMax = m_AnalysisExtent.getXMax();
65
      final double dXMin = m_AnalysisExtent.getXMin();
66
      final double dYMax = m_AnalysisExtent.getYMax();
67
      final double dYMin = m_AnalysisExtent.getYMin();
68

    
69
      final double dIntervalX = m_Parameters.getParameterValueAsDouble(INTERVALX);
70
      final double dIntervalY = m_Parameters.getParameterValueAsDouble(INTERVALY);
71
      final int iType = m_Parameters.getParameterValueAsInt(TYPE);
72

    
73
      iCountX = (int) ((dXMax - dXMin) / dIntervalX);
74
      iCountY = (int) ((dYMax - dYMin) / dIntervalY);
75

    
76
      switch (iType) {
77
         case TYPE_RECTANGLES:
78
            iShapeType = IVectorLayer.SHAPE_TYPE_POLYGON;
79
            break;
80
         case TYPE_LINES:
81
            iShapeType = IVectorLayer.SHAPE_TYPE_LINE;
82
            iCountX *= 2;
83
            break;
84
         case TYPE_POINTS:
85
         default:
86
            iShapeType = IVectorLayer.SHAPE_TYPE_POINT;
87
            break;
88
      }
89

    
90
      final IVectorLayer output = getNewVectorLayer(GRATICULE, Sextante.getText("Graticule"), iShapeType, types, sNames);
91
      final Object[] value = new Object[4];
92
      final GeometryFactory gf = new GeometryFactory();
93
      switch (iType) {
94
         case TYPE_RECTANGLES:
95
            Geometry geom;
96
            for (x = dXMin; (x < dXMax - dIntervalX) & setProgress(i++, iCountX); x = x + dIntervalX) {
97
               for (y = dYMin; y < dYMax - dIntervalY; y = y + dIntervalY) {
98
                  final Coordinate[] coords = new Coordinate[5];
99
                  coords[0] = new Coordinate(x, y);
100
                  coords[1] = new Coordinate(x, y + dIntervalY);
101
                  coords[2] = new Coordinate(x + dIntervalX, y + dIntervalY);
102
                  coords[3] = new Coordinate(x + dIntervalX, y);
103
                  coords[4] = new Coordinate(x, y);
104
                  value[0] = new Integer(iID++);
105
                  value[1] = new Double(x);
106
                  value[2] = new Double(y);
107
                  if ((x == dXMin) || (x + dIntervalX >= dXMax - dIntervalX) || (y == dYMin)
108
                      || (y + dIntervalY >= dYMax - dIntervalY)) {
109
                     value[3] = new Integer(1);
110
                  }
111
                  else {
112
                     value[3] = new Integer(0);
113
                  }
114
                  final LinearRing ring = gf.createLinearRing(coords);
115
                  geom = gf.createPolygon(ring, null);
116
                  output.addFeature(geom, value);
117
               }
118
            }
119
            break;
120
         case TYPE_LINES:
121
            Geometry line;
122

    
123
            for (x = dXMin; (x <= dXMax) && setProgress(i++, iCountX); x = x + dIntervalX) {
124
               final Coordinate[] coords = new Coordinate[2];
125
               coords[0] = new Coordinate(x, dYMin);
126
               coords[1] = new Coordinate(x, dYMax);
127
               line = gf.createLineString(coords);
128
               value[0] = new Integer(iID++);
129
               value[1] = new Double(x);
130
               value[2] = new Double(dYMin);
131
               if ((x == dXMin) || (x + dIntervalX > dXMax - dIntervalX)) {
132
                  value[3] = new Integer(1);
133
               }
134
               else {
135
                  value[3] = new Integer(0);
136
               }
137
               output.addFeature(line, value);
138
            }
139
            i = 0;
140
            for (y = dYMin; (y <= dYMax) && setProgress(i++, iCountY); y = y + dIntervalY) {
141
               final Coordinate[] coords = new Coordinate[2];
142
               coords[0] = new Coordinate(dXMin, y);
143
               coords[1] = new Coordinate(dXMax, y);
144
               line = gf.createLineString(coords);
145
               value[0] = new Integer(iID++);
146
               value[1] = new Double(dXMin);
147
               value[2] = new Double(y);
148
               if ((y == dYMin) || (y + dIntervalY > dYMax - dIntervalY)) {
149
                  value[3] = new Integer(1);
150
               }
151
               else {
152
                  value[3] = new Integer(0);
153
               }
154
               output.addFeature(line, value);
155
            }
156
            break;
157
         case TYPE_POINTS:
158
            Geometry point;
159
            for (x = dXMin; (x <= dXMax) && setProgress(i++, iCountX); x = x + dIntervalX) {
160
               for (y = dYMin; y <= dYMax; y = y + dIntervalY) {
161
                  point = gf.createPoint(new Coordinate(x, y));
162
                  value[0] = new Integer(iID++);
163
                  value[1] = new Double(x);
164
                  value[2] = new Double(y);
165
                  if ((x == dXMin) || (x + dIntervalX > dXMax) || (y == dYMin) || (y + dIntervalY > dYMax)) {
166
                     value[3] = new Integer(1);
167
                  }
168
                  else {
169
                     value[3] = new Integer(0);
170
                  }
171
                  output.addFeature(point, value);
172
               }
173
            }
174
            break;
175
      }
176

    
177
      return !m_Task.isCanceled();
178

    
179
   }
180

    
181

    
182
}