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.gtopo30;
018:
019: import java.io.File;
020: import java.net.URL;
021:
022: import javax.media.jai.JAI;
023: import javax.media.jai.RecyclingTileFactory;
024: import javax.media.jai.TileCache;
025:
026: import org.geotools.coverage.grid.GridCoverage2D;
027: import org.geotools.coverage.grid.io.AbstractGridFormat;
028: import org.geotools.resources.TestData;
029: import org.geotools.resources.image.CoverageUtilities;
030: import org.opengis.coverage.grid.GridCoverageReader;
031: import org.opengis.coverage.grid.GridCoverageWriter;
032:
033: /**
034: * Purpose of this class is testing the ability of this plug in to read and
035: * write back the in gtopo30 format.
036: *
037: * @author Simone Giannecchini
038: * @source $URL:
039: * http://svn.geotools.org/geotools/trunk/gt/plugin/gtopo30/test/org/geotools/gce/gtopo30/GT30ReaderWriterTest.java $
040: */
041: public class GT30ReaderWriterTest extends GT30TestBase {
042: /**
043: * Constructor for GT30ReaderTest.
044: *
045: * @param arg0
046: */
047: public GT30ReaderWriterTest(String arg0) {
048: super (arg0);
049: }
050:
051: /**
052: * Testing reader and writer for gtopo. This test first of all read an
053: * existing gtopo tessel into a coverage object, therefore it writes it back
054: * onto the disk. Once the coverage is written back\ it loads it again
055: * building a new coverage which is finally visualized.
056: *
057: * @throws Exception
058: */
059: public void test() throws Exception {
060:
061: URL statURL = TestData.url(this , (new StringBuffer(
062: this .fileName).append(".DEM").toString()));
063: AbstractGridFormat format = (AbstractGridFormat) new GTopo30FormatFactory()
064: .createFormat();
065:
066: // using a big tile cache
067: final JAI jaiDef = JAI.getDefaultInstance();
068: final TileCache cache = jaiDef.getTileCache();
069: cache.setMemoryCapacity(64 * 1024 * 1024);
070: cache.setMemoryThreshold(1.0f);
071: // final TCTool tool= new TCTool();
072:
073: // setting JAI wide hints
074: jaiDef.setRenderingHint(JAI.KEY_CACHED_TILE_RECYCLING_ENABLED,
075: Boolean.TRUE);
076:
077: // tile factory and recycler
078: final RecyclingTileFactory recyclingFactory = new RecyclingTileFactory();
079: jaiDef.setRenderingHint(JAI.KEY_TILE_FACTORY, recyclingFactory);
080: jaiDef
081: .setRenderingHint(JAI.KEY_TILE_RECYCLER,
082: recyclingFactory);
083:
084: if (format.accepts(statURL)) {
085:
086: /**
087: *
088: * STEP 1 Reading the coverage into memory in order to write it down
089: * again
090: *
091: */
092: // get a reader
093: GridCoverageReader reader = format.getReader(statURL);
094:
095: // get a grid coverage
096: gc = ((GridCoverage2D) reader.read(null));
097: assertTrue(CoverageUtilities.hasRenderingCategories(gc));
098: if (TestData.isInteractiveTest())
099: gc.show();
100:
101: // preparing to write it down
102: File testDir = TestData.file(this , "");
103: newDir = new File(testDir.getAbsolutePath() + "/newDir");
104: newDir.mkdir();
105:
106: // writing it down
107: GridCoverageWriter writer = format.getWriter(newDir);
108: writer.write(gc, null);
109:
110: /**
111: *
112: * STEP 2 Reading back into memory the previos coverage.
113: *
114: */
115: // preparing the URL
116: statURL = TestData.getResource(this , "newDir/"
117: + this .fileName + ".DEM");
118:
119: // read it again
120: reader = format.getReader(statURL);
121: gc = ((GridCoverage2D) reader.read(null));
122:
123: /**
124: *
125: * STEP 3 Visualizing the lcoverage we just read in order to see if
126: * everything is fine.
127: *
128: */
129: // packed view for this coverage
130: GridCoverage2D gc1 = gc.geophysics(false);
131: if (TestData.isInteractiveTest()) {
132: gc1.show();
133: // logging some info
134: logger.info(gc.getCoordinateReferenceSystem2D().toWKT()
135: + "\n" + gc.toString());
136: logger.info(gc1.getCoordinateReferenceSystem2D()
137: .toWKT()
138: + "\n" + gc1.toString());
139: } else {
140: gc1.getRenderedImage().getData();
141:
142: }
143:
144: }
145: }
146:
147: public static final void main(String[] args) throws Exception {
148: junit.textui.TestRunner.run(GT30ReaderWriterTest.class);
149: }
150:
151: }
|