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 @ 47790

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