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 / MarkerPlacementAroundPoint.java @ 40911

History | View | Annotate | Download (6.48 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2005 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41
package org.gvsig.labeling.placements;
42

    
43
import java.awt.Rectangle;
44
import java.awt.geom.NoninvertibleTransformException;
45
import java.util.ArrayList;
46

    
47
import org.gvsig.fmap.geom.Geometry;
48
import org.gvsig.fmap.geom.Geometry.TYPES;
49
import org.gvsig.fmap.geom.primitive.Point;
50
import org.gvsig.fmap.mapcontext.ViewPort;
51
import org.gvsig.fmap.mapcontext.rendering.legend.styling.ILabelClass;
52
import org.gvsig.fmap.mapcontext.rendering.legend.styling.IPlacementConstraints;
53
import org.gvsig.symbology.fmap.mapcontext.rendering.legend.styling.LabelLocationMetrics;
54
import org.gvsig.tools.task.Cancellable;
55
import org.slf4j.Logger;
56
import org.slf4j.LoggerFactory;
57

    
58
public class MarkerPlacementAroundPoint implements ILabelPlacement {
59

    
60
        private static Logger logger =
61
                        LoggerFactory.getLogger(MarkerPlacementAroundPoint.class);
62
        private static final MarkerPlacementOnPoint pos = new MarkerPlacementOnPoint();
63

    
64
        public ArrayList<LabelLocationMetrics> guess(
65
                        ILabelClass lc,
66
                        Geometry geom,
67
                        IPlacementConstraints placementConstraints,
68
                        double cartographicSymbolSize, Cancellable cancel, ViewPort vp) {
69
                
70
        if (cancel.isCanceled()) return CannotPlaceLabel.NO_PLACES;
71

    
72
        Geometry shp = geom.cloneGeometry();
73
        shp.transform(vp.getAffineTransform());
74

    
75
                if (placementConstraints instanceof PointPlacementConstraints) {
76
                        PointPlacementConstraints ppc = (PointPlacementConstraints) placementConstraints;
77
                        PointLabelPositioner plp = ppc.getPositioner();
78

    
79
                        if (plp != null) {
80
                                Point po = null;
81
                                try {
82
                                        po = shp.centroid();
83
                                } catch (Exception exc) {
84
                                        logger.error("While getting centroid.", exc);
85
                                }
86
                                
87
                                int[] preferredPositions = plp.getPreferenceVector();
88
                                ArrayList<LabelLocationMetrics> highPreference = new ArrayList<LabelLocationMetrics>();
89
                                ArrayList<LabelLocationMetrics> normalPreference = new ArrayList<LabelLocationMetrics>();
90
                                ArrayList<LabelLocationMetrics> lowPreference = new ArrayList<LabelLocationMetrics>();
91
                                Rectangle bounds = lc.getBounds();
92
                                double width = bounds.getWidth()*.5; // + 2; //?por qu? el +2?
93
                                double heigth = bounds.getHeight()*.5;
94

    
95
                                double offsetX = 0, offsetY = 0;
96
                                for (int i = 0; i < preferredPositions.length; i++) {
97
                                        switch (i) {
98
                                        case 0:
99
                                        case 3:
100
                                        case 5:
101
                                                // left
102
                                                offsetX = -width;
103
                                                break;
104
                                        case 2:
105
                                        case 4:
106
                                        case 7:
107
                                                // rigth
108
                                                offsetX = width;
109
                                                break;
110
                                        case 1:
111
                                        case 6:
112
                                        default:
113
                                                // horizontally centered
114
                                                offsetX = 0;
115
                                                break;
116
                                        }
117
                                        switch (i) {
118
                                        case 0:
119
                                        case 1:
120
                                        case 2:
121
                                                // top
122
                                                offsetY = -heigth;
123
                                                break;
124
                                        case 5:
125
                                        case 6:
126
                                        case 7:
127
                                                // below
128
                                                offsetY = heigth;
129
                                                break;
130
                                        case 3:
131
                                        case 4:
132
                                        default:
133
                                                // vertically centered
134
                                                offsetY = 0;
135
                                                break;
136

    
137
                                        }
138
//                                        // xOffset
139
//                                        switch (i % 3) {
140
//                                        case 0:
141
//                                                // left
142
//                                                offsetX = -width;
143
//                                                break;
144
//                                        case 2:
145
//                                                // rigth
146
//                                                offsetX = width;
147
//                                                break;
148
//                                        case 1:
149
//                                        default:
150
//                                                // horizontally centered
151
//                                                offsetX = 0;
152
//                                                break;
153
//
154
//                                        }
155
//
156
//                                        switch (i / 3) {
157
//                                        case 0:
158
//                                                // top
159
//                                                offsetY = -heigth;
160
//                                                break;
161
//                                        case 2:
162
//                                                // below
163
//                                                offsetY = heigth;
164
//                                                break;
165
//                                        case 1:
166
//                                        default:
167
//                                                // vertically centered
168
//                                                offsetY = 0;
169
//                                                break;
170
//
171
//                                        }
172
                                        
173
                                        po.setX(po.getX() + offsetX);
174
                                        po.setY(po.getY() + offsetY);
175
                                        try {
176
                                                po.transform(vp.getAffineTransform().createInverse());
177
                                        } catch (NoninvertibleTransformException e) {
178
                                                logger.error("uninvertible matrix.", e);
179
                                        }
180

    
181
                                        switch (preferredPositions[i]) {
182
                                        case PointLabelPositioner.FORBIDDEN:
183
                                                break;
184
                                        case PointLabelPositioner.PREFERENCE_HIGH:
185
                                                highPreference.addAll(
186
                                                                pos.guess(lc, po, placementConstraints, cartographicSymbolSize, cancel,vp));
187
                                                break;
188
                                        case PointLabelPositioner.PREFERENCE_NORMAL:
189

    
190
                                                normalPreference.addAll(
191
                                                                pos.guess(lc, po, placementConstraints, cartographicSymbolSize, cancel,vp));
192
                                                break;
193
                                        case PointLabelPositioner.PREFERENCE_LOW:
194
                                                lowPreference.addAll(
195
                                                                pos.guess(lc, po, placementConstraints, cartographicSymbolSize, cancel,vp));
196
                                                break;
197

    
198
                                        default:
199
                                                throw new Error("unrecognised label position preference value: "+preferredPositions[i]);
200
                                        }
201
                                }
202

    
203
                                ArrayList<LabelLocationMetrics> guessed = new ArrayList<LabelLocationMetrics>();
204
                                for (int j = 0; j < highPreference.size(); j++)                guessed.add(highPreference.get(j));
205
                                for (int j = 0; j < normalPreference.size(); j++)        guessed.add(normalPreference.get(j));
206
                                for (int j = 0; j < lowPreference.size(); j++)                guessed.add(lowPreference.get(j));
207
                                return guessed;
208
                        }
209
                }
210

    
211
                return CannotPlaceLabel.NO_PLACES;
212
        }
213

    
214

    
215

    
216
        public boolean isSuitableFor(IPlacementConstraints placementConstraints,
217
                        int shapeType) {
218
                
219
                if (shapeType == TYPES.POINT
220
                                || shapeType == TYPES.MULTIPOINT) {
221
                        // TODO (09/01/08) is this correct? if not fix it also in PlacementManager, PlacementProperties
222
                        return placementConstraints.isAroundThePoint();
223
                } else {
224
                        return false;
225
                }
226
                
227
        }
228

    
229

    
230

    
231
}