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.arcsde.gce;
018:
019: import java.awt.Point;
020: import java.awt.Rectangle;
021: import java.awt.image.BufferedImage;
022: import java.io.File;
023: import java.io.IOException;
024: import java.io.InputStream;
025: import java.net.URL;
026: import java.util.HashMap;
027: import java.util.Iterator;
028: import java.util.Map;
029: import java.util.Properties;
030: import java.util.Vector;
031: import java.util.logging.Level;
032: import java.util.logging.Logger;
033:
034: import javax.imageio.ImageIO;
035:
036: import junit.framework.TestCase;
037:
038: import org.geotools.arcsde.data.ArcSDEQueryTest;
039: import org.geotools.arcsde.gce.ArcSDEPyramid;
040: import org.geotools.arcsde.gce.imageio.ArcSDERasterImageReadParam;
041: import org.geotools.arcsde.gce.imageio.ArcSDERasterReader;
042: import org.geotools.arcsde.gce.imageio.ArcSDERasterReaderSpi;
043: import org.geotools.arcsde.pool.ArcSDEConnectionConfig;
044: import org.geotools.arcsde.pool.ArcSDEConnectionPool;
045: import org.geotools.arcsde.pool.ArcSDEConnectionPoolFactory;
046: import org.geotools.arcsde.pool.ArcSDEPooledConnection;
047: import org.geotools.arcsde.pool.UnavailableArcSDEConnectionException;
048: import org.geotools.coverage.grid.GeneralGridRange;
049: import org.geotools.coverage.grid.GridCoverage2D;
050: import org.geotools.coverage.grid.GridGeometry2D;
051: import org.geotools.data.DataSourceException;
052: import org.geotools.data.coverage.grid.AbstractGridCoverage2DReader;
053: import org.geotools.data.coverage.grid.AbstractGridFormat;
054: import org.geotools.geometry.jts.ReferencedEnvelope;
055: import org.geotools.parameter.Parameter;
056: import org.geotools.referencing.CRS;
057: import org.opengis.coverage.grid.Format;
058: import org.opengis.coverage.grid.GridCoverage;
059: import org.opengis.coverage.grid.GridCoverageReader;
060: import org.opengis.coverage.grid.GridRange;
061: import org.opengis.parameter.GeneralParameterValue;
062: import org.opengis.referencing.crs.CoordinateReferenceSystem;
063:
064: import com.esri.sde.sdk.client.SeConnection;
065: import com.esri.sde.sdk.client.SeException;
066: import com.esri.sde.sdk.client.SeExtent;
067: import com.esri.sde.sdk.client.SeFilter;
068: import com.esri.sde.sdk.client.SeLayer;
069: import com.esri.sde.sdk.client.SeQuery;
070: import com.esri.sde.sdk.client.SeRaster;
071: import com.esri.sde.sdk.client.SeRasterAttr;
072: import com.esri.sde.sdk.client.SeRasterBand;
073: import com.esri.sde.sdk.client.SeRasterColumn;
074: import com.esri.sde.sdk.client.SeRasterConstraint;
075: import com.esri.sde.sdk.client.SeRasterTile;
076: import com.esri.sde.sdk.client.SeRow;
077: import com.esri.sde.sdk.client.SeShape;
078: import com.esri.sde.sdk.client.SeShapeFilter;
079: import com.esri.sde.sdk.client.SeSqlConstruct;
080: import com.esri.sde.sdk.client.SeTable;
081:
082: /**
083: * Tests the functionality of the ArcSDE raster-display package to read rasters
084: * from an ArcSDE database
085: *
086: * @author Saul Farber, (based on ArcSDEPoolTest by Gabriel Roldan)
087: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/unsupported/arcsde/datastore/src/test/java/org/geotools/arcsde/gce/ArcSDEImageIOReaderFunctionalTest.java $
088: * @version $Id: ArcSDEImageIOReaderFunctionalTest.java 27863 2007-11-12 20:34:34Z desruisseaux $
089: */
090: public class ArcSDEImageIOReaderFunctionalTest extends TestCase {
091:
092: private static Logger LOGGER = org.geotools.util.logging.Logging
093: .getLogger("org.geotools.arcsde.gce");
094:
095: private Properties conProps;
096:
097: private ArcSDEConnectionPool pool = null;
098:
099: private HashMap fourBandReaderProps, threeBandReaderProps;
100:
101: private SeRasterAttr rasterAttrThreeBand, rasterAttrFourBand;
102:
103: /**
104: * Creates a new ArcSDEConnectionPoolTest object.
105: *
106: * Lots of one-time setup operations in here. Rather than re-do
107: * everything for each test, it's just done once in the class constructor.
108: *
109: * Not sure how this jives with JUnits testing framework though...
110: *
111: */
112: public ArcSDEImageIOReaderFunctionalTest(String name)
113: throws Exception {
114: super (name);
115: }
116:
117: protected void setUp() throws Exception {
118: super .setUp();
119:
120: //do the setup one-time only.
121: if (conProps != null)
122: return;
123:
124: InputStream in = org.geotools.test.TestData.url(null,
125: "raster-testparams.properties").openStream();
126: conProps = new Properties();
127: conProps.load(in);
128: in.close();
129:
130: ArcSDEConnectionConfig connectionConfig = new ArcSDEConnectionConfig(
131: conProps);
132: pool = ArcSDEConnectionPoolFactory.getInstance().createPool(
133: connectionConfig);
134:
135: ArcSDEPooledConnection scon = null;
136: SeQuery q = null;
137: ArcSDEPyramid pyramid;
138: SeRow r;
139: CoordinateReferenceSystem crs = CRS.decode(conProps
140: .getProperty("tableCRS"));
141: String tableName;
142: try {
143:
144: // Set up a pyramid and readerprops for the four-band 2005 imagery
145: scon = pool.getConnection();
146: tableName = conProps.getProperty("fourbandtable");
147: q = new SeQuery(scon, new String[] { "RASTER" },
148: new SeSqlConstruct(tableName));
149: q.prepareQuery();
150: q.execute();
151: r = q.fetch();
152: rasterAttrFourBand = r.getRaster(0);
153: pyramid = new ArcSDEPyramid(rasterAttrFourBand, crs);
154:
155: fourBandReaderProps = new HashMap();
156: fourBandReaderProps.put(ArcSDERasterReaderSpi.PYRAMID,
157: pyramid);
158: fourBandReaderProps.put(ArcSDERasterReaderSpi.RASTER_TABLE,
159: tableName);
160: fourBandReaderProps.put(
161: ArcSDERasterReaderSpi.RASTER_COLUMN, "RASTER");
162: } catch (SeException se) {
163: LOGGER.log(Level.SEVERE, se.getSeError().getErrDesc(), se);
164: throw se;
165: } finally {
166: if (q != null)
167: q.close();
168: if (scon != null) {
169: scon.close();
170: }
171: }
172:
173: try {
174:
175: scon = pool.getConnection();
176: tableName = conProps.getProperty("threebandtable");
177: q = new SeQuery(scon, new String[] { "RASTER" },
178: new SeSqlConstruct(tableName));
179: q.prepareQuery();
180: q.execute();
181: r = q.fetch();
182: rasterAttrThreeBand = r.getRaster(0);
183: pyramid = new ArcSDEPyramid(rasterAttrThreeBand, crs);
184:
185: threeBandReaderProps = new HashMap();
186: threeBandReaderProps.put(ArcSDERasterReaderSpi.PYRAMID,
187: pyramid);
188: threeBandReaderProps.put(
189: ArcSDERasterReaderSpi.RASTER_TABLE, tableName);
190: threeBandReaderProps.put(
191: ArcSDERasterReaderSpi.RASTER_COLUMN, "RASTER");
192: } catch (SeException se) {
193: LOGGER.log(Level.SEVERE, se.getSeError().getErrDesc(), se);
194: throw se;
195: } finally {
196: if (q != null)
197: q.close();
198: if (scon != null) {
199: scon.close();
200: }
201: }
202: }
203:
204: /**
205: * Tests reading the first three bands of a 4-band image (1 = RED, 2 =
206: * GREEN, 3 = BLUE, 4 = NEAR_INFRARED) into a TYPE_INT_RGB image.
207: *
208: * Bands are mapped as follows: rasterband 1 => image band 0 rasterband 2 =>
209: * image band 1 rasterband 3 => image band 2
210: *
211: */
212: public void testReadOutsideImageBounds() throws Exception {
213:
214: ArcSDERasterReader reader = (ArcSDERasterReader) new ArcSDERasterReaderSpi()
215: .createReaderInstance(fourBandReaderProps);
216:
217: ArcSDEPooledConnection scon = null;
218: try {
219: scon = pool.getConnection();
220:
221: SeRasterBand[] bands = rasterAttrFourBand.getBands();
222: HashMap bandMapper = new HashMap();
223: bandMapper.put(new Integer((int) bands[0].getId()
224: .longValue()), new Integer(0));
225: // blue band
226: bandMapper.put(new Integer((int) bands[1].getId()
227: .longValue()), new Integer(1));
228: // green band
229: bandMapper.put(new Integer((int) bands[2].getId()
230: .longValue()), new Integer(2));
231:
232: BufferedImage image;
233: int[] opaque;
234:
235: ArcSDERasterImageReadParam rParam = new ArcSDERasterImageReadParam();
236: rParam.setSourceBands(new int[] { 1, 2, 3 });
237: rParam.setConnection(scon);
238: rParam.setSourceRegion(new Rectangle(0, 0, 100, 100));
239: image = new BufferedImage(100, 100,
240: BufferedImage.TYPE_INT_RGB);
241: opaque = new int[image.getWidth() * image.getHeight()];
242: for (int i = 0; i < opaque.length; i++) {
243: opaque[i] = opaque[i] = 0xff000000;
244: }
245: rParam.setDestination(image);
246: rParam.setBandMapper(bandMapper);
247:
248: reader.read(9, rParam);
249:
250: //ImageIO.write(image, "PNG", new File("testReadOutsideImageBounds.png"));
251: assertTrue(
252: "Image from SDE isn't what we expected.",
253: RasterTestUtils
254: .imageEquals(
255: image,
256: conProps
257: .getProperty("testReadOutsideImageBounds.image")));
258:
259: } catch (Exception e) {
260: throw e;
261: } finally {
262: if (scon != null && !scon.isClosed())
263: scon.close();
264: }
265: }
266:
267: public void testReadOffsetImage() throws Exception {
268:
269: ArcSDERasterReader reader = (ArcSDERasterReader) new ArcSDERasterReaderSpi()
270: .createReaderInstance(fourBandReaderProps);
271:
272: ArcSDEPooledConnection scon = null;
273: try {
274: scon = pool.getConnection();
275:
276: SeRasterBand[] bands = rasterAttrFourBand.getBands();
277: HashMap bandMapper = new HashMap();
278: // red band
279: bandMapper.put(new Integer((int) bands[0].getId()
280: .longValue()), new Integer(0));
281: // blue band
282: bandMapper.put(new Integer((int) bands[1].getId()
283: .longValue()), new Integer(1));
284: // green band
285: bandMapper.put(new Integer((int) bands[2].getId()
286: .longValue()), new Integer(2));
287:
288: BufferedImage image;
289: int[] opaque;
290:
291: ArcSDERasterImageReadParam rParam = new ArcSDERasterImageReadParam();
292:
293: image = new BufferedImage(1000, 1000,
294: BufferedImage.TYPE_INT_ARGB);
295: opaque = new int[image.getWidth() * image.getHeight()];
296: for (int i = 0; i < opaque.length; i++) {
297: opaque[i] = 0xff000000;
298: }
299: image.getSampleModel().setSamples(0, 0, image.getWidth(),
300: image.getHeight(), 3, opaque,
301: image.getRaster().getDataBuffer());
302: rParam.setDestination(image);
303: rParam.setDestinationOffset(new Point(100, 100));
304: rParam.setSourceBands(new int[] { 1, 2, 3 });
305: rParam.setConnection(scon);
306: rParam.setSourceRegion(new Rectangle(0, 0, 100, 100));
307: rParam.setBandMapper(bandMapper);
308: reader.read(8, rParam);
309:
310: //ImageIO.write(image, "PNG", new File("testReadOffsetImage.png"));
311: assertTrue("Image from SDE isn't what we expected.",
312: RasterTestUtils.imageEquals(image, conProps
313: .getProperty("testReadOffsetImage.image")));
314:
315: } catch (Exception e) {
316: e.printStackTrace();
317: throw e;
318: } finally {
319: if (scon != null && !scon.isClosed())
320: scon.close();
321: }
322: }
323:
324: public void testReadOffLeftImage() throws Exception {
325:
326: ArcSDERasterReader reader = (ArcSDERasterReader) new ArcSDERasterReaderSpi()
327: .createReaderInstance(fourBandReaderProps);
328:
329: ArcSDEPooledConnection scon = null;
330: try {
331: scon = pool.getConnection();
332:
333: SeRasterBand[] bands = rasterAttrFourBand.getBands();
334: HashMap bandMapper = new HashMap();
335: // red band
336: bandMapper.put(new Integer((int) bands[0].getId()
337: .longValue()), new Integer(0));
338: // blue band
339: bandMapper.put(new Integer((int) bands[1].getId()
340: .longValue()), new Integer(1));
341: // green band
342: bandMapper.put(new Integer((int) bands[2].getId()
343: .longValue()), new Integer(2));
344:
345: BufferedImage image;
346: int[] opaque;
347:
348: ArcSDERasterImageReadParam rParam = new ArcSDERasterImageReadParam();
349:
350: rParam = new ArcSDERasterImageReadParam();
351: rParam.setSourceBands(new int[] { 1, 2, 3 });
352: rParam.setConnection(scon);
353: rParam.setSourceRegion(new Rectangle(0, 0, 67, 86));
354: image = new BufferedImage(294, 207,
355: BufferedImage.TYPE_INT_ARGB);
356: opaque = new int[image.getWidth() * image.getHeight()];
357: for (int i = 0; i < opaque.length; i++) {
358: opaque[i] = opaque[i] = 0xff000000;
359: }
360: rParam.setDestination(image);
361: rParam.setBandMapper(bandMapper);
362:
363: reader.read(11, rParam);
364:
365: //ImageIO.write(image, "PNG", new File("testReadOffLeftImage.png"));
366: assertTrue("Image from SDE isn't what we expected.",
367: RasterTestUtils.imageEquals(image, conProps
368: .getProperty("testReadOffLeftImage.image")));
369:
370: } catch (Exception e) {
371: throw e;
372: } finally {
373: if (scon != null && !scon.isClosed())
374: scon.close();
375: }
376: }
377:
378: public void testReadStateWide() throws Exception {
379:
380: ArcSDERasterReader reader = (ArcSDERasterReader) new ArcSDERasterReaderSpi()
381: .createReaderInstance(fourBandReaderProps);
382:
383: ArcSDEPooledConnection scon = null;
384: try {
385: scon = pool.getConnection();
386:
387: SeRasterBand[] bands = rasterAttrFourBand.getBands();
388: HashMap bandMapper = new HashMap();
389: // red band
390: bandMapper.put(new Integer((int) bands[0].getId()
391: .longValue()), new Integer(0));
392: // blue band
393: bandMapper.put(new Integer((int) bands[1].getId()
394: .longValue()), new Integer(1));
395: // green band
396: bandMapper.put(new Integer((int) bands[2].getId()
397: .longValue()), new Integer(2));
398:
399: BufferedImage image;
400: int[] opaque;
401:
402: ArcSDERasterImageReadParam rParam = new ArcSDERasterImageReadParam();
403:
404: rParam = new ArcSDERasterImageReadParam();
405: rParam.setSourceBands(new int[] { 1, 2, 3 });
406: rParam.setConnection(scon);
407: rParam.setSourceRegion(new Rectangle(0, 16, 586, 335));
408: image = new BufferedImage(587, 335,
409: BufferedImage.TYPE_INT_ARGB);
410: opaque = new int[image.getWidth() * image.getHeight()];
411: for (int i = 0; i < opaque.length; i++) {
412: opaque[i] = opaque[i] = 0xff000000;
413: }
414: rParam.setDestination(image);
415: rParam.setBandMapper(bandMapper);
416:
417: reader.read(10, rParam);
418:
419: //ImageIO.write(image, "PNG", new File("testReadStateWide.png"));
420: assertTrue("Image from SDE isn't what we expected.",
421: RasterTestUtils.imageEquals(image, conProps
422: .getProperty("testReadStateWide.image")));
423:
424: } catch (Exception e) {
425: throw e;
426: } finally {
427: if (scon != null && !scon.isClosed())
428: scon.close();
429: }
430: }
431: }
|