Search in sources :

Example 1 with TileRenderContext

use of org.opentripplanner.inspector.TileRenderer.TileRenderContext in project OpenTripPlanner by opentripplanner.

the class TileRendererManager method renderTile.

public BufferedImage renderTile(final TileRequest tileRequest, String layer) {
    TileRenderContext context = new TileRenderContext() {

        @Override
        public Envelope expandPixels(double marginXPixels, double marginYPixels) {
            Envelope retval = new Envelope(bbox);
            retval.expandBy(marginXPixels / tileRequest.width * (bbox.getMaxX() - bbox.getMinX()), marginYPixels / tileRequest.height * (bbox.getMaxY() - bbox.getMinY()));
            return retval;
        }
    };
    context.graph = graph;
    TileRenderer renderer = renderers.get(layer);
    if (renderer == null)
        throw new IllegalArgumentException("Unknown layer: " + layer);
    // The best place for caching tiles may be here
    BufferedImage image = new BufferedImage(tileRequest.width, tileRequest.height, renderer.getColorModel());
    context.graphics = image.createGraphics();
    Envelope2D trbb = tileRequest.bbox;
    context.bbox = new Envelope(trbb.x, trbb.x + trbb.width, trbb.y, trbb.y + trbb.height);
    context.transform = new AffineTransformation();
    double xScale = tileRequest.width / trbb.width;
    double yScale = tileRequest.height / trbb.height;
    context.transform.translate(-trbb.x, -trbb.y - trbb.height);
    context.transform.scale(xScale, -yScale);
    context.metersPerPixel = Math.toRadians(trbb.height) * 6371000 / tileRequest.height;
    context.tileWidth = tileRequest.width;
    context.tileHeight = tileRequest.height;
    long start = System.currentTimeMillis();
    renderer.renderTile(context);
    LOG.debug("Rendered tile at {},{} in {} ms", tileRequest.bbox.y, tileRequest.bbox.x, System.currentTimeMillis() - start);
    return image;
}
Also used : TileRenderContext(org.opentripplanner.inspector.TileRenderer.TileRenderContext) AffineTransformation(com.vividsolutions.jts.geom.util.AffineTransformation) Envelope(com.vividsolutions.jts.geom.Envelope) Envelope2D(org.geotools.geometry.Envelope2D) BufferedImage(java.awt.image.BufferedImage)

Aggregations

Envelope (com.vividsolutions.jts.geom.Envelope)1 AffineTransformation (com.vividsolutions.jts.geom.util.AffineTransformation)1 BufferedImage (java.awt.image.BufferedImage)1 Envelope2D (org.geotools.geometry.Envelope2D)1 TileRenderContext (org.opentripplanner.inspector.TileRenderer.TileRenderContext)1