Search in sources :

Example 1 with NumberGraduation

use of org.geotoolkit.display.axis.NumberGraduation 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)

Aggregations

Graphics2D (java.awt.Graphics2D)1 Rectangle (java.awt.Rectangle)1 RenderingHints (java.awt.RenderingHints)1 Shape (java.awt.Shape)1 PathIterator (java.awt.geom.PathIterator)1 ArrayList (java.util.ArrayList)1 GeneralEnvelope (org.apache.sis.geometry.GeneralEnvelope)1 NumberGraduation (org.geotoolkit.display.axis.NumberGraduation)1 TickIterator (org.geotoolkit.display.axis.TickIterator)1 TransformedShape (org.geotoolkit.display.shape.TransformedShape)1 ProjectedGeometry (org.geotoolkit.display2d.primitive.ProjectedGeometry)1 DefaultLabelLayer (org.geotoolkit.display2d.style.labeling.DefaultLabelLayer)1 DefaultLinearLabelDescriptor (org.geotoolkit.display2d.style.labeling.DefaultLinearLabelDescriptor)1 LabelLayer (org.geotoolkit.display2d.style.labeling.LabelLayer)1 LabelRenderer (org.geotoolkit.display2d.style.labeling.LabelRenderer)1 LinearLabelDescriptor (org.geotoolkit.display2d.style.labeling.LinearLabelDescriptor)1 Coordinate (org.locationtech.jts.geom.Coordinate)1 Geometry (org.locationtech.jts.geom.Geometry)1 GeometryFactory (org.locationtech.jts.geom.GeometryFactory)1 LinearRing (org.locationtech.jts.geom.LinearRing)1