001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2004-2006, Geotools Project Managment Committee (PMC)
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: */
016: package org.geotools.renderer.lite;
017:
018: import java.awt.Color;
019: import java.awt.geom.Rectangle2D;
020: import java.awt.image.RenderedImage;
021: import java.io.File;
022: import java.io.IOException;
023: import java.text.ParseException;
024:
025: import javax.imageio.ImageIO;
026:
027: import junit.framework.TestCase;
028: import junit.textui.TestRunner;
029:
030: import org.geotools.coverage.Category;
031: import org.geotools.coverage.GridSampleDimension;
032: import org.geotools.coverage.grid.GridCoverage2D;
033: import org.geotools.coverage.grid.GridCoverageFactory;
034: import org.geotools.geometry.GeneralEnvelope;
035: import org.geotools.geometry.jts.ReferencedEnvelope;
036: import org.geotools.map.DefaultMapContext;
037: import org.geotools.map.MapContext;
038: import org.geotools.referencing.crs.DefaultGeographicCRS;
039: import org.geotools.referencing.crs.DefaultProjectedCRS;
040: import org.geotools.referencing.cs.DefaultCartesianCS;
041: import org.geotools.referencing.operation.DefaultMathTransformFactory;
042: import org.geotools.referencing.operation.DefaultOperationMethod;
043: import org.geotools.referencing.operation.transform.LinearTransform1D;
044: import org.geotools.styling.RasterSymbolizer;
045: import org.geotools.styling.Style;
046: import org.geotools.styling.StyleBuilder;
047: import org.geotools.test.TestData;
048: import org.geotools.util.NumberRange;
049: import org.opengis.coverage.grid.GridCoverage;
050: import org.opengis.parameter.ParameterValueGroup;
051: import org.opengis.referencing.FactoryException;
052: import org.opengis.referencing.NoSuchIdentifierException;
053: import org.opengis.referencing.crs.CoordinateReferenceSystem;
054: import org.opengis.referencing.crs.GeographicCRS;
055: import org.opengis.referencing.datum.Ellipsoid;
056: import org.opengis.referencing.datum.GeodeticDatum;
057: import org.opengis.referencing.operation.MathTransform;
058:
059: /**
060: * @author Simone Giannecchini
061: * @source $URL:
062: * http://svn.geotools.org/geotools/trunk/gt/module/render/test/org/geotools/renderer/lite/GridCoverageRendererTest.java $
063: */
064: public class GridCoverageRendererTest extends TestCase {
065:
066: public GridCoverageRendererTest(String testName) {
067: super (testName);
068:
069: }
070:
071: String FILENAME = "TestGridCoverage.jpg";
072:
073: /**
074: * Returns a {@link GridCoverage} which may be used as a "real world"
075: * example.
076: *
077: * @param number
078: * The example number. Numbers are numeroted from 0 to
079: * {@link #getNumExamples()} exclusive.
080: * @return The "real world" grid coverage.
081: * @throws IOException
082: * if an I/O operation was needed and failed.
083: * @throws ParseException
084: * @throws IllegalArgumentException
085: */
086: private final GridCoverage2D getGC() throws IOException,
087: IllegalArgumentException, ParseException {
088: final String path;
089: final Rectangle2D bounds;
090:
091: // unit = "°C";
092: path = "TestGridCoverage.tif";
093: final CoordinateReferenceSystem crs = DefaultGeographicCRS.WGS84;
094:
095: // 41°S - 5°N ; 35°E - 80°E (450 x 460 pixels)
096: bounds = new Rectangle2D.Double(35, -41, 45, 46);
097: final Category values = new Category("values",
098: new Color[] { Color.BLACK }, new NumberRange(0, 255),
099: LinearTransform1D.IDENTITY);
100: final GeneralEnvelope envelope = new GeneralEnvelope(bounds);
101: final RenderedImage image = ImageIO.read(TestData.getResource(
102: this , path));
103: final int numBands = image.getSampleModel().getNumBands();
104: final GridSampleDimension[] bands = new GridSampleDimension[numBands];
105: // setting bands names.
106: for (int i = 0; i < numBands; i++) {
107:
108: bands[i] = new GridSampleDimension("band " + i,
109: new Category[] { values }, null).geophysics(true);
110: }
111: final String filename = new File(path).getName();
112: final GridCoverageFactory factory = org.geotools.coverage.FactoryFinder
113: .getGridCoverageFactory(null);
114: envelope.setCoordinateReferenceSystem(crs);
115: return (GridCoverage2D) factory.create(filename, image,
116: envelope, bands, null, null);
117: }
118:
119: /**
120: * Returns a projected CRS for test purpose.
121: */
122: private static CoordinateReferenceSystem getProjectedCRS(
123: final GridCoverage2D coverage) {
124: try {
125: final GeographicCRS base = (GeographicCRS) coverage
126: .getCoordinateReferenceSystem();
127: final Ellipsoid ellipsoid = ((GeodeticDatum) base
128: .getDatum()).getEllipsoid();
129: final DefaultMathTransformFactory factory = new DefaultMathTransformFactory();
130: final ParameterValueGroup parameters = factory
131: .getDefaultParameters("Oblique_Stereographic");
132: parameters.parameter("semi_major").setValue(
133: ellipsoid.getSemiMajorAxis());
134: parameters.parameter("semi_minor").setValue(
135: ellipsoid.getSemiMinorAxis());
136: parameters.parameter("central_meridian").setValue(5);
137: parameters.parameter("latitude_of_origin").setValue(-5);
138: final MathTransform mt;
139: try {
140: mt = factory.createParameterizedTransform(parameters);
141: } catch (FactoryException exception) {
142: fail(exception.getLocalizedMessage());
143: return null;
144: }
145: return new DefaultProjectedCRS("Stereographic",
146: new DefaultOperationMethod(mt), base, mt,
147: DefaultCartesianCS.PROJECTED);
148: } catch (NoSuchIdentifierException exception) {
149: fail(exception.getLocalizedMessage());
150: return null;
151: }
152: }
153:
154: public void testPaint() throws Exception {
155:
156: //
157: // /////////////////////////////////////////////////////////////////
158: //
159: // CREATING A GRID COVERAGE
160: //
161: //
162: // /////////////////////////////////////////////////////////////////
163: final GridCoverage2D gc = getGC();
164:
165: //
166: //
167: // /////////////////////////////////////////////////////////////////
168: //
169: // MAP CONTEXT
170: //
171: //
172: // /////////////////////////////////////////////////////////////////
173: final MapContext context = new DefaultMapContext(
174: DefaultGeographicCRS.WGS84);
175: final Style style = getStyle();
176: context.addLayer(gc, style);
177:
178: // /////////////////////////////////////////////////////////////////
179: //
180: // Streaming renderer
181: //
182: //
183: // ///////////////////////////////////////////////////////////////
184: final StreamingRenderer renderer = new StreamingRenderer();
185: renderer.setContext(context);
186:
187: RendererBaseTest.showRender("testGridCoverage", renderer, 1000,
188: context.getLayerBounds());
189:
190: }
191:
192: public void testReproject() throws Exception {
193:
194: // ///////////////////////////////////////////////////////////////////
195: //
196: // CREATING A GRID COVERAGE
197: //
198: //
199: // // /////////////////////////////////////////////////////////////////
200: final GridCoverage2D coverage = getGC();
201: //
202: // ///////////////////////////////////////////////////////////////////
203: //
204: // MAP CONTEXT
205: // We want to show the context in a different CRS
206: //
207: //
208: // /////////////////////////////////////////////////////////////////
209: final MapContext context = new DefaultMapContext(
210: DefaultGeographicCRS.WGS84);
211: final Style style = getStyle();
212: context.addLayer(coverage, style);
213:
214: // transform to a new crs
215: final CoordinateReferenceSystem destCRS = getProjectedCRS(coverage);
216:
217: //
218: // ///////////////////////////////////////////////////////////////////
219: //
220: // Streaming renderer
221: //
222: //
223: // /////////////////////////////////////////////////////////////////
224: final StreamingRenderer renderer = new StreamingRenderer();
225: renderer.setContext(context);
226:
227: ReferencedEnvelope env = context.getLayerBounds();
228: env = new ReferencedEnvelope(env.getMinX(), env.getMaxX(), env
229: .getMinY(), env.getMaxY(), DefaultGeographicCRS.WGS84);
230: final ReferencedEnvelope newbounds = env.transform(destCRS,
231: true);
232:
233: RendererBaseTest.showRender("testGridCoverageReprojection",
234: renderer, 1000, newbounds);
235:
236: }
237:
238: private Style getStyle() {
239: StyleBuilder sb = new StyleBuilder();
240: Style rasterstyle = sb.createStyle();
241: RasterSymbolizer raster = sb.createRasterSymbolizer();
242:
243: rasterstyle.addFeatureTypeStyle(sb
244: .createFeatureTypeStyle(raster));
245: rasterstyle.getFeatureTypeStyles()[0]
246: .setFeatureTypeName("GridCoverage");
247: return rasterstyle;
248: }
249:
250: public static void main(final String[] args) {
251: TestRunner.run(GridCoverageRendererTest.class);
252: }
253: }
|