Hello,
Sorry if this question has been asked before, but I am just getting up to speed and I didn’t quite understand some of the previous answers I read.
My application is creating a geotiff, registering it with geoserver, and displaying it in openlayers via WMS with transparent = true. I would like for parts of the geotiff to be transparent and other parts to be translucent. I have total control over the generation of the file. I have not been able to make any part of the file transparent or translucent.
I am setting the alpha value of the pixels when creating the file, 0.4 for those that should be translucent and 0.0 for those that should be transparent. But that doesn’t seem to be helping.
Here is the code I am using to generate the geotiff:
public static void createGeotiff(File output, String name, float data,
int xdim, int ydim, float maxlat, float minlon, float minlat,
float maxlon, Color palette) throws IOException {
GeographicBoundingBox gbox = new GeographicBoundingBoxImpl(minlon,
maxlon, minlat, maxlat);
org.opengis.geometry.Envelope envelope = new GeneralEnvelope(gbox);
GridCoverageFactory gcf = new GridCoverageFactory();
ColorModel colorModel = getColorModelForPalette(palette);
SampleModel sampleModel = colorModel.createCompatibleSampleModel(200,
200);
int counts
byte pixels = new byte[200 * 200];
for (int i = 0; i < 200; i++) {
for (int j = 0; j < 200; j++) {
pixels[200 * i + j] = (byte) ((data[i][j] * palette.length));
}
}
DataBuffer buf = new DataBufferByte(pixels, pixels.length);
WritableRaster raster = Raster.createWritableRaster(sampleModel, buf,
null);
BufferedImage image = new BufferedImage(colorModel, raster, false, null);
GridCoverage2D gc = gcf.create(name, image, envelope);
GeoTiffFormat format = new GeoTiffFormat();
CoordinateReferenceSystem sourceCRS =
gc.getCoordinateReferenceSystem2D();
GeneralEnvelope sourceEnvelope = (GeneralEnvelope) gc.getEnvelope();
GridGeometry2D sourcedGG = (GridGeometry2D) gc.getGridGeometry();
GeoTiffWriter writer = (GeoTiffWriter) format.getWriter(output);
writer.write(gc, null);
}
private static ColorModel getColorModelForPalette(Color palette) {
byte r = new byte[palette.length + 2];
byte g = new byte[palette.length + 2];
byte b = new byte[palette.length + 2];
byte a = new byte[palette.length + 2];
for (int i = 0; i < palette.length; i++) {
r[i] = (byte) palette[i].getRed();
g[i] = (byte) palette[i].getGreen();
b[i] = (byte) palette[i].getBlue();
a[i] = (byte) palette[i].getAlpha();
}
// background color
r[palette.length] = (byte) Color.WHITE.getRed();
g[palette.length] = (byte) Color.WHITE.getGreen();
b[palette.length] = (byte) Color.WHITE.getBlue();
a[palette.length] = (byte) 0;
// out-of-range
r[palette.length + 1] = (byte) Color.WHITE.getRed();
g[palette.length + 1] = (byte) Color.WHITE.getGreen();
b[palette.length + 1] = (byte) Color.WHITE.getBlue();
a[palette.length + 1] = (byte) 0;
return new IndexColorModel(8, palette.length + 2, r, g, b, a);
}
The palette array is something like this:
Color palette = new Color {
new Color(255,255,255,0),
new Color(255,255,255,0),
new Color(255,255,255,0),
new Color(255,255,255,0),
new Color(255,255,255,0),
new Color(255,255,255,0),
new Color(255,255,255,0),
new Color(255,255,255,0),
new Color(255,255,255,0),
new Color(255,255,255,0),
new Color(255,255,255,0),
new Color(255,255,255,0),
new Color(255,255,255,0),
new Color(255,255,255,0),
new Color(255,255,255,0),
new Color(255,255,255,0),
new Color(255,150,122,0.4),
new Color(255,150,122,0.4),
new Color(255,150,122,0.4),
new Color(255,0,0,0.4),
};
I am currently using GeoServer 1.6.4 but could move to 1.7.0 if necessary. What is the best way to get transparency? Also my geotiff is currently registered to use the style called ‘raster’ that ships with geoserver 1.6.4.
Thanks,
Kevin