int height = request.getHeight();
int width = request.getWidth();
if ((height>512)||(width>512)){
throw new ServiceException("Cannot get WMS bil" +
" tiles bigger than 512x512, try WCS");
}
MapLayerInfo[] reqlayers = request.getLayers();
//Can't fetch bil for more than 1 layer
if (reqlayers.length > 1)
{
throw new ServiceException("Cannot combine layers into BIL output");
}
MapLayerInfo mapLayerInfo = reqlayers[0];
/*
final ParameterValueGroup writerParams = format.getWriteParameters();
writerParams.parameter(AbstractGridFormat.GEOTOOLS_WRITE_PARAMS.getName().toString())
.setValue(wp);
*/
GridCoverageReader coverageReader = mapLayerInfo.getCoverageReader();
/*
* Try to use a gridcoverage style render
*/
GridCoverage2D subCov = null;
try {
subCov = BilMapProducer.getFinalCoverage(request,
mapLayerInfo, (AbstractGridCoverage2DReader)coverageReader);
} catch (IndexOutOfBoundsException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FactoryException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TransformException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(subCov!=null)
{
/*
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out));
writer.write(subCov.toString());
writer.flush();
writer.close();
*/
RenderedImage image = subCov.getRenderedImage();
if(image!=null)
{
int dtype = image.getData().getDataBuffer().getDataType();
/* Throw exception if required to perform conversion */
/*
if((bilEncoding.equals("application/bil32"))&&(dtype!=DataBuffer.TYPE_FLOAT)) {
throw new ServiceException("Cannot fetch BIL float data,"+
"Wrong underlying data type");
}
if((bilEncoding.equals("application/bil16"))&&(dtype!=DataBuffer.TYPE_SHORT)) {
throw new ServiceException("Cannot fetch BIL int data,"+
"Wrong underlying data type");
}
if((bilEncoding.equals("application/bil8"))&&(dtype!=DataBuffer.TYPE_BYTE)) {
throw new ServiceException("Cannot fetch BIL byte data,"+
"Wrong underlying data type");
}
*/
/*
* Perform format conversion
* Operator is not created if no conversion is necessary
*/
RenderedOp formcov = null;
if((bilEncoding.equals("application/bil32"))&&(dtype!=DataBuffer.TYPE_FLOAT)) {
formcov = FormatDescriptor.create(image,DataBuffer.TYPE_FLOAT ,null);
}
if((bilEncoding.equals("application/bil16"))&&(dtype!=DataBuffer.TYPE_SHORT)) {
formcov = FormatDescriptor.create(image,DataBuffer.TYPE_SHORT ,null);
}
if((bilEncoding.equals("application/bil8"))&&(dtype!=DataBuffer.TYPE_BYTE)) {
formcov = FormatDescriptor.create(image,DataBuffer.TYPE_BYTE ,null);
}
TiledImage tiled = null;
if (formcov!= null)
tiled = new TiledImage(formcov,width,height);
else
tiled = new TiledImage(image,width,height);
final ImageOutputStream imageOutStream = ImageIO.createImageOutputStream(out);
final ImageWriter writer = writerSPI.createWriterInstance();
writer.setOutput(imageOutStream);
writer.write(tiled);
imageOutStream.flush();
imageOutStream.close();
}
else
{
throw new ServiceException("Cannot render to BIL");
}
}
else
{
throw new ServiceException("You requested a bil of size:"+
height+"x"+width+",but you can't have it!!");
}
}