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 / PolygonPlacementInside.java @ 43510

History | View | Annotate | Download (3.9 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 PolygonPlacementInside extends MarkerPlacementOnPoint implements ILabelPlacement {
44
45
        private static final Logger logger =
46
                        LoggerFactory.getLogger(PolygonPlacementInside.class);
47
48 43510 jjdelcerro
    @Override
49 40911 jldominguez
        public ArrayList<LabelLocationMetrics> guess(
50
                        ILabelClass lc, Geometry geom,
51
                        IPlacementConstraints placementConstraints,
52
                        double cartographicSymbolSize, Cancellable cancel, ViewPort vp) {
53
54 43510 jjdelcerro
                if (cancel.isCanceled()) {
55
            return CannotPlaceLabel.NO_PLACES;
56
        }
57 40911 jldominguez
58
                Point int_po = null;
59
60
                try {
61
62
                        Geometry vp_env = vp.getAdjustedEnvelope().getGeometry();
63
                        if (geom.intersects(vp_env)) {
64
                                geom = geom.intersection(vp_env);
65
                        }
66
67
                        int_po = geom.getInteriorPoint();
68
                } catch (Exception e) {
69
                        logger.error("While getting interior point.", e);
70
                }
71
                return super.guess(lc,
72
                                int_po,
73
                                placementConstraints, cartographicSymbolSize, cancel,vp);
74
        }
75
76 43510 jjdelcerro
    @Override
77 40911 jldominguez
        public ArrayList<LabelLocationMetrics> guess(
78
                        ILabelClass lc, Geometry geom,
79
                        IPlacementConstraints placementConstraints,
80
                        double cartographicSymbolSize, Cancellable cancel) {
81
82
                if (cancel.isCanceled()) return CannotPlaceLabel.NO_PLACES;
83
84
                ArrayList<LabelLocationMetrics> guessed =
85
                                new ArrayList<LabelLocationMetrics>();
86
87
                Point int_po = null;
88
                try {
89
                        int_po = geom.getInteriorPoint();
90
                } catch (Exception e) {
91
                        logger.error("While getting interior point.", e);
92
                }
93
                Point2D p = new Point2D.Double(int_po.getX(), int_po.getY());
94
                Rectangle2D bounds = lc.getBounds();
95
                p.setLocation(p.getX() - (bounds.getWidth()*0.5), 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(IPlacementConstraints placementConstraints,
102
                        int shapeType) {
103
104 43510 jjdelcerro
        GeometryManager geomManager = GeometryLocator.getGeometryManager();
105
        if( geomManager.isSubtype(TYPES.SURFACE, shapeType) ||
106
            geomManager.isSubtype(TYPES.MULTISURFACE, shapeType) ) {
107
            return placementConstraints != null &&
108
                (placementConstraints.isFitInsidePolygon() && !placementConstraints.isParallel());
109 40911 jldominguez
                } else {
110
                        return false;
111
                }
112
113
        }
114
115
}