Revision 19360 trunk/extensions/extSymbology/src/org/gvsig/symbology/fmap/labeling/GeneralLabelingStrategy.java
GeneralLabelingStrategy.java | ||
---|---|---|
144 | 144 |
import com.iver.cit.gvsig.fmap.rendering.styling.labeling.LabelClass; |
145 | 145 |
import com.iver.cit.gvsig.fmap.rendering.styling.labeling.LabelLocationMetrics; |
146 | 146 |
import com.iver.cit.gvsig.fmap.rendering.styling.labeling.LabelingFactory; |
147 |
import com.iver.cit.gvsig.fmap.rendering.styling.labeling.RemoveDuplicatesComparator; |
|
147 | 148 |
import com.iver.utiles.XMLEntity; |
148 | 149 |
import com.iver.utiles.swing.threads.Cancellable; |
149 | 150 |
|
... | ... | |
161 | 162 |
private IPlacementConstraints placementConstraints; |
162 | 163 |
private FLyrVect layer; |
163 | 164 |
private IZoomConstraints zoomConstraints; |
164 |
|
|
165 |
|
|
165 | 166 |
public void setLayer(FLayer layer) throws ReadDriverException { |
166 | 167 |
FLyrVect l = (FLyrVect) layer; |
167 | 168 |
this.layer = l; |
... | ... | |
178 | 179 |
public void draw(BufferedImage mapImage, Graphics2D mapGraphics, ViewPort viewPort, |
179 | 180 |
Cancellable cancel, double dpi) |
180 | 181 |
throws ReadDriverException { |
182 |
|
|
183 |
TreeSet<?> placedLabels = null; |
|
184 |
|
|
181 | 185 |
long t1 = System.currentTimeMillis(); |
182 | 186 |
String[] usedFields = getUsedFields(); |
183 | 187 |
|
... | ... | |
185 | 189 |
int placedCount = 0; |
186 | 190 |
ILabelPlacement placement = PlacementManager.getPlacement(getPlacementConstraints(), layer.getShapeType()); |
187 | 191 |
|
192 |
/* handle the duplicates mode */ |
|
193 |
int duplicateMode = getPlacementConstraints().getDuplicateLabelsMode(); |
|
188 | 194 |
|
189 | 195 |
/* |
190 | 196 |
* get an ordered set of the LabelClasses up on the |
... | ... | |
204 | 210 |
*/ |
205 | 211 |
for (LabelClass lc : ts) { |
206 | 212 |
IFeatureIterator it = method.getFeatureIteratorByLabelClass(layer, lc, viewPort, usedFields); |
213 |
|
|
214 |
// duplicates treatment stuff |
|
215 |
if (duplicateMode == IPlacementConstraints.REMOVE_DUPLICATE_LABELS) { |
|
216 |
// we need to register the labels already placed |
|
217 |
if (placedLabels == null) |
|
218 |
placedLabels = new TreeSet<String[]>( |
|
219 |
new RemoveDuplicatesComparator()); |
|
220 |
else placedLabels.clear(); |
|
221 |
} |
|
222 |
|
|
223 |
// TODO if (duplicateMode == IPlacementConstraints.ONE_LABEL_PER_FEATURE) { |
|
224 |
// // we need to register the labels already placed |
|
225 |
// if (placedLabels == null) placedLabels = new TreeSet(); |
|
226 |
// else placedLabels.clear(); |
|
227 |
// } |
|
228 |
//duplicates stuff |
|
229 |
|
|
207 | 230 |
boolean bLabelsReallocatable = true; |
208 | 231 |
BufferedImage levelImg = null, |
209 | 232 |
bi = new BufferedImage(mapImage.getWidth(),mapImage.getHeight(),BufferedImage.TYPE_INT_ARGB); |
... | ... | |
220 | 243 |
IFeature feat = it.next(); |
221 | 244 |
IGeometry geom = feat.getGeometry(); |
222 | 245 |
|
223 |
if (!setupLabel(feat, lc, cancel, usedFields, viewPort, dpi)) |
|
246 |
if (!setupLabel(feat, lc, cancel, usedFields, viewPort, dpi, duplicateMode, placedLabels))
|
|
224 | 247 |
continue; |
225 | 248 |
|
226 | 249 |
// Check if size is a pixel |
... | ... | |
249 | 272 |
} |
250 | 273 |
} |
251 | 274 |
|
252 |
// Place a label |
|
275 |
|
|
276 |
|
|
277 |
|
|
278 |
// Calculate the label possible places |
|
253 | 279 |
ArrayList<LabelLocationMetrics> llm = placement.guess( |
254 | 280 |
lc, |
255 | 281 |
FConverter.transformToInts(geom, viewPort.getAffineTransform()), |
... | ... | |
281 | 307 |
logger.info("Labeled layer '"+layer.getName()+"' "+(System.currentTimeMillis()-t1)/1000D+" seconds. "+placedCount+"/"+total+" labels placed ("+NumberFormat.getInstance().format(100*placedCount/(double) total)+"%)"); |
282 | 308 |
} |
283 | 309 |
|
284 |
|
|
310 |
|
|
285 | 311 |
private boolean lookupAndPlaceLabel(BufferedImage[] bis, Graphics2D g, ArrayList<LabelLocationMetrics> llm, ILabelPlacement placement, LabelClass lc, IGeometry geom, ViewPort viewPort, Cancellable cancel, boolean bLabelsReallocatable) { |
286 |
|
|
287 |
|
|
288 | 312 |
int i; |
289 | 313 |
for (i = 0; !cancel.isCanceled() && i < llm.size(); i++) { |
290 | 314 |
LabelLocationMetrics labelMetrics = llm.get(i); |
... | ... | |
309 | 333 |
return false; |
310 | 334 |
} |
311 | 335 |
|
312 |
private boolean setupLabel(IFeature feat, LabelClass lc, Cancellable cancel, String[] usedFields, ViewPort viewPort, double dpi) { |
|
313 |
if (true) { |
|
336 |
private boolean setupLabel(IFeature feat, LabelClass lc, |
|
337 |
Cancellable cancel, String[] usedFields, ViewPort viewPort, |
|
338 |
double dpi, int duplicateMode, TreeSet<?> placedLabels) { |
|
339 |
|
|
314 | 340 |
Value[] vv = feat.getAttributes(); |
315 | 341 |
String expr = lc.getLabelExpression(); |
316 | 342 |
LabelExpressionParser parser = new LabelExpressionParser( |
... | ... | |
322 | 348 |
String[] texts; |
323 | 349 |
try { |
324 | 350 |
texts = parser.LabelExpression(); |
325 |
|
|
351 |
|
|
352 |
if (duplicateMode == IPlacementConstraints.REMOVE_DUPLICATE_LABELS) { |
|
353 |
// check if this text (so label) is already present in the map |
|
354 |
if (placedLabels.contains(texts)) |
|
355 |
// the label has already placed before |
|
356 |
return false; |
|
357 |
|
|
358 |
/* |
|
359 |
* the text is not present, it will be registered for next to |
|
360 |
* be avoided |
|
361 |
*/ |
|
362 |
((TreeSet<String[]>) placedLabels).add(texts); |
|
363 |
} |
|
326 | 364 |
lc.setTexts(texts); |
327 | 365 |
} catch (ParseException e) { |
328 | 366 |
e.printStackTrace(); |
329 | 367 |
return false; |
330 | 368 |
} |
331 |
} else { |
|
332 |
lc.setTexts(new String[0]); |
|
333 |
} |
|
334 | 369 |
|
335 | 370 |
lc.toCartographicSize(viewPort, dpi, null); |
336 | 371 |
return true; |
Also available in: Unified diff