[Geoserver-users] How to handle transparency in geotiffs

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

I should add that my data is originally being stored in a NetCDF file. I am
only converting to a GeoTiff because I don't know how to display the NetCDF
data directly. I think that my be possible though, so if you have advice to
that end it would also be welcome. If it's relevant, I am trying to display
the end result with OpenLayers, and I am using PostGIS for some of the
feature layers so it would be possible to also use a PostGrid / NetCDF
approach if that makes sense.
--
View this message in context: http://www.nabble.com/How-to-handle-transparency-in-geotiffs-tp19731223p19742486.html
Sent from the GeoServer - User mailing list archive at Nabble.com.

Hello,

Recommend looking at the Raster Symbolizer that is implemented in GeoServer
1.7.0 RC2.
http://geoserver.org/display/GEOS/GeoServer+1.7.0-RC2
http://docs.codehaus.org/display/GEOTOOLS/Raster+Symbolizer+support

I've attached a a .zip file that has an .sld and an image. The GeoTIFF that
has a swath of black data at the bottom. The attached .sld will make that
area transparent when viewed in OpenLayers.

Good Luck,
-Sam

ktcorby wrote:

I should add that my data is originally being stored in a NetCDF file. I
am only converting to a GeoTiff because I don't know how to display the
NetCDF data directly. I think that my be possible though, so if you have
advice to that end it would also be welcome. If it's relevant, I am trying
to display the end result with OpenLayers, and I am using PostGIS for some
of the feature layers so it would be possible to also use a PostGrid /
NetCDF approach if that makes sense.

http://www.nabble.com/file/p19747508/RSExample.zip RSExample.zip
--
View this message in context: http://www.nabble.com/How-to-handle-transparency-in-geotiffs-tp19731223p19747508.html
Sent from the GeoServer - User mailing list archive at Nabble.com.

Sam, thanks. I can get your image to work with your sld file, but I am having
problems with http://www.nabble.com/file/p19765621/DEFAULT34174.tiff mine
which I have attempted to attach.

I want the white values in this file to be transparent, so I modified your
sld file slightly and applied it to mine:

$ diff raster.sld rastersymbolizer.sld
17c17
< <Opacity>0.4</Opacity>
---

            <Opacity>1.0</Opacity>

19c19
< <ColorMapEntry color="#ffffff" quantity="-500"
label="nodata" o
pacity="0.0"/>
---

               <ColorMapEntry color="#000000" quantity="-500"
label="nodata" o

pacity="0.0"/>

Here is the gdalinfo output from your tiff file and from mine:

*** Yours ***
Driver: GTiff/GeoTIFF
Files: RSExample/westout.tif
Size is 3100, 2280
Coordinate System is:
GEOGCS["WGS 84",
    DATUM["WGS_1984",
        SPHEROID["WGS 84",6378137,298.2572235630016,
            AUTHORITY["EPSG","7030"]],
        AUTHORITY["EPSG","6326"]],
    PRIMEM["Greenwich",0],
    UNIT["degree",0.0174532925199433],
    AUTHORITY["EPSG","4326"]]
Origin = (-196.750000000000000,65.989999999999995)
Pixel Size = (0.035978201714683,-0.035978201714683)
Metadata:
  AREA_OR_POINT=Area
Image Structure Metadata:
  INTERLEAVE=BAND
Corner Coordinates:
Upper Left ( -196.750, 65.990) (196d45'0.00"W, 65d59'24.00"N)
Lower Left ( -196.750, -16.040) (196d45'0.00"W, 16d 2'25.08"S)
Upper Right ( -85.2175747, 65.9900000) ( 85d13'3.27"W, 65d59'24.00"N)
Lower Right ( -85.2175747, -16.0402999) ( 85d13'3.27"W, 16d 2'25.08"S)
Center (-140.9837873, 24.9748500) (140d59'1.63"W, 24d58'29.46"N)
Band 1 Block=3100x2 Type=Byte, ColorInterp=Gray

*** Mine ***
Driver: GTiff/GeoTIFF
Files: ../gotm-data/P20-54-Wed_Oct_01_11_45_39_EDT_2008/coverages/DEFAULT3
iff
Size is 200, 200
Coordinate System is:
GEOGCS["m",
    DATUM["unknown",
        SPHEROID["unnamed",6378137,298.2572235630016]],
    PRIMEM["Greenwich",0],
    UNIT[,0.0174532925199433]]
Origin = (44.275798606872556,33.383650941848757)
Pixel Size = (0.000800704956055,-0.000680599212646)
Metadata:
  AREA_OR_POINT=Point
  TIFFTAG_XRESOLUTION=1
  TIFFTAG_YRESOLUTION=1
  TIFFTAG_RESOLUTIONUNIT=1 (unitless)
Image Structure Metadata:
  INTERLEAVE=BAND
Corner Coordinates:
Upper Left ( 44.2757986, 33.3836509) ( 44d16'32.87"E, 33d23'1.14"N)
Lower Left ( 44.2757986, 33.2475311) ( 44d16'32.87"E, 33d14'51.11"N)
Upper Right ( 44.4359396, 33.3836509) ( 44d26'9.38"E, 33d23'1.14"N)
Lower Right ( 44.4359396, 33.2475311) ( 44d26'9.38"E, 33d14'51.11"N)
Center ( 44.3558691, 33.3155910) ( 44d21'21.13"E, 33d18'56.13"N)
Band 1 Block=200x40 Type=Byte, ColorInterp=Palette
  Color Table (RGB with 256 entries)
    0: 255,255,255,255
    1: 255,255,255,255
    2: 255,255,255,255
    3: 255,255,255,255
    4: 255,255,255,255
    5: 255,255,255,255
    6: 255,255,255,255
    7: 255,255,255,255
    8: 255,255,255,255
    9: 255,255,255,255
   10: 255,255,255,255
   11: 255,255,255,255
   12: 255,255,255,255
   13: 255,255,255,255
   14: 255,255,255,255
   15: 255,255,255,255
   16: 255,160,122,255
   17: 255,160,122,255
   18: 255,160,122,255
   19: 255,0,0,255
   20: 255,255,255,255
   21: 255,255,255,255
   22: 0,0,0,255
   23: 0,0,0,255
... repeats to 255: ...

Any idea?

Thanks,
Kevin
--
View this message in context: http://www.nabble.com/How-to-handle-transparency-in-geotiffs-tp19731223p19765621.html
Sent from the GeoServer - User mailing list archive at Nabble.com.

I now think that my problem is related to a problem with RasterSymbolizer
support in 1.7RC3. I posted the details as a reply to another thread called
"GS 7.1, Byte Coverages and Nodata - once again" which seems to be related.
--
View this message in context: http://www.nabble.com/How-to-handle-transparency-in-geotiffs-tp19731223p19766715.html
Sent from the GeoServer - User mailing list archive at Nabble.com.

I was able to get this working by changing my geotiff file. I am no longer producing a geotiff using an indexedcolormap as in my original source code, and the gdalinfo for the produced tiff is no closer to the example that was posted earlier in the thread. This works.

My problem now is that the image, as styled by geoserver, seems to be of a low quality. The colors are not bright or distinct enough. I am attaching the original geotiff (DEFAULT29062.tiff) that I was producing, which looks like I want it to. I abandoned this one because I was unable to get geoserver to make the white parts transparent. I am also producing the new geotiff (DEFAULT11214.tiff), which is grayscale, along with the sld style I am using for it. If you try this image in geoserver with the style and compare it to the original you should see that is basically the same image in the same colors, but that the original looks much better.

Does anyone have a suggestion for tweaking the style or the tiff to improve this?

Thanks,
Kevin

On Wed, Oct 1, 2008 at 2:44 PM, ktcorby <ktcorby@anonymised.com> wrote:

I now think that my problem is related to a problem with RasterSymbolizer
support in 1.7RC3. I posted the details as a reply to another thread called
“GS 7.1, Byte Coverages and Nodata - once again” which seems to be related.

View this message in context: http://www.nabble.com/How-to-handle-transparency-in-geotiffs-tp19731223p19766715.html
Sent from the GeoServer - User mailing list archive at Nabble.com.


This SF.Net email is sponsored by the Moblin Your Move Developer’s challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/


Geoserver-users mailing list
Geoserver-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/geoserver-users

DEFAULT29062.tiff (41.1 KB)

DEFAULT11214.tiff (39.4 KB)

raster.sld (1.84 KB)

Ciao Kevin,
a few things:

1> You are trying to apply the raster symbolizer to a raster which has
its own color model, a paletted one in this case. Be aware that the
raster symbolizer should be mainly used with raster data that does not
have its own color model, like float data, double data, short data,
etc.. because it does (in most cases) simply ignores the original
color model and applies the specified rules on the native data
contained in the raster. As an instance, without selecting a specific
band, it would be illegal to try and apply the raster symbolizer to a
paletter (RGB or RGBA) raster, hence what I do is simply apply the
specified color map the raster data, which consists in the indexes
pointing to the palette. If you get this you can get you data to
change colors pretty easily.

2> I understand your data is in netcdf originally, so why don't you
avoid adding colors yourself and use the raster symbolizer? If you
provide a geotiff with raw data and some hints on how you'd like to
see it rendered, I could help you with generating a specific SLD.

3> You said your original data is in netcdf, could you please send us
a sample, we could see if we can reuse the GDAL-based plugin or own
plugin to read this file directly.

4>Just for the records, you are giiving me the tiff fyou generate but
not the style. Can you send the style as well?

Ciao,
Simone.

On Tue, Sep 30, 2008 at 3:50 PM, ktcorby <ktcorby@anonymised.com> wrote:

I should add that my data is originally being stored in a NetCDF file. I am
only converting to a GeoTiff because I don't know how to display the NetCDF
data directly. I think that my be possible though, so if you have advice to
that end it would also be welcome. If it's relevant, I am trying to display
the end result with OpenLayers, and I am using PostGIS for some of the
feature layers so it would be possible to also use a PostGrid / NetCDF
approach if that makes sense.
--
View this message in context: http://www.nabble.com/How-to-handle-transparency-in-geotiffs-tp19731223p19742486.html
Sent from the GeoServer - User mailing list archive at Nabble.com.

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Geoserver-users mailing list
Geoserver-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/geoserver-users

--
-------------------------------------------------------
Eng. Simone Giannecchini
GeoSolutions S.A.S.
Owner - Software Engineer
Via Carignoni 51
55041 Camaiore (LU)
Italy

phone: +39 0584983027
fax: +39 0584983027
mob: +39 333 8128928

http://www.geo-solutions.it
http://www.geo-solutions.it/simone.giannecchini
http://www.linkedin.com/in/simonegiannecchini

-------------------------------------------------------

Ciao Kevin,
I have played with your image and your style.
A few annotations first.

1> Using a grayscale image and applying a style using the raster
symbolizer is correct. This way you are basically asking the
RasterSymbolizer to apply a ColorMap to your data and this is the
intended behavior (at least IMHO) for the SLD ColorMap element.

2> In general, you need to know what you want to get from the
RasterSymbolizer to get something nice and optimized. Your style is
repeating the same element a few times, but this is useless and also
can cause the creation of an unneded long colormap with the same color
at different indexes. You should try to avoid this (some optimizations
are implemented in my code, but better to be proactive :slight_smile: ).

Now, a hint, when you want to apply colors to raster data check the
histogram first (this is what I did for your raw data) in order to get
an idea about where you should head.
I did that and afterward I modified the original style you sent over
(I am attaching it to this email) in order to get a more colourful
image. I think this should help you out with choosing a better
ColorMap element for your data.

Simone.

On Thu, Oct 2, 2008 at 6:38 PM, Kevin Corby <ktcorby@anonymised.com> wrote:

I was able to get this working by changing my geotiff file. I am no longer
producing a geotiff using an indexedcolormap as in my original source code,
and the gdalinfo for the produced tiff is no closer to the example that was
posted earlier in the thread. This works.

My problem now is that the image, as styled by geoserver, seems to be of a
low quality. The colors are not bright or distinct enough. I am attaching
the original geotiff (DEFAULT29062.tiff) that I was producing, which looks
like I want it to. I abandoned this one because I was unable to get
geoserver to make the white parts transparent. I am also producing the new
geotiff (DEFAULT11214.tiff), which is grayscale, along with the sld style I
am using for it. If you try this image in geoserver with the style and
compare it to the original you should see that is basically the same image
in the same colors, but that the original looks much better.

Does anyone have a suggestion for tweaking the style or the tiff to improve
this?

Thanks,
Kevin

On Wed, Oct 1, 2008 at 2:44 PM, ktcorby <ktcorby@anonymised.com> wrote:

I now think that my problem is related to a problem with RasterSymbolizer
support in 1.7RC3. I posted the details as a reply to another thread
called
"GS 7.1, Byte Coverages and Nodata - once again" which seems to be
related.
--
View this message in context:
http://www.nabble.com/How-to-handle-transparency-in-geotiffs-tp19731223p19766715.html
Sent from the GeoServer - User mailing list archive at Nabble.com.

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's
challenge
Build the coolest Linux based applications with Moblin SDK & win great
prizes
Grand prize is a trip for two to an Open Source event anywhere in the
world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Geoserver-users mailing list
Geoserver-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/geoserver-users

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great
prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Geoserver-users mailing list
Geoserver-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/geoserver-users

--
-------------------------------------------------------
Eng. Simone Giannecchini
GeoSolutions S.A.S.
Owner - Software Engineer
Via Carignoni 51
55041 Camaiore (LU)
Italy

phone: +39 0584983027
fax: +39 0584983027
mob: +39 333 8128928

http://www.geo-solutions.it
http://www.geo-solutions.it/simone.giannecchini
http://www.linkedin.com/in/simonegiannecchini

-------------------------------------------------------

raster.sld (1.33 KB)