root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / rendering / styling / GeneralLabelingStrategy.java @ 13749
History | View | Annotate | Download (10.5 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 |
|
42 |
/* CVS MESSAGES:
|
43 |
*
|
44 |
* $Id: GeneralLabelingStrategy.java 13749 2007-09-17 14:16:11Z jaume $
|
45 |
* $Log$
|
46 |
* Revision 1.2 2007-09-17 14:16:11 jaume
|
47 |
* multilayer symbols sizing bug fixed
|
48 |
*
|
49 |
* Revision 1.1 2007/05/22 12:17:41 jaume
|
50 |
* *** empty log message ***
|
51 |
*
|
52 |
* Revision 1.1 2007/05/22 10:05:31 jaume
|
53 |
* *** empty log message ***
|
54 |
*
|
55 |
* Revision 1.10 2007/05/17 09:32:06 jaume
|
56 |
* *** empty log message ***
|
57 |
*
|
58 |
* Revision 1.9 2007/05/09 11:04:58 jaume
|
59 |
* refactored legend hierarchy
|
60 |
*
|
61 |
* Revision 1.8 2007/04/13 11:59:30 jaume
|
62 |
* *** empty log message ***
|
63 |
*
|
64 |
* Revision 1.7 2007/04/12 14:28:43 jaume
|
65 |
* basic labeling support for lines
|
66 |
*
|
67 |
* Revision 1.6 2007/04/11 16:01:08 jaume
|
68 |
* maybe a label placer refactor
|
69 |
*
|
70 |
* Revision 1.5 2007/04/10 16:34:01 jaume
|
71 |
* towards a styled labeling
|
72 |
*
|
73 |
* Revision 1.4 2007/04/02 16:34:56 jaume
|
74 |
* Styled labeling (start commiting)
|
75 |
*
|
76 |
* Revision 1.3 2007/03/28 16:48:01 jaume
|
77 |
* *** empty log message ***
|
78 |
*
|
79 |
* Revision 1.2 2007/03/26 14:40:38 jaume
|
80 |
* added print method (BUT UNIMPLEMENTED)
|
81 |
*
|
82 |
* Revision 1.1 2007/03/20 16:16:20 jaume
|
83 |
* refactored to use ISymbol instead of FSymbol
|
84 |
*
|
85 |
* Revision 1.2 2007/03/09 11:20:57 jaume
|
86 |
* Advanced symbology (start committing)
|
87 |
*
|
88 |
* Revision 1.1 2007/03/09 08:33:43 jaume
|
89 |
* *** empty log message ***
|
90 |
*
|
91 |
* Revision 1.1.2.5 2007/02/21 07:34:08 jaume
|
92 |
* labeling starts working
|
93 |
*
|
94 |
* Revision 1.1.2.4 2007/02/15 16:23:44 jaume
|
95 |
* *** empty log message ***
|
96 |
*
|
97 |
* Revision 1.1.2.3 2007/02/09 07:47:05 jaume
|
98 |
* Isymbol moved
|
99 |
*
|
100 |
* Revision 1.1.2.2 2007/02/02 16:21:24 jaume
|
101 |
* start commiting labeling stuff
|
102 |
*
|
103 |
* Revision 1.1.2.1 2007/02/01 17:46:49 jaume
|
104 |
* *** empty log message ***
|
105 |
*
|
106 |
*
|
107 |
*/
|
108 |
package com.iver.cit.gvsig.fmap.rendering.styling; |
109 |
|
110 |
import java.awt.Graphics2D; |
111 |
import java.awt.geom.Rectangle2D; |
112 |
import java.awt.image.BufferedImage; |
113 |
import java.util.ArrayList; |
114 |
import java.util.Hashtable; |
115 |
import java.util.logging.Level; |
116 |
import java.util.logging.Logger; |
117 |
|
118 |
import javax.print.attribute.PrintRequestAttributeSet; |
119 |
|
120 |
import com.hardcode.gdbms.driver.exceptions.ReadDriverException; |
121 |
import com.hardcode.gdbms.engine.values.Value; |
122 |
import com.iver.cit.gvsig.exceptions.expansionfile.ExpansionFileReadException; |
123 |
import com.iver.cit.gvsig.exceptions.visitors.VisitorException; |
124 |
import com.iver.cit.gvsig.fmap.ViewPort; |
125 |
import com.iver.cit.gvsig.fmap.core.IFeature; |
126 |
import com.iver.cit.gvsig.fmap.core.IGeometry; |
127 |
import com.iver.cit.gvsig.fmap.drivers.IFeatureIterator; |
128 |
import com.iver.cit.gvsig.fmap.layers.FBitSet; |
129 |
import com.iver.cit.gvsig.fmap.layers.FLayer; |
130 |
import com.iver.cit.gvsig.fmap.layers.FLyrVect; |
131 |
import com.iver.cit.gvsig.fmap.layers.ReadableVectorial; |
132 |
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource; |
133 |
import com.iver.utiles.XMLEntity; |
134 |
import com.iver.utiles.swing.threads.Cancellable; |
135 |
|
136 |
/**
|
137 |
* This is the default and simplest (therefore fastest) labeling strategy.
|
138 |
* @author jaume dominguez faus - jaume.dominguez@iver.es
|
139 |
*/
|
140 |
public class GeneralLabelingStrategy implements ILabelingStrategy { |
141 |
private static Logger logger = Logger.getAnonymousLogger(); |
142 |
private ILabelingMethod method;
|
143 |
private IPlacementConstraints placementConstraints;
|
144 |
private FLyrVect layer;
|
145 |
private IZoomConstraints zoomConstraints;
|
146 |
private Hashtable texts = new Hashtable(); |
147 |
|
148 |
// public GeneralLabelingStrategy() {}
|
149 |
// protected GeneralLabelingStrategy(FLayer layer) throws ReadDriverException {
|
150 |
// setLayer(layer);
|
151 |
// }
|
152 |
|
153 |
public void setLayer(FLayer layer) throws ReadDriverException, Error { |
154 |
FLyrVect l = (FLyrVect) layer; |
155 |
this.layer = l;
|
156 |
if (layer instanceof FLyrVect) { |
157 |
FLyrVect lyrVect = (FLyrVect) layer; |
158 |
placementConstraints = LabelingFactory.createPlacementConstraints(lyrVect); |
159 |
} else {
|
160 |
throw new Error("Layer type not yet supported"); |
161 |
} |
162 |
} |
163 |
|
164 |
public ILabelingMethod getLabelingMethod() {
|
165 |
return method;
|
166 |
} |
167 |
|
168 |
public void setLabelingMethod(ILabelingMethod method) { |
169 |
this.method = method;
|
170 |
} |
171 |
|
172 |
public void draw(BufferedImage image, Graphics2D g, ViewPort viewPort, |
173 |
Cancellable cancel) |
174 |
throws ReadDriverException {
|
175 |
|
176 |
|
177 |
if (method == null) { |
178 |
Logger.getAnonymousLogger().warning("Layer '"+layer.getName()+"'. No labeling method was set, labels will not be drawn"); |
179 |
return;
|
180 |
} |
181 |
|
182 |
if (zoomConstraints != null) { |
183 |
double scale = viewPort.getScale();
|
184 |
if (scale < zoomConstraints.getMinScale() ||
|
185 |
scale > zoomConstraints.getMaxScale()) |
186 |
return;
|
187 |
} |
188 |
|
189 |
IFeatureIterator it = layer.getSource().getFeatureIterator( |
190 |
viewPort.getAdjustedExtent(), |
191 |
getUsedFields(), |
192 |
viewPort.getProjection(), |
193 |
true);
|
194 |
|
195 |
try {
|
196 |
while (!cancel.isCanceled() && it.hasNext()) {
|
197 |
IFeature feat = it.next(); |
198 |
IGeometry geom = feat.getGeometry(); |
199 |
|
200 |
LabelClass lc = method.getDefaultLabelClass(); |
201 |
|
202 |
Value[] vv = feat.getAttributes();
|
203 |
String[] texts = new String[vv.length]; |
204 |
|
205 |
for (int i = 0; i < texts.length; i++) { |
206 |
texts[i] = vv[i].toString(); |
207 |
} |
208 |
|
209 |
lc.setTexts(texts); |
210 |
|
211 |
// refactor "if label shape is big enough"
|
212 |
placementConstraints.placeLabel(g, geom, lc, null, viewPort.getAffineTransform());
|
213 |
|
214 |
} |
215 |
} catch (ExpansionFileReadException e) {
|
216 |
throw new ReadDriverException( |
217 |
layer.getSource().getDriver().getName(), e); |
218 |
} |
219 |
} |
220 |
|
221 |
|
222 |
public void _draw(BufferedImage image, Graphics2D g, ViewPort viewPort, |
223 |
Cancellable cancel) |
224 |
throws ReadDriverException {
|
225 |
|
226 |
Rectangle2D theExtent = viewPort.getAdjustedExtent();
|
227 |
|
228 |
if (method == null) { |
229 |
Logger.getAnonymousLogger().warning("Layer '"+layer.getName()+"'. No labeling method was set, labels will not be drawn"); |
230 |
return;
|
231 |
} |
232 |
|
233 |
if (zoomConstraints != null) { |
234 |
double scale = viewPort.getScale();
|
235 |
if (scale < zoomConstraints.getMinScale() ||
|
236 |
scale > zoomConstraints.getMaxScale()) |
237 |
return;
|
238 |
} |
239 |
|
240 |
FBitSet bs; |
241 |
int i = -1; |
242 |
try {
|
243 |
bs = layer.queryByRect(theExtent); |
244 |
LabelClass lc = method.getDefaultLabelClass(); |
245 |
|
246 |
ReadableVectorial rv = layer.getSource(); |
247 |
SelectableDataSource sds = rv.getRecordset(); |
248 |
|
249 |
for(i=bs.nextSetBit(0); i>=0 && !cancel.isCanceled(); i=bs.nextSetBit(i+1)) { |
250 |
IGeometry geom; |
251 |
Integer index = new Integer(i); |
252 |
|
253 |
geom = rv.getShape(i); |
254 |
lc.setTexts(getText(index, sds)); |
255 |
// refactor
|
256 |
// if (labelShape is big enough)
|
257 |
{ |
258 |
// String[] texts = getText(index, sds);
|
259 |
|
260 |
placementConstraints.placeLabel(g, geom, lc, null, viewPort.getAffineTransform());
|
261 |
} |
262 |
|
263 |
|
264 |
// este ?s el cas quan gaste placementConstraints.getLocationsFor(...)
|
265 |
// if (labelShape != null) {
|
266 |
// FShape[] places = placementConstraints.getLocationsFor(
|
267 |
// geom, labelShape,
|
268 |
// null);
|
269 |
//
|
270 |
// for (int j = 0; j < places.length; j++) {
|
271 |
//// places[j].transform(viewPort.getAffineTransform());
|
272 |
// lc.drawInsideRectangle(g, places[j].getBounds());
|
273 |
//
|
274 |
// }
|
275 |
// }
|
276 |
} |
277 |
} catch (ExpansionFileReadException e) {
|
278 |
logger.log(Level.SEVERE, e.getMessage());
|
279 |
} catch (ReadDriverException e) {
|
280 |
logger.log(Level.SEVERE, "Getting label text for "+i+"th shape of "+layer.getName()); |
281 |
} catch (VisitorException e) {
|
282 |
logger.log(Level.SEVERE, "Getting label text for "+i+"th shape of "+layer.getName()); |
283 |
} |
284 |
} |
285 |
|
286 |
private String[] getText(Integer index, SelectableDataSource sds) throws ReadDriverException { |
287 |
String text = (String) texts.get(index); |
288 |
if (text == null) { |
289 |
String expr = method.getDefaultLabelClass().getLabelExpression();
|
290 |
|
291 |
// TODO el analizador sint?ctico de momento s?lo pilla un valor del combo por
|
292 |
// defecto
|
293 |
String fieldName = expr.replaceAll("\\[", "").replaceAll("\\]", "").trim(); |
294 |
|
295 |
text = sds.getFieldValue(index.longValue(), sds.getFieldIndexByName(fieldName)).toString(); |
296 |
texts.put(index, text); |
297 |
} |
298 |
return new String[] { text }; |
299 |
} |
300 |
|
301 |
|
302 |
public String getClassName() { |
303 |
return getClass().getName();
|
304 |
} |
305 |
|
306 |
public XMLEntity getXMLEntity() {
|
307 |
// TODO Implement it
|
308 |
// throw new Error("Not yet implemented!");
|
309 |
return new XMLEntity(); |
310 |
} |
311 |
|
312 |
public void setXMLEntity(XMLEntity xml) { |
313 |
// TODO Implement it
|
314 |
// throw new Error("Not yet implemented!");
|
315 |
|
316 |
} |
317 |
|
318 |
public IPlacementConstraints getPlacementConstraints() {
|
319 |
return placementConstraints;
|
320 |
} |
321 |
|
322 |
public void setPlacementConstraints(IPlacementConstraints constraints) { |
323 |
this.placementConstraints = constraints;
|
324 |
} |
325 |
|
326 |
public IZoomConstraints getZoomConstraints() {
|
327 |
return zoomConstraints;
|
328 |
} |
329 |
|
330 |
public void setZoomConstraints(IZoomConstraints constraints) { |
331 |
this.zoomConstraints = constraints;
|
332 |
} |
333 |
|
334 |
public void print(Graphics2D g, ViewPort viewPort, Cancellable cancel, PrintRequestAttributeSet properties) throws ReadDriverException { |
335 |
// double originalScaleFactor = unitFactor;
|
336 |
// PrintQuality pq = (PrintQuality) properties.get(PrintQuality.class);
|
337 |
// if (pq.equals(PrintQuality.NORMAL)){
|
338 |
// unitFactor *= (double) 300/72;
|
339 |
// }else if (pq.equals(PrintQuality.HIGH)){
|
340 |
// unitFactor *= (double) 600/72;
|
341 |
// }else if (pq.equals(PrintQuality.DRAFT)){
|
342 |
// // unitFactor *= 72; (which is the same than doing nothing)
|
343 |
// }
|
344 |
// draw(null, g, viewPort, cancel);
|
345 |
// unitFactor = originalScaleFactor;
|
346 |
} |
347 |
|
348 |
public String[] getUsedFields() { |
349 |
LabelClass[] lcs = method.getLabelClasses();
|
350 |
ArrayList<String> fieldNames = new ArrayList<String>(); |
351 |
for (int i = 0; i < lcs.length; i++) { |
352 |
String expr = lcs[i].getLabelExpression();
|
353 |
/*
|
354 |
* TODO
|
355 |
* implement the expression parser, and extract the names of the
|
356 |
* fields involved
|
357 |
*/
|
358 |
// this is just a provisional
|
359 |
fieldNames.add( expr.replaceAll(" *", "").replaceAll("\\[|\\]", "") ); |
360 |
|
361 |
} |
362 |
return fieldNames.toArray(new String[fieldNames.size()]); |
363 |
} |
364 |
} |