Revision 24226 trunk/extensions/extSymbology/src/org/gvsig/symbology/fmap/labeling/GeneralLabelingStrategy.java
GeneralLabelingStrategy.java | ||
---|---|---|
40 | 40 |
*/ |
41 | 41 |
|
42 | 42 |
/* CVS MESSAGES: |
43 |
* |
|
44 |
* $Id: GeneralLabelingStrategy.java 13749 2007-09-17 14:16:11Z jaume $ |
|
45 |
* $Log$ |
|
46 |
* Revision 1.2 2007-09-17 14:16:11 jaume |
|
47 |
* multilayer symbols sizing bug fixed |
|
48 |
* |
|
49 |
* Revision 1.1 2007/05/22 12:17:41 jaume |
|
50 |
* *** empty log message *** |
|
51 |
* |
|
52 |
* Revision 1.1 2007/05/22 10:05:31 jaume |
|
53 |
* *** empty log message *** |
|
54 |
* |
|
55 |
* Revision 1.10 2007/05/17 09:32:06 jaume |
|
56 |
* *** empty log message *** |
|
57 |
* |
|
58 |
* Revision 1.9 2007/05/09 11:04:58 jaume |
|
59 |
* refactored legend hierarchy |
|
60 |
* |
|
61 |
* Revision 1.8 2007/04/13 11:59:30 jaume |
|
62 |
* *** empty log message *** |
|
63 |
* |
|
64 |
* Revision 1.7 2007/04/12 14:28:43 jaume |
|
65 |
* basic labeling support for lines |
|
66 |
* |
|
67 |
* Revision 1.6 2007/04/11 16:01:08 jaume |
|
68 |
* maybe a label placer refactor |
|
69 |
* |
|
70 |
* Revision 1.5 2007/04/10 16:34:01 jaume |
|
71 |
* towards a styled labeling |
|
72 |
* |
|
73 |
* Revision 1.4 2007/04/02 16:34:56 jaume |
|
74 |
* Styled labeling (start commiting) |
|
75 |
* |
|
76 |
* Revision 1.3 2007/03/28 16:48:01 jaume |
|
77 |
* *** empty log message *** |
|
78 |
* |
|
79 |
* Revision 1.2 2007/03/26 14:40:38 jaume |
|
80 |
* added print method (BUT UNIMPLEMENTED) |
|
81 |
* |
|
82 |
* Revision 1.1 2007/03/20 16:16:20 jaume |
|
83 |
* refactored to use ISymbol instead of FSymbol |
|
84 |
* |
|
85 |
* Revision 1.2 2007/03/09 11:20:57 jaume |
|
86 |
* Advanced symbology (start committing) |
|
87 |
* |
|
88 |
* Revision 1.1 2007/03/09 08:33:43 jaume |
|
89 |
* *** empty log message *** |
|
90 |
* |
|
91 |
* Revision 1.1.2.5 2007/02/21 07:34:08 jaume |
|
92 |
* labeling starts working |
|
93 |
* |
|
94 |
* Revision 1.1.2.4 2007/02/15 16:23:44 jaume |
|
95 |
* *** empty log message *** |
|
96 |
* |
|
97 |
* Revision 1.1.2.3 2007/02/09 07:47:05 jaume |
|
98 |
* Isymbol moved |
|
99 |
* |
|
100 |
* Revision 1.1.2.2 2007/02/02 16:21:24 jaume |
|
101 |
* start commiting labeling stuff |
|
102 |
* |
|
103 |
* Revision 1.1.2.1 2007/02/01 17:46:49 jaume |
|
104 |
* *** empty log message *** |
|
105 |
* |
|
106 |
* |
|
107 |
*/ |
|
43 |
*
|
|
44 |
* $Id: GeneralLabelingStrategy.java 13749 2007-09-17 14:16:11Z jaume $
|
|
45 |
* $Log$
|
|
46 |
* Revision 1.2 2007-09-17 14:16:11 jaume
|
|
47 |
* multilayer symbols sizing bug fixed
|
|
48 |
*
|
|
49 |
* Revision 1.1 2007/05/22 12:17:41 jaume
|
|
50 |
* *** empty log message ***
|
|
51 |
*
|
|
52 |
* Revision 1.1 2007/05/22 10:05:31 jaume
|
|
53 |
* *** empty log message ***
|
|
54 |
*
|
|
55 |
* Revision 1.10 2007/05/17 09:32:06 jaume
|
|
56 |
* *** empty log message ***
|
|
57 |
*
|
|
58 |
* Revision 1.9 2007/05/09 11:04:58 jaume
|
|
59 |
* refactored legend hierarchy
|
|
60 |
*
|
|
61 |
* Revision 1.8 2007/04/13 11:59:30 jaume
|
|
62 |
* *** empty log message ***
|
|
63 |
*
|
|
64 |
* Revision 1.7 2007/04/12 14:28:43 jaume
|
|
65 |
* basic labeling support for lines
|
|
66 |
*
|
|
67 |
* Revision 1.6 2007/04/11 16:01:08 jaume
|
|
68 |
* maybe a label placer refactor
|
|
69 |
*
|
|
70 |
* Revision 1.5 2007/04/10 16:34:01 jaume
|
|
71 |
* towards a styled labeling
|
|
72 |
*
|
|
73 |
* Revision 1.4 2007/04/02 16:34:56 jaume
|
|
74 |
* Styled labeling (start commiting)
|
|
75 |
*
|
|
76 |
* Revision 1.3 2007/03/28 16:48:01 jaume
|
|
77 |
* *** empty log message ***
|
|
78 |
*
|
|
79 |
* Revision 1.2 2007/03/26 14:40:38 jaume
|
|
80 |
* added print method (BUT UNIMPLEMENTED)
|
|
81 |
*
|
|
82 |
* Revision 1.1 2007/03/20 16:16:20 jaume
|
|
83 |
* refactored to use ISymbol instead of FSymbol
|
|
84 |
*
|
|
85 |
* Revision 1.2 2007/03/09 11:20:57 jaume
|
|
86 |
* Advanced symbology (start committing)
|
|
87 |
*
|
|
88 |
* Revision 1.1 2007/03/09 08:33:43 jaume
|
|
89 |
* *** empty log message ***
|
|
90 |
*
|
|
91 |
* Revision 1.1.2.5 2007/02/21 07:34:08 jaume
|
|
92 |
* labeling starts working
|
|
93 |
*
|
|
94 |
* Revision 1.1.2.4 2007/02/15 16:23:44 jaume
|
|
95 |
* *** empty log message ***
|
|
96 |
*
|
|
97 |
* Revision 1.1.2.3 2007/02/09 07:47:05 jaume
|
|
98 |
* Isymbol moved
|
|
99 |
*
|
|
100 |
* Revision 1.1.2.2 2007/02/02 16:21:24 jaume
|
|
101 |
* start commiting labeling stuff
|
|
102 |
*
|
|
103 |
* Revision 1.1.2.1 2007/02/01 17:46:49 jaume
|
|
104 |
* *** empty log message ***
|
|
105 |
*
|
|
106 |
*
|
|
107 |
*/
|
|
108 | 108 |
package org.gvsig.symbology.fmap.labeling; |
109 | 109 |
|
110 | 110 |
import java.awt.Graphics2D; |
... | ... | |
141 | 141 |
|
142 | 142 |
import com.hardcode.gdbms.driver.exceptions.ReadDriverException; |
143 | 143 |
import com.hardcode.gdbms.engine.values.Value; |
144 |
import com.iver.andami.PluginServices; |
|
144 | 145 |
import com.iver.cit.gvsig.fmap.MapContext; |
145 | 146 |
import com.iver.cit.gvsig.fmap.ViewPort; |
146 | 147 |
import com.iver.cit.gvsig.fmap.core.CartographicSupport; |
... | ... | |
178 | 179 |
public static IPlacementConstraints DefaultPointPlacementConstraints = new PointPlacementConstraints(); |
179 | 180 |
public static IPlacementConstraints DefaultLinePlacementConstraints = new LinePlacementConstraints(); |
180 | 181 |
public static IPlacementConstraints DefaultPolygonPlacementConstraints = new PolygonPlacementConstraints(); |
181 |
private static String[] NO_TEXT = new String[] { "text " };
|
|
182 |
private static String[] NO_TEXT = {PluginServices.getText(null, "text_field")};
|
|
182 | 183 |
private static MultiShapePlacementConstraints DefaultMultiShapePlacementConstratints = new MultiShapePlacementConstraints(); |
183 | 184 |
private ILabelingMethod method; |
184 | 185 |
private IPlacementConstraints placementConstraints; |
... | ... | |
203 | 204 |
} |
204 | 205 |
|
205 | 206 |
public void draw(BufferedImage mapImage, Graphics2D mapGraphics, |
206 |
ViewPort viewPort, Cancellable cancel, double dpi) throws ReadDriverException { |
|
207 |
ViewPort viewPort, Cancellable cancel, double dpi) throws ReadDriverException {
|
|
207 | 208 |
|
208 | 209 |
// boolean bVisualFXEnabled = false; // if true, the user can see how the labeling is drawing up |
209 | 210 |
TreeSet<?> placedLabels = null; |
... | ... | |
234 | 235 |
// BufferedImage visualFXim = null; |
235 | 236 |
// Graphics2D visualFXgr = null; |
236 | 237 |
// if (bVisualFXEnabled) { |
237 |
// visualFXim = new BufferedImage(mapImage.getWidth(),
|
|
238 |
// mapImage.getHeight(),
|
|
239 |
// BufferedImage.TYPE_INT_ARGB);
|
|
240 |
// visualFXgr = visualFXim.createGraphics();
|
|
241 |
// visualFXgr.drawImage(mapImage, null, null);
|
|
238 |
// visualFXim = new BufferedImage(mapImage.getWidth(), |
|
239 |
// mapImage.getHeight(), |
|
240 |
// BufferedImage.TYPE_INT_ARGB); |
|
241 |
// visualFXgr = visualFXim.createGraphics(); |
|
242 |
// visualFXgr.drawImage(mapImage, null, null); |
|
242 | 243 |
// } |
243 | 244 |
// int drawEach = 300; // (milliseconds) |
244 | 245 |
// long lastTime = System.currentTimeMillis(); |
... | ... | |
262 | 263 |
if (placedLabels == null) |
263 | 264 |
placedLabels = new TreeSet<String[]>( |
264 | 265 |
new RemoveDuplicatesComparator()); |
265 |
else placedLabels.clear(); |
|
266 |
else placedLabels.clear();
|
|
266 | 267 |
} |
267 | 268 |
|
268 | 269 |
|
... | ... | |
272 | 273 |
Graphics2D overlapDetectGraphics = null; |
273 | 274 |
if (bLabelsReallocatable) { |
274 | 275 |
overlapDetectImage = new BufferedImage( |
275 |
mapImage.getWidth(), |
|
276 |
mapImage.getHeight(), |
|
276 |
mapImage.getWidth()+(int)viewPort.getOffset().getX(),
|
|
277 |
mapImage.getHeight()+(int)viewPort.getOffset().getY(),
|
|
277 | 278 |
BufferedImage.TYPE_INT_ARGB |
278 | 279 |
); |
279 | 280 |
overlapDetectGraphics = overlapDetectImage.createGraphics(); |
... | ... | |
295 | 296 |
ArrayList<LabelLocationMetrics> llm = null; |
296 | 297 |
// Calculate the label possible places |
297 | 298 |
// if (placement instanceof PolygonPlacementOnCentroid){ |
298 |
// llm = ((PolygonPlacementOnCentroid)placement).guess(
|
|
299 |
// lc,
|
|
300 |
// (FShape)geom.getInternalShape(),
|
|
301 |
// getPlacementConstraints(),
|
|
302 |
// 0,
|
|
303 |
// cancel,viewPort);
|
|
299 |
// llm = ((PolygonPlacementOnCentroid)placement).guess( |
|
300 |
// lc, |
|
301 |
// (FShape)geom.getInternalShape(), |
|
302 |
// getPlacementConstraints(), |
|
303 |
// 0, |
|
304 |
// cancel,viewPort); |
|
304 | 305 |
// }else if(placement instanceof PolygonPlacementParallel){ |
305 |
// llm = ((PolygonPlacementParallel)placement).guess(
|
|
306 |
// lc,
|
|
307 |
// (FShape)geom.getInternalShape(),
|
|
308 |
// getPlacementConstraints(),
|
|
309 |
// 0,
|
|
310 |
// cancel,viewPort);
|
|
306 |
// llm = ((PolygonPlacementParallel)placement).guess( |
|
307 |
// lc, |
|
308 |
// (FShape)geom.getInternalShape(), |
|
309 |
// getPlacementConstraints(), |
|
310 |
// 0, |
|
311 |
// cancel,viewPort); |
|
311 | 312 |
// }else{ |
312 |
// llm = placement.guess(
|
|
313 |
// lc,
|
|
314 |
// FConverter.transformToInts(geom, viewPort.getAffineTransform()),
|
|
315 |
// getPlacementConstraints(),
|
|
316 |
// 0,
|
|
317 |
// cancel);
|
|
313 |
// llm = placement.guess( |
|
314 |
// lc, |
|
315 |
// FConverter.transformToInts(geom, viewPort.getAffineTransform()), |
|
316 |
// getPlacementConstraints(), |
|
317 |
// 0, |
|
318 |
// cancel); |
|
318 | 319 |
// } |
319 | 320 |
|
321 |
|
|
322 |
|
|
323 |
|
|
324 |
|
|
320 | 325 |
llm = placement.guess( |
321 | 326 |
lc, |
322 | 327 |
geom, |
... | ... | |
332 | 337 |
} else { |
333 | 338 |
// // -- visual FX stuff |
334 | 339 |
// if (bVisualFXEnabled) { |
335 |
// targetBI = visualFXim;
|
|
336 |
// targetGr = visualFXgr;
|
|
340 |
// targetBI = visualFXim; |
|
341 |
// targetGr = visualFXgr; |
|
337 | 342 |
// } else { |
338 | 343 |
// // -- end visual FX stuff |
339 |
targetBI = mapImage;
|
|
340 |
targetGr = mapGraphics;
|
|
344 |
targetBI = mapImage; |
|
345 |
targetGr = mapGraphics; |
|
341 | 346 |
// } |
342 | 347 |
} |
343 | 348 |
|
349 |
|
|
350 |
|
|
351 |
setReferenceSystem(lc.getReferenceSystem()); |
|
352 |
setUnit(lc.getUnit()); |
|
353 |
|
|
354 |
double sizeBefore = lc.getTextSymbol().getFont().getSize(); |
|
355 |
|
|
356 |
|
|
357 |
double sizeAfter = CartographicSupportToolkit.getCartographicLength(this, |
|
358 |
sizeBefore, |
|
359 |
viewPort, |
|
360 |
MapContext.getScreenDPI()); |
|
361 |
|
|
362 |
lc.getTextSymbol().setFontSize(sizeAfter * FConstant.FONT_HEIGHT_SCALE_FACTOR); |
|
363 |
|
|
364 |
|
|
344 | 365 |
/* |
345 | 366 |
* search if there is room left by the previous and |
346 | 367 |
* with more priority labels, then check the current |
347 | 368 |
* level |
348 | 369 |
*/ |
349 | 370 |
if (lookupAndPlaceLabel(targetBI, targetGr, llm, |
350 |
placement, lc, geom, viewPort, cancel, bLabelsReallocatable)) { |
|
371 |
placement, lc, geom, viewPort, cancel, bLabelsReallocatable)) {
|
|
351 | 372 |
placedCount++; |
352 | 373 |
} else { |
353 | 374 |
notPlacedCount++; |
354 | 375 |
} |
355 | 376 |
|
377 |
lc.getTextSymbol().setFontSize(sizeBefore); |
|
378 |
|
|
356 | 379 |
// // -- visual FX stuff |
357 | 380 |
// if (bVisualFXEnabled && System.currentTimeMillis() - lastTime > drawEach) { |
358 |
// lastTime = System.currentTimeMillis();
|
|
359 |
// mapGraphics.drawImage(visualFXim, null, null);
|
|
381 |
// lastTime = System.currentTimeMillis(); |
|
382 |
// mapGraphics.drawImage(visualFXim, null, null); |
|
360 | 383 |
// } |
361 | 384 |
// // -- end visual FX stuff |
362 | 385 |
} |
... | ... | |
364 | 387 |
if (bLabelsReallocatable) { |
365 | 388 |
// // -- visual FX stuff |
366 | 389 |
// if (bVisualFXEnabled) |
367 |
// visualFXgr.drawImage(overlapDetectImage, null, null);
|
|
390 |
// visualFXgr.drawImage(overlapDetectImage, null, null); |
|
368 | 391 |
// else |
369 | 392 |
// // -- end visual FX stuff |
370 |
mapGraphics.drawImage(overlapDetectImage, null, null);
|
|
393 |
mapGraphics.drawImage(overlapDetectImage, null, null); |
|
371 | 394 |
} |
372 | 395 |
} |
373 | 396 |
|
... | ... | |
392 | 415 |
|
393 | 416 |
// // -- visual FX stuff |
394 | 417 |
// if (bVisualFXEnabled) { |
395 |
// mapGraphics.drawImage(visualFXim, null, null);
|
|
418 |
// mapGraphics.drawImage(visualFXim, null, null); |
|
396 | 419 |
// } |
397 | 420 |
// // -- end visual FX stuff |
398 | 421 |
} |
... | ... | |
409 | 432 |
LabelClass lc, IGeometry geom, ViewPort viewPort, |
410 | 433 |
Cancellable cancel, boolean bLabelsReallocatable) { |
411 | 434 |
int i; |
435 |
|
|
412 | 436 |
for (i = 0; !cancel.isCanceled() && i < llm.size(); i++) { |
413 | 437 |
LabelLocationMetrics labelMetrics = llm.get(i); |
414 | 438 |
|
415 | 439 |
if (bLabelsReallocatable) { |
416 |
if (!isOverlapping(bi, lc.getShape(labelMetrics))) {
|
|
440 |
if (!isOverlapping(bi, lc.getShape(labelMetrics))) { |
|
417 | 441 |
if(!getPlacementConstraints().isFollowingLine()){ |
418 | 442 |
lc.draw(g, labelMetrics, (FShape) geom.getInternalShape()); |
419 | 443 |
} |
... | ... | |
431 | 455 |
if(!getPlacementConstraints().isFollowingLine()) |
432 | 456 |
lc.draw(g, labelMetrics, null); |
433 | 457 |
else{ |
434 |
SmartTextSymbolLabelClass smsLc = new SmartTextSymbolLabelClass();
|
|
435 |
SmartTextSymbol sms = new SmartTextSymbol(lc.getTextSymbol(),getPlacementConstraints());
|
|
436 |
smsLc.setTextSymbol(sms);
|
|
437 |
geom.transform(viewPort.getAffineTransform());
|
|
438 |
smsLc.draw(g, null, (FShape) geom.getInternalShape());
|
|
439 |
}
|
|
458 |
SmartTextSymbolLabelClass smsLc = new SmartTextSymbolLabelClass(); |
|
459 |
SmartTextSymbol sms = new SmartTextSymbol(lc.getTextSymbol(),getPlacementConstraints()); |
|
460 |
smsLc.setTextSymbol(sms); |
|
461 |
geom.transform(viewPort.getAffineTransform()); |
|
462 |
smsLc.draw(g, null, (FShape) geom.getInternalShape()); |
|
463 |
} |
|
440 | 464 |
return true; |
441 | 465 |
} |
442 | 466 |
} |
... | ... | |
445 | 469 |
|
446 | 470 |
@SuppressWarnings("unchecked") |
447 | 471 |
private boolean setupLabel(IFeature feat, LabelClass lc, |
448 |
Cancellable cancel, String[] usedFields, ViewPort viewPort, |
|
449 |
double dpi, int duplicateMode, TreeSet<?> placedLabels) { |
|
472 |
Cancellable cancel, String[] usedFields, ViewPort viewPort,
|
|
473 |
double dpi, int duplicateMode, TreeSet<?> placedLabels) {
|
|
450 | 474 |
|
451 | 475 |
Value[] vv = feat.getAttributes(); |
452 | 476 |
String expr = lc.getStringLabelExpression(); |
453 | 477 |
|
454 | 478 |
long pt1 = System.currentTimeMillis(); |
455 | 479 |
String[] texts = NO_TEXT; |
480 |
// String[] texts = {PluginServices.getText(this, "text_field")}; |
|
456 | 481 |
try { |
457 | 482 |
|
458 | 483 |
for (int i = 0; !cancel.isCanceled() && i < usedFields.length; i++) { |
... | ... | |
494 | 519 |
} |
495 | 520 |
lc.setTexts(texts); |
496 | 521 |
|
497 |
setReferenceSystem(lc.getReferenceSystem()); |
|
498 |
setUnit(lc.getUnit()); |
|
499 |
|
|
500 |
double size = lc.getTextSymbol().getFont().getSize(); |
|
501 |
|
|
502 |
|
|
503 |
size = CartographicSupportToolkit.getCartographicLength(this, |
|
504 |
size, |
|
505 |
viewPort, |
|
506 |
MapContext.getScreenDPI()); |
|
507 |
|
|
508 |
lc.getTextSymbol().setFontSize(size); |
|
509 |
|
|
510 | 522 |
} catch (ExpressionException e) { |
511 | 523 |
e.printStackTrace(); |
512 | 524 |
return false; |
... | ... | |
538 | 550 |
private boolean isOverlapping(BufferedImage bi, FShape labelShape) { |
539 | 551 |
|
540 | 552 |
Rectangle2D rPixels = labelShape.getBounds2D(); |
541 |
for (int i= (int) rPixels.getX(); i<rPixels.getMaxX(); i++){
|
|
542 |
for (int j= (int) rPixels.getY(); j<rPixels.getMaxY(); j++){
|
|
543 |
if (!labelShape.contains(i, j)) {
|
|
544 |
continue;
|
|
545 |
}
|
|
553 |
for (int i= (int) rPixels.getX(); i<rPixels.getMaxX(); i++){
|
|
554 |
for (int j= (int) rPixels.getY(); j<rPixels.getMaxY(); j++){
|
|
555 |
if (!labelShape.contains(i, j)) {
|
|
556 |
continue;
|
|
557 |
}
|
|
546 | 558 |
|
547 |
if (i<0 || j<0) {
|
|
548 |
continue;
|
|
549 |
}
|
|
559 |
if (i<0 || j<0) {
|
|
560 |
continue;
|
|
561 |
}
|
|
550 | 562 |
|
551 |
if (bi.getWidth()<i+1 || bi.getHeight()<j+1) {
|
|
552 |
continue;
|
|
553 |
}
|
|
563 |
if (bi.getWidth()<i+1 || bi.getHeight()<j+1) {
|
|
564 |
continue;
|
|
565 |
}
|
|
554 | 566 |
|
555 |
if (bi.getRGB(i,j)!=0){
|
|
556 |
return true;
|
|
557 |
}
|
|
558 |
}
|
|
559 |
}
|
|
560 |
return false;
|
|
567 |
if (bi.getRGB(i,j)!=0){
|
|
568 |
return true;
|
|
569 |
}
|
|
570 |
}
|
|
571 |
}
|
|
572 |
return false;
|
|
561 | 573 |
} |
562 | 574 |
|
563 | 575 |
private boolean isOnePoint(ViewPort viewPort, IGeometry geom) { |
Also available in: Unified diff