001: /*
002: * Geotools2 - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2002, 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;
009: * version 2.1 of the License.
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: */
017: package org.geotools.gce.arcgrid;
018:
019: import java.awt.image.Raster;
020: import java.io.File;
021: import java.io.IOException;
022: import java.net.URL;
023: import java.util.Random;
024:
025: import org.geotools.TestData;
026: import org.geotools.coverage.grid.GridCoverage2D;
027: import org.geotools.coverage.grid.io.AbstractGridFormat;
028: import org.geotools.geometry.GeneralEnvelope;
029: import org.geotools.resources.coverage.CoverageUtilities;
030: import org.opengis.coverage.grid.GridCoverageReader;
031: import org.opengis.coverage.grid.GridCoverageWriter;
032: import org.opengis.parameter.GeneralParameterValue;
033: import org.opengis.parameter.ParameterValueGroup;
034:
035: /**
036: * Test reading and writing arcgrid grid coverages.
037: *
038: * @author Daniele Romagnoli
039: * @author Simone Giannecchini (simboss)
040: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/plugin/arcgrid/src/test/java/org/geotools/gce/arcgrid/ArcGridReadWriteTest.java $
041: */
042: public class ArcGridReadWriteTest extends ArcGridBaseTestCase {
043: private final Random generator = new Random();
044:
045: /**
046: * Creates a new instance of ArcGridReadWriteTest
047: *
048: * @param name
049: * DOCUMENT ME!
050: */
051: public ArcGridReadWriteTest(String name) {
052: super (name);
053: }
054:
055: /**
056: *
057: * @param testParam
058: * @throws Exception
059: */
060: public void runMe(final File testFile) throws Exception {
061:
062: // create a temporary output file
063: // temporary file to use
064: File tmpFile = TestData.temp(this , Long.toString(Math
065: .round(100000 * generator.nextDouble()))
066: + testFile.getName());
067: tmpFile.deleteOnExit();
068:
069: // ESRI
070: LOGGER.info(testFile.getName() + " ESRI");
071: writeEsriUnCompressed(testFile, tmpFile);
072:
073: tmpFile = TestData.temp(this , Long.toString(Math
074: .round(100000 * generator.nextDouble()))
075: + testFile.getName());
076: tmpFile.deleteOnExit();
077: // GRASS
078: LOGGER.info(testFile.getName() + " GRASS");
079: writeGrassUnCompressed(testFile, tmpFile);
080:
081: }
082:
083: /**
084: * Compares 2 grid covareages, throws an exception if they are not the same.
085: *
086: * @param gc1
087: * First Grid Coverage
088: * @param gc2
089: * Second Grid Coverage
090: *
091: * @throws Exception
092: * If Coverages are not equal
093: */
094: void compare(GridCoverage2D gc1, GridCoverage2D gc2)
095: throws Exception {
096: final GeneralEnvelope e1 = (GeneralEnvelope) gc1.getEnvelope();
097: final GeneralEnvelope e2 = (GeneralEnvelope) gc2.getEnvelope();
098:
099: /** Checking Envelopes */
100: if ((e1.getLowerCorner().getOrdinate(0) != e1.getLowerCorner()
101: .getOrdinate(0))
102: || (e1.getLowerCorner().getOrdinate(1) != e1
103: .getLowerCorner().getOrdinate(1))
104: || (e1.getUpperCorner().getOrdinate(0) != e1
105: .getUpperCorner().getOrdinate(0))
106: || (e1.getUpperCorner().getOrdinate(1) != e1
107: .getUpperCorner().getOrdinate(1))) {
108: throw new Exception("GridCoverage Envelopes are not equal"
109: + e1.toString() + e2.toString());
110: }
111:
112: /** Checking CRS */
113: if (e1.getCoordinateReferenceSystem().toWKT()
114: .compareToIgnoreCase(
115: e2.getCoordinateReferenceSystem().toWKT()) != 0) {
116: throw new Exception("GridCoverage CRS are not equal"
117: + e1.getCoordinateReferenceSystem().toWKT()
118: + e2.getCoordinateReferenceSystem().toWKT());
119: }
120:
121: /** Checking values */
122: final Double noData1 = new Double(ArcGridWriter
123: .getCandidateNoData(gc1));
124: final Double noData2 = new Double(ArcGridWriter
125: .getCandidateNoData(gc2));
126: final int minTileX1 = gc1.getRenderedImage().getMinTileX();
127: final int minTileY1 = gc1.getRenderedImage().getMinTileY();
128: final int width = gc1.getRenderedImage().getWidth();
129: final int height = gc1.getRenderedImage().getHeight();
130: final int maxTileX1 = minTileX1
131: + gc1.getRenderedImage().getNumXTiles();
132: final int maxTileY1 = minTileY1
133: + gc1.getRenderedImage().getNumYTiles();
134: double value1 = 0, value2 = 0;
135:
136: for (int tileIndexX = minTileX1; tileIndexX < maxTileX1; tileIndexX++)
137: for (int tileIndexY = minTileY1; tileIndexY < maxTileY1; tileIndexY++) {
138:
139: final Raster r1 = gc1.getRenderedImage().getTile(
140: tileIndexX, tileIndexY);
141: final Raster r2 = gc2.getRenderedImage().getTile(
142: tileIndexX, tileIndexY);
143:
144: for (int i = r1.getMinX(); i < width; i++) {
145: for (int j = r1.getMinY(); j < height; j++) {
146: value1 = r1.getSampleDouble(i, j, 0);
147: value2 = r2.getSampleDouble(i, j, 0);
148:
149: if (!((noData1.compareTo(new Double(value1))) == 0 && (noData2
150: .compareTo(new Double(value2))) == 0)
151: && (value1 != value2)) {
152: throw new Exception(
153: "GridCoverage Values are not equal: "
154: + value1 + ", " + value2);
155: }
156:
157: }
158: }
159: }
160:
161: }
162:
163: /**
164: * A Simple Test Method which read an arcGrid and write it as a GRASS Ascii
165: * Grid
166: *
167: * @param wf
168: * @param rf
169: */
170: public void writeGrassUnCompressed(File rf, File wf)
171: throws Exception {
172:
173: /** Step 1: Reading the coverage */
174: GridCoverageReader reader = new ArcGridReader(rf);
175: final GridCoverage2D gc1 = (GridCoverage2D) reader.read(null);
176: assertTrue(CoverageUtilities.hasRenderingCategories(gc1));
177:
178: /** Step 2: Write grid coverage out to temp file */
179: final GridCoverageWriter writer = new ArcGridWriter(wf);
180:
181: // setting write parameters
182: ParameterValueGroup params;
183: params = writer.getFormat().getWriteParameters();
184: params.parameter("GRASS").setValue(true);
185: final ArcGridWriteParams wp = new ArcGridWriteParams();
186: wp.setSourceBands(new int[] { 0 });
187: params.parameter(
188: AbstractGridFormat.GEOTOOLS_WRITE_PARAMS.getName()
189: .toString()).setValue(wp);
190: // params.parameter("compressed").setValue(false);
191: GeneralParameterValue[] gpv = {
192: params.parameter("GRASS"),
193: params
194: .parameter(AbstractGridFormat.GEOTOOLS_WRITE_PARAMS
195: .getName().toString()) };
196: writer.write(gc1, gpv);
197: writer.dispose();
198:
199: /** Step 3: Read the just written coverage */
200: GridCoverageReader reader2 = new ArcGridReader(wf);
201: final GridCoverage2D gc2 = (GridCoverage2D) reader2.read(null);
202:
203: /** Step 4: Check if the 2 coverage are equals */
204: compare(gc1, gc2);
205:
206: /** Step 5: Show the new coverage */
207: if (TestData.isInteractiveTest()) {
208: gc1.show();
209: gc2.show();
210: } else {
211: gc1.getRenderedImage().getData();
212: gc2.getRenderedImage().getData();
213: }
214: }
215:
216: /**
217: * A Simple Test Method which read an arcGrid and write it as an ArcGrid
218: *
219: * @param rf
220: * @param wf
221: */
222: public void writeEsriUnCompressed(File rf, File wf)
223: throws Exception {
224:
225: /** Step 1: Reading the coverage */
226: GridCoverageReader reader = new ArcGridReader(rf);
227: final GridCoverage2D gc1 = (GridCoverage2D) reader.read(null);
228: assertTrue(CoverageUtilities.hasRenderingCategories(gc1));
229:
230: /** Step 2: Write grid coverage out to temp file */
231: final GridCoverageWriter writer = new ArcGridWriter(wf);
232:
233: // setting write parameters
234: ParameterValueGroup params;
235: params = writer.getFormat().getWriteParameters();
236: params.parameter("GRASS").setValue(false);
237: final ArcGridWriteParams wp = new ArcGridWriteParams();
238: wp.setSourceBands(new int[] { 0 });
239: params.parameter(
240: AbstractGridFormat.GEOTOOLS_WRITE_PARAMS.getName()
241: .toString()).setValue(wp);
242: // params.parameter("compressed").setValue(false);
243: GeneralParameterValue[] gpv = {
244: params.parameter("GRASS"),
245: params
246: .parameter(AbstractGridFormat.GEOTOOLS_WRITE_PARAMS
247: .getName().toString()) };
248: writer.write(gc1, gpv);
249: writer.dispose();
250:
251: /** Step 3: Read the just written coverage */
252: GridCoverageReader reader2 = new ArcGridReader(wf);
253: final GridCoverage2D gc2 = (GridCoverage2D) reader2.read(null);
254:
255: /** Step 4: Check if the 2 coverage are equals */
256: compare(gc1, gc2);
257:
258: /** Step 5: Show the new coverage */
259: if (TestData.isInteractiveTest()) {
260: gc1.show();
261: gc2.show();
262: } else {
263: gc1.getRenderedImage().getData();
264: gc2.getRenderedImage().getData();
265: }
266:
267: }
268:
269: public void testErrorConditions() throws IOException {
270:
271: // testing format and reader
272: LOGGER.info("testErrorConditions");
273: final File testFile = TestData.file(this ,
274: "arcgrid/precip30min.asc");
275: final URL testURL = TestData.url(this ,
276: "arcgrid/precip30min.asc");
277: final ArcGridFormat af = new ArcGridFormat();
278: assertTrue("Unable to get an a reader for a file", af
279: .accepts(testFile));
280: assertTrue("Unable to get an a reader for a URL", af
281: .accepts(testURL));
282:
283: assertNotNull("Unable to get an a reader for a file", af
284: .getReader(testFile));
285: assertNotNull("Unable to get an a reader for a URL", af
286: .getReader(testURL));
287: assertFalse(
288: "We should not get a reader for a non existing file",
289: af.accepts(TestData.temp(this , "temp")));
290: assertTrue(
291: "Write params of incorrect type",
292: af.getDefaultImageIOWriteParameters() instanceof ArcGridWriteParams);
293:
294: boolean caught = false;
295: try {
296: af.getReader(TestData.openStream(this ,
297: "arcgrid/precip30min.asc"));
298: } catch (IllegalArgumentException e) {
299: caught = true;
300: }
301: assertTrue("Streams are supported now", !caught);
302:
303: // testing writer
304: assertNotNull("Unable to get a writer for a file", af
305: .getWriter(TestData.temp(this , "temp")));
306: assertNotNull("Unable to get a writer for a url", af
307: .getWriter(TestData.temp(this , "temp").toURL()));
308: assertNotNull("We should be ablet to write on an http link", af
309: .getWriter(new URL("http://www.geo-solutions.it")));
310:
311: }
312:
313: public static final void main(String[] args) throws Exception {
314: junit.textui.TestRunner.run(ArcGridReadWriteTest.class);
315: }
316:
317: }
|