Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.labeling.app / org.gvsig.labeling.app.mainplugin / src / main / java / org / gvsig / labeling / placements / PolygonPlacementOnCentroid.java @ 43510

History | View | Annotate | Download (3.75 KB)

1 43510 jjdelcerro
/**
2
 * gvSIG. Desktop Geographic Information System.
3 40911 jldominguez
 *
4 43510 jjdelcerro
 * Copyright (C) 2007-2013 gvSIG Association.
5 40911 jldominguez
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8 43510 jjdelcerro
 * as published by the Free Software Foundation; either version 3
9 40911 jldominguez
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18 43510 jjdelcerro
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20 40911 jldominguez
 *
21 43510 jjdelcerro
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23 40911 jldominguez
 */
24
package org.gvsig.labeling.placements;
25
26
import java.awt.geom.Point2D;
27
import java.awt.geom.Rectangle2D;
28
import java.util.ArrayList;
29
30
import org.gvsig.fmap.geom.Geometry;
31
import org.gvsig.fmap.geom.Geometry.TYPES;
32
import org.gvsig.fmap.geom.GeometryLocator;
33 43510 jjdelcerro
import org.gvsig.fmap.geom.GeometryManager;
34 40911 jldominguez
import org.gvsig.fmap.geom.primitive.Point;
35
import org.gvsig.fmap.mapcontext.ViewPort;
36
import org.gvsig.fmap.mapcontext.rendering.legend.styling.ILabelClass;
37
import org.gvsig.fmap.mapcontext.rendering.legend.styling.IPlacementConstraints;
38
import org.gvsig.symbology.fmap.mapcontext.rendering.legend.styling.LabelLocationMetrics;
39
import org.gvsig.tools.task.Cancellable;
40
import org.slf4j.Logger;
41
import org.slf4j.LoggerFactory;
42
43
public class PolygonPlacementOnCentroid extends MarkerPlacementOnPoint
44
implements ILabelPlacement {
45
46
        private static final Logger logger = LoggerFactory.getLogger(
47
                        PolygonPlacementOnCentroid.class);
48
49 43510 jjdelcerro
    @Override
50 40911 jldominguez
        public ArrayList<LabelLocationMetrics> guess(
51
                        ILabelClass lc, Geometry geom,
52
                        IPlacementConstraints placementConstraints,
53
                        double cartographicSymbolSize, Cancellable cancel, ViewPort vp) {
54
55 43510 jjdelcerro
                if (cancel.isCanceled()) {
56
            return CannotPlaceLabel.NO_PLACES;
57
        }
58 40911 jldominguez
59
                Point cen = null;
60
                try {
61
                        cen = geom.centroid();
62
                } catch (Exception e) {
63 43510 jjdelcerro
                        logger.warn("Can't get centroid of geometry.", e);
64 40911 jldominguez
                }
65
                return super.guess(
66
                                lc,
67
                                cen,
68
                                placementConstraints,
69
                                cartographicSymbolSize, cancel,vp);
70
        }
71
72 43510 jjdelcerro
    @Override
73 40911 jldominguez
        public ArrayList<LabelLocationMetrics> guess(
74
                        ILabelClass lc, Geometry geom,
75
                        IPlacementConstraints placementConstraints,
76
                        double cartographicSymbolSize, Cancellable cancel) {
77
78 43510 jjdelcerro
                if (cancel.isCanceled()) {
79
            return CannotPlaceLabel.NO_PLACES;
80
        }
81 40911 jldominguez
82
                ArrayList<LabelLocationMetrics> guessed =
83
                                new ArrayList<LabelLocationMetrics>();
84
85
                Point cen = null;
86
                try {
87
                        cen = geom.centroid();
88
                } catch (Exception e) {
89 43510 jjdelcerro
                        logger.warn("While getting centroid.", e);
90 40911 jldominguez
                }
91
                Point2D p = new Point2D.Double(cen.getX(), cen.getY());
92
                Rectangle2D bounds = lc.getBounds();
93
                p.setLocation(
94
                                p.getX() - (bounds.getWidth()*0.5),
95
                                p.getY() - (bounds.getHeight()*0.5));// - 2);
96
                guessed.add(new LabelLocationMetrics(p, 0, true));
97
                return guessed;
98
        }
99
100 43510 jjdelcerro
    @Override
101 40911 jldominguez
        public boolean isSuitableFor(
102
                        IPlacementConstraints placementConstraints,
103
                        int shapeType) {
104
105 43510 jjdelcerro
        GeometryManager geomManager = GeometryLocator.getGeometryManager();
106
        if( geomManager.isSubtype(TYPES.SURFACE, shapeType) ||
107
            geomManager.isSubtype(TYPES.MULTISURFACE, shapeType) ) {
108
                        return placementConstraints != null &&
109
                    (placementConstraints.isHorizontal() &&
110 40911 jldominguez
                                        !placementConstraints.isFitInsidePolygon());
111
                }
112
                return false;
113
        }
114
115
}