root / org.gvsig.toolbox / trunk / org.gvsig.toolbox / org.gvsig.toolbox.algorithm / src / main / java / es / unex / sextante / vectorTools / placePointsOnLines / PlacePointsOnLinesAlgorithm.java @ 59
History | View | Annotate | Download (5.33 KB)
1 | 59 | nbrodin | |
---|---|---|---|
2 | |||
3 | package es.unex.sextante.vectorTools.placePointsOnLines; |
||
4 | |||
5 | import java.util.ArrayList; |
||
6 | import java.util.HashMap; |
||
7 | |||
8 | import com.vividsolutions.jts.geom.Coordinate; |
||
9 | import com.vividsolutions.jts.geom.GeometryFactory; |
||
10 | |||
11 | import es.unex.sextante.additionalInfo.AdditionalInfoVectorLayer; |
||
12 | import es.unex.sextante.core.GeoAlgorithm; |
||
13 | import es.unex.sextante.core.Sextante; |
||
14 | import es.unex.sextante.dataObjects.IFeature; |
||
15 | import es.unex.sextante.dataObjects.IFeatureIterator; |
||
16 | import es.unex.sextante.dataObjects.IVectorLayer; |
||
17 | import es.unex.sextante.exceptions.GeoAlgorithmExecutionException; |
||
18 | import es.unex.sextante.outputs.OutputVectorLayer; |
||
19 | import es.unex.sextante.parameters.FixedTableModel; |
||
20 | |||
21 | |||
22 | public class PlacePointsOnLinesAlgorithm |
||
23 | extends
|
||
24 | GeoAlgorithm { |
||
25 | |||
26 | public static final String RESULT = "RESULT"; |
||
27 | public static final String FIELD = "FIELD"; |
||
28 | public static final String TABLE = "TABLE"; |
||
29 | public static final String LINES = "LINES"; |
||
30 | |||
31 | private IVectorLayer m_Output;
|
||
32 | private HashMap<String, ArrayList<Double>> m_Map; |
||
33 | private int m_iField; |
||
34 | |||
35 | |||
36 | @Override
|
||
37 | public boolean processAlgorithm() throws GeoAlgorithmExecutionException { |
||
38 | |||
39 | int i;
|
||
40 | |||
41 | m_Map = new HashMap<String, ArrayList<Double>>(); |
||
42 | |||
43 | IVectorLayer lines; |
||
44 | |||
45 | try {
|
||
46 | m_iField = m_Parameters.getParameterValueAsInt(FIELD); |
||
47 | lines = m_Parameters.getParameterValueAsVectorLayer(LINES); |
||
48 | final FixedTableModel table = (FixedTableModel) m_Parameters.getParameterValueAsObject(TABLE);
|
||
49 | for (int j = 0; j < table.getRowCount(); j++) { |
||
50 | final String sName = table.getValueAt(j, 0).toString(); |
||
51 | final String sDistance = table.getValueAt(j, 1).toString(); |
||
52 | ArrayList<Double> distances = m_Map.get(sName); |
||
53 | if (distances == null) { |
||
54 | distances = new ArrayList<Double>(); |
||
55 | m_Map.put(sName, distances); |
||
56 | } |
||
57 | distances.add(Double.parseDouble(sDistance));
|
||
58 | } |
||
59 | |||
60 | m_Output = getNewVectorLayer(RESULT, Sextante.getText("Points") + "(" + lines.getName() + ")", |
||
61 | IVectorLayer.SHAPE_TYPE_POINT, new Class[] { String.class }, new String[] { "ID" }); |
||
62 | |||
63 | i = 0;
|
||
64 | final int iShapeCount = lines.getShapesCount(); |
||
65 | final IFeatureIterator iter = lines.iterator();
|
||
66 | while (iter.hasNext() && setProgress(i, iShapeCount)) {
|
||
67 | final IFeature feature = iter.next();
|
||
68 | processLine(feature); |
||
69 | i++; |
||
70 | } |
||
71 | } |
||
72 | catch (final Exception e) { |
||
73 | throw new GeoAlgorithmExecutionException(e.getMessage()); |
||
74 | } |
||
75 | |||
76 | return !m_Task.isCanceled();
|
||
77 | |||
78 | } |
||
79 | |||
80 | |||
81 | private void processLine(final IFeature feature) { |
||
82 | |||
83 | final String sName = feature.getRecord().getValue(m_iField).toString(); |
||
84 | |||
85 | final ArrayList<Double> distances = m_Map.get(sName); |
||
86 | |||
87 | if (distances != null) { |
||
88 | for (int i = 0; i < distances.size(); i++) { |
||
89 | addPoint(feature, distances.get(i)); |
||
90 | } |
||
91 | } |
||
92 | |||
93 | } |
||
94 | |||
95 | |||
96 | private void addPoint(final IFeature feature, |
||
97 | final Double dist) { |
||
98 | |||
99 | final GeometryFactory gf = new GeometryFactory(); |
||
100 | boolean bPointAdded = false; |
||
101 | double dDist = 0; |
||
102 | double dDistToNextPoint;
|
||
103 | |||
104 | final Coordinate[] coords = feature.getGeometry().getCoordinates(); |
||
105 | |||
106 | for (int i = 0; i < coords.length - 1; i++) { |
||
107 | dDistToNextPoint = coords[i].distance(coords[i + 1]);
|
||
108 | if (dDist + dDistToNextPoint > dist.doubleValue()) {
|
||
109 | final double dDistToPoint = dist.doubleValue() - dDist; |
||
110 | final double dRatio = dDistToPoint / dDistToNextPoint; |
||
111 | final double dDifX = coords[i + 1].x - coords[i].x; |
||
112 | final double dDifY = coords[i + 1].y - coords[i].y; |
||
113 | final Coordinate coord = new Coordinate(coords[i].x + dDifX * dRatio, coords[i].y + dDifY * dRatio); |
||
114 | m_Output.addFeature(gf.createPoint(coord), feature.getRecord().getValues()); |
||
115 | bPointAdded = true;
|
||
116 | break;
|
||
117 | } |
||
118 | dDist += dDistToNextPoint; |
||
119 | } |
||
120 | |||
121 | if (!bPointAdded) {
|
||
122 | final String sName = feature.getRecord().getValue(m_iField).toString(); |
||
123 | Sextante.addWarningToLog(Sextante.getText("Could_not_add_point_distance_too_large") + ": " + sName + "," |
||
124 | + dist.toString()); |
||
125 | } |
||
126 | |||
127 | |||
128 | } |
||
129 | |||
130 | |||
131 | @Override
|
||
132 | public void defineCharacteristics() { |
||
133 | |||
134 | setName(Sextante.getText("Place_point_on_line_at_distance"));
|
||
135 | setGroup(Sextante.getText("Tools_for_line_layers"));
|
||
136 | setUserCanDefineAnalysisExtent(true);
|
||
137 | |||
138 | try {
|
||
139 | m_Parameters.addInputVectorLayer(LINES, Sextante.getText("Lines"), AdditionalInfoVectorLayer.SHAPE_TYPE_LINE, true); |
||
140 | m_Parameters.addTableField(FIELD, Sextante.getText("Field"), LINES);
|
||
141 | m_Parameters.addFixedTable(TABLE, Sextante.getText("Table"), new String[] { Sextante.getText("Name"), |
||
142 | Sextante.getText("Distance") }, 1, false); |
||
143 | addOutputVectorLayer(RESULT, Sextante.getText("Points"), OutputVectorLayer.SHAPE_TYPE_POINT);
|
||
144 | } |
||
145 | catch (final Exception e) { |
||
146 | Sextante.addErrorToLog(e); |
||
147 | } |
||
148 | |||
149 | } |
||
150 | |||
151 | } |