root / org.gvsig.toolbox / trunk / org.gvsig.toolbox / org.gvsig.toolbox.algorithm / src / main / java / es / unex / sextante / lighting / los / LOSAlgorithm.java @ 59
History | View | Annotate | Download (7.25 KB)
1 |
package es.unex.sextante.lighting.los; |
---|---|
2 |
|
3 |
import java.awt.Color; |
4 |
import java.awt.geom.Point2D; |
5 |
|
6 |
import org.jfree.chart.ChartFactory; |
7 |
import org.jfree.chart.ChartPanel; |
8 |
import org.jfree.chart.JFreeChart; |
9 |
import org.jfree.chart.plot.PlotOrientation; |
10 |
import org.jfree.chart.plot.XYPlot; |
11 |
import org.jfree.chart.renderer.xy.XYItemRenderer; |
12 |
import org.jfree.data.xy.XYSeries; |
13 |
import org.jfree.data.xy.XYSeriesCollection; |
14 |
|
15 |
import es.unex.sextante.additionalInfo.AdditionalInfoNumericalValue; |
16 |
import es.unex.sextante.core.AnalysisExtent; |
17 |
import es.unex.sextante.core.GeoAlgorithm; |
18 |
import es.unex.sextante.core.Sextante; |
19 |
import es.unex.sextante.dataObjects.IRasterLayer; |
20 |
import es.unex.sextante.exceptions.GeoAlgorithmExecutionException; |
21 |
import es.unex.sextante.exceptions.RepeatedParameterNameException; |
22 |
import es.unex.sextante.rasterWrappers.GridCell; |
23 |
|
24 |
public class LOSAlgorithm |
25 |
extends
|
26 |
GeoAlgorithm { |
27 |
|
28 |
public static final String DEM = "DEM"; |
29 |
public static final String POINT = "POINT"; |
30 |
public static final String POINT2 = "POINT2"; |
31 |
public static final String HEIGHT = "HEIGHT"; |
32 |
public static final String HEIGHT2 = "HEIGHT2"; |
33 |
public static final String GRAPH = "GRAPH"; |
34 |
|
35 |
private IRasterLayer m_DEM = null; |
36 |
private GridCell m_Point, m_Point2;
|
37 |
private double m_dHeight, m_dHeight2; |
38 |
private final XYSeriesCollection m_Dataset = new XYSeriesCollection(); |
39 |
private double m_dMin = Double.MAX_VALUE; |
40 |
|
41 |
|
42 |
@Override
|
43 |
public boolean processAlgorithm() throws GeoAlgorithmExecutionException { |
44 |
|
45 |
m_DEM = m_Parameters.getParameterValueAsRasterLayer(DEM); |
46 |
final Point2D pt = m_Parameters.getParameterValueAsPoint(POINT); |
47 |
final Point2D pt2 = m_Parameters.getParameterValueAsPoint(POINT2); |
48 |
m_dHeight = m_Parameters.getParameterValueAsDouble(HEIGHT); |
49 |
m_dHeight2 = m_Parameters.getParameterValueAsDouble(HEIGHT2); |
50 |
|
51 |
final AnalysisExtent ge = new AnalysisExtent(m_DEM); |
52 |
m_Point = ge.getGridCoordsFromWorldCoords(pt); |
53 |
m_Point2 = ge.getGridCoordsFromWorldCoords(pt2); |
54 |
|
55 |
final double x = ge.getXMin() + m_Point.getX() * ge.getCellSize(); |
56 |
final double y = ge.getYMax() - m_Point.getY() * ge.getCellSize(); |
57 |
final double x2 = ge.getXMin() + m_Point2.getX() * ge.getCellSize(); |
58 |
final double y2 = ge.getYMax() - m_Point2.getY() * ge.getCellSize(); |
59 |
|
60 |
ge.setXRange(x, x2, true);
|
61 |
ge.setYRange(y, y2, true);
|
62 |
ge.enlargeOneCell(); |
63 |
|
64 |
m_Point = ge.getGridCoordsFromWorldCoords(pt); |
65 |
m_Point2 = ge.getGridCoordsFromWorldCoords(pt2); |
66 |
|
67 |
m_DEM.setWindowExtent(ge); |
68 |
|
69 |
calculateLOS(m_Point.getX(), m_Point.getY(), m_Point2.getX(), m_Point2.getY()); |
70 |
createChart(); |
71 |
|
72 |
return !m_Task.isCanceled();
|
73 |
|
74 |
} |
75 |
|
76 |
|
77 |
@Override
|
78 |
public void defineCharacteristics() { |
79 |
|
80 |
try {
|
81 |
m_Parameters.addInputRasterLayer(DEM, Sextante.getText("Elevation"), true); |
82 |
m_Parameters.addPoint(POINT, Sextante.getText("point") + " 1"); |
83 |
m_Parameters.addPoint(POINT2, Sextante.getText("point") + " 2"); |
84 |
m_Parameters.addNumericalValue(HEIGHT, Sextante.getText("Height_of_point") + " 1", 0, |
85 |
AdditionalInfoNumericalValue.NUMERICAL_VALUE_DOUBLE); |
86 |
m_Parameters.addNumericalValue(HEIGHT2, Sextante.getText("Height_of_point") + " 2", 0, |
87 |
AdditionalInfoNumericalValue.NUMERICAL_VALUE_DOUBLE); |
88 |
addOutputChart(GRAPH, Sextante.getText("Line_of_sight"));
|
89 |
} |
90 |
catch (final RepeatedParameterNameException e) { |
91 |
Sextante.addErrorToLog(e); |
92 |
} |
93 |
|
94 |
setName(Sextante.getText("Line_of_sight"));
|
95 |
setGroup(Sextante.getText("Visibility_and_lighting"));
|
96 |
|
97 |
} |
98 |
|
99 |
|
100 |
private void calculateLOS(int x, |
101 |
int y,
|
102 |
final int x2, |
103 |
final int y2) { |
104 |
|
105 |
double dx, dy;
|
106 |
double ix, iy, iz, id, d, dist, z;
|
107 |
double dSlope, dMaxSlope = Double.MAX_VALUE; |
108 |
double dLastZ, dLastD = 0; |
109 |
boolean bSeen;
|
110 |
boolean bLast = true; |
111 |
XYSeries serie = new XYSeries(""); |
112 |
final XYSeries lineSerie = new XYSeries(""); |
113 |
|
114 |
|
115 |
m_Dataset.addSeries(serie); |
116 |
dx = x2 - x; |
117 |
dy = y2 - y; |
118 |
|
119 |
d = Math.abs(dx) > Math.abs(dy) ? Math.abs(dx) : Math.abs(dy); |
120 |
|
121 |
if (d > 0) { |
122 |
dist = Math.sqrt(dx * dx + dy * dy);
|
123 |
|
124 |
dx /= d; |
125 |
dy /= d; |
126 |
|
127 |
d = dist / d; |
128 |
|
129 |
id = 0.0;
|
130 |
ix = x + 0.5;
|
131 |
iy = y + 0.5;
|
132 |
iz = m_DEM.getCellValueAsDouble(x, y); |
133 |
|
134 |
if (m_DEM.isNoDataValue(iz)) {
|
135 |
return;
|
136 |
} |
137 |
|
138 |
iz += m_dHeight; // Add watcher height to watcher Z value
|
139 |
dLastZ = iz; |
140 |
|
141 |
while (id < dist) {
|
142 |
id += d; |
143 |
|
144 |
ix += dx; |
145 |
iy += dy; |
146 |
|
147 |
x = (int) ix;
|
148 |
y = (int) iy;
|
149 |
|
150 |
z = m_DEM.getCellValueAsDouble(x, y); |
151 |
if (!m_DEM.isNoDataValue(z)) {
|
152 |
dSlope = (z - iz) / id; |
153 |
if (dMaxSlope == Double.MAX_VALUE) { |
154 |
dMaxSlope = dSlope; |
155 |
bSeen = bLast = true;
|
156 |
serie.add(0, iz);
|
157 |
} |
158 |
else if (dSlope <= dMaxSlope) { |
159 |
bSeen = false;
|
160 |
} |
161 |
else {
|
162 |
bSeen = true;
|
163 |
dMaxSlope = dSlope; |
164 |
} |
165 |
|
166 |
if (bSeen == bLast) {
|
167 |
serie.add(id * m_DEM.getWindowCellSize(), z); |
168 |
dLastD = id * m_DEM.getWindowCellSize(); |
169 |
dLastZ = z; |
170 |
} |
171 |
else {
|
172 |
serie = new XYSeries(""); |
173 |
m_Dataset.addSeries(serie); |
174 |
serie.add(dLastD, dLastZ); |
175 |
dLastD = id * m_DEM.getWindowCellSize(); |
176 |
dLastZ = z; |
177 |
serie.add(dLastD, dLastZ); |
178 |
bLast = bSeen; |
179 |
} |
180 |
m_dMin = Math.min(m_dMin, dLastZ);
|
181 |
} |
182 |
} |
183 |
lineSerie.add(0, iz);
|
184 |
lineSerie.add(dLastD, dLastZ + m_dHeight2); |
185 |
m_Dataset.addSeries(lineSerie); |
186 |
|
187 |
} |
188 |
|
189 |
} |
190 |
|
191 |
|
192 |
private void createChart() { |
193 |
|
194 |
int i;
|
195 |
|
196 |
final JFreeChart chart = ChartFactory.createXYLineChart(null, null, null, m_Dataset, PlotOrientation.VERTICAL, false, true, |
197 |
true);
|
198 |
|
199 |
final ChartPanel jPanelChart = new ChartPanel(chart); |
200 |
jPanelChart.setPreferredSize(new java.awt.Dimension(500, 300)); |
201 |
jPanelChart.setBorder(javax.swing.BorderFactory.createLineBorder(java.awt.Color.gray, 1));
|
202 |
final XYPlot plot = (XYPlot) chart.getPlot();
|
203 |
final XYItemRenderer ren = plot.getRenderer();
|
204 |
for (i = 0; i < m_Dataset.getSeriesCount() - 1; i++) { |
205 |
if (i % 2 != 0) { |
206 |
ren.setSeriesPaint(i, Color.red);
|
207 |
} |
208 |
else {
|
209 |
ren.setSeriesPaint(i, Color.green);
|
210 |
} |
211 |
} |
212 |
ren.setSeriesPaint(i, Color.BLACK);
|
213 |
plot.getRangeAxis().setAutoRange(false);
|
214 |
plot.getRangeAxis().setLowerBound(m_dMin); |
215 |
addOutputChart("GRAPH", Sextante.getText("Line_of_sight"), jPanelChart); |
216 |
|
217 |
|
218 |
} |
219 |
|
220 |
|
221 |
} |