Exemplo n.º 1
0
  /**
   * Creates the {@link RenderedImage} corresponding to the tile at index {@code tileIdx} and uses a
   * {@link RenderedImageMapResponse} to encode it into the {@link #getResponseFormat() response
   * format}.
   *
   * @see org.geowebcache.layer.MetaTile#writeTileToStream(int, org.geowebcache.io.Resource)
   * @see RenderedImageMapResponse#write
   */
  @Override
  public boolean writeTileToStream(final int tileIdx, Resource target) throws IOException {

    checkNotNull(metaTileMap, "webMap is not set");
    if (!(metaTileMap instanceof RenderedImageMap)) {
      throw new IllegalArgumentException(
          "Only RenderedImageMaps are supported so far: " + metaTileMap.getClass().getName());
    }
    final RenderedImageMapResponse mapEncoder;
    {
      final GWC mediator = GWC.get();
      final Response responseEncoder = mediator.getResponseEncoder(responseFormat, metaTileMap);
      mapEncoder = (RenderedImageMapResponse) responseEncoder;
    }

    RenderedImage tile = metaTileMap.getImage();
    WMSMapContent tileContext = metaTileMap.getMapContext();

    if (this.tiles.length > 1 || (this.tiles.length == 1 && metaHasGutter())) {
      final Rectangle tileDim = this.tiles[tileIdx];
      tile = createTile(tileDim.x, tileDim.y, tileDim.width, tileDim.height);
      disposeLater(tile);
      {
        final WMSMapContent metaTileContext = metaTileMap.getMapContext();
        // do not create tileContext with metaTileContext.getLayers() as the layer list.
        // It is not needed at this stage and the constructor would force a
        // MapLayer.getBounds() that might fail
        tileContext = new WMSMapContent();
        tileContext.setRequest(metaTileContext.getRequest());
        tileContext.setBgColor(metaTileContext.getBgColor());
        tileContext.setMapWidth(tileDim.width);
        tileContext.setMapHeight(tileDim.height);
        tileContext.setPalette(metaTileContext.getPalette());
        tileContext.setTransparent(tileContext.isTransparent());
        long[][] tileIndexes = getTilesGridPositions();
        BoundingBox tileBounds = gridSubset.boundsFromIndex(tileIndexes[tileIdx]);
        ReferencedEnvelope tilebbox =
            new ReferencedEnvelope(metaTileContext.getCoordinateReferenceSystem());
        tilebbox.init(
            tileBounds.getMinX(), tileBounds.getMaxX(), tileBounds.getMinY(), tileBounds.getMaxY());
        tileContext.getViewport().setBounds(tilebbox);
      }
    }

    OutputStream outStream = target.getOutputStream();
    try {
      // call formatImageOuputStream instead of write to avoid disposition of rendered images
      // when processing a tile from a metatile and instead defer it to this class' dispose()
      // method
      mapEncoder.formatImageOutputStream(tile, outStream, tileContext);
      return true;
    } finally {
      outStream.close();
    }
  }
Exemplo n.º 2
0
  byte[] toBytes(WebMap map) throws IOException {
    ByteArrayOutputStream bout = new ByteArrayOutputStream();

    if (map instanceof RenderedImageMap) {
      RenderedImageMapResponse response =
          JPEG_MIME_TYPE.equals(map.getMimeType())
              ? new JPEGMapResponse(wms)
              : new PNGMapResponse(wms);
      response.write(map, bout, null);
    } else if (map instanceof RawMap) {
      ((RawMap) map).writeTo(bout);
    }
    bout.flush();
    return bout.toByteArray();
  }