root / trunk / extensions / extSymbology / src / org / gvsig / symbology / fmap / labeling / placements / MarkerPlacementAroundPoint.java @ 26037
History | View | Annotate | Download (6.35 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.symbology.fmap.labeling.placements; |
42 |
|
43 |
import java.awt.Rectangle; |
44 |
import java.awt.geom.NoninvertibleTransformException; |
45 |
import java.util.ArrayList; |
46 |
|
47 |
import org.gvsig.symbology.fmap.styles.PointLabelPositioneer; |
48 |
|
49 |
import com.iver.cit.gvsig.fmap.ViewPort; |
50 |
import com.iver.cit.gvsig.fmap.core.FPoint2D; |
51 |
import com.iver.cit.gvsig.fmap.core.FShape; |
52 |
import com.iver.cit.gvsig.fmap.core.IGeometry; |
53 |
import com.iver.cit.gvsig.fmap.core.ShapeFactory; |
54 |
import com.iver.cit.gvsig.fmap.core.v02.FConverter; |
55 |
import com.iver.cit.gvsig.fmap.rendering.styling.labeling.IPlacementConstraints; |
56 |
import com.iver.cit.gvsig.fmap.rendering.styling.labeling.LabelClass; |
57 |
import com.iver.cit.gvsig.fmap.rendering.styling.labeling.LabelLocationMetrics; |
58 |
import com.iver.utiles.swing.threads.Cancellable; |
59 |
|
60 |
public class MarkerPlacementAroundPoint implements ILabelPlacement { |
61 |
private static final MarkerCenteredAtPoint pos = new MarkerCenteredAtPoint(); |
62 |
|
63 |
public ArrayList<LabelLocationMetrics> guess(LabelClass lc, IGeometry geom, IPlacementConstraints placementConstraints, double cartographicSymbolSize, Cancellable cancel, ViewPort vp) { |
64 |
if (cancel.isCanceled()) return CannotPlaceLabel.NO_PLACES; |
65 |
|
66 |
FShape shp = FConverter.transformToInts(geom, vp.getAffineTransform()); |
67 |
|
68 |
if (placementConstraints instanceof PointPlacementConstraints) { |
69 |
PointPlacementConstraints ppc = (PointPlacementConstraints) placementConstraints; |
70 |
PointLabelPositioneer plp = ppc.getPositioneer(); |
71 |
|
72 |
if (plp != null) { |
73 |
FPoint2D p = (FPoint2D) shp; |
74 |
byte[] preferredPositions = plp.getPreferenceVector(); |
75 |
ArrayList<LabelLocationMetrics> highPreference = new ArrayList<LabelLocationMetrics>(); |
76 |
ArrayList<LabelLocationMetrics> normalPreference = new ArrayList<LabelLocationMetrics>(); |
77 |
ArrayList<LabelLocationMetrics> lowPreference = new ArrayList<LabelLocationMetrics>(); |
78 |
Rectangle bounds = lc.getBounds();
|
79 |
double width = bounds.getWidth();// *.5 + 2; |
80 |
double heigth = bounds.getHeight();
|
81 |
|
82 |
double offsetX = 0, offsetY = 0; |
83 |
for (int i = 0; i < preferredPositions.length; i++) { |
84 |
switch (i) {
|
85 |
case 0: |
86 |
case 3: |
87 |
case 5: |
88 |
// left
|
89 |
offsetX = -width; |
90 |
break;
|
91 |
case 2: |
92 |
case 4: |
93 |
case 7: |
94 |
// rigth
|
95 |
offsetX = width; |
96 |
break;
|
97 |
case 1: |
98 |
case 6: |
99 |
default:
|
100 |
// horizontally centered
|
101 |
offsetX = 0;
|
102 |
break;
|
103 |
} |
104 |
switch (i) {
|
105 |
case 0: |
106 |
case 1: |
107 |
case 2: |
108 |
// top
|
109 |
offsetY = -heigth; |
110 |
break;
|
111 |
case 5: |
112 |
case 6: |
113 |
case 7: |
114 |
// below
|
115 |
offsetY = heigth; |
116 |
break;
|
117 |
case 3: |
118 |
case 4: |
119 |
default:
|
120 |
// vertically centered
|
121 |
offsetY = 0;
|
122 |
break;
|
123 |
|
124 |
} |
125 |
// // xOffset
|
126 |
// switch (i % 3) {
|
127 |
// case 0:
|
128 |
// // left
|
129 |
// offsetX = -width;
|
130 |
// break;
|
131 |
// case 2:
|
132 |
// // rigth
|
133 |
// offsetX = width;
|
134 |
// break;
|
135 |
// case 1:
|
136 |
// default:
|
137 |
// // horizontally centered
|
138 |
// offsetX = 0;
|
139 |
// break;
|
140 |
//
|
141 |
// }
|
142 |
//
|
143 |
// switch (i / 3) {
|
144 |
// case 0:
|
145 |
// // top
|
146 |
// offsetY = -heigth;
|
147 |
// break;
|
148 |
// case 2:
|
149 |
// // below
|
150 |
// offsetY = heigth;
|
151 |
// break;
|
152 |
// case 1:
|
153 |
// default:
|
154 |
// // vertically centered
|
155 |
// offsetY = 0;
|
156 |
// break;
|
157 |
//
|
158 |
// }
|
159 |
FPoint2D aux = new FPoint2D(p.getX() + offsetX, p.getY()+offsetY);
|
160 |
|
161 |
IGeometry g=ShapeFactory.createPoint2D(aux); |
162 |
try {
|
163 |
g.transform(vp.getAffineTransform().createInverse()); |
164 |
} catch (NoninvertibleTransformException e) { |
165 |
e.printStackTrace(); |
166 |
} |
167 |
|
168 |
switch (preferredPositions[i]) {
|
169 |
case PointLabelPositioneer.FORBIDDEN:
|
170 |
break;
|
171 |
case PointLabelPositioneer.PREFERENCE_HIGH:
|
172 |
highPreference.addAll( |
173 |
pos.guess(lc, g, placementConstraints, cartographicSymbolSize, cancel,vp)); |
174 |
break;
|
175 |
case PointLabelPositioneer.PREFERENCE_NORMAL:
|
176 |
|
177 |
normalPreference.addAll( |
178 |
pos.guess(lc, g, placementConstraints, cartographicSymbolSize, cancel,vp)); |
179 |
break;
|
180 |
case PointLabelPositioneer.PREFERENCE_LOW:
|
181 |
lowPreference.addAll( |
182 |
pos.guess(lc, g, placementConstraints, cartographicSymbolSize, cancel,vp)); |
183 |
break;
|
184 |
|
185 |
default:
|
186 |
throw new Error("unrecognised label position preference value: "+preferredPositions[i]); |
187 |
} |
188 |
} |
189 |
|
190 |
ArrayList<LabelLocationMetrics> guessed = new ArrayList<LabelLocationMetrics>(); |
191 |
for (int j = 0; j < highPreference.size(); j++) guessed.add(highPreference.get(j)); |
192 |
for (int j = 0; j < normalPreference.size(); j++) guessed.add(normalPreference.get(j)); |
193 |
for (int j = 0; j < lowPreference.size(); j++) guessed.add(lowPreference.get(j)); |
194 |
return guessed;
|
195 |
} |
196 |
} |
197 |
|
198 |
return CannotPlaceLabel.NO_PLACES;
|
199 |
} |
200 |
|
201 |
|
202 |
|
203 |
public boolean isSuitableFor(IPlacementConstraints placementConstraints, |
204 |
int shapeType) {
|
205 |
if (shapeType == FShape.POINT || shapeType == FShape.MULTIPOINT) {// TODO (09/01/08) is this correct? if not fix it also in PlacementManager, PlacementProperties |
206 |
return placementConstraints.isAroundThePoint();
|
207 |
} |
208 |
return false; |
209 |
} |
210 |
|
211 |
|
212 |
|
213 |
} |