001    //$HeadURL$
002    /*----------------------------------------------------------------------------
003     This file is part of deegree, http://deegree.org/
004     Copyright (C) 2001-2009 by:
005     Department of Geography, University of Bonn
006     and
007     lat/lon GmbH
008    
009     This library is free software; you can redistribute it and/or modify it under
010     the terms of the GNU Lesser General Public License as published by the Free
011     Software Foundation; either version 2.1 of the License, or (at your option)
012     any later version.
013     This library is distributed in the hope that it will be useful, but WITHOUT
014     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
015     FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
016     details.
017     You should have received a copy of the GNU Lesser General Public License
018     along with this library; if not, write to the Free Software Foundation, Inc.,
019     59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020    
021     Contact information:
022    
023     lat/lon GmbH
024     Aennchenstr. 19, 53177 Bonn
025     Germany
026     http://lat-lon.de/
027    
028     Department of Geography, University of Bonn
029     Prof. Dr. Klaus Greve
030     Postfach 1147, 53001 Bonn
031     Germany
032     http://www.geographie.uni-bonn.de/deegree/
033    
034     e-mail: info@deegree.org
035     ----------------------------------------------------------------------------*/
036    package org.deegree.tools.raster;
037    
038    import java.awt.image.BufferedImage;
039    import java.io.IOException;
040    import java.io.InputStream;
041    import java.sql.Connection;
042    import java.util.Properties;
043    
044    import org.deegree.framework.util.FileUtils;
045    import org.deegree.framework.util.ImageUtils;
046    import org.deegree.io.DBConnectionPool;
047    import org.deegree.io.oraclegeoraster.GeoRasterReader;
048    import org.deegree.model.spatialschema.Envelope;
049    import org.deegree.model.spatialschema.GeometryFactory;
050    
051    /**
052     * Utitliy program to export an image from Oracle 10g GeoRaster Database
053     * 
054     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
055     * @author last edited by: $Author: poth $
056     * 
057     * @version $Revision: 6251 $, $Date: 2007-03-19 16:59:28 +0100 (Mo, 19 Mrz 2007) $
058     */
059    public class OracleGeoRasterExporter {
060    
061        /**
062         * @param args
063         * @throws Exception
064         */
065        public static void main( String[] args )
066                                throws Exception {
067            Properties map = new Properties();
068            for ( int i = 0; i < args.length; i += 2 ) {
069                if ( args[i].equals( "-h" ) || args[i].equals( "-?" ) ) {
070                    printHelp();
071                    System.exit( 0 );
072                }
073                map.put( args[i], args[i + 1] );
074            }
075            validate( map );
076    
077            DBConnectionPool pool = DBConnectionPool.getInstance();
078    
079            Connection con = pool.acquireConnection( map.getProperty( "-driver" ), map.getProperty( "-url" ),
080                                                     map.getProperty( "-user" ), map.getProperty( "-password" ) );
081    
082            try {
083                Envelope env = GeometryFactory.createEnvelope( map.getProperty( "-envelope" ), null );
084                int level = Integer.parseInt( map.getProperty( "-level" ) );
085                float width = 1000;
086                if ( map.getProperty( "-width" ) != null ) {
087                    width = Float.parseFloat( map.getProperty( "-width" ) );
088                }
089                float height = 1000;
090                if ( map.getProperty( "-height" ) != null ) {
091                    height = Float.parseFloat( map.getProperty( "-height" ) );
092                }
093    
094                BufferedImage bi = (BufferedImage) GeoRasterReader.exportRaster(
095                                                                                 con,
096                                                                                 env,
097                                                                                 map.getProperty( "-rdtName" ).toUpperCase(),
098                                                                                 map.getProperty( "-imageTableName" ).toUpperCase(),
099                                                                                 map.getProperty( "-georColName" ).toUpperCase(),
100                                                                                 map.getProperty( "-identification" ),
101                                                                                 level, width, height );
102                ImageUtils.saveImage( bi, map.getProperty( "-outFile" ), 1 );
103            } catch ( Exception e ) {
104                throw e;
105            } finally {
106                con.close();
107                System.exit( 0 );
108            }
109    
110        }
111    
112        /**
113         * prints help text read from OracleGeoRasterExporterHelp.txt
114         * 
115         * @throws IOException
116         */
117        private static void printHelp()
118                                throws IOException {
119            InputStream is = OracleGeoRasterExporter.class.getResourceAsStream( "OracleGeoRasterExporterHelp.txt" );
120            String s = FileUtils.readTextFile( is ).toString();
121            System.out.println( s );
122        }
123    
124        /**
125         * validates that all required parameters has been set
126         * 
127         * @param map
128         * @return true if valid
129         */
130        private static boolean validate( Properties map ) {
131            if ( map.getProperty( "-driver" ) == null ) {
132                System.out.println( "-driver must be defined" );
133                return false;
134            }
135            if ( map.getProperty( "-url" ) == null ) {
136                System.out.println( "-url must be defined" );
137                return false;
138            }
139            if ( map.getProperty( "-user" ) == null ) {
140                map.put( "-user", "" );
141            }
142            if ( map.getProperty( "-password" ) == null ) {
143                map.put( "-password", "" );
144            }
145            if ( map.getProperty( "-rdtName" ) == null ) {
146                System.out.println( "-rdtName must be defined" );
147                return false;
148            }
149            if ( map.getProperty( "-imageTableName" ) == null ) {
150                System.out.println( "-imageTableName must be defined" );
151                return false;
152            }
153            if ( map.getProperty( "-georColName" ) == null ) {
154                System.out.println( "-georColName must be defined" );
155                return false;
156            }
157            if ( map.getProperty( "-envelope" ) == null ) {
158                System.out.println( "-envelope must be defined" );
159                return false;
160            }
161            if ( map.getProperty( "-level" ) == null ) {
162                System.out.println( "-level must be defined" );
163                return false;
164            }
165            if ( map.getProperty( "-outFile" ) == null ) {
166                System.out.println( "-outFile must be defined" );
167                return false;
168            }
169            if ( map.getProperty( "-identification" ) == null ) {
170                System.out.println( "-identification must be defined" );
171                return false;
172            }
173    
174            return true;
175    
176        }
177    
178    }