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