Search in sources :

Example 36 with Length

use of javax.measure.quantity.Length in project com.revolsys.open by revolsys.

the class Viewport2D method toDisplayValue.

public double toDisplayValue(final Quantity<Length> value) {
    double convertedValue;
    final Unit<Length> unit = value.getUnit();
    if (unit.equals(CustomUnits.PIXEL)) {
        convertedValue = QuantityType.doubleValue(value, CustomUnits.PIXEL);
    } else {
        convertedValue = QuantityType.doubleValue(value, Units.METRE);
        final CoordinateSystem coordinateSystem = this.geometryFactory2d.getCoordinateSystem();
        if (coordinateSystem instanceof GeographicCoordinateSystem) {
            final GeographicCoordinateSystem geoCs = (GeographicCoordinateSystem) coordinateSystem;
            final double radius = geoCs.getDatum().getEllipsoid().getSemiMajorAxis();
            convertedValue = Math.toDegrees(convertedValue / radius);
        }
        final double modelUnitsPerViewUnit = getModelUnitsPerViewUnit();
        convertedValue = convertedValue / modelUnitsPerViewUnit;
    }
    return convertedValue;
}
Also used : Length(javax.measure.quantity.Length) CoordinateSystem(com.revolsys.geometry.cs.CoordinateSystem) GeographicCoordinateSystem(com.revolsys.geometry.cs.GeographicCoordinateSystem) GeographicCoordinateSystem(com.revolsys.geometry.cs.GeographicCoordinateSystem)

Example 37 with Length

use of javax.measure.quantity.Length in project com.revolsys.open by revolsys.

the class Viewport2D method setBoundingBoxAndUnitsPerPixel.

private void setBoundingBoxAndUnitsPerPixel(final BoundingBox boundingBox, double unitsPerPixel) {
    final double oldScale = getScale();
    final double oldUnitsPerPixel = getUnitsPerPixel();
    final BoundingBox oldBoundingBox = this.boundingBox;
    synchronized (this) {
        final int viewWidthPixels = getViewWidthPixels();
        final int viewHeightPixels = getViewHeightPixels();
        if (Double.isFinite(unitsPerPixel)) {
            if (unitsPerPixel < this.minUnitsPerPixel) {
                unitsPerPixel = this.minUnitsPerPixel;
            }
            final double pixelSizeMetres = QuantityType.doubleValue(Quantities.getQuantity(1, getScreenUnit()), Units.METRE);
            this.unitsPerPixel = unitsPerPixel;
            setModelToScreenTransform(newModelToScreenTransform(boundingBox, viewWidthPixels, viewHeightPixels));
            this.screenToModelTransform = newScreenToModelTransform(boundingBox, viewWidthPixels, viewHeightPixels);
            final Quantity<Length> modelWidthLength = boundingBox.getWidthLength();
            double modelWidthMetres;
            if (getGeometryFactory().isProjected()) {
                modelWidthMetres = QuantityType.doubleValue(modelWidthLength, Units.METRE);
            } else {
                final double minX = boundingBox.getMinX();
                final double centreY = boundingBox.getCentreY();
                final double maxX = boundingBox.getMaxX();
                modelWidthMetres = GeographicCoordinateSystem.distanceMetres(minX, centreY, maxX, centreY);
            }
            this.metresPerPixel = modelWidthMetres / viewWidthPixels;
            this.scale = this.metresPerPixel / pixelSizeMetres;
        } else {
            this.metresPerPixel = 0;
            this.unitsPerPixel = 0;
            setModelToScreenTransform(null);
            this.screenToModelTransform = null;
            this.scale = 0;
        }
        setBoundingBoxInternal(boundingBox);
    }
    if (this.unitsPerPixel > 0) {
        this.propertyChangeSupport.firePropertyChange("unitsPerPixel", oldUnitsPerPixel, this.unitsPerPixel);
    }
    if (this.scale > 0) {
        this.propertyChangeSupport.firePropertyChange("scale", oldScale, this.scale);
    }
    this.propertyChangeSupport.firePropertyChange("boundingBox", oldBoundingBox, boundingBox);
}
Also used : Length(javax.measure.quantity.Length) BoundingBox(com.revolsys.geometry.model.BoundingBox) Point(com.revolsys.geometry.model.Point)

Example 38 with Length

use of javax.measure.quantity.Length in project com.revolsys.open by revolsys.

the class TextStyleRenderer method renderText.

public static void renderText(final Viewport2D viewport, final Graphics2D graphics, final String label, final Geometry geometry, final TextStyle style) {
    if (Property.hasValue(label) && geometry != null || viewport == null) {
        final String textPlacementType = style.getTextPlacementType();
        final PointDoubleXYOrientation point = getPointWithOrientation(viewport, geometry, textPlacementType);
        if (point != null) {
            double orientation;
            final String orientationType = style.getTextOrientationType();
            if ("none".equals(orientationType)) {
                orientation = 0;
            } else {
                orientation = point.getOrientation();
                if (orientation > 270) {
                    orientation -= 360;
                }
            }
            orientation += style.getTextOrientation();
            final Paint paint = graphics.getPaint();
            final Composite composite = graphics.getComposite();
            try {
                graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB);
                final double x = point.getX();
                final double y = point.getY();
                final double[] location;
                if (viewport == null) {
                    location = new double[] { x, y };
                } else {
                    location = viewport.toViewCoordinates(x, y);
                }
                final AffineTransform savedTransform = graphics.getTransform();
                style.setTextStyle(viewport, graphics);
                final Quantity<Length> textDx = style.getTextDx();
                double dx = Viewport2D.toDisplayValue(viewport, textDx);
                final Quantity<Length> textDy = style.getTextDy();
                double dy = -Viewport2D.toDisplayValue(viewport, textDy);
                final FontMetrics fontMetrics = graphics.getFontMetrics();
                double maxWidth = 0;
                final String[] lines = label.split("[\\r\\n]");
                for (final String line : lines) {
                    final Rectangle2D bounds = fontMetrics.getStringBounds(line, graphics);
                    final double width = bounds.getWidth();
                    maxWidth = Math.max(width, maxWidth);
                }
                final int descent = fontMetrics.getDescent();
                final int ascent = fontMetrics.getAscent();
                final int leading = fontMetrics.getLeading();
                final double maxHeight = lines.length * (ascent + descent) + (lines.length - 1) * leading;
                final String verticalAlignment = style.getTextVerticalAlignment();
                if ("top".equals(verticalAlignment)) {
                } else if ("middle".equals(verticalAlignment)) {
                    dy -= maxHeight / 2;
                } else {
                    dy -= maxHeight;
                }
                String horizontalAlignment = style.getTextHorizontalAlignment();
                double screenX = location[0];
                double screenY = location[1];
                final String textPlacement = textPlacementType;
                if ("auto".equals(textPlacement) && viewport != null) {
                    if (screenX < 0) {
                        screenX = 1;
                        dx = 0;
                        horizontalAlignment = "left";
                    }
                    final int viewWidth = viewport.getViewWidthPixels();
                    if (screenX + maxWidth > viewWidth) {
                        screenX = (int) (viewWidth - maxWidth - 1);
                        dx = 0;
                        horizontalAlignment = "left";
                    }
                    if (screenY < maxHeight) {
                        screenY = 1;
                        dy = 0;
                    }
                    final int viewHeight = viewport.getViewHeightPixels();
                    if (screenY > viewHeight) {
                        screenY = viewHeight - 1 - maxHeight;
                        dy = 0;
                    }
                }
                graphics.translate(screenX, screenY);
                if (orientation != 0) {
                    graphics.rotate(-Math.toRadians(orientation), 0, 0);
                }
                graphics.translate(dx, dy);
                for (final String line : lines) {
                    graphics.translate(0, ascent);
                    final AffineTransform lineTransform = graphics.getTransform();
                    final Rectangle2D bounds = fontMetrics.getStringBounds(line, graphics);
                    final double width = bounds.getWidth();
                    final double height = bounds.getHeight();
                    if ("right".equals(horizontalAlignment)) {
                        graphics.translate(-width, 0);
                    } else if ("center".equals(horizontalAlignment) || "auto".equals(horizontalAlignment)) {
                        graphics.translate(-width / 2, 0);
                    }
                    graphics.translate(dx, 0);
                    graphics.scale(1, 1);
                    if (Math.abs(orientation) > 90) {
                        graphics.rotate(Math.PI, maxWidth / 2, -height / 4);
                    }
                    final int textBoxOpacity = style.getTextBoxOpacity();
                    final Color textBoxColor = style.getTextBoxColor();
                    if (textBoxOpacity > 0 && textBoxColor != null) {
                        graphics.setPaint(textBoxColor);
                        final double cornerSize = Math.max(height / 2, 5);
                        final RoundRectangle2D.Double box = new RoundRectangle2D.Double(bounds.getX() - 3, bounds.getY() - 1, width + 6, height + 2, cornerSize, cornerSize);
                        graphics.fill(box);
                    }
                    final double radius = style.getTextHaloRadius();
                    final Unit<Length> unit = style.getTextSizeUnit();
                    final double textHaloRadius = Viewport2D.toDisplayValue(viewport, Quantities.getQuantity(radius, unit));
                    if (textHaloRadius > 0) {
                        final Stroke savedStroke = graphics.getStroke();
                        final Stroke outlineStroke = new BasicStroke((float) (textHaloRadius + 1), BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL);
                        graphics.setColor(style.getTextHaloFill());
                        graphics.setStroke(outlineStroke);
                        final Font font = graphics.getFont();
                        final FontRenderContext fontRenderContext = graphics.getFontRenderContext();
                        final TextLayout textLayout = new TextLayout(line, font, fontRenderContext);
                        final Shape outlineShape = textLayout.getOutline(NOOP_TRANSFORM);
                        graphics.draw(outlineShape);
                        graphics.setStroke(savedStroke);
                    }
                    graphics.setColor(style.getTextFill());
                    if (textBoxOpacity > 0 && textBoxOpacity < 255) {
                        graphics.setComposite(AlphaComposite.SrcOut);
                        graphics.drawString(line, (float) 0, (float) 0);
                        graphics.setComposite(AlphaComposite.DstOver);
                        graphics.drawString(line, (float) 0, (float) 0);
                    } else {
                        graphics.setComposite(AlphaComposite.SrcOver);
                        graphics.drawString(line, (float) 0, (float) 0);
                    }
                    graphics.setTransform(lineTransform);
                    graphics.translate(0, leading + descent);
                }
                graphics.setTransform(savedTransform);
            } finally {
                graphics.setPaint(paint);
                graphics.setComposite(composite);
            }
        }
    }
}
Also used : BasicStroke(java.awt.BasicStroke) Stroke(java.awt.Stroke) BasicStroke(java.awt.BasicStroke) Shape(java.awt.Shape) AlphaComposite(java.awt.AlphaComposite) Composite(java.awt.Composite) Color(java.awt.Color) RoundRectangle2D(java.awt.geom.RoundRectangle2D) Rectangle2D(java.awt.geom.Rectangle2D) RoundRectangle2D(java.awt.geom.RoundRectangle2D) Paint(java.awt.Paint) Paint(java.awt.Paint) Font(java.awt.Font) TextLayout(java.awt.font.TextLayout) PointDoubleXYOrientation(com.revolsys.geometry.model.impl.PointDoubleXYOrientation) Length(javax.measure.quantity.Length) FontMetrics(java.awt.FontMetrics) AffineTransform(java.awt.geom.AffineTransform) FontRenderContext(java.awt.font.FontRenderContext)

Example 39 with Length

use of javax.measure.quantity.Length in project com.revolsys.open by revolsys.

the class ImageMarker method render.

@Override
public void render(final Viewport2D viewport, final Graphics2D graphics, final MarkerStyle style, final double modelX, final double modelY, double orientation) {
    if (this.image != null) {
        final AffineTransform savedTransform = graphics.getTransform();
        final Quantity<Length> markerWidth = style.getMarkerWidth();
        final double mapWidth = Viewport2D.toDisplayValue(viewport, markerWidth);
        final Quantity<Length> markerHeight = style.getMarkerHeight();
        final double mapHeight = Viewport2D.toDisplayValue(viewport, markerHeight);
        final String orientationType = style.getMarkerOrientationType();
        if ("none".equals(orientationType)) {
            orientation = 0;
        }
        translateMarker(viewport, graphics, style, modelX, modelY, mapWidth, mapHeight, orientation);
        final AffineTransform shapeTransform = AffineTransform.getScaleInstance(mapWidth / this.image.getWidth(null), mapHeight / this.image.getHeight(null));
        graphics.drawImage(this.image, shapeTransform, null);
        graphics.setTransform(savedTransform);
    }
}
Also used : Length(javax.measure.quantity.Length) AffineTransform(java.awt.geom.AffineTransform)

Example 40 with Length

use of javax.measure.quantity.Length in project com.revolsys.open by revolsys.

the class SvgMarker method render.

@Override
public void render(final Viewport2D viewport, final Graphics2D graphics, final MarkerStyle style, final double modelX, final double modelY, double orientation) {
    final TranscoderInput transcoderInput = this.transcoderInput;
    if (transcoderInput != null) {
        try (BaseCloseable transformCloseable = new CloseableAffineTransform(graphics)) {
            Viewport2D.setUseModelCoordinates(viewport, graphics, false);
            final Quantity<Length> markerWidth = style.getMarkerWidth();
            final double mapWidth = Viewport2D.toDisplayValue(viewport, markerWidth);
            final Quantity<Length> markerHeight = style.getMarkerHeight();
            final double mapHeight = Viewport2D.toDisplayValue(viewport, markerHeight);
            final String orientationType = style.getMarkerOrientationType();
            if ("none".equals(orientationType)) {
                orientation = 0;
            }
            Viewport2D.translateModelToViewCoordinates(viewport, graphics, modelX, modelY);
            final double markerOrientation = style.getMarkerOrientation();
            orientation = orientation + markerOrientation;
            if (orientation != 0) {
                graphics.rotate(Math.toRadians(orientation));
            }
            final Quantity<Length> deltaX = style.getMarkerDx();
            final Quantity<Length> deltaY = style.getMarkerDy();
            double dx = Viewport2D.toDisplayValue(viewport, deltaX);
            double dy = Viewport2D.toDisplayValue(viewport, deltaY);
            final String verticalAlignment = style.getMarkerVerticalAlignment();
            if ("bottom".equals(verticalAlignment)) {
                dy -= mapHeight;
            } else if ("middle".equals(verticalAlignment)) {
                dy -= mapHeight / 2;
            }
            final String horizontalAlignment = style.getMarkerHorizontalAlignment();
            if ("right".equals(horizontalAlignment)) {
                dx -= mapWidth;
            } else if ("center".equals(horizontalAlignment)) {
                dx -= mapWidth / 2;
            }
            graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
            graphics.translate(dx, dy);
            // changeAttribute(root, "stroke", "#ffffff",
            // WebColors.toHex(style.getMarkerLineColor()));
            // changeAttribute(root, "color", "#ffffff",
            // WebColors.toHex(style.getMarkerLineColor()));
            // changeAttribute(root, "fill", "#ffffff",
            // WebColors.toHex(style.getMarkerLineColor()));
            // 
            // changeAttribute(root, "stroke", "#000000",
            // WebColors.toHex(style.getMarkerFill()));
            // changeAttribute(root, "color", "#000000",
            // WebColors.toHex(style.getMarkerFill()));
            // changeAttribute(root, "fill", "#000000",
            // WebColors.toHex(style.getMarkerFill()));
            // changeAttribute(root, "stroke", "#444444",
            // WebColors.toHex(style.getMarkerFill()));
            // changeAttribute(root, "color", "#444444",
            // WebColors.toHex(style.getMarkerFill()));
            // changeAttribute(root, "fill", "#444444",
            // WebColors.toHex(style.getMarkerFill()));
            // shape.render(graphics);
            final Graphics2DTranscoder transcoder = new Graphics2DTranscoder(graphics);
            synchronized (transcoderInput) {
                transcoder.transcode(transcoderInput, null);
            }
        } catch (final Throwable e) {
            Logs.error(this, "Unable to render", e);
        }
    }
}
Also used : BaseCloseable(com.revolsys.io.BaseCloseable) TranscoderInput(org.apache.batik.transcoder.TranscoderInput) Length(javax.measure.quantity.Length) CloseableAffineTransform(com.revolsys.awt.CloseableAffineTransform)

Aggregations

Length (javax.measure.quantity.Length)44 Angle (javax.measure.quantity.Angle)7 Test (org.junit.Test)7 Color (java.awt.Color)5 Unit (javax.measure.Unit)5 CoordinateSystem (com.revolsys.geometry.cs.CoordinateSystem)4 AffineTransform (java.awt.geom.AffineTransform)4 ArrayList (java.util.ArrayList)4 GeographicCoordinateSystem (com.revolsys.geometry.cs.GeographicCoordinateSystem)3 BoundingBox (com.revolsys.geometry.model.BoundingBox)3 LineString (com.revolsys.geometry.model.LineString)3 Point (com.revolsys.geometry.model.Point)3 Shape (java.awt.Shape)3 Rectangle2D (java.awt.geom.Rectangle2D)3 Path (java.nio.file.Path)3 AtomicLong (java.util.concurrent.atomic.AtomicLong)3 Quantity (javax.measure.Quantity)3 CloseableAffineTransform (com.revolsys.awt.CloseableAffineTransform)2 ProjectedCoordinateSystem (com.revolsys.geometry.cs.ProjectedCoordinateSystem)2 ChainedCoordinatesOperation (com.revolsys.geometry.cs.projection.ChainedCoordinatesOperation)2