01: /*
02: * Geotools2 - OpenSource mapping toolkit
03: * (C) 2002, Geotools Project Managment Committee (PMC)
04: *
05: * This library is free software; you can redistribute it and/or
06: * modify it under the terms of the GNU Lesser General Public
07: * License as published by the Free Software Foundation;
08: * version 2.1 of the License.
09: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: */
15: package org.geotools.demo.coverage;
16:
17: // J2SE and JAI dependencies
18: import java.awt.Color;
19: import java.awt.image.DataBuffer;
20: import java.awt.image.WritableRaster;
21: import javax.media.jai.RasterFactory;
22:
23: // GeoAPI dependencies
24: import org.opengis.coverage.grid.GridCoverage;
25: import org.opengis.spatialschema.geometry.Envelope;
26: import org.opengis.referencing.crs.CoordinateReferenceSystem;
27:
28: // Geotools dependencies
29: import org.geotools.geometry.Envelope2D;
30: import org.geotools.coverage.FactoryFinder;
31: import org.geotools.coverage.grid.GridCoverage2D;
32: import org.geotools.coverage.grid.GridCoverageFactory;
33: import org.geotools.referencing.crs.DefaultGeographicCRS;
34:
35: /**
36: * A simple demo computing a {@linkplain WritableRaster raster} and displaying it. The raster uses
37: * {@code float} data type with arbitrary sample values. This demo consider the image as one and
38: * only one tile. Consequently, sample values are set directly in the raster (no need to deal for
39: * multi-tiles).
40: *
41: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/demo/coverage/src/main/java/org/geotools/demo/coverage/FloatRasterDemo.java $
42: * @version $Id: FloatRasterDemo.java 18471 2006-03-06 13:10:10Z desruisseaux $
43: * @author Martin Desruisseaux
44: */
45: public class FloatRasterDemo extends junit.framework.TestCase {
46: /**
47: * Run the demo.
48: */
49: public static void main(String[] args) {
50: /*
51: * Set the pixel values. Because we use only one tile with one band, the code below
52: * is pretty similar to the code we would have if we were just setting the values in
53: * a matrix.
54: */
55: final int width = 500;
56: final int height = 500;
57: WritableRaster raster = RasterFactory.createBandedRaster(
58: DataBuffer.TYPE_FLOAT, width, height, 1, null);
59: for (int y = 0; y < height; y++) {
60: for (int x = 0; x < width; x++) {
61: raster.setSample(x, y, 0, x + y);
62: }
63: }
64: /*
65: * Set some metadata (the CRS, the geographic envelope, etc.) and display the image.
66: * The display may be slow, since the translation from floating-point values to some
67: * color (or grayscale) is performed on the fly everytime the image is rendered.
68: */
69: CoordinateReferenceSystem crs = DefaultGeographicCRS.WGS84;
70: Envelope envelope = new Envelope2D(crs, 0, 0, 30, 30);
71: GridCoverageFactory factory = FactoryFinder
72: .getGridCoverageFactory(null);
73: GridCoverage gc = factory.create("My grayscale coverage",
74: raster, envelope);
75: ((GridCoverage2D) gc).show(); // Convenience method specific to Geotools.
76: /*
77: * The above example created a grayscale image. The example below creates a new grid
78: * coverage for the same data, but using a specified color map. Note that the factory
79: * used allows more details to be specified, for example units. Setting some of those
80: * arguments to null (as in this example) lets GridCoverage computes automatically a
81: * default value.
82: */
83: Color[] colors = new Color[] { Color.BLUE, Color.CYAN,
84: Color.WHITE, Color.YELLOW, Color.RED };
85: gc = factory.create("My colored coverage", raster, envelope,
86: null, null, null, new Color[][] { colors }, null);
87: ((GridCoverage2D) gc).geophysics(false).show();
88: }
89:
90: public void testCoverage() {
91: main(null);
92: }
93: }
|