root / org.gvsig.toolbox / trunk / org.gvsig.toolbox / org.gvsig.toolbox.algorithm / src / main / java / es / unex / sextante / hydrology / setLinesDirectionsWithDEM / SetLinesDirectionsWithDEMAlgorithm.java @ 59
History | View | Annotate | Download (4.03 KB)
1 |
|
---|---|
2 |
|
3 |
package es.unex.sextante.hydrology.setLinesDirectionsWithDEM; |
4 |
|
5 |
import java.util.ArrayList; |
6 |
|
7 |
import com.vividsolutions.jts.geom.Coordinate; |
8 |
import com.vividsolutions.jts.geom.Geometry; |
9 |
import com.vividsolutions.jts.geom.GeometryFactory; |
10 |
import com.vividsolutions.jts.geom.LineString; |
11 |
|
12 |
import es.unex.sextante.additionalInfo.AdditionalInfoVectorLayer; |
13 |
import es.unex.sextante.core.GeoAlgorithm; |
14 |
import es.unex.sextante.core.Sextante; |
15 |
import es.unex.sextante.dataObjects.IFeature; |
16 |
import es.unex.sextante.dataObjects.IFeatureIterator; |
17 |
import es.unex.sextante.dataObjects.IRasterLayer; |
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.RepeatedParameterNameException; |
22 |
import es.unex.sextante.outputs.OutputVectorLayer; |
23 |
|
24 |
|
25 |
public class SetLinesDirectionsWithDEMAlgorithm |
26 |
extends
|
27 |
GeoAlgorithm { |
28 |
|
29 |
public static final String RESULT = "RESULT"; |
30 |
public static final String LINES = "LINES"; |
31 |
public static final String DEM = "DEM"; |
32 |
|
33 |
private IVectorLayer m_Output;
|
34 |
private ArrayList m_Lines; |
35 |
private IRasterLayer m_DEM;
|
36 |
|
37 |
|
38 |
@Override
|
39 |
public void defineCharacteristics() { |
40 |
|
41 |
setName(Sextante.getText("Change_line_direction"));
|
42 |
setGroup(Sextante.getText("Tools_for_line_layers"));
|
43 |
setUserCanDefineAnalysisExtent(true);
|
44 |
|
45 |
try {
|
46 |
m_Parameters.addInputRasterLayer(DEM, Sextante.getText("Elevation"), true); |
47 |
m_Parameters.addInputVectorLayer(LINES, Sextante.getText("Lines"), AdditionalInfoVectorLayer.SHAPE_TYPE_LINE, true); |
48 |
addOutputVectorLayer(RESULT, Sextante.getText("Lines"), OutputVectorLayer.SHAPE_TYPE_LINE, LINES);
|
49 |
} |
50 |
catch (final RepeatedParameterNameException e) { |
51 |
Sextante.addErrorToLog(e); |
52 |
} |
53 |
|
54 |
} |
55 |
|
56 |
|
57 |
@Override
|
58 |
public boolean processAlgorithm() throws GeoAlgorithmExecutionException { |
59 |
|
60 |
m_DEM = m_Parameters.getParameterValueAsRasterLayer(DEM); |
61 |
|
62 |
final IVectorLayer lines = m_Parameters.getParameterValueAsVectorLayer(LINES);
|
63 |
if (!m_bIsAutoExtent) {
|
64 |
lines.addFilter(new BoundingBoxFilter(m_AnalysisExtent));
|
65 |
} |
66 |
|
67 |
m_Output = getNewVectorLayer(RESULT, Sextante.getText("Result"), IVectorLayer.SHAPE_TYPE_LINE, lines.getFieldTypes(),
|
68 |
lines.getFieldNames()); |
69 |
|
70 |
int i = 0; |
71 |
final int iShapeCount = lines.getShapesCount(); |
72 |
final IFeatureIterator iter = lines.iterator();
|
73 |
final GeometryFactory gf = new GeometryFactory(); |
74 |
while (iter.hasNext() && setProgress(i, iShapeCount)) {
|
75 |
final IFeature feature = iter.next();
|
76 |
final Geometry geom = feature.getGeometry();
|
77 |
m_Lines = new ArrayList(); |
78 |
for (int j = 0; j < geom.getNumGeometries(); j++) { |
79 |
final Geometry line = geom.getGeometryN(j);
|
80 |
addLine(line); |
81 |
} |
82 |
final LineString[] lineStrings = new LineString[m_Lines.size()]; |
83 |
for (int j = 0; j < lineStrings.length; j++) { |
84 |
lineStrings[j] = (LineString) m_Lines.get(j); |
85 |
} |
86 |
m_Output.addFeature(gf.createMultiLineString(lineStrings), feature.getRecord().getValues()); |
87 |
i++; |
88 |
} |
89 |
iter.close(); |
90 |
|
91 |
return !m_Task.isCanceled();
|
92 |
|
93 |
} |
94 |
|
95 |
|
96 |
private void addLine(final Geometry line) { |
97 |
|
98 |
|
99 |
final Coordinate[] coords = line.getCoordinates(); |
100 |
final double z = m_DEM.getValueAt(coords[0].x, coords[0].y); |
101 |
final double z2 = m_DEM.getValueAt(coords[coords.length - 1].x, coords[coords.length - 1].y); |
102 |
|
103 |
if (z2 > z) {
|
104 |
final Coordinate[] newCoords = new Coordinate[coords.length]; |
105 |
for (int i = 0; i < coords.length; i++) { |
106 |
newCoords[i] = coords[coords.length - i - 1];
|
107 |
} |
108 |
final GeometryFactory gf = new GeometryFactory(); |
109 |
m_Lines.add(gf.createLineString(newCoords)); |
110 |
} |
111 |
else {
|
112 |
m_Lines.add(line); |
113 |
} |
114 |
|
115 |
} |
116 |
|
117 |
|
118 |
} |