gvsig-lrs / org.gvsig.lrs / trunk / org.gvsig.lrs / org.gvsig.lrs.app / org.gvsig.lrs.app.mainplugin / src / main / java / org / gvsig / lrs / app / showmeasures / ShowMeasuresExtension.java @ 22
History | View | Annotate | Download (19 KB)
1 |
/* gvSIG. Desktop Geographic Information System.
|
---|---|
2 |
*
|
3 |
* Copyright ? 2007-2015 gvSIG Association
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* as published by the Free Software Foundation; either version 2
|
8 |
* of the License, or (at your option) any later version.
|
9 |
*
|
10 |
* This program is distributed in the hope that it will be useful,
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
* GNU General Public License for more details.
|
14 |
*
|
15 |
* You should have received a copy of the GNU General Public License
|
16 |
* along with this program; if not, write to the Free Software
|
17 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
18 |
* MA 02110-1301, USA.
|
19 |
*
|
20 |
* For any additional information, do not hesitate to contact us
|
21 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
22 |
*/
|
23 |
package org.gvsig.lrs.app.showmeasures; |
24 |
|
25 |
import java.awt.Color; |
26 |
import java.awt.event.ActionEvent; |
27 |
import java.awt.event.ActionListener; |
28 |
import java.awt.event.ComponentEvent; |
29 |
import java.awt.event.ComponentListener; |
30 |
import java.text.DecimalFormat; |
31 |
import java.util.ArrayList; |
32 |
import java.util.Iterator; |
33 |
import java.util.List; |
34 |
|
35 |
import javax.swing.JOptionPane; |
36 |
|
37 |
import org.apache.commons.lang3.StringUtils; |
38 |
import org.slf4j.Logger; |
39 |
import org.slf4j.LoggerFactory; |
40 |
|
41 |
import org.gvsig.andami.IconThemeHelper; |
42 |
import org.gvsig.andami.plugins.Extension; |
43 |
import org.gvsig.app.ApplicationLocator; |
44 |
import org.gvsig.app.ApplicationManager; |
45 |
import org.gvsig.app.project.documents.view.ViewDocument; |
46 |
import org.gvsig.app.project.documents.view.gui.IView; |
47 |
import org.gvsig.fmap.dal.DataSet; |
48 |
import org.gvsig.fmap.dal.exception.DataException; |
49 |
import org.gvsig.fmap.dal.feature.Feature; |
50 |
import org.gvsig.fmap.dal.feature.FeatureSelection; |
51 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
52 |
import org.gvsig.fmap.geom.Geometry; |
53 |
import org.gvsig.fmap.geom.GeometryLocator; |
54 |
import org.gvsig.fmap.geom.GeometryManager; |
55 |
import org.gvsig.fmap.geom.aggregate.MultiLine; |
56 |
import org.gvsig.fmap.geom.exception.CreateGeometryException; |
57 |
import org.gvsig.fmap.geom.primitive.Line; |
58 |
import org.gvsig.fmap.geom.primitive.Point; |
59 |
import org.gvsig.fmap.mapcontext.MapContext; |
60 |
import org.gvsig.fmap.mapcontext.MapContextLocator; |
61 |
import org.gvsig.fmap.mapcontext.layers.FLayer; |
62 |
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
63 |
import org.gvsig.fmap.mapcontext.layers.vectorial.GraphicLayer; |
64 |
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol; |
65 |
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolManager; |
66 |
import org.gvsig.gui.beans.ProgressDialog; |
67 |
import org.gvsig.lrs.lib.api.LrsAlgorithm; |
68 |
import org.gvsig.lrs.lib.api.LrsAlgorithmsLocator; |
69 |
import org.gvsig.lrs.lib.api.LrsAlgorithmsManager; |
70 |
import org.gvsig.lrs.lib.api.LrsShowMeasuresAlgorithmParams; |
71 |
import org.gvsig.lrs.lib.api.exceptions.LrsGettingParametersException; |
72 |
import org.gvsig.lrs.lib.api.exceptions.LrsNeededParameterException; |
73 |
import org.gvsig.lrs.swing.api.JLrsAlgorithmParams; |
74 |
import org.gvsig.lrs.swing.api.JLrsProgressDialog; |
75 |
import org.gvsig.lrs.swing.api.LrsAlgorithmsSwingLocator; |
76 |
import org.gvsig.lrs.swing.api.LrsAlgorithmsSwingManager; |
77 |
import org.gvsig.lrs.swing.impl.JLrsShowMeasuresParamsController; |
78 |
import org.gvsig.lrs.swing.impl.JLrsUtils; |
79 |
import org.gvsig.symbology.SymbologyLocator; |
80 |
import org.gvsig.symbology.SymbologyManager; |
81 |
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.text.ISimpleTextSymbol; |
82 |
import org.gvsig.tools.ToolsLocator; |
83 |
import org.gvsig.tools.dispose.DisposableIterator; |
84 |
import org.gvsig.tools.i18n.I18nManager; |
85 |
import org.gvsig.tools.locator.LocatorException; |
86 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
87 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager; |
88 |
import org.gvsig.tools.task.SimpleTaskStatus; |
89 |
|
90 |
/**
|
91 |
* @author dmartinez
|
92 |
*
|
93 |
*/
|
94 |
public class ShowMeasuresExtension extends Extension implements ComponentListener { |
95 |
|
96 |
private static final Logger logger = LoggerFactory.getLogger(ShowMeasuresExtension.class); |
97 |
|
98 |
public static final String SHOW_MEASURES_GRAPHICS_ID = "ShowMeasures"; |
99 |
|
100 |
private JLrsShowMeasuresParamsController panel;
|
101 |
|
102 |
|
103 |
/*
|
104 |
* (non-Javadoc)
|
105 |
*
|
106 |
* @see org.gvsig.andami.plugins.IExtension#execute(java.lang.String)
|
107 |
*/
|
108 |
public void execute(String actionCommand) { |
109 |
if (StringUtils.equalsIgnoreCase(actionCommand, "show-measures")) { |
110 |
IView view = getActiveView(); |
111 |
final MapContext mapContext = view.getMapControl().getMapContext();
|
112 |
final GraphicLayer graphics = mapContext.getGraphicsLayer();
|
113 |
|
114 |
final FLyrVect activeLayer = getActiveLayer(view);
|
115 |
|
116 |
final WindowManager winManager = ToolsSwingLocator.getWindowManager();
|
117 |
|
118 |
final LrsAlgorithmsSwingManager manager = LrsAlgorithmsSwingLocator.getLrsAlgorithmsSwingManager();
|
119 |
|
120 |
final I18nManager i18nManager = ToolsLocator.getI18nManager();
|
121 |
|
122 |
try {
|
123 |
panel = |
124 |
(JLrsShowMeasuresParamsController) manager.createJLrsShowMeasuresAlgorithmParameters(activeLayer, |
125 |
null);
|
126 |
panel.addComponentListener(this);
|
127 |
} catch (LrsNeededParameterException e2) {
|
128 |
logger.error("Error creating panel", e2);
|
129 |
JOptionPane.showMessageDialog(
|
130 |
null,
|
131 |
new StringBuilder().append(i18nManager.getTranslation("error_creating_panel")).append(":") |
132 |
.append(e2.getLocalizedMessage()), i18nManager.getTranslation("error"),
|
133 |
JOptionPane.ERROR_MESSAGE);
|
134 |
return;
|
135 |
} |
136 |
panel.setVisibleAceptCancel(true);
|
137 |
panel.addActionListener(new ActionListener() { |
138 |
|
139 |
public void actionPerformed(ActionEvent e) { |
140 |
JLrsAlgorithmParams panel = (JLrsAlgorithmParams) e.getSource(); |
141 |
if (panel.isCanceled()) {
|
142 |
return;
|
143 |
} |
144 |
LrsShowMeasuresAlgorithmParams params = null;
|
145 |
try {
|
146 |
params = (LrsShowMeasuresAlgorithmParams) panel.getParams(); |
147 |
} catch (LrsGettingParametersException e1) {
|
148 |
logger.error("Error getting parameters", e1);
|
149 |
JOptionPane.showMessageDialog(
|
150 |
null,
|
151 |
new StringBuilder().append(i18nManager.getTranslation("error_getting_parameters")) |
152 |
.append(":").append(e1.getLocalizedMessage()), i18nManager.getTranslation("error"), |
153 |
JOptionPane.ERROR_MESSAGE);
|
154 |
return;
|
155 |
} catch (LrsNeededParameterException e1) {
|
156 |
logger.warn("Error getting parameters", e1);
|
157 |
JOptionPane.showMessageDialog(null, i18nManager.getTranslation(e1.getMessage()), |
158 |
i18nManager.getTranslation("warning"), JOptionPane.WARNING_MESSAGE); |
159 |
return;
|
160 |
} |
161 |
final SimpleTaskStatus taskStatus =
|
162 |
ToolsLocator.getTaskStatusManager().createDefaultSimpleTaskStatus( |
163 |
i18nManager.getTranslation("progress"));
|
164 |
|
165 |
final JLrsProgressDialog progressDialog = manager.createJLrsProgressDialog(taskStatus);
|
166 |
|
167 |
winManager.showWindow(progressDialog.asJComponent(), i18nManager.getTranslation("show_measures"),
|
168 |
WindowManager.MODE.WINDOW); |
169 |
|
170 |
final double distance = params.getDistance(); |
171 |
|
172 |
Thread task = new Thread(new Runnable() { |
173 |
|
174 |
public void run() { |
175 |
try {
|
176 |
showMeasures(graphics, activeLayer, distance, taskStatus); |
177 |
mapContext.invalidate(); |
178 |
if(!taskStatus.isRunning() && progressDialog.asJComponent().isVisible()){
|
179 |
progressDialog.asJComponent().setVisible(false);
|
180 |
} |
181 |
} catch (Exception e) { |
182 |
logger.error("Error showing measures", e);
|
183 |
String message = e.getMessage();
|
184 |
if (e.getCause() != null) { |
185 |
message = e.getCause().getMessage(); |
186 |
} |
187 |
JOptionPane.showMessageDialog(progressDialog.asJComponent(), message,
|
188 |
i18nManager.getTranslation("show_measures"), JOptionPane.ERROR_MESSAGE); |
189 |
} |
190 |
} |
191 |
}); |
192 |
task.start(); |
193 |
|
194 |
} |
195 |
}); |
196 |
winManager.showWindow(panel.asJComponent(), i18nManager.getTranslation("show_measures"),
|
197 |
WindowManager.MODE.WINDOW); |
198 |
} |
199 |
} |
200 |
|
201 |
/*
|
202 |
* (non-Javadoc)
|
203 |
*
|
204 |
* @see org.gvsig.andami.plugins.IExtension#initialize()
|
205 |
*/
|
206 |
public void initialize() { |
207 |
registerIcons(); |
208 |
} |
209 |
|
210 |
/*
|
211 |
* (non-Javadoc)
|
212 |
*
|
213 |
* @see org.gvsig.andami.plugins.IExtension#isEnabled()
|
214 |
*/
|
215 |
public boolean isEnabled() { |
216 |
IView view = getActiveView(); |
217 |
FLyrVect activeLayer = getActiveLayer(view); |
218 |
if (panel == null && activeLayer != null) { |
219 |
return JLrsUtils.hasMLayersWithSelectedFeatures(activeLayer);
|
220 |
} |
221 |
return false; |
222 |
} |
223 |
|
224 |
/*
|
225 |
* (non-Javadoc)
|
226 |
*
|
227 |
* @see org.gvsig.andami.plugins.IExtension#isVisible()
|
228 |
*/
|
229 |
public boolean isVisible() { |
230 |
return true; |
231 |
} |
232 |
|
233 |
private void registerIcons() { |
234 |
IconThemeHelper.registerIcon("lrs", "pk_green", this); |
235 |
} |
236 |
|
237 |
/*
|
238 |
* Shows measurements along the selected features in a layer with a fixed spacing
|
239 |
*
|
240 |
*/
|
241 |
private void showMeasures(GraphicLayer graphics, FLyrVect layer, double distance, SimpleTaskStatus taskStatus) |
242 |
throws DataException, CreateGeometryException, LocatorException {
|
243 |
FeatureStore featureStore = layer.getFeatureStore(); |
244 |
FeatureSelection selection = featureStore.getFeatureSelection(); |
245 |
if (selection != null && !selection.isEmpty()) { |
246 |
long range = getRange(selection);
|
247 |
taskStatus.setRangeOfValues(0, range);
|
248 |
long curValue = 0; |
249 |
taskStatus.setCurValue(curValue); |
250 |
DisposableIterator it = selection.fastIterator(); |
251 |
while (it.hasNext()) {
|
252 |
Feature feature = (Feature) it.next(); |
253 |
Geometry geometry = feature.getDefaultGeometry(); |
254 |
if (geometry instanceof Line) { |
255 |
curValue = showLineMeasures(graphics, curValue, (Line) geometry, distance);
|
256 |
taskStatus.setCurValue(curValue); |
257 |
} else if (geometry instanceof MultiLine) { |
258 |
MultiLine multiLine = (MultiLine) geometry; |
259 |
int primitives = multiLine.getPrimitivesNumber();
|
260 |
for (int i = 0; i < primitives; i++) { |
261 |
curValue = showLineMeasures(graphics, curValue, (Line) multiLine.getPrimitiveAt(i), distance);
|
262 |
taskStatus.setCurValue(curValue); |
263 |
} |
264 |
} |
265 |
} |
266 |
it.dispose(); |
267 |
} |
268 |
taskStatus.terminate(); |
269 |
} |
270 |
|
271 |
/**
|
272 |
* Shows measurements along a line with a fixed spacing
|
273 |
*
|
274 |
* @param curValue
|
275 |
* @param taskStatus
|
276 |
* @param geometry
|
277 |
* @return
|
278 |
* @throws LocatorException
|
279 |
* @throws CreateGeometryException
|
280 |
*/
|
281 |
private long showLineMeasures(GraphicLayer graphics, long curValue, Line line, Double distance) throws CreateGeometryException, LocatorException { |
282 |
long value = curValue;
|
283 |
for (int i = 0; i < line.getNumVertices() - 1; i++) { |
284 |
value++; |
285 |
Point vertex = line.getVertex(i);
|
286 |
double m0 = vertex.getCoordinateAt(vertex.getDimension() - 1); |
287 |
double div = m0 / distance;
|
288 |
if (isMultiple(m0, distance)) {
|
289 |
showMeasure(graphics, vertex, m0); |
290 |
} |
291 |
Point nextVertex = line.getVertex(i + 1); |
292 |
double m1 = nextVertex.getCoordinateAt(vertex.getDimension() - 1); |
293 |
List<Double> multiples = calculateMultiplesBetweenTwoValues(distance, m0, m1); |
294 |
for (Iterator iterator = multiples.iterator(); iterator.hasNext();) { |
295 |
Double m = (Double) iterator.next(); |
296 |
Point point = getPointWithMBetweenTwoVertex(m, vertex, nextVertex);
|
297 |
showMeasure(graphics, point, m); |
298 |
} |
299 |
} |
300 |
value++; |
301 |
return value;
|
302 |
} |
303 |
|
304 |
/**
|
305 |
* Show the m in the point p on the graphicLayer
|
306 |
*
|
307 |
* @param m
|
308 |
*/
|
309 |
private void showMeasure(GraphicLayer graphics, Point p, double m) { |
310 |
|
311 |
GeometryManager geomManager = GeometryLocator.getGeometryManager(); |
312 |
SymbolManager symbolManager = MapContextLocator.getSymbolManager(); |
313 |
|
314 |
ISymbol markerSymbol = symbolManager.createSymbol(Geometry.TYPES.POINT, Color.RED);
|
315 |
ISimpleTextSymbol textSymbol = getTextSymbol(); |
316 |
|
317 |
DecimalFormat formato = new DecimalFormat("####.000"); |
318 |
textSymbol.setText(formato.format(m).replace(",", " + ")); |
319 |
int idMarkerSymbol = graphics.addSymbol(markerSymbol);
|
320 |
int idTextSymbol = graphics.addSymbol(textSymbol);
|
321 |
graphics.addGraphic(SHOW_MEASURES_GRAPHICS_ID, p, idTextSymbol); |
322 |
graphics.addGraphic(SHOW_MEASURES_GRAPHICS_ID, p.cloneGeometry(), idMarkerSymbol); |
323 |
} |
324 |
|
325 |
/*
|
326 |
* Creates a text symbol to be used in showMeasure
|
327 |
*/
|
328 |
private ISimpleTextSymbol getTextSymbol(){
|
329 |
SymbologyManager symbologyManager = SymbologyLocator.getSymbologyManager(); |
330 |
ISimpleTextSymbol textSymbol = symbologyManager.createSimpleTextSymbol(); |
331 |
textSymbol.setFontSize(10);
|
332 |
return textSymbol;
|
333 |
} |
334 |
|
335 |
/**
|
336 |
* Dados dos puntos con coordenada M, calcula el punto correspondiente a la m dada.
|
337 |
*
|
338 |
* @param m
|
339 |
* @param vertex
|
340 |
* @param nextVertex
|
341 |
* @return
|
342 |
* @throws LocatorException
|
343 |
* @throws CreateGeometryException
|
344 |
*/
|
345 |
private Point getPointWithMBetweenTwoVertex(Double m, Point vertex, Point nextVertex) throws CreateGeometryException, LocatorException { |
346 |
Point point = (Point)GeometryLocator.getGeometryManager().create(Geometry.TYPES.POINT, Geometry.SUBTYPES.GEOM2D); |
347 |
point.setX( |
348 |
getCoordinateBetweenTwoValues( |
349 |
m, |
350 |
vertex.getCoordinateAt(vertex.getDimension()-1),
|
351 |
nextVertex.getCoordinateAt(vertex.getDimension()-1),
|
352 |
vertex.getX(), |
353 |
nextVertex.getX())); |
354 |
point.setY( |
355 |
getCoordinateBetweenTwoValues( |
356 |
m, |
357 |
vertex.getCoordinateAt(vertex.getDimension()-1),
|
358 |
nextVertex.getCoordinateAt(vertex.getDimension()-1),
|
359 |
vertex.getY(), |
360 |
nextVertex.getY())); |
361 |
|
362 |
return point;
|
363 |
} |
364 |
|
365 |
/**
|
366 |
* Dados los valores en una dimensi?n de dos puntos y sus "m" correspondientes,
|
367 |
* calcula el valor en esa dimensi?n correspondiente a la "m" dada.
|
368 |
*
|
369 |
* @param m
|
370 |
* @param m1
|
371 |
* @param m2
|
372 |
* @param v1
|
373 |
* @param v2
|
374 |
* @return
|
375 |
*/
|
376 |
private double getCoordinateBetweenTwoValues(double m, double m1, double m2, double v1, double v2) { |
377 |
if (v1==v2){
|
378 |
return Double.POSITIVE_INFINITY; |
379 |
} |
380 |
return ((v2-v1)*(m-m1)/(m2-m1))+v1;
|
381 |
} |
382 |
|
383 |
/**
|
384 |
* Calcula los m?ltiplos de "distance" que existen entre los valores "value0" y "value1"
|
385 |
*
|
386 |
* @param distance
|
387 |
* @param value0
|
388 |
* @param value1
|
389 |
* @return
|
390 |
*/
|
391 |
private List<Double> calculateMultiplesBetweenTwoValues(Double distance, double value0, double value1) { |
392 |
List<Double> result = new ArrayList<Double>(); |
393 |
|
394 |
double v0;
|
395 |
double v1;
|
396 |
if(value0<=value1){
|
397 |
v0 = value0; |
398 |
v1 = value1; |
399 |
} else {
|
400 |
v0 = value1; |
401 |
v1 = value0; |
402 |
} |
403 |
|
404 |
int factor1 = (int) Math.ceil(v0/distance); |
405 |
int factor2 = (int) Math.floor(v1/distance); |
406 |
for (int factor = factor1; factor <= factor2; factor++) { |
407 |
double m = factor * distance;
|
408 |
if(m>=v0 && m<=v1){
|
409 |
result.add(m); |
410 |
} |
411 |
} |
412 |
return result;
|
413 |
} |
414 |
|
415 |
|
416 |
/**
|
417 |
* Calcula si el valor del par?metro n es m?ltiplo del valor del par?metro d
|
418 |
* @param n
|
419 |
* @param d
|
420 |
*/
|
421 |
private boolean isMultiple(double n, double d) { |
422 |
double div = n / d;
|
423 |
return ((div - (int) div) < 0.000001); |
424 |
} |
425 |
|
426 |
/**
|
427 |
* @param selection
|
428 |
* @throws DataException
|
429 |
*/
|
430 |
private long getRange(FeatureSelection selection) throws DataException { |
431 |
long range = 0; |
432 |
DisposableIterator it = selection.fastIterator(); |
433 |
while (it.hasNext()) {
|
434 |
Feature feature = (Feature) it.next(); |
435 |
Geometry geometry = feature.getDefaultGeometry(); |
436 |
if (geometry instanceof Line) { |
437 |
range += ((Line) geometry).getNumVertices();
|
438 |
} else if (geometry instanceof MultiLine) { |
439 |
MultiLine multiLine = (MultiLine) geometry; |
440 |
int primitives = multiLine.getPrimitivesNumber();
|
441 |
for (int i = 0; i < primitives; i++) { |
442 |
range += ((Line) multiLine.getPrimitiveAt(i)).getNumVertices();
|
443 |
} |
444 |
} |
445 |
} |
446 |
it.dispose(); |
447 |
return range;
|
448 |
} |
449 |
|
450 |
private IView getActiveView() {
|
451 |
ApplicationManager application = ApplicationLocator.getManager(); |
452 |
IView view = (IView) application.getActiveComponent(ViewDocument.class); |
453 |
return view;
|
454 |
} |
455 |
|
456 |
private FLyrVect getActiveLayer(IView vista) {
|
457 |
if (vista != null) { |
458 |
ViewDocument viewDocument = vista.getViewDocument(); |
459 |
FLayer[] actives = viewDocument.getMapContext().getLayers().getActives();
|
460 |
|
461 |
if ((actives.length == 1) && (actives[0] instanceof FLyrVect)) { |
462 |
return (FLyrVect) actives[0]; |
463 |
} |
464 |
} |
465 |
return null; |
466 |
} |
467 |
|
468 |
/*
|
469 |
* (non-Javadoc)
|
470 |
*
|
471 |
* @see java.awt.event.ComponentListener#componentResized(java.awt.event.
|
472 |
* ComponentEvent)
|
473 |
*/
|
474 |
public void componentResized(ComponentEvent e) { |
475 |
// TODO Auto-generated method stub
|
476 |
|
477 |
} |
478 |
|
479 |
/*
|
480 |
* (non-Javadoc)
|
481 |
*
|
482 |
* @see
|
483 |
* java.awt.event.ComponentListener#componentMoved(java.awt.event.ComponentEvent
|
484 |
* )
|
485 |
*/
|
486 |
public void componentMoved(ComponentEvent e) { |
487 |
// TODO Auto-generated method stub
|
488 |
|
489 |
} |
490 |
|
491 |
/*
|
492 |
* (non-Javadoc)
|
493 |
*
|
494 |
* @see
|
495 |
* java.awt.event.ComponentListener#componentShown(java.awt.event.ComponentEvent
|
496 |
* )
|
497 |
*/
|
498 |
public void componentShown(ComponentEvent e) { |
499 |
// TODO Auto-generated method stub
|
500 |
|
501 |
} |
502 |
|
503 |
/*
|
504 |
* (non-Javadoc)
|
505 |
*
|
506 |
* @see java.awt.event.ComponentListener#componentHidden(java.awt.event.
|
507 |
* ComponentEvent)
|
508 |
*/
|
509 |
public void componentHidden(ComponentEvent e) { |
510 |
panel = null;
|
511 |
} |
512 |
} |