001: //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/trunk/src/org/deegree/io/oraclegeoraster/GeoRasterReader.java $
002: /*---------------- FILE HEADER ------------------------------------------
003:
004: This file is part of deegree.
005: Copyright (C) 2001-2008 by:
006: EXSE, Department of Geography, University of Bonn
007: http://www.giub.uni-bonn.de/deegree/
008: lat/lon GmbH
009: http://www.lat-lon.de
010:
011: This library is free software; you can redistribute it and/or
012: modify it under the terms of the GNU Lesser General Public
013: License as published by the Free Software Foundation; either
014: version 2.1 of the License, or (at your option) any later version.
015:
016: This library is distributed in the hope that it will be useful,
017: but WITHOUT ANY WARRANTY; without even the implied warranty of
018: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019: Lesser General Public License for more details.
020:
021: You should have received a copy of the GNU Lesser General Public
022: License along with this library; if not, write to the Free Software
023: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
024:
025: Contact:
026:
027: Andreas Poth
028: lat/lon GmbH
029: Aennchenstr. 19
030: 53177 Bonn
031: Germany
032: E-Mail: poth@lat-lon.de
033:
034: Prof. Dr. Klaus Greve
035: Department of Geography
036: University of Bonn
037: Meckenheimer Allee 166
038: 53115 Bonn
039: Germany
040: E-Mail: greve@giub.uni-bonn.de
041:
042:
043: ---------------------------------------------------------------------------*/
044: package org.deegree.io.oraclegeoraster;
045:
046: import java.awt.Graphics2D;
047: import java.awt.Image;
048: import java.awt.image.BufferedImage;
049: import java.awt.image.RenderedImage;
050: import java.io.IOException;
051: import java.lang.reflect.Method;
052: import java.sql.Connection;
053: import java.sql.PreparedStatement;
054: import java.sql.ResultSet;
055: import java.sql.SQLException;
056: import java.sql.Statement;
057: import java.util.Properties;
058:
059: import oracle.spatial.georaster.GeoRasterException;
060: import oracle.spatial.georaster.JGeoRaster;
061: import oracle.spatial.georaster.JGeoRasterMeta;
062: import oracle.sql.STRUCT;
063:
064: import org.deegree.framework.log.ILogger;
065: import org.deegree.framework.log.LoggerFactory;
066: import org.deegree.framework.util.StringTools;
067: import org.deegree.io.DBConnectionPool;
068: import org.deegree.io.JDBCConnection;
069: import org.deegree.model.spatialschema.Envelope;
070: import org.deegree.ogcwebservices.InvalidParameterValueException;
071:
072: /**
073: *
074: *
075: *
076: * @version $Revision: 9342 $
077: * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
078: * @author last edited by: $Author: apoth $
079: *
080: * @version 1.0. $Revision: 9342 $, $Date: 2007-12-27 04:32:57 -0800 (Thu, 27 Dec 2007) $
081: *
082: * @since 2.0
083: */
084: public class GeoRasterReader {
085:
086: private static final ILogger LOG = LoggerFactory
087: .getLogger(GeoRasterReader.class);
088:
089: /**
090: *
091: * @param grDesc
092: * @param envelope
093: * requested envelope
094: * @param level
095: * requested level (resolution)
096: * @return
097: * @throws SQLException
098: * @throws IOException
099: * @throws GeoRasterException
100: * @throws Exception
101: */
102: public static RenderedImage exportRaster(
103: GeoRasterDescription grDesc, Envelope envelope)
104: throws SQLException, IOException, GeoRasterException,
105: Exception {
106:
107: DBConnectionPool pool = DBConnectionPool.getInstance();
108: JDBCConnection jdbc = grDesc.getJdbcConnection();
109: Connection con = pool.acquireConnection(jdbc.getDriver(), jdbc
110: .getURL(), jdbc.getUser(), jdbc.getPassword());
111:
112: RenderedImage ri = exportRaster(con, envelope, grDesc
113: .getRdtTable(), grDesc.getTable(), grDesc.getColumn(),
114: grDesc.getIdentification(), grDesc.getLevel());
115:
116: /*
117: * System.out.println( grDesc.getTable() ); System.out.println( grDesc.getLevel() );
118: * System.out.println(ri.getWidth() ); System.out.println(ri.getHeight() );
119: */
120:
121: pool.releaseConnection(con, jdbc.getDriver(), jdbc.getURL(),
122: jdbc.getUser(), jdbc.getPassword());
123:
124: return ri;
125: }
126:
127: /**
128: *
129: * @param connection
130: * connnection to Oracle database
131: * @param envelope
132: * requested area
133: * @param rasterRDT
134: * name of the RDT-table
135: * @param rasterTable
136: * name of the table containing a georaster col
137: * @param geoRasterCol
138: * name of the geoRaster column
139: * @param identification
140: * SQL where clause that identifies the raster of interest
141: * @param level
142: * requested resolution level
143: * @return
144: * @throws SQLException
145: * @throws IOException
146: * @throws GeoRasterException
147: * @throws Exception
148: */
149: public static RenderedImage exportRaster(Connection connection,
150: Envelope envelope, String rasterRDT, String rasterTable,
151: String geoRasterCol, String identification, int level)
152: throws Exception {
153: RenderedImage img = null;
154: try {
155:
156: int rasterID = readRasterID(connection, identification,
157: rasterTable, geoRasterCol);
158:
159: STRUCT struct = readGeoRasterMetadata(connection,
160: rasterRDT, rasterTable, geoRasterCol, rasterID);
161:
162: int major = connection.getMetaData()
163: .getDriverMajorVersion();
164: int minor = connection.getMetaData()
165: .getDriverMinorVersion();
166:
167: LOG.logDebug("Envelope: ", envelope);
168: LOG.logDebug("mapping STRUCT to a JGeoRaster object");
169: LOG.logDebug("identified Oracle version: ", major + "."
170: + minor);
171:
172: JGeoRaster jGeoRaster = null;
173: if (major == 10 && minor == 1) {
174: // synthax for Oracle 10g R1
175: Class[] clzz = new Class[] { STRUCT.class };
176: Method method = JGeoRaster.class
177: .getMethod("load", clzz);
178: jGeoRaster = (JGeoRaster) method.invoke(null,
179: new Object[] { struct });
180: jGeoRaster = JGeoRaster.load(struct);
181: } else if (major == 10 && minor == 2) {
182: // synthax for Oracle 10g R2
183: Class[] clzz = new Class[] { STRUCT.class,
184: Connection.class, boolean.class };
185: Method method = JGeoRaster.class
186: .getMethod("load", clzz);
187: Object[] params = new Object[] { struct, connection,
188: false };
189: jGeoRaster = (JGeoRaster) method.invoke(null, params);
190: } else {
191: throw new InvalidParameterValueException(
192: "Oracle must have version 10.1 or 10.2 for using Georaster functionality");
193: }
194: jGeoRaster.setViewerUse(true);
195: Properties props = jGeoRaster.getProperties();
196:
197: int maxWidth = Integer.parseInt(props
198: .getProperty("rasterInfo/dimensionSize_column"));
199: int maxHeight = Integer.parseInt(props
200: .getProperty("rasterInfo/dimensionSize_row"));
201:
202: JGeoRasterMeta metaObj = jGeoRaster.getMetadataObject();
203:
204: double xMin = metaObj.getX(0, 0);
205: double xMax = metaObj.getX(maxWidth - 1, maxHeight - 1);
206: double sc = Math.pow(2, level);
207: double lenX = (xMax - xMin) * sc;
208: double yMin = metaObj.getY(0, 0);
209: double yMax = metaObj.getY(maxWidth - 1, maxHeight - 1);
210: double lenY = (yMax - yMin) * sc;
211:
212: int xMinCell = (int) Math
213: .round((envelope.getMin().getX() - xMin) * maxWidth
214: / lenX);
215: int xMaxCell = (int) Math
216: .round((envelope.getMax().getX() - xMin) * maxWidth
217: / lenX) - 1;
218: int yMaxCell = (int) Math
219: .round((envelope.getMin().getY() - yMin)
220: * maxHeight / lenY);
221: int yMinCell = (int) Math
222: .round((envelope.getMax().getY() - yMin)
223: * maxHeight / lenY) - 1;
224:
225: String bb = StringTools.concat(100, xMinCell, " ",
226: yMinCell, " ", xMaxCell, " ", yMaxCell);
227: LOG.logInfo("requested box:", bb);
228:
229: LOG.logDebug("reading georaster image, with level: "
230: + level);
231: img = jGeoRaster.getRasterImage(connection, level,
232: xMinCell, yMinCell, xMaxCell, yMaxCell);
233:
234: int xDiff = (xMaxCell - xMinCell) + 1;
235: int yDiff = (yMaxCell - yMinCell) + 1;
236: if (img != null
237: && (img.getWidth() != xDiff || img.getHeight() != yDiff)
238: && xDiff > 0 && yDiff > 0) {
239: // request img size != result
240: LOG.logDebug(StringTools.concat(100,
241: "request img size != result; new image size: ",
242: xDiff, "x", yDiff));
243: BufferedImage bimg = new BufferedImage(xDiff, yDiff,
244: BufferedImage.TYPE_INT_ARGB);
245: Graphics2D bg = bimg.createGraphics();
246:
247: int posX = 0;
248: int posY = 0;
249:
250: if (xMinCell < 0) {
251: posX = Math.abs(xMinCell);
252: }
253: if (yMinCell < 0) {
254: posY = Math.abs(yMinCell);
255: }
256:
257: bg.drawImage((Image) img, posX, posY, null);
258: bg.dispose();
259:
260: img = bimg;
261: }
262:
263: } catch (SQLException e1) {
264: e1.printStackTrace();
265: String s = StringTools.concat(1000, e1.getMessage(), " ",
266: rasterTable, "; ", rasterRDT, "; ", geoRasterCol,
267: "; ", identification, "; level: ", level);
268: throw new RuntimeException(s);
269: } catch (Exception e) {
270: e.printStackTrace();
271: throw new RuntimeException(e);
272: }
273: return img;
274: }
275:
276: /**
277: *
278: * @param connection
279: * @param rasterRDT
280: * @param rasterTable
281: * @param geoRasterCol
282: * @param rasterID
283: * @return
284: * @throws SQLException
285: */
286: private static STRUCT readGeoRasterMetadata(Connection connection,
287: String rasterRDT, String rasterTable, String geoRasterCol,
288: int rasterID) throws SQLException {
289: LOG.logDebug("reading georaster");
290: PreparedStatement ps = connection.prepareStatement("select "
291: + geoRasterCol + " from " + rasterTable + " a where a."
292: + geoRasterCol + ".rasterid = " + rasterID + " and a."
293: + geoRasterCol + ".rasterdatatable = '"
294: + rasterRDT.toUpperCase() + "'");
295: ResultSet resultset = ps.executeQuery();
296: if (!resultset.next()) {
297: throw new SQLException(
298: "No GeoRaster object exists at rasterid = "
299: + rasterID + ", RDT = " + rasterRDT);
300: }
301:
302: STRUCT struct = (STRUCT) resultset.getObject(geoRasterCol
303: .toUpperCase());
304: resultset.close();
305: return struct;
306: }
307:
308: /**
309: * returns the rasterID of the requested GeoRaster
310: *
311: * @param connection
312: * @param identification
313: * @param sql
314: * @return
315: * @throws SQLException
316: * @throws GeoRasterException
317: */
318: private static int readRasterID(Connection connection,
319: String identification, String rasterTable,
320: String geoRasterCol) throws SQLException,
321: GeoRasterException {
322:
323: LOG.logDebug("reading rasterid ");
324:
325: String sql = "SELECT a." + geoRasterCol.toUpperCase()
326: + ".rasterid FROM " + rasterTable.toUpperCase()
327: + " a where " + identification;
328: LOG.logDebug(sql);
329: Statement stmt = connection.createStatement();
330: ResultSet rs = stmt.executeQuery(sql);
331: if (!rs.next()) {
332: throw new GeoRasterException(
333: "Georaster with identification = " + identification
334: + " not found!");
335: }
336: int rasterID = rs.getInt(1);
337: stmt.close();
338: rs.close();
339: return rasterID;
340: }
341:
342: }
|