Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.symbology / org.gvsig.symbology.lib / org.gvsig.symbology.lib.impl / src / main / java / org / gvsig / symbology / fmap / mapcontext / rendering / dynamiclegend / DefaultDynamicLabelingStrategy.java @ 47464

History | View | Annotate | Download (21.6 KB)

1 44247 omartinez
/*
2
 * To change this license header, choose License Headers in Project Properties.
3
 * To change this template file, choose Tools | Templates
4
 * and open the template in the editor.
5
 */
6
package org.gvsig.symbology.fmap.mapcontext.rendering.dynamiclegend;
7
8
import java.awt.Color;
9
import java.awt.Font;
10
import java.awt.Graphics2D;
11
import java.awt.image.BufferedImage;
12 44249 omartinez
import java.util.ArrayList;
13
import java.util.List;
14
import org.cresques.cts.ICoordTrans;
15
import org.cresques.cts.IProjection;
16 44247 omartinez
import org.gvsig.compat.print.PrintAttributes;
17 44249 omartinez
import org.gvsig.expressionevaluator.Expression;
18
import org.gvsig.expressionevaluator.ExpressionEvaluatorLocator;
19
import org.gvsig.expressionevaluator.MutableSymbolTable;
20
import org.gvsig.expressionevaluator.SymbolTable;
21
import org.gvsig.fmap.dal.DALLocator;
22 44247 omartinez
import org.gvsig.fmap.dal.exception.ReadException;
23 44249 omartinez
import org.gvsig.fmap.dal.expressionevaluator.FeatureSymbolTable;
24
import org.gvsig.fmap.dal.feature.Feature;
25
import org.gvsig.fmap.dal.feature.FeatureQuery;
26
import org.gvsig.fmap.dal.feature.FeatureSelection;
27
import org.gvsig.fmap.dal.feature.FeatureSet;
28
import org.gvsig.fmap.dal.feature.FeatureStore;
29 44247 omartinez
import org.gvsig.fmap.geom.Geometry;
30 44249 omartinez
import org.gvsig.fmap.geom.exception.CreateGeometryException;
31
import org.gvsig.fmap.geom.operation.GeometryOperationException;
32
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException;
33
import org.gvsig.fmap.geom.primitive.Envelope;
34
import org.gvsig.fmap.geom.primitive.Point;
35
import org.gvsig.fmap.mapcontext.MapContext;
36 44247 omartinez
import org.gvsig.fmap.mapcontext.ViewPort;
37
import org.gvsig.fmap.mapcontext.layers.FLayer;
38 44249 omartinez
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
39
import org.gvsig.fmap.mapcontext.layers.vectorial.SpatialEvaluatorsFactory;
40 44247 omartinez
import org.gvsig.fmap.mapcontext.rendering.legend.styling.ILabelingMethod;
41
import org.gvsig.fmap.mapcontext.rendering.legend.styling.IPlacementConstraints;
42
import org.gvsig.fmap.mapcontext.rendering.legend.styling.IZoomConstraints;
43 44534 omartinez
import org.gvsig.symbology.SymbologyLocator;
44 44249 omartinez
import org.gvsig.symbology.fmap.mapcontext.rendering.legend.styling.AttrInTableLabelingStrategy;
45
import static org.gvsig.symbology.fmap.mapcontext.rendering.legend.styling.AttrInTableLabelingStrategy.MIN_TEXT_SIZE;
46
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.impl.CartographicSupportToolkit;
47
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.text.impl.SimpleTextSymbol;
48
import org.gvsig.tools.dispose.DisposableIterator;
49
import org.gvsig.tools.evaluator.Evaluator;
50
import org.gvsig.tools.exception.BaseException;
51 44247 omartinez
import org.gvsig.tools.persistence.PersistentState;
52
import org.gvsig.tools.persistence.exception.PersistenceException;
53
import org.gvsig.tools.task.Cancellable;
54 44249 omartinez
import org.slf4j.Logger;
55
import org.slf4j.LoggerFactory;
56 44247 omartinez
57
/**
58
 *
59
 * @author osc
60
 */
61 44534 omartinez
public class DefaultDynamicLabelingStrategy implements DynamicLabelingStrategy {
62 44247 omartinez
63 44249 omartinez
    private Feature feature;
64 44257 omartinez
    public String LROTATION = "LROTATION";
65
    public String LTEXT = "LTEXT";
66
    public String LHEIGHT = "LHEIGHT";
67
    public String LCOLOR = "LCOLOR";
68
    public String LFONT = "LFONT";
69
    public String LFONTSTYLE = "LFONTS";
70
71
    public static Expression expRotation = null;
72
    public static Expression expText = null;
73
    public static Expression expHeight = null;
74
    public static Expression expColor = null;
75
    public static Expression expFont = null;
76
    public static Expression expFontStyle = null;
77
    public static Expression expUnit;
78
    public static Expression expReferenceSystem;
79
80 44249 omartinez
    private MutableSymbolTable symbolTable = null;
81 44257 omartinez
    private FeatureSymbolTable featureSymbolTable = null;
82 44249 omartinez
    private FLyrVect layer;
83
    private List<Geometry> drawnGeometryLabels;
84
    private boolean printMode = false;
85
    private IZoomConstraints zoom = null;
86
    private static final Logger logger = LoggerFactory.getLogger(AttrInTableLabelingStrategy.class);
87
    private int referenceSystem;
88
    private String textField;
89 44534 omartinez
90 44249 omartinez
91
    public SymbolTable getFeatureSymbolTable() {
92
        if (symbolTable == null) {
93
            MutableSymbolTable s = ExpressionEvaluatorLocator.getManager().createSymbolTable();
94 44257 omartinez
            featureSymbolTable = DALLocator.getDataManager().createFeatureSymbolTable();
95
            s.addSymbolTable(featureSymbolTable);
96
            if (feature!=null) {
97
                featureSymbolTable.setFeature(feature);
98
            }
99 44249 omartinez
            symbolTable = s;
100
        }
101
        return symbolTable;
102
103 44247 omartinez
    }
104
105 44257 omartinez
//    private Expression getExpressionFromString(String value) {
106
//        Expression expression = ExpressionEvaluatorLocator.getManager().createExpression();
107
//        expression.setPhrase(value);
108
//        return expression;
109
//    }
110 44249 omartinez
    private Object getValueFromExpression(Expression exp) {
111
        if (exp == null) {
112
            return null;
113
        }
114
        Object value = exp.execute(getFeatureSymbolTable());
115
        return value;
116
    }
117
118
    public Object getComputedValue(String field) {
119
        Object value;
120
        value = feature.get(field);
121
122
        return value;
123
    }
124
125 44247 omartinez
    @Override
126 44249 omartinez
    public Expression getRotation() {
127 44257 omartinez
        return expRotation;
128 44247 omartinez
    }
129
130
    @Override
131 44249 omartinez
    public int getComputedRotation() {
132
        Integer value = (Integer) getValueFromExpression(getRotation());
133
        return value;
134 44247 omartinez
    }
135
136
    @Override
137 44249 omartinez
    public Expression getText() {
138 44257 omartinez
        return expText;
139 44247 omartinez
    }
140
141
    @Override
142 44249 omartinez
    public String getComputedText() {
143
        String value = (String) getValueFromExpression(getText());
144
        return value;
145 44247 omartinez
    }
146
147
    @Override
148 44249 omartinez
    public Expression getHeight() {
149 44257 omartinez
        return expHeight;
150 44247 omartinez
    }
151
152
    @Override
153 44249 omartinez
    public int getComputedHeight() {
154
        int value = (int) getValueFromExpression(getHeight());
155
        return value;
156
    }
157
158
    @Override
159
    public Expression getColor() {
160 44257 omartinez
        return expColor;
161 44249 omartinez
    }
162
163
    @Override
164
    public Color getComputedColor() {
165
        int rgbcolor = (int) getColor().execute(getFeatureSymbolTable());
166
        Color color = new Color(rgbcolor, true);
167
        return color;
168
    }
169
170
    @Override
171
    public Expression getFixedSize() {
172 44257 omartinez
        return expHeight;
173 44249 omartinez
    }
174
175
    @Override
176
    public double getComputedFixedSize() {
177
        double value = (double) getValueFromExpression(getFixedSize());
178
        return value;
179
    }
180
181
    @Override
182
    public int getComputedUnit() {
183
        int value = (int) getValueFromExpression(getUnitExp());
184
        return value;
185
    }
186
187
    @Override
188 44257 omartinez
    public void setText(Expression expression) {
189
        expText = expression;
190 44247 omartinez
    }
191
192
    @Override
193 44257 omartinez
    public void setRotation(Expression expression) {
194
        expRotation = expression;
195 44247 omartinez
    }
196
197
    @Override
198 44257 omartinez
    public void setHeight(Expression expression) {
199
        expHeight = expression;
200 44247 omartinez
    }
201
202
    @Override
203 44257 omartinez
    public void setColor(Expression expression) {
204
        expColor = expression;
205 44247 omartinez
    }
206
207
    @Override
208
    public void setUsesFixedSize(boolean b) {
209
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
210
    }
211
212
    @Override
213
    public boolean usesFixedSize() {
214 44249 omartinez
        return false;
215 44247 omartinez
    }
216
217
    @Override
218
    public void setFixedSize(double fixedSize) {
219
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
220
    }
221
222
    @Override
223
    public void setUsesFixedColor(boolean b) {
224
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
225
    }
226
227
    @Override
228
    public boolean usesFixedColor() {
229 44249 omartinez
        return true;
230 44247 omartinez
    }
231
232
    @Override
233 44249 omartinez
    public Expression getFixedColor() {
234 44257 omartinez
        return expColor;
235 44247 omartinez
    }
236
237
    @Override
238 44249 omartinez
    public Color getComputedFixedColor() {
239
        int rgbcolor = (int) getFixedColor().execute(getFeatureSymbolTable());
240
        Color color = new Color(rgbcolor, true);
241
        return color;
242
    }
243
244
    @Override
245 44247 omartinez
    public void setFixedColor(Color fixedColor) {
246
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
247
    }
248
249
    @Override
250 44249 omartinez
    public Expression getColorFont() {
251 44257 omartinez
        return expColor;
252 44247 omartinez
    }
253
254
    @Override
255 44249 omartinez
    public Color getComputedColorFont() {
256
        int rgbcolor = (int) getColorFont().execute(getFeatureSymbolTable());
257
        Color color = new Color(rgbcolor, true);
258
        return color;
259
    }
260
261
    @Override
262 44247 omartinez
    public void setColorFont(Color colorFont) {
263
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
264
    }
265
266
    @Override
267 44249 omartinez
    public Expression getFont() {
268 44257 omartinez
        return expFont;
269 44247 omartinez
    }
270
271
    @Override
272 44249 omartinez
    public Font getComputedFont() {
273
        String value = (String) getFont().execute(getFeatureSymbolTable());
274
        Font font = new Font(value, getComputedFontStyle(), getComputedHeight());
275
        return font;
276
    }
277
278
    @Override
279 44247 omartinez
    public void setFont(Font selFont) {
280
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
281
    }
282
283 44249 omartinez
    @Override
284
    public Expression getFontStyle() {
285 44257 omartinez
        return expFontStyle;
286 44249 omartinez
    }
287
288
    @Override
289
    public int getComputedFontStyle() {
290
        int value = (int) getFontStyle().execute(getFeatureSymbolTable());
291
        return value;
292
    }
293
294
    @Override
295 44257 omartinez
    public void setFontStyle(Expression expression) {
296
        expFontStyle = expression;
297 44249 omartinez
    }
298
299
    public void setFeature(Feature f) {
300
        feature = f;
301 44257 omartinez
        if (featureSymbolTable==null) {
302
            getFeatureSymbolTable();
303
        }
304
        featureSymbolTable.setFeature(f);
305 44249 omartinez
    }
306
307
    @Override
308
    public ILabelingMethod getLabelingMethod() {
309
        throw new UnsupportedOperationException("Not supported yet.");
310
    }
311
312
    @Override
313
    public void setLabelingMethod(ILabelingMethod method) {
314
        throw new UnsupportedOperationException("Not supported yet.");
315
    }
316
317
    @Override
318
    public IPlacementConstraints getPlacementConstraints() {
319
        throw new UnsupportedOperationException("Not supported yet.");
320
    }
321
322
    @Override
323
    public void setPlacementConstraints(IPlacementConstraints constraints) {
324
        throw new UnsupportedOperationException("Not supported yet.");
325
    }
326
327
    @Override
328
    public IZoomConstraints getZoomConstraints() {
329
        throw new UnsupportedOperationException("Not supported yet.");
330
    }
331
332
    @Override
333
    public void setZoomConstraints(IZoomConstraints constraints) {
334
        throw new UnsupportedOperationException("Not supported yet.");
335
    }
336
337
    @Override
338
    public Expression getUnitExp() {
339 44257 omartinez
//        String value = "-1";
340
//        Expression exp = getExpressionFromString(value);
341
//        return exp;
342
        return expUnit;
343 44249 omartinez
    }
344
345
    public Expression getReferenceSystemExp() {
346 44257 omartinez
//        String value = "0";
347
//        Expression exp = getExpressionFromString(value);
348
        return expReferenceSystem;
349 44249 omartinez
    }
350
351
    private org.gvsig.fmap.geom.primitive.Point createLabelPoint(org.gvsig.fmap.geom.Geometry geom)
352
            throws CreateGeometryException {
353
354
        try {
355
            return geom.centroid();
356
        } catch (GeometryOperationNotSupportedException e) {
357
            return null;
358
        } catch (GeometryOperationException e) {
359
            return null;
360
        }
361
    }
362
363
    @Override
364
    public void draw(BufferedImage mapImage, Graphics2D mapGraphics, double scale, ViewPort viewPort, Cancellable cancel, double dpi) throws ReadException {
365
//        double scale = getScale(viewPort, props);
366
        PrintAttributes props = null;
367
        if (layer == null) {
368
            return;
369
        }
370
371
        if (zoom == null
372
                || (zoom.isUserDefined() && (scale >= zoom.getMaxScale())
373
                && (scale <= zoom.getMinScale()))) {
374
            FeatureSet set = null;
375
            DisposableIterator iterator = null;
376
            try {
377
//                AttrInTableLabelingStrategy.FilteredLogger logger = new AttrInTableLabelingStrategy.FilteredLogger(AttrInTableLabelingStrategy.logger, 10);
378
379
                // limit the labeling to the visible extent
380
                List<String> fields = new ArrayList<String>();
381
382
                FeatureStore featureStore = layer.getFeatureStore();
383
                ICoordTrans ct = layer.getCoordTrans();
384
385
                String geomName = featureStore.getDefaultFeatureType().getDefaultGeometryAttributeName();
386 44256 omartinez
                //TODO
387 44249 omartinez
                fields.add(LCOLOR);
388
                fields.add(LFONT);
389
                fields.add(LFONTSTYLE);
390
                fields.add(LHEIGHT);
391
                fields.add(LROTATION);
392
                fields.add(LTEXT);
393
                fields.add(geomName);
394
395
                FeatureQuery featureQuery = layer.createFeatureQuery();
396
397
                if (viewPort.getAdjustedEnvelope().contains(
398
                        layer.getFullEnvelope())) {
399
                    /*
400
                     * viewport contains layer completely
401
                     */
402
                    set = featureStore.getFeatureSet(featureQuery);
403
                } else {
404
                    /*
405
                     * view port does not contain layer completely
406
                     */
407
                    Evaluator iee = null;
408
409
                    IProjection data_proj = null;
410
                    Envelope env_in_store_crs = null;
411
412
                    if (ct == null) {
413
                        env_in_store_crs = viewPort.getAdjustedEnvelope();
414
                        data_proj = viewPort.getProjection();
415
                    } else {
416
                        env_in_store_crs = viewPort.getAdjustedEnvelope().convert(
417
                                ct.getInverted());
418
                        data_proj = ct.getPOrig();
419
                    }
420
                    iee = SpatialEvaluatorsFactory.getInstance().intersects(
421
                            env_in_store_crs,
422
                            data_proj,
423
                            featureStore
424
                    );
425
                    featureQuery.setAttributeNames((String[]) fields.toArray(new String[fields.size()]));
426
                    featureQuery.setFilter(iee);
427
                    set = featureStore.getFeatureSet(featureQuery);
428
                }
429
430
                /*
431
                 * 'set' now has the features that have to be labeled
432
                 */
433
                FeatureSelection selection = this.layer.getFeatureStore().getFeatureSelection();
434
                iterator = set.fastIterator();
435
                while (iterator.hasNext()) {
436
                    if (cancel.isCanceled()) {
437
                        return;
438
                    }
439
                    try {
440
                        Feature feat = (Feature) iterator.next();
441
                        Geometry geom = feat.getDefaultGeometry();
442
                        if (geom == null) {
443
                            continue;
444
                        }
445
446
                        double size = 0;
447
                        Color color = null;
448
                        this.setFeature(feat);
449
                        if ("".equals(getComputedText())) {
450
                            continue;
451
                        } else if (getComputedText() == null) {
452
                            continue;
453
                        }
454
455
                        SimpleTextSymbol sym = new SimpleTextSymbol();
456
                        sym.setFont(getComputedFont());
457
                        sym.setUnit(getComputedUnit());
458 44257 omartinez
                        sym.setReferenceSystem(getComputedReferenceSystem());
459 44249 omartinez
                        if (this.usesFixedSize()) { //(useFixedSize) {
460
                            // uses fixed size
461
                            size = getComputedFixedSize();// * fontScaleFactor;
462
                        } else if (true) { //(getComputedHeight() ) {
463
                            // text size is defined in the table
464
                            size = getComputedHeight();
465
                        } else {
466
                            // otherwise will use the size in the symbol
467
                            size = sym.getFont().getSize();
468
                        }
469
470
                        double the_dpi = viewPort.getDPI();
471
                        if (props != null) {
472
                            int len = PrintAttributes.PRINT_QUALITY_DPI.length;
473
                            int priq = props.getPrintQuality();
474
                            if (priq < 0 || priq >= len) {
475
                                throw new ReadException(featureStore.getName(),
476
                                        new Exception("Bad print quality value: " + priq));
477
                            } else {
478
                                the_dpi = PrintAttributes.PRINT_QUALITY_DPI[priq];
479
                            }
480
                        }
481
482
                        size = CartographicSupportToolkit.getCartographicLength(this,
483
                                size,
484
                                viewPort,
485
                                the_dpi);
486
                        if (size <= MIN_TEXT_SIZE) {
487
                            // label is too small to be readable, will be skipped
488
                            // this speeds up the rendering in wider zooms
489
                            continue;
490
                        }
491
492
                        /*
493
                         * ym.setFontSize(int) assumes dpi = 72
494
                         */
495
                        double font_size = size * 72 / the_dpi;
496
                        sym.setFontSize(font_size);
497
498
//                        if (false) { //useFixedColor) {
499
//                            color = getComputedFixedColor();
500
//                        } else if (true) { //colorFieldName != null) {
501
//                            // text size is defined in the table
502
                        if (selection.isSelected(feat)) {
503
                            color = MapContext.getSelectionColor();
504
                        } else {
505
                            color = getComputedColorFont();
506
                        }
507
508
                        sym.setTextColor(color);
509
510
                        if (ct != null) {
511
                            /*
512
                             * Reproject from store CRS to view CRS
513
                             */
514
                            geom = geom.cloneGeometry();
515
                            geom.reProject(ct);
516
                        }
517
518
                        Point p = null;
519
                        // text rotation is defined in the table
520
                        double rotation = -Math.toRadians(((Number) getComputedRotation()).doubleValue());
521
                        p = createLabelPoint(geom);
522
523
                        sym.setText(getComputedText());
524
                        sym.setRotation(rotation);
525
526
                        if (p != null) {
527
                            if (props == null) {
528
                                sym.draw(mapGraphics, viewPort.getAffineTransform(), p, feat, cancel);
529
                            } else {
530
                                sym.print(mapGraphics, viewPort.getAffineTransform(), p, props);
531
                            }
532
                        }
533
                    } catch (Exception ex) {
534
                        logger.warn("", ex);
535
                    }
536
                }
537
538
            } catch (BaseException e) {
539
                throw new ReadException(
540
                        "Could not draw annotation in the layer.", e);
541
            } finally {
542
                if (iterator != null) {
543
                    iterator.dispose();
544
                }
545
                if (set != null) {
546
                    set.dispose();
547
                }
548
549
            }
550
551
        }
552
553
    }
554
555
    @Override
556
    public void print(Graphics2D g, double scale, ViewPort viewPort, Cancellable cancel, PrintAttributes properties) throws ReadException {
557
        throw new UnsupportedOperationException("Not supported yet.");
558
    }
559
560
    @Override
561
    public String[] getUsedFields() {
562
        throw new UnsupportedOperationException("Not supported yet.");
563
    }
564
565
    @Override
566
    public void setLayer(FLayer flayer) {
567
        layer = (FLyrVect) flayer;
568
    }
569
570
    @Override
571
    public boolean shouldDrawLabels(double scale) {
572
        return true;
573
    }
574
575
    @Override
576
    public void saveToState(PersistentState state) throws PersistenceException {
577
        throw new UnsupportedOperationException("Not supported yet.");
578
    }
579
580
    @Override
581
    public void loadFromState(PersistentState state) throws PersistenceException {
582
        throw new UnsupportedOperationException("Not supported yet.");
583
    }
584
585
    @Override
586
    public void setUnit(int unitIndex) {
587
        throw new UnsupportedOperationException("Not supported yet.");
588
    }
589
590 44257 omartinez
    public void setUnit(Expression expression) {
591
        expUnit = expression;
592
    }
593
594 44249 omartinez
    @Override
595
    public int getUnit() {
596
        return getComputedUnitExp();
597
    }
598
599
    public int getComputedUnitExp() {
600
        int value = (int) getValueFromExpression(getUnitExp());
601
        return value;
602
    }
603
604 44257 omartinez
605
    public int getComputedReferenceSystem() {
606 44249 omartinez
        int value = (int) getValueFromExpression(getReferenceSystemExp());
607
        return value;
608
    }
609 44257 omartinez
610 44249 omartinez
    @Override
611
    public int getReferenceSystem() {
612 44257 omartinez
        return getComputedReferenceSystem();
613 44249 omartinez
    }
614
615
    @Override
616
    public void setReferenceSystem(int referenceSystem) {
617 44257 omartinez
//        this.referenceSystem = referenceSystem;
618 44249 omartinez
    }
619
620
    @Override
621
    public double toCartographicSize(ViewPort viewPort, double dpi, Geometry geom) {
622
        throw new UnsupportedOperationException("Not supported yet.");
623
    }
624
625
    @Override
626
    public void setCartographicSize(double cartographicSize, Geometry geom) {
627
        throw new UnsupportedOperationException("Not supported yet.");
628
    }
629
630
    @Override
631
    public double getCartographicSize(ViewPort viewPort, double dpi, Geometry geom) {
632
        throw new UnsupportedOperationException("Not supported yet.");
633
    }
634
635
    @Override
636 44257 omartinez
    public void setFont(Expression expression) {
637
        expFont = expression;
638 44249 omartinez
    }
639
640
    @Override
641 44257 omartinez
    public void setFontStyle(int style) {
642
643 44249 omartinez
    }
644
645 44257 omartinez
    @Override
646
    public void setReferenceSystem(Expression expression) {
647
        expReferenceSystem = expression;
648
    }
649
650 44247 omartinez
}