Search in sources :

Example 1 with ShapePresentation

use of org.geotoolkit.display2d.presentation.ShapePresentation in project geotoolkit by Geomatys.

the class TileDebugSymbolizerRenderer method presentations.

@Override
public Stream<Presentation> presentations(MapLayer layer, Resource resource) {
    if (!(resource instanceof TiledResource)) {
        return Stream.empty();
    }
    final TiledResource mrm = (TiledResource) resource;
    final List<Presentation> presentations = new ArrayList<>();
    final Graphics2D graphics = renderingContext.getGraphics();
    final Color textColor = Color.YELLOW;
    final Color bgColor = new Color(0, 0, 0, 150);
    final Stroke stroke = new BasicStroke(1);
    final Font font = new Font("Dialog", Font.BOLD, 12);
    final FontMetrics fontMetrics = graphics.getFontMetrics(font);
    try {
        final Map.Entry<Envelope, List<TileMatrix>> intersect = TileMatrixSetCoverageReader.intersect(mrm, getRenderingContext().getGridGeometry());
        final List<TileMatrix> mosaics = intersect.getValue();
        final Envelope wantedEnv = intersect.getKey();
        for (TileMatrix m : mosaics) {
            final CoordinateReferenceSystem crs = m.getUpperLeftCorner().getCoordinateReferenceSystem();
            final CoordinateReferenceSystem crs2d = CRS.getHorizontalComponent(crs);
            final Rectangle rectangle;
            try {
                rectangle = TileMatrices.getTilesInEnvelope(m, wantedEnv);
            } catch (NoSuchDataException ex) {
                continue;
            }
            for (int x = 0; x < rectangle.width; x++) {
                for (int y = 0; y < rectangle.height; y++) {
                    final GridGeometry gridgeom = TileMatrices.getTileGridGeometry2D(m, new Point(rectangle.x + x, rectangle.y + y), crs2d);
                    Geometry geom = GeometricUtilities.toJTSGeometry(gridgeom.getEnvelope(), GeometricUtilities.WrapResolution.NONE);
                    geom.setUserData(crs2d);
                    geom = org.apache.sis.internal.feature.jts.JTS.transform(geom, renderingContext.getDisplayCRS());
                    Shape shp = new JTSGeometryJ2D(geom);
                    final ShapePresentation border = new ShapePresentation(layer, resource, null);
                    border.stroke = stroke;
                    border.strokePaint = Color.BLACK;
                    border.shape = shp;
                    presentations.add(border);
                    final Rectangle bounds = shp.getBounds();
                    final double centerX = bounds.getCenterX();
                    final double centerY = bounds.getCenterY();
                    String mid = m.getIdentifier();
                    if (mid.length() > 10) {
                        mid = mid.substring(0, 9) + "..";
                    }
                    final String mosaicId = "Z: " + mid;
                    final String mosaicScale = "S: " + new DecimalFormat("#0.00000").format(m.getScale());
                    final String strX = "X: " + (rectangle.x + x);
                    final String strY = "Y: " + (rectangle.y + y);
                    String longest = mosaicId;
                    if (mosaicScale.length() > longest.length())
                        longest = mosaicScale;
                    if (strX.length() > longest.length())
                        longest = strX;
                    if (strY.length() > longest.length())
                        longest = strY;
                    Font ft = font;
                    FontMetrics fm = fontMetrics;
                    while (fm.stringWidth(longest) > bounds.width && ft.getSize() > 8) {
                        ft = new Font(ft.getFamily(), ft.getStyle(), ft.getSize() - 1);
                        fm = graphics.getFontMetrics(ft);
                    }
                    graphics.setFont(ft);
                    final double fntHeight = fm.getHeight();
                    Rectangle2D txtbbox = fm.getStringBounds(mosaicId, g2d);
                    {
                        final AttributedString as = new AttributedString(mosaicId);
                        as.addAttribute(TextAttribute.FONT, ft);
                        as.addAttribute(TextAttribute.BACKGROUND, bgColor);
                        final TextPresentation2 tp = new TextPresentation2(layer, resource, null);
                        tp.forGrid(renderingContext);
                        tp.text = as;
                        tp.paint = textColor;
                        tp.x = (float) (centerX - txtbbox.getWidth() / 2.0);
                        tp.y = (float) (centerY - fntHeight);
                        presentations.add(tp);
                    }
                    {
                        final AttributedString as = new AttributedString(mosaicScale);
                        as.addAttribute(TextAttribute.FONT, ft);
                        as.addAttribute(TextAttribute.BACKGROUND, bgColor);
                        txtbbox = fm.getStringBounds(mosaicScale, g2d);
                        final TextPresentation2 tp = new TextPresentation2(layer, resource, null);
                        tp.forGrid(renderingContext);
                        tp.text = as;
                        tp.paint = textColor;
                        tp.x = (float) (centerX - txtbbox.getWidth() / 2.0);
                        tp.y = (float) (centerY);
                        presentations.add(tp);
                    }
                    {
                        final AttributedString as = new AttributedString(strX);
                        as.addAttribute(TextAttribute.FONT, ft);
                        as.addAttribute(TextAttribute.BACKGROUND, bgColor);
                        txtbbox = fm.getStringBounds(strX, g2d);
                        final TextPresentation2 tp = new TextPresentation2(layer, resource, null);
                        tp.forGrid(renderingContext);
                        tp.text = as;
                        tp.paint = textColor;
                        tp.x = (float) (centerX - txtbbox.getWidth() / 2.0);
                        tp.y = (float) (centerY + fntHeight);
                        presentations.add(tp);
                    }
                    {
                        final AttributedString as = new AttributedString(strY);
                        as.addAttribute(TextAttribute.FONT, ft);
                        as.addAttribute(TextAttribute.BACKGROUND, bgColor);
                        txtbbox = fm.getStringBounds(strY, g2d);
                        final TextPresentation2 tp = new TextPresentation2(layer, resource, null);
                        tp.forGrid(renderingContext);
                        tp.text = as;
                        tp.paint = textColor;
                        tp.x = (float) (centerX - txtbbox.getWidth() / 2.0);
                        tp.y = (float) (centerY + 2 * fntHeight);
                        presentations.add(tp);
                    }
                }
            }
        }
    } catch (DataStoreException | TransformException | FactoryException ex) {
        ExceptionPresentation ep = new ExceptionPresentation(ex);
        ep.setLayer(layer);
        ep.setResource(resource);
        presentations.add(ep);
    }
    return presentations.stream();
}
Also used : BasicStroke(java.awt.BasicStroke) Shape(java.awt.Shape) FactoryException(org.opengis.util.FactoryException) DecimalFormat(java.text.DecimalFormat) ArrayList(java.util.ArrayList) Rectangle(java.awt.Rectangle) AttributedString(java.text.AttributedString) Envelope(org.opengis.geometry.Envelope) JTSGeometryJ2D(org.geotoolkit.geometry.jts.awt.JTSGeometryJ2D) Font(java.awt.Font) AttributedString(java.text.AttributedString) TiledResource(org.geotoolkit.storage.multires.TiledResource) FontMetrics(java.awt.FontMetrics) ExceptionPresentation(org.apache.sis.internal.map.ExceptionPresentation) ArrayList(java.util.ArrayList) List(java.util.List) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) TextPresentation2(org.geotoolkit.display2d.presentation.TextPresentation2) TileMatrix(org.geotoolkit.storage.multires.TileMatrix) GridGeometry(org.apache.sis.coverage.grid.GridGeometry) Stroke(java.awt.Stroke) BasicStroke(java.awt.BasicStroke) DataStoreException(org.apache.sis.storage.DataStoreException) Color(java.awt.Color) Rectangle2D(java.awt.geom.Rectangle2D) TransformException(org.opengis.referencing.operation.TransformException) Point(java.awt.Point) ShapePresentation(org.geotoolkit.display2d.presentation.ShapePresentation) ExceptionPresentation(org.apache.sis.internal.map.ExceptionPresentation) Presentation(org.apache.sis.internal.map.Presentation) NoSuchDataException(org.apache.sis.storage.NoSuchDataException) Point(java.awt.Point) Graphics2D(java.awt.Graphics2D) GridGeometry(org.apache.sis.coverage.grid.GridGeometry) Geometry(org.locationtech.jts.geom.Geometry) ShapePresentation(org.geotoolkit.display2d.presentation.ShapePresentation) Map(java.util.Map)

Example 2 with ShapePresentation

use of org.geotoolkit.display2d.presentation.ShapePresentation in project geotoolkit by Geomatys.

the class LineSymbolizerRenderer method portray.

public static List<Presentation> portray(MapLayer layer, CachedSymbolizer symbol, Shape j2dShape, CachedStroke cachedStroke, Feature feature, float coeff, RenderingHints hints, RenderingContext2D ctx) {
    final List<Presentation> presentations = new ArrayList<>();
    if (cachedStroke instanceof CachedStrokeSimple) {
        final CachedStrokeSimple cs = (CachedStrokeSimple) cachedStroke;
        final ShapePresentation presentation = new ShapePresentation(layer, layer.getData(), feature);
        presentation.forGrid(ctx);
        presentation.strokeComposite = cs.getJ2DComposite(feature);
        presentation.stroke = cs.getJ2DStroke(feature, coeff);
        presentation.shape = j2dShape;
        if (cs.isMosaicPaint()) {
            // we need to find the top left bounds of the geometry
            final float margin = symbol.getMargin(feature, ctx) / 2f;
            final Rectangle2D bounds = j2dShape.getBounds2D();
            final int x = (int) (bounds.getMinX() - margin);
            final int y = (int) (bounds.getMinY() - margin);
            presentation.strokePaint = cs.getJ2DPaint(feature, x, y, coeff, hints);
        } else {
            presentation.strokePaint = cs.getJ2DPaint(feature, 0, 0, coeff, hints);
        }
        presentations.add(presentation);
    } else if (cachedStroke instanceof CachedStrokeGraphic) {
        final CachedStrokeGraphic gc = (CachedStrokeGraphic) cachedStroke;
        final float initGap = gc.getInitialGap(feature);
        final Point2D pt = new Point2D.Double();
        final CachedGraphicStroke cgs = gc.getCachedGraphic();
        final BufferedImage img = cgs.getImage(feature, 1, hints);
        final float imgWidth = img.getWidth(null);
        final float imgHeight = img.getHeight(null);
        final float gap = gc.getGap(feature) + imgWidth;
        final AffineTransform trs = new AffineTransform();
        final PathIterator ite = j2dShape.getPathIterator(null);
        final PathWalker walker = new PathWalker(ite);
        walker.walk(initGap);
        while (!walker.isFinished()) {
            // paint the motif --------------------------------------------------
            walker.getPosition(pt);
            final float angle = walker.getRotation();
            trs.setToTranslation(pt.getX(), pt.getY());
            trs.rotate(angle);
            final float[] anchor = cgs.getAnchor(feature, null);
            final float[] disp = cgs.getDisplacement(feature, null);
            trs.translate(-imgWidth * anchor[0], -imgHeight * anchor[1]);
            trs.translate(disp[0], -disp[1]);
            final PointPresentation presentation = new PointPresentation(layer, layer.getData(), feature);
            presentation.forGrid(ctx);
            presentation.composite = GO2Utilities.ALPHA_COMPOSITE_1F;
            presentation.image = img;
            presentation.displayTransform = trs;
            presentations.add(presentation);
            // walk over the gap ------------------------------------------------
            walker.walk(gap);
        }
    }
    return presentations;
}
Also used : CachedStrokeGraphic(org.geotoolkit.display2d.style.CachedStrokeGraphic) PathIterator(java.awt.geom.PathIterator) ArrayList(java.util.ArrayList) Rectangle2D(java.awt.geom.Rectangle2D) PathWalker(org.geotoolkit.display2d.style.j2d.PathWalker) ShapePresentation(org.geotoolkit.display2d.presentation.ShapePresentation) ExceptionPresentation(org.apache.sis.internal.map.ExceptionPresentation) PointPresentation(org.geotoolkit.display2d.presentation.PointPresentation) Presentation(org.apache.sis.internal.map.Presentation) BufferedImage(java.awt.image.BufferedImage) CachedGraphicStroke(org.geotoolkit.display2d.style.CachedGraphicStroke) ShapePresentation(org.geotoolkit.display2d.presentation.ShapePresentation) Point2D(java.awt.geom.Point2D) AffineTransform(java.awt.geom.AffineTransform) CachedStrokeSimple(org.geotoolkit.display2d.style.CachedStrokeSimple) PointPresentation(org.geotoolkit.display2d.presentation.PointPresentation)

Example 3 with ShapePresentation

use of org.geotoolkit.display2d.presentation.ShapePresentation in project geotoolkit by Geomatys.

the class PolygonSymbolizerRenderer method presentations.

@Override
public Stream<Presentation> presentations(MapLayer layer, Feature feature) {
    final float offset = symbol.getOffset(feature, coeff);
    final Shape[] shapes;
    // calculate displacement
    final float[] disps = symbol.getDisplacement(feature);
    Point2D dispStep = null;
    if (disps[0] != 0 || disps[1] != 0) {
        dispStep = new Point2D.Float(disps[0], -disps[1]);
    }
    final ProjectedGeometry projectedGeometry = new ProjectedGeometry(renderingContext);
    projectedGeometry.setDataGeometry(GO2Utilities.getGeometry(feature, symbol.getSource().getGeometry()), null);
    float sizeCorrection = 1f;
    try {
        if (dispGeom) {
            shapes = (offset != 0) ? bufferDisplayGeometry(projectedGeometry, offset) : projectedGeometry.getDisplayShape();
        } else {
            // NOTE : Java2d has issues when rendering shapes with large strokes when
            // there is a given transform, we cheat by converting the geometry in
            // display unit.
            // 
            // renderingContext.switchToObjectiveCRS();
            // shapes = (offset != 0) ? bufferObjectiveGeometry(renderingContext, projectedGeometry, symbolUnit, offset)
            // : projectedGeometry.getObjectiveShape();
            // adjust displacement, displacement is expressed in pixel units
            // final AffineTransform inverse = renderingContext.getDisplayToObjective();
            // if(dispStep!=null) dispStep = inverse.deltaTransform(dispStep, dispStep);
            sizeCorrection = (float) AffineTransforms2D.getScale(renderingContext.getObjectiveToDisplay());
            if (offset != 0) {
                shapes = bufferDisplayGeometry(projectedGeometry, offset * sizeCorrection);
            } else {
                shapes = projectedGeometry.getDisplayShape();
            }
        }
    } catch (TransformException ex) {
        ExceptionPresentation ep = new ExceptionPresentation(ex);
        ep.setLayer(layer);
        return Stream.of(ep);
    }
    if (shapes == null) {
        // no geometry, end here
        return Stream.empty();
    }
    final float coeff = this.coeff * sizeCorrection;
    final List<Presentation> presentations = new ArrayList<>();
    for (Shape shape : shapes) {
        // we apply the displacement ---------------------------------------
        if (dispStep != null) {
            final AffineTransform trs = new AffineTransform();
            trs.setToTranslation(dispStep.getX(), dispStep.getY());
            shape = trs.createTransformedShape(shape);
        }
        final int x;
        final int y;
        if (mosaic) {
            // we need the upperleft point to properly paint the polygon
            final float margin = symbol.getMargin(feature, coeff) / 2f;
            final Rectangle2D bounds = shape.getBounds2D();
            if (bounds == null)
                return Stream.empty();
            x = (int) (bounds.getMinX() - margin);
            y = (int) (bounds.getMinY() - margin);
        } else {
            x = 0;
            y = 0;
        }
        if (symbol.isFillVisible(feature)) {
            final ShapePresentation presentation = new ShapePresentation(layer, layer.getData(), feature);
            presentation.forGrid(renderingContext);
            presentation.fillComposite = symbol.getJ2DFillComposite(feature);
            presentation.fillPaint = symbol.getJ2DFillPaint(feature, x, y, coeff, hints);
            presentation.shape = shape;
            presentations.add(presentation);
        }
        if (symbol.isStrokeVisible(feature)) {
            final CachedStroke cachedStroke = symbol.getCachedStroke();
            presentations.addAll(LineSymbolizerRenderer.portray(layer, symbol, shape, cachedStroke, feature, coeff, hints, renderingContext));
        }
    }
    return presentations.stream();
}
Also used : CachedStroke(org.geotoolkit.display2d.style.CachedStroke) Shape(java.awt.Shape) TransformException(org.opengis.referencing.operation.TransformException) ArrayList(java.util.ArrayList) Rectangle2D(java.awt.geom.Rectangle2D) ShapePresentation(org.geotoolkit.display2d.presentation.ShapePresentation) ExceptionPresentation(org.apache.sis.internal.map.ExceptionPresentation) Presentation(org.apache.sis.internal.map.Presentation) ShapePresentation(org.geotoolkit.display2d.presentation.ShapePresentation) Point2D(java.awt.geom.Point2D) ProjectedGeometry(org.geotoolkit.display2d.primitive.ProjectedGeometry) ExceptionPresentation(org.apache.sis.internal.map.ExceptionPresentation) AffineTransform(java.awt.geom.AffineTransform)

Aggregations

Rectangle2D (java.awt.geom.Rectangle2D)3 ArrayList (java.util.ArrayList)3 ExceptionPresentation (org.apache.sis.internal.map.ExceptionPresentation)3 Presentation (org.apache.sis.internal.map.Presentation)3 ShapePresentation (org.geotoolkit.display2d.presentation.ShapePresentation)3 Shape (java.awt.Shape)2 AffineTransform (java.awt.geom.AffineTransform)2 Point2D (java.awt.geom.Point2D)2 TransformException (org.opengis.referencing.operation.TransformException)2 BasicStroke (java.awt.BasicStroke)1 Color (java.awt.Color)1 Font (java.awt.Font)1 FontMetrics (java.awt.FontMetrics)1 Graphics2D (java.awt.Graphics2D)1 Point (java.awt.Point)1 Rectangle (java.awt.Rectangle)1 Stroke (java.awt.Stroke)1 PathIterator (java.awt.geom.PathIterator)1 BufferedImage (java.awt.image.BufferedImage)1 AttributedString (java.text.AttributedString)1