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 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
5 |
*
|
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 |
* as published by the Free Software Foundation; either version 3
|
9 |
* 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 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
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 |
import org.gvsig.fmap.geom.GeometryManager; |
34 |
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 |
@Override
|
50 |
public ArrayList<LabelLocationMetrics> guess( |
51 |
ILabelClass lc, Geometry geom, |
52 |
IPlacementConstraints placementConstraints, |
53 |
double cartographicSymbolSize, Cancellable cancel, ViewPort vp) {
|
54 |
|
55 |
if (cancel.isCanceled()) {
|
56 |
return CannotPlaceLabel.NO_PLACES;
|
57 |
} |
58 |
|
59 |
Point cen = null; |
60 |
try {
|
61 |
cen = geom.centroid(); |
62 |
} catch (Exception e) { |
63 |
logger.warn("Can't get centroid of geometry.", e);
|
64 |
} |
65 |
return super.guess( |
66 |
lc, |
67 |
cen, |
68 |
placementConstraints, |
69 |
cartographicSymbolSize, cancel,vp); |
70 |
} |
71 |
|
72 |
@Override
|
73 |
public ArrayList<LabelLocationMetrics> guess( |
74 |
ILabelClass lc, Geometry geom, |
75 |
IPlacementConstraints placementConstraints, |
76 |
double cartographicSymbolSize, Cancellable cancel) {
|
77 |
|
78 |
if (cancel.isCanceled()) {
|
79 |
return CannotPlaceLabel.NO_PLACES;
|
80 |
} |
81 |
|
82 |
ArrayList<LabelLocationMetrics> guessed =
|
83 |
new ArrayList<LabelLocationMetrics>(); |
84 |
|
85 |
Point cen = null; |
86 |
try {
|
87 |
cen = geom.centroid(); |
88 |
} catch (Exception e) { |
89 |
logger.warn("While getting centroid.", e);
|
90 |
} |
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 |
@Override
|
101 |
public boolean isSuitableFor( |
102 |
IPlacementConstraints placementConstraints, |
103 |
int shapeType) {
|
104 |
|
105 |
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 |
!placementConstraints.isFitInsidePolygon()); |
111 |
} |
112 |
return false; |
113 |
} |
114 |
|
115 |
} |