Hi Jan,

Below is one way of doing it. See if it works for you.

Michael


package org.geotools.stuff;

import java.awt.geom.Rectangle2D;
import java.awt.image.ColorModel;
import java.awt.image.ComponentSampleModel;
import java.awt.image.DataBuffer;
import java.awt.image.SampleModel;
import java.io.File;
import java.io.IOException;
import javax.media.jai.TiledImage;
import org.geotools.coverage.CoverageFactoryFinder;
import org.geotools.coverage.grid.GridCoverage2D;
import org.geotools.coverage.grid.GridCoverageFactory;
import org.geotools.gce.geotiff.GeoTiffWriter;
import org.geotools.geometry.jts.ReferencedEnvelope;

/**
 * @author Michael Bedward
 */
public class Foo {

    public static void main(String[] args) throws Exception {
        (new Foo()).geotiffExample(new File("/tmp/walls.tif"));
    }

    public void geotiffExample(File file) throws IOException {
        int[][] data = new int[1000][1000];

        // create some walls (assume data is in row-major order
        int y = 200;
        for (int x = 200; x < 800; x++) { data[y][x] = 1; }

        y = 500;
        for (int x = 200; x < 600; x++) { data[y][x] = 1; }

        y = 800;
        for (int x = 400; x < 800; x++) { data[y][x] = 1; }

        GridCoverage2D cov = makeBinaryCoverage(data);
        GeoTiffWriter writer = new GeoTiffWriter(file);
        writer.write(cov, null);
    }

    /**
     * Create a new coverage from the given data array. All non-zero array
     * values are written to the coverage as 1; zero values as 0.
     */
    public GridCoverage2D makeBinaryCoverage(int[][] data) {
        GridCoverageFactory gcf =
CoverageFactoryFinder.getGridCoverageFactory(null);

        // Assume data array is in row-major order
        final int dataW = data[0].length;
        final int dataH = data.length;
        final int imgTileW = 128;

        // image tile sample model
        SampleModel sm = new ComponentSampleModel(DataBuffer.TYPE_BYTE,
                imgTileW, imgTileW, 1, imgTileW, new int[]{0});

        ColorModel cm = TiledImage.createColorModel(sm);

        TiledImage img = new TiledImage(0, 0, dataW, dataH, 0, 0, sm, cm);

        for (int y = 0; y < dataH; y++) {
            for (int x = 0; x < dataW; x++) {
                if (data[y][x] != 0) {
                    img.setSample(x, y, 0, 1);
                }
            }
        }

        // Set world coords as 1:1 with image coords for this example
        ReferencedEnvelope env = new ReferencedEnvelope(
                new Rectangle2D.Double(0, 0, dataW, dataH), null);

        return gcf.create("coverage", img, env);
    }
}

------------------------------------------------------------------------------
Join us December 9, 2009 for the Red Hat Virtual Experience,
a free event focused on virtualization and cloud computing. 
Attend in-depth sessions from your desk. Your couch. Anywhere.
http://p.sf.net/sfu/redhat-sfdev2dev
_______________________________________________
Geotools-gt2-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users

Reply via email to