Search in sources :

Example 1 with LabelLayer

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

the class J2DPainter method paint.

public boolean paint(RenderingContext2D renderingContext, Stream<Presentation> presentations, boolean labelsOnTop) throws PortrayalException {
    boolean dataPainted = false;
    final List<TextPresentation> labels = new ArrayList<>();
    final Iterator<Presentation> iterator = presentations.iterator();
    while (iterator.hasNext()) {
        Presentation next = iterator.next();
        if (labelsOnTop && (next instanceof TextPresentation)) {
            labels.add((TextPresentation) next);
        } else {
            paint(renderingContext, next);
        }
        dataPainted = true;
    }
    if (labelsOnTop) {
        final LabelRenderer lr = renderingContext.getLabelRenderer(true);
        LabelLayer ll = lr.createLabelLayer();
        for (TextPresentation tp : labels) {
            ll.labels().add(tp.labelDesc);
        }
        lr.append(ll);
        try {
            lr.portrayLabels();
        } catch (TransformException ex) {
            throw new PortrayalException(ex.getMessage(), ex);
        }
    }
    return dataPainted;
}
Also used : LabelRenderer(org.geotoolkit.display2d.style.labeling.LabelRenderer) LabelLayer(org.geotoolkit.display2d.style.labeling.LabelLayer) ArrayList(java.util.ArrayList) TransformException(org.opengis.referencing.operation.TransformException) Grid2DPresentation(org.geotoolkit.display2d.presentation.Grid2DPresentation) ExceptionPresentation(org.apache.sis.internal.map.ExceptionPresentation) GroupPresentation(org.geotoolkit.renderer.GroupPresentation) Presentation(org.apache.sis.internal.map.Presentation) TextPresentation(org.geotoolkit.display2d.presentation.TextPresentation) TextPresentation(org.geotoolkit.display2d.presentation.TextPresentation) PortrayalException(org.geotoolkit.display.PortrayalException)

Example 2 with LabelLayer

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

the class J2DGridUtilities method paint.

public static void paint(final RenderingContext2D context, final GridTemplate template) {
    CoordinateReferenceSystem gridCRS = template.getCRS();
    // use context crs if gridcrs is not defined
    if (gridCRS == null)
        gridCRS = context.getObjectiveCRS();
    final Graphics2D g = context.getGraphics();
    context.switchToDisplayCRS();
    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g.setComposite(GO2Utilities.ALPHA_COMPOSITE_1F);
    // calculate the cliping zone for texts
    final float xTextOffset = template.getXTextOffset();
    final float yTextOffset = template.getYTextOffset();
    final Rectangle clip = new Rectangle(context.getCanvasDisplayBounds());
    clip.x += xTextOffset;
    clip.height -= yTextOffset;
    final Shape shp = new TransformedShape(clip, context.getDisplayToObjective());
    final List<Coordinate> coords = new ArrayList<>();
    final PathIterator ite = shp.getPathIterator(null);
    final double[] vals = new double[3];
    while (!ite.isDone()) {
        ite.currentSegment(vals);
        coords.add(new Coordinate(vals[0], vals[1]));
        ite.next();
    }
    final GeometryFactory fact = JTS.getFactory();
    final LinearRing ring = fact.createLinearRing(coords.toArray(new Coordinate[coords.size()]));
    final Polygon bounds = fact.createPolygon(ring, new LinearRing[0]);
    final LabelRenderer renderer = context.getLabelRenderer(true);
    final LabelLayer layer = new DefaultLabelLayer(false, true);
    final RenderingHints tickHint = new RenderingHints(null);
    tickHint.put(Graduation.VISUAL_AXIS_LENGTH, context.getCanvasDisplayBounds().width);
    tickHint.put(Graduation.VISUAL_TICK_SPACING, 200);
    // number of point by line
    final int nbPoint = 20;
    final CoordinateReferenceSystem objectiveCRS = context.getObjectiveCRS2D();
    try {
        // reduce grid bounds to validity area
        Envelope gridBounds = Envelopes.transform(context.getCanvasObjectiveBounds2D(), gridCRS);
        if (new GeneralEnvelope(gridBounds).isEmpty()) {
            // envelope likely contains NaN values
            gridBounds = CRS.getDomainOfValidity(gridCRS);
            if (gridBounds == null) {
                // try to convert target CRS validity to grid crs
                gridBounds = CRS.getDomainOfValidity(objectiveCRS);
                if (gridBounds == null) {
                    // nothing we can do
                    return;
                }
                gridBounds = Envelopes.transform(gridBounds, gridCRS);
            }
        }
        if (Math.abs(gridBounds.getSpan(0)) < MIN || Math.abs(gridBounds.getSpan(1)) < MIN) {
            return;
        }
        Envelope validity = CRS.getDomainOfValidity(gridCRS);
        if (validity != null) {
            GeneralEnvelope env = new GeneralEnvelope(gridBounds);
            env.intersect(validity);
            gridBounds = env;
        }
        final MathTransform gridToObj = CRS.findOperation(gridCRS, objectiveCRS, null).getMathTransform();
        final MathTransform objToGrid = gridToObj.inverse();
        // grid on X axis ---------------------------------------------------
        final NumberGraduation graduationX = new NumberGraduation(null);
        graduationX.setRange(gridBounds.getMinimum(0), gridBounds.getMaximum(0), gridBounds.getCoordinateReferenceSystem().getCoordinateSystem().getAxis(0).getUnit());
        TickIterator tickIte = graduationX.getTickIterator(tickHint, null);
        while (!tickIte.isDone()) {
            tickIte.next();
            final String label = tickIte.currentLabel();
            final double d = tickIte.currentPosition();
            if (d > gridBounds.getMaximum(0))
                continue;
            final ArrayList<Coordinate> lineCoords = new ArrayList<>();
            final double maxY = gridBounds.getMaximum(1);
            final double step = gridBounds.getSpan(1) / nbPoint;
            for (double k = Math.nextUp(gridBounds.getMinimum(1)); k < maxY; k += step) {
                lineCoords.add(new Coordinate(d, k));
            }
            lineCoords.add(new Coordinate(d, Math.nextAfter(maxY, Double.NEGATIVE_INFINITY)));
            Geometry geom = fact.createLineString(lineCoords.toArray(new Coordinate[lineCoords.size()]));
            if (geom == null)
                continue;
            final ProjectedGeometry pg = new ProjectedGeometry(context);
            pg.setDataGeometry(geom, gridCRS);
            // draw line
            if (tickIte.isMajorTick()) {
                g.setPaint(template.getMainLinePaint());
                g.setStroke(template.getMainLineStroke());
            } else {
                g.setPaint(template.getLinePaint());
                g.setStroke(template.getLineStroke());
            }
            for (Shape ds : pg.getDisplayShape()) g.draw(ds);
            // clip geometry to avoid text outside visible area
            geom = org.apache.sis.internal.feature.jts.JTS.transform(geom, gridToObj);
            if (geom == null)
                continue;
            geom = geom.intersection(bounds);
            pg.setDataGeometry(geom, objectiveCRS);
            // draw text
            final LinearLabelDescriptor desc;
            if (tickIte.isMajorTick()) {
                desc = new DefaultLinearLabelDescriptor(label, template.getMainLabelFont(), template.getMainLabelPaint(), template.getMainHaloWidth(), template.getMainHaloPaint(), 0, 10, 3, false, false, false, pg);
            } else {
                desc = new DefaultLinearLabelDescriptor(label, template.getLabelFont(), template.getLabelPaint(), template.getHaloWidth(), template.getHaloPaint(), 0, 10, 3, false, false, false, pg);
            }
            layer.labels().add(desc);
        }
        // grid on Y axis ---------------------------------------------------
        final NumberGraduation graduationY = new NumberGraduation(null);
        graduationY.setRange(gridBounds.getMinimum(1), gridBounds.getMaximum(1), gridBounds.getCoordinateReferenceSystem().getCoordinateSystem().getAxis(1).getUnit());
        tickIte = graduationY.getTickIterator(tickHint, null);
        while (!tickIte.isDone()) {
            tickIte.next();
            final String label = tickIte.currentLabel();
            final double d = tickIte.currentPosition();
            if (d > gridBounds.getMaximum(1))
                continue;
            final ArrayList<Coordinate> lineCoords = new ArrayList<>();
            final double maxX = gridBounds.getMaximum(0);
            final double step = gridBounds.getSpan(0) / nbPoint;
            for (double k = Math.nextUp(gridBounds.getMinimum(0)); k < maxX; k += step) {
                lineCoords.add(new Coordinate(k, d));
            }
            lineCoords.add(new Coordinate(Math.nextAfter(maxX, Double.NEGATIVE_INFINITY), d));
            Geometry geom = fact.createLineString(lineCoords.toArray(new Coordinate[lineCoords.size()]));
            final ProjectedGeometry pg = new ProjectedGeometry(context);
            pg.setDataGeometry(geom, gridCRS);
            // draw line
            if (tickIte.isMajorTick()) {
                g.setPaint(template.getMainLinePaint());
                g.setStroke(template.getMainLineStroke());
            } else {
                g.setPaint(template.getLinePaint());
                g.setStroke(template.getLineStroke());
            }
            for (Shape ds : pg.getDisplayShape()) g.draw(ds);
            // clip geometry to avoid text outside visible area
            geom = org.apache.sis.internal.feature.jts.JTS.transform(geom, gridToObj);
            if (geom == null)
                continue;
            geom = geom.intersection(bounds);
            pg.setDataGeometry(geom, objectiveCRS);
            // draw text
            final LinearLabelDescriptor desc;
            if (tickIte.isMajorTick()) {
                desc = new DefaultLinearLabelDescriptor(label, template.getMainLabelFont(), template.getMainLabelPaint(), template.getMainHaloWidth(), template.getMainHaloPaint(), 0, 10, 3, false, false, false, pg);
            } else {
                desc = new DefaultLinearLabelDescriptor(label, template.getLabelFont(), template.getLabelPaint(), template.getHaloWidth(), template.getHaloPaint(), 0, 10, 3, false, false, false, pg);
            }
            layer.labels().add(desc);
        }
    } catch (Exception ex) {
        // TODO: NO!!
        ex.printStackTrace();
    }
    renderer.portrayImmidiately(layer);
}
Also used : LabelRenderer(org.geotoolkit.display2d.style.labeling.LabelRenderer) TransformedShape(org.geotoolkit.display.shape.TransformedShape) Shape(java.awt.Shape) GeometryFactory(org.locationtech.jts.geom.GeometryFactory) LinearLabelDescriptor(org.geotoolkit.display2d.style.labeling.LinearLabelDescriptor) DefaultLinearLabelDescriptor(org.geotoolkit.display2d.style.labeling.DefaultLinearLabelDescriptor) MathTransform(org.opengis.referencing.operation.MathTransform) PathIterator(java.awt.geom.PathIterator) Rectangle(java.awt.Rectangle) ArrayList(java.util.ArrayList) Envelope(org.opengis.geometry.Envelope) GeneralEnvelope(org.apache.sis.geometry.GeneralEnvelope) RenderingHints(java.awt.RenderingHints) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) Polygon(org.locationtech.jts.geom.Polygon) DefaultLinearLabelDescriptor(org.geotoolkit.display2d.style.labeling.DefaultLinearLabelDescriptor) TickIterator(org.geotoolkit.display.axis.TickIterator) Graphics2D(java.awt.Graphics2D) ProjectedGeometry(org.geotoolkit.display2d.primitive.ProjectedGeometry) Geometry(org.locationtech.jts.geom.Geometry) LabelLayer(org.geotoolkit.display2d.style.labeling.LabelLayer) DefaultLabelLayer(org.geotoolkit.display2d.style.labeling.DefaultLabelLayer) TransformedShape(org.geotoolkit.display.shape.TransformedShape) Coordinate(org.locationtech.jts.geom.Coordinate) ProjectedGeometry(org.geotoolkit.display2d.primitive.ProjectedGeometry) LinearRing(org.locationtech.jts.geom.LinearRing) DefaultLabelLayer(org.geotoolkit.display2d.style.labeling.DefaultLabelLayer) GeneralEnvelope(org.apache.sis.geometry.GeneralEnvelope) NumberGraduation(org.geotoolkit.display.axis.NumberGraduation)

Example 3 with LabelLayer

use of org.geotoolkit.display2d.style.labeling.LabelLayer 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

ArrayList (java.util.ArrayList)3 LabelLayer (org.geotoolkit.display2d.style.labeling.LabelLayer)3 Graphics2D (java.awt.Graphics2D)2 LabelRenderer (org.geotoolkit.display2d.style.labeling.LabelRenderer)2 LinearLabelDescriptor (org.geotoolkit.display2d.style.labeling.LinearLabelDescriptor)2 Rectangle (java.awt.Rectangle)1 RenderingHints (java.awt.RenderingHints)1 Shape (java.awt.Shape)1 PathIterator (java.awt.geom.PathIterator)1 GeneralEnvelope (org.apache.sis.geometry.GeneralEnvelope)1 ExceptionPresentation (org.apache.sis.internal.map.ExceptionPresentation)1 Presentation (org.apache.sis.internal.map.Presentation)1 PortrayalException (org.geotoolkit.display.PortrayalException)1 NumberGraduation (org.geotoolkit.display.axis.NumberGraduation)1 TickIterator (org.geotoolkit.display.axis.TickIterator)1 TransformedShape (org.geotoolkit.display.shape.TransformedShape)1 Grid2DPresentation (org.geotoolkit.display2d.presentation.Grid2DPresentation)1 TextPresentation (org.geotoolkit.display2d.presentation.TextPresentation)1 ProjectedGeometry (org.geotoolkit.display2d.primitive.ProjectedGeometry)1 DefaultLabelLayer (org.geotoolkit.display2d.style.labeling.DefaultLabelLayer)1