Hi Michael, Thanks a lot for your help! It works for my scenario.
In this case, I would like to translate the pixels to real-world cartesian coordinates, in meters. Is Projected CRS a way to do it? Since my map is not localised based on latitude/longitude, but rather just relative distances in meters, is there a simple method to create a CRS to do that? (the space would be roughly 100 x 100 meters) Many thanks, Jan On Mon, Dec 7, 2009 at 4:11 AM, Michael Bedward <[email protected]>wrote: > 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); > } > } >
------------------------------------------------------------------------------ Return on Information: Google Enterprise Search pays you back Get the facts. http://p.sf.net/sfu/google-dev2dev
_______________________________________________ Geotools-gt2-users mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users
