Search in sources :

Example 1 with PointCandidate

use of org.geotoolkit.display2d.style.labeling.candidate.PointCandidate in project geotoolkit by Geomatys.

the class PointLabelCandidateRenderer method render.

@Override
public void render(final Candidate candidate) {
    if (!(candidate instanceof PointCandidate))
        return;
    final PointCandidate pointCandidate = (PointCandidate) candidate;
    final PointLabelDescriptor label = pointCandidate.getDescriptor();
    final double rotation = Math.toRadians(label.getRotation());
    context.switchToDisplayCRS();
    // rotation--------------------------------------------------------------
    if (rotation != 0) {
        g2.rotate(rotation, pointCandidate.getCorrectedX(), pointCandidate.getCorrectedY());
    }
    final String text = label.getText();
    final Font font = label.getTextFont();
    g2.setFont(font);
    // paint halo------------------------------------------------------------
    final float haloWidth = label.getHaloWidth();
    if (haloWidth > 0) {
        final float haloWidth2 = haloWidth + haloWidth;
        final GlyphVector glyphVector = font.createGlyphVector(g2.getFontRenderContext(), text);
        final Rectangle2D bounds = glyphVector.getVisualBounds();
        Shape shape = new RoundRectangle2D.Double(bounds.getMinX() + pointCandidate.getCorrectedX() - haloWidth, bounds.getMinY() + pointCandidate.getCorrectedY() - haloWidth, bounds.getWidth() + haloWidth2, bounds.getHeight() + haloWidth2, 2 + haloWidth2, 2 + haloWidth2);
        g2.setPaint(label.getHaloPaint());
        g2.fill(shape);
    }
    // paint text------------------------------------------------------------
    g2.setPaint(label.getTextPaint());
    g2.drawString(text, pointCandidate.getCorrectedX(), pointCandidate.getCorrectedY());
    // reset rotation
    if (rotation != 0) {
        g2.rotate(-rotation, pointCandidate.getCorrectedX(), pointCandidate.getCorrectedY());
    }
}
Also used : GlyphVector(java.awt.font.GlyphVector) Shape(java.awt.Shape) PointCandidate(org.geotoolkit.display2d.style.labeling.candidate.PointCandidate) Rectangle2D(java.awt.geom.Rectangle2D) RoundRectangle2D(java.awt.geom.RoundRectangle2D) Font(java.awt.Font) PointLabelDescriptor(org.geotoolkit.display2d.style.labeling.PointLabelDescriptor)

Example 2 with PointCandidate

use of org.geotoolkit.display2d.style.labeling.candidate.PointCandidate in project geotoolkit by Geomatys.

the class SimulatedAnnealing method simulate.

public static Set<Candidate> simulate(final List<Candidate> cdts, double temperature, final double coolDown) {
    final Map<Candidate, Point> candidates = new HashMap<Candidate, Point>();
    for (Candidate cdt : cdts) {
        candidates.put(cdt, new Point((int) Math.floor(Math.random() * temperature - temperature / 2), (int) Math.floor(Math.random() * temperature - temperature / 2)));
    }
    // calculate the cost without any changes
    for (Candidate c : candidates.keySet()) {
        Point p = candidates.get(c);
        c.setCost(solutionCost(c, p, candidates));
    }
    // double bestSolutionCost = solutionCost(candidates);
    int sameResult = 0;
    int count = 0;
    // when we reach 10 times the same result, that means we haven't find anything better
    while (sameResult < 10) {
        System.out.println(count++ + " " + sameResult);
        boolean change = false;
        for (Candidate c : candidates.keySet()) {
            if (!(c instanceof PointCandidate))
                continue;
            Point p = candidates.get(c);
            // TODO find the best local solution using label intersecting this label
            boolean better = findBestLocalCombinaison((PointCandidate) c, p, candidates, temperature);
            if (better) {
                System.out.println("found better");
                change = true;
            }
        }
        sameResult = (change) ? 0 : sameResult + 1;
        // This approach of a global solution cost to much CPU,
        // we must find a way to make a local area best solution
        // // See if this improved the global solution
        // double solutionCost = solutionCost(candidates);
        // if (solutionCost < bestSolutionCost) {
        // bestSolutionCost = solutionCost;
        // 
        // //solution is better, change the candidate displacements
        // for(Candidate c : candidates.keySet()){
        // Point p = candidates.get(c);
        // if(c instanceof PointCandidate){
        // PointCandidate pc = (PointCandidate) c;
        // pc.x += pc.correctionX;
        // pc.y += pc.correctionY;
        // pc.correctionX = 0;
        // pc.correctionY = 0;
        // }
        // }
        // 
        // sameResult = 0;
        // } else {
        // sameResult++;
        // }
        // reduce temperature
        temperature = coolDown * temperature;
    }
    for (Candidate c : candidates.keySet()) {
        Point p = candidates.get(c);
        if (c instanceof PointCandidate) {
            PointCandidate pc = (PointCandidate) c;
            pc.correctionX = p.x;
            pc.correctionY = p.y;
        }
    }
    return candidates.keySet();
}
Also used : Candidate(org.geotoolkit.display2d.style.labeling.candidate.Candidate) PointCandidate(org.geotoolkit.display2d.style.labeling.candidate.PointCandidate) HashMap(java.util.HashMap) PointCandidate(org.geotoolkit.display2d.style.labeling.candidate.PointCandidate) Point(java.awt.Point) Point(java.awt.Point)

Example 3 with PointCandidate

use of org.geotoolkit.display2d.style.labeling.candidate.PointCandidate in project geotoolkit by Geomatys.

the class DecimationLabelRenderer method portrayLabels.

/**
 * {@inheritDoc }
 */
@Override
public boolean portrayLabels() {
    final Graphics2D g2 = context.getGraphics();
    // enable antialiasing for labels
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    // paint the remaining candidates
    for (Candidate candidate : candidates) {
        if (candidate instanceof PointCandidate) {
            pointRenderer.render(candidate);
        } else if (candidate instanceof LinearCandidate) {
            LinearRenderer.render(candidate);
        }
    }
    layers.clear();
    return !candidates.isEmpty();
}
Also used : Candidate(org.geotoolkit.display2d.style.labeling.candidate.Candidate) PointCandidate(org.geotoolkit.display2d.style.labeling.candidate.PointCandidate) LinearCandidate(org.geotoolkit.display2d.style.labeling.candidate.LinearCandidate) LinearCandidate(org.geotoolkit.display2d.style.labeling.candidate.LinearCandidate) PointCandidate(org.geotoolkit.display2d.style.labeling.candidate.PointCandidate) Graphics2D(java.awt.Graphics2D)

Example 4 with PointCandidate

use of org.geotoolkit.display2d.style.labeling.candidate.PointCandidate in project geotoolkit by Geomatys.

the class PointLabelCandidateRenderer method generateCandidat.

@Override
public Candidate[] generateCandidat(final PointLabelDescriptor label) {
    Geometry[] shapes = null;
    try {
        shapes = label.getGeometry().getDisplayGeometryJTS();
    } catch (TransformException ex) {
        Logger.getLogger("org.geotoolkit.display2d.style.labeling.decimate").log(Level.WARNING, null, ex);
    }
    if (shapes == null)
        return null;
    final List<Candidate> candidates = new ArrayList<>(shapes.length);
    for (int i = 0; i < shapes.length; i++) {
        final Geometry shape = shapes[i];
        final Point pt = GO2Utilities.getBestPoint(shape);
        if (pt == null || pt.isEmpty())
            continue;
        final Coordinate point = pt.getCoordinate();
        final FontMetrics metric = context.getFontMetrics(label.getTextFont());
        final int textUpper = metric.getAscent();
        final int textLower = metric.getDescent();
        final int textWidth = metric.stringWidth(label.getText());
        float refX = (float) point.x;
        float refY = (float) point.y;
        refX = refX + label.getDisplacementX();
        refY = refY - label.getDisplacementY();
        refX = refX - (label.getAnchorX() * textWidth);
        // text is draw above reference point so use +
        refY = refY + (label.getAnchorY() * textUpper);
        candidates.add(new PointCandidate(label, textWidth, textUpper, textLower, refX, refY));
    }
    return candidates.toArray(EMPTY);
}
Also used : Geometry(org.locationtech.jts.geom.Geometry) Candidate(org.geotoolkit.display2d.style.labeling.candidate.Candidate) PointCandidate(org.geotoolkit.display2d.style.labeling.candidate.PointCandidate) Coordinate(org.locationtech.jts.geom.Coordinate) FontMetrics(java.awt.FontMetrics) TransformException(org.opengis.referencing.operation.TransformException) ArrayList(java.util.ArrayList) PointCandidate(org.geotoolkit.display2d.style.labeling.candidate.PointCandidate) Point(org.locationtech.jts.geom.Point) Point(org.locationtech.jts.geom.Point)

Example 5 with PointCandidate

use of org.geotoolkit.display2d.style.labeling.candidate.PointCandidate in project geotoolkit by Geomatys.

the class DisplacementLabelRenderer method portrayLabels.

/**
 * {@inheritDoc }
 */
@Override
public boolean portrayLabels() {
    final Graphics2D g2 = context.getGraphics();
    // enable antialiasing for labels
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    List<Candidate> candidates = new ArrayList<Candidate>();
    // generate all the candidates
    // priority is in the order of the layers provided.
    int priority = layers.size();
    for (final LabelLayer layer : layers) {
        for (LabelDescriptor label : layer.labels()) {
            Candidate[] cs = null;
            if (label instanceof PointLabelDescriptor) {
                cs = pointRenderer.generateCandidat((PointLabelDescriptor) label);
            } else if (label instanceof LinearLabelDescriptor) {
                cs = LinearRenderer.generateCandidat((LinearLabelDescriptor) label);
            } else {
                cs = null;
            }
            if (cs != null) {
                for (Candidate c : cs) {
                    c.setPriority(priority);
                    candidates.add(c);
                }
            }
        }
        priority--;
    }
    // displace or remove the candidates
    candidates = optimize(candidates);
    // paint the remaining candidates
    for (Candidate candidate : candidates) {
        if (candidate instanceof PointCandidate) {
            pointRenderer.render(candidate);
        } else if (candidate instanceof LinearCandidate) {
            LinearRenderer.render(candidate);
        }
    }
    layers.clear();
    return !candidates.isEmpty();
}
Also used : Candidate(org.geotoolkit.display2d.style.labeling.candidate.Candidate) PointCandidate(org.geotoolkit.display2d.style.labeling.candidate.PointCandidate) LinearCandidate(org.geotoolkit.display2d.style.labeling.candidate.LinearCandidate) LinearCandidate(org.geotoolkit.display2d.style.labeling.candidate.LinearCandidate) LabelLayer(org.geotoolkit.display2d.style.labeling.LabelLayer) LinearLabelDescriptor(org.geotoolkit.display2d.style.labeling.LinearLabelDescriptor) ArrayList(java.util.ArrayList) PointCandidate(org.geotoolkit.display2d.style.labeling.candidate.PointCandidate) LabelDescriptor(org.geotoolkit.display2d.style.labeling.LabelDescriptor) LinearLabelDescriptor(org.geotoolkit.display2d.style.labeling.LinearLabelDescriptor) PointLabelDescriptor(org.geotoolkit.display2d.style.labeling.PointLabelDescriptor) Graphics2D(java.awt.Graphics2D) PointLabelDescriptor(org.geotoolkit.display2d.style.labeling.PointLabelDescriptor)

Aggregations

PointCandidate (org.geotoolkit.display2d.style.labeling.candidate.PointCandidate)6 Candidate (org.geotoolkit.display2d.style.labeling.candidate.Candidate)5 Graphics2D (java.awt.Graphics2D)2 Point (java.awt.Point)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 PointLabelDescriptor (org.geotoolkit.display2d.style.labeling.PointLabelDescriptor)2 LinearCandidate (org.geotoolkit.display2d.style.labeling.candidate.LinearCandidate)2 Font (java.awt.Font)1 FontMetrics (java.awt.FontMetrics)1 Shape (java.awt.Shape)1 GlyphVector (java.awt.font.GlyphVector)1 Rectangle2D (java.awt.geom.Rectangle2D)1 RoundRectangle2D (java.awt.geom.RoundRectangle2D)1 LabelDescriptor (org.geotoolkit.display2d.style.labeling.LabelDescriptor)1 LabelLayer (org.geotoolkit.display2d.style.labeling.LabelLayer)1 LinearLabelDescriptor (org.geotoolkit.display2d.style.labeling.LinearLabelDescriptor)1 Coordinate (org.locationtech.jts.geom.Coordinate)1 Geometry (org.locationtech.jts.geom.Geometry)1 Point (org.locationtech.jts.geom.Point)1