001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2006, Geotools Project Managment Committee (PMC)
005: * (C) 2006, Institut de Recherche pour le Développement
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: */
017: package org.geotools.coverage.grid;
018:
019: // J2SE and JAI dependencies
020: import java.awt.Color;
021: import java.awt.image.DataBuffer;
022: import java.awt.image.WritableRaster;
023: import javax.media.jai.RasterFactory;
024:
025: // JUnit dependencies
026: import junit.framework.Test;
027: import junit.framework.TestCase;
028: import junit.framework.TestSuite;
029:
030: // GeoAPI dependencies
031: import org.opengis.coverage.grid.GridCoverage;
032: import org.opengis.geometry.Envelope;
033: import org.opengis.referencing.crs.CoordinateReferenceSystem;
034:
035: // Geotools dependencies
036: import org.geotools.geometry.Envelope2D;
037: import org.geotools.coverage.FactoryFinder;
038: import org.geotools.coverage.grid.GridCoverage2D;
039: import org.geotools.coverage.grid.GridCoverageFactory;
040: import org.geotools.referencing.crs.DefaultGeographicCRS;
041:
042: /**
043: * Tests the creation of a grid coverage using floating point value.
044: *
045: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/coverage/src/test/java/org/geotools/coverage/grid/FloatRasterTest.java $
046: * @version $Id: FloatRasterTest.java 24925 2007-03-27 20:12:08Z jgarnett $
047: * @author Martin Desruisseaux
048: */
049: public class FloatRasterTest extends TestCase {
050: /**
051: * Tells if the test should show the image in a windows. Set to {@code true} only if this
052: * test is executed from the command line (instead than from Maven or Netbeans for example).
053: */
054: private static boolean display;
055:
056: /**
057: * Run the suite from the command line.
058: */
059: public static void main(final String[] args) {
060: display = true;
061: junit.textui.TestRunner.run(suite());
062: }
063:
064: /**
065: * Returns the test suite.
066: */
067: public static Test suite() {
068: return new TestSuite(FloatRasterTest.class);
069: }
070:
071: /**
072: * Constructs a test case with the given name.
073: */
074: public FloatRasterTest(final String name) {
075: super (name);
076: }
077:
078: /**
079: * Tests the creation of a floating point {@link WritableRaster}.
080: */
081: public void testRaster() {
082: /*
083: * Set the pixel values. Because we use only one tile with one band, the code below
084: * is pretty similar to the code we would have if we were just setting the values in
085: * a matrix.
086: */
087: final int width = 500;
088: final int height = 500;
089: WritableRaster raster = RasterFactory.createBandedRaster(
090: DataBuffer.TYPE_FLOAT, width, height, 1, null);
091: for (int y = 0; y < height; y++) {
092: for (int x = 0; x < width; x++) {
093: raster.setSample(x, y, 0, x + y);
094: }
095: }
096: /*
097: * Set some metadata (the CRS, the geographic envelope, etc.) and display the image.
098: * The display may be slow, since the translation from floating-point values to some
099: * color (or grayscale) is performed on the fly everytime the image is rendered.
100: */
101: CoordinateReferenceSystem crs = DefaultGeographicCRS.WGS84;
102: Envelope envelope = new Envelope2D(crs, 0, 0, 30, 30);
103: GridCoverageFactory factory = FactoryFinder
104: .getGridCoverageFactory(null);
105: GridCoverage gc = factory.create("My grayscale coverage",
106: raster, envelope);
107: if (display)
108: ((GridCoverage2D) gc).show(); // Convenience method specific to Geotools.
109: /*
110: * The above example created a grayscale image. The example below creates a new grid
111: * coverage for the same data, but using a specified color map. Note that the factory
112: * used allows more details to be specified, for example units. Setting some of those
113: * arguments to null (as in this example) lets GridCoverage computes automatically a
114: * default value.
115: */
116: Color[] colors = new Color[] { Color.BLUE, Color.CYAN,
117: Color.WHITE, Color.YELLOW, Color.RED };
118: gc = factory.create("My colored coverage", raster, envelope,
119: null, null, null, new Color[][] { colors }, null);
120: if (display)
121: ((GridCoverage2D) gc).geophysics(false).show();
122: }
123:
124: /**
125: * Tests the creation of a floating point matrix.
126: */
127: public void testMatrix() {
128: final int width = 500;
129: final int height = 500;
130: final float[][] matrix = new float[height][width];
131: for (int y = 0; y < height; y++) {
132: for (int x = 0; x < width; x++) {
133: matrix[y][x] = x + y;
134: }
135: }
136: CoordinateReferenceSystem crs = DefaultGeographicCRS.WGS84;
137: Envelope envelope = new Envelope2D(crs, 0, 0, 30, 30);
138: GridCoverageFactory factory = FactoryFinder
139: .getGridCoverageFactory(null);
140: GridCoverage gc = factory.create("My grayscale matrix", matrix,
141: envelope);
142: if (display)
143: ((GridCoverage2D) gc).show(); // Convenience method specific to Geotools.
144: }
145: }
|