Hi,

I am loading a tif image to a MapContent and then writing it to a grayscale
png file, but the resulting image is rotated.
I used code from the raster image tutorial, but something is missing or
incorrect.

Can someone help me with the following code?

public class Convert {

    private static final String INPUT_FILE = "/tmp/bluemarble.tif";
    private static final String OUTPUT_FILE = "/tmp/image.png";

    private static StyleFactory sf = CommonFactoryFinder.getStyleFactory();
    private static FilterFactory2 ff =
CommonFactoryFinder.getFilterFactory2();

    public static void main(String[] args) {
        File rasterFile = new File(INPUT_FILE);

        AbstractGridFormat format = GridFormatFinder.findFormat(rasterFile);
        AbstractGridCoverage2DReader reader = format.getReader(rasterFile);

        Style rasterStyle = createGreyscaleStyle(1);

        final MapContent map = new MapContent();

        Layer rasterLayer = new GridReaderLayer(reader, rasterStyle);
        map.addLayer(rasterLayer);

        saveImage(map, OUTPUT_FILE, 1000);
    }

    private static Style createGreyscaleStyle(int band) {
        ContrastEnhancement ce = sf.contrastEnhancement(ff.literal(1.0),
ContrastMethod.NORMALIZE);
        SelectedChannelType sct =
sf.createSelectedChannelType(String.valueOf(band), ce);

        RasterSymbolizer sym = sf.getDefaultRasterSymbolizer();
        ChannelSelection sel = sf.channelSelection(sct);
        sym.setChannelSelection(sel);

        return SLD.wrapSymbolizers(sym);
    }

    public static void saveImage(final MapContent map, final String file,
final int imageWidth) {

        GTRenderer renderer = new StreamingRenderer();
        renderer.setMapContent(map);

        Rectangle imageBounds = null;
        ReferencedEnvelope mapBounds = null;
        try {
            mapBounds = map.getMaxBounds();
            double heightToWidth = mapBounds.getSpan(1) /
mapBounds.getSpan(0);
            imageBounds = new Rectangle(0, 0, imageWidth, (int)
Math.round(imageWidth
                    * heightToWidth));

        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        BufferedImage image = new BufferedImage(imageBounds.width,
imageBounds.height,
                BufferedImage.TYPE_INT_RGB);

        Graphics2D gr = image.createGraphics();
        gr.setPaint(Color.WHITE);
        gr.fill(imageBounds);

        try {
            renderer.paint(gr, imageBounds, mapBounds);
            File fileToSave = new File(file);
            ImageIO.write(image, "png", fileToSave);

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

<geotools-gt2-users@lists.sourceforge.net>Best regards,
Miguel

-- 
Miguel Figueiredo
Software Developer
http://jaragua.hopto.org <http://jaragua.dyndns.org>

"I'm a pretty lazy person and am prepared to work quite hard in order to
avoid work."
-- Martin Fowler
------------------------------------------------------------------------------
Want fast and easy access to all the code in your enterprise? Index and
search up to 200,000 lines of code with a free copy of Black Duck
Code Sight - the same software that powers the world's largest code
search on Ohloh, the Black Duck Open Hub! Try it now.
http://p.sf.net/sfu/bds
_______________________________________________
GeoTools-GT2-Users mailing list
GeoTools-GT2-Users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users

Reply via email to