Hello,
I've created a single band geotiff (based on an existing geotiff, using
geotools -8.0-M0) and can use it in arcMap, and then classify the values
with various colors. Now I'd like to create a version of that geotiff with
colors, which can be used in google earth pro. I've searched the forum and
tried a number of different things (you may notice direct copies of code
from previous postings shown below). I am able to create the geotiff with 4
bands, and can look at them in arcMap, successfully.
However, windows picture viewer, and google earth pro both give errors or
crash. I've also tried to create a png file, but that just comes out black
and white. Below is my code, any suggestions would be appreciated.
Thank you,
Paul
Hints hints = GeoTools.getDefaultHints();
System.setProperty("com.sun.media.jai.disableMediaLib", "true");
try {
GridCoverage2D orgCov = reader.getGrid();
int numBands = orgCov.getNumSampleDimensions();
GridSampleDimension[] sample = orgCov.getSampleDimensions();
WritableRaster writeableRaster = null;
RenderedImage image = orgCov.getRenderedImage();
int width = image.getWidth();
int height = image.getHeight();
writeableRaster = image.copyData(writeableRaster);
DataBuffer buffer = image.getData().getDataBuffer();
ColorModel colorModel = new ComponentColorModel(
ColorSpace.getInstance(ColorSpace.CS_sRGB), true, false,
ColorModel.TRANSLUCENT,DataBuffer.TYPE_FLOAT);
BufferedImage bufImage = new BufferedImage(colorModel, rast, false, null);
Color red = new Color(255,0,0,255);
int redInt = red.getRGB();
Color yellow = new Color(255,255,0,255);
int yellowInt = yellow.getRGB();
Color green = new Color(0,255,0,255);
int greenInt = green.getRGB();
for (int i=0 ; i<width ; i++){
for (int j=0 ; j<height ; j++){
bufImage.setRGB(i, j, greenInt); // initially set all pixels green
if(writeableRaster.getSample(i, j, 0) <= 2)
{
bufImage.setRGB(i, j, redInt);
}
else if(writeableRaster.getSample(i, j, 0) <= 15)
{
bufImage.setRGB(i, j, yellowInt);
}
}
}
System.out.println ("\n\n trying to write file " + file);
File nFile = new File(file);
if(nFile.exists())
nFile.delete();
final GeoTiffFormat format = new GeoTiffFormat();
final GeoTiffWriteParams wp = new GeoTiffWriteParams();
wp.setCompressionMode(GeoTiffWriteParams.MODE_EXPLICIT);
wp.setCompressionType("LZW");
wp.setCompressionQuality(0.75F);
wp.setTilingMode(GeoToolsWriteParams.MODE_DEFAULT);
wp.setSourceBands(null);
GeoTiffWriter geoWrite = (GeoTiffWriter) format.getWriter(new File(file));
final ParameterValueGroup params = format.getWriteParameters();
params.parameter(AbstractGridFormat.GEOTOOLS_WRITE_PARAMS.getName().toString()).setValue(wp);
GridGeometry2D geom =reader.getGeom();
WritableRaster newraster = bufImage.copyData(null);
GridCoverage2D obsCoverage = new
GridCoverageFactory(hints).create("obstaclesNorth",newraster,
geom.getEnvelope());
geoWrite.write( obsCoverage,
(GeneralParameterValue[])params.values().toArray(new
GeneralParameterValue[1]));
geoWrite.dispose();
//Now write it as a png
String pngFile = file.replace("tif", "png");
final WorldImageWriter wiWriter = new WorldImageWriter(new File (pngFile));
final Format writerFormat = wiWriter.getFormat();
// setting write parameters
final ParameterValueGroup newParams = writerFormat.getWriteParameters();
newParams.parameter(WorldImageFormat.FORMAT.getName().toString())
.setValue("png");
final GeneralParameterValue[] gpv = { newParams
.parameter(WorldImageFormat.FORMAT.getName().toString()) };
wiWriter.write(obsCoverage, gpv);
wiWriter.dispose();
} catch (Exception e) {
e.printStackTrace();
}
}
--
View this message in context:
http://osgeo-org.1560.n6.nabble.com/Coloring-a-grey-scale-geotiff-tp4375785p4375785.html
Sent from the geotools-gt2-users mailing list archive at Nabble.com.
------------------------------------------------------------------------------
Keep Your Developer Skills Current with LearnDevNow!
The most comprehensive online learning library for Microsoft developers
is just $99.99! Visual Studio, SharePoint, SQL - plus HTML5, CSS3, MVC3,
Metro Style Apps, more. Free future releases when you subscribe now!
http://p.sf.net/sfu/learndevnow-d2d
_______________________________________________
GeoTools-GT2-Users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users