Search in sources :

Example 6 with Envelope2D

use of org.geotools.geometry.Envelope2D 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

Envelope2D (org.geotools.geometry.Envelope2D)6 GET (javax.ws.rs.GET)5 Produces (javax.ws.rs.Produces)5 TileRequest (org.opentripplanner.analyst.request.TileRequest)5 Router (org.opentripplanner.standalone.Router)5 Path (javax.ws.rs.Path)4 RenderRequest (org.opentripplanner.analyst.request.RenderRequest)4 TimeSurface (org.opentripplanner.analyst.TimeSurface)3 MIMEImageFormat (org.opentripplanner.api.parameter.MIMEImageFormat)3 BufferedImage (java.awt.image.BufferedImage)2 Envelope (com.vividsolutions.jts.geom.Envelope)1 AffineTransformation (com.vividsolutions.jts.geom.util.AffineTransformation)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 CacheControl (javax.ws.rs.core.CacheControl)1 Layer (org.opentripplanner.api.parameter.Layer)1 Style (org.opentripplanner.api.parameter.Style)1 TileRenderContext (org.opentripplanner.inspector.TileRenderer.TileRenderContext)1 RoutingRequest (org.opentripplanner.routing.core.RoutingRequest)1