001    // $HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/branches/2.2_testing/src/org/deegree/tools/shape/Shape2GML_new.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     53115 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.tools.shape;
045    
046    import java.io.FileOutputStream;
047    import java.io.IOException;
048    
049    import org.deegree.framework.log.ILogger;
050    import org.deegree.framework.log.LoggerFactory;
051    import org.deegree.io.dbaseapi.DBaseException;
052    import org.deegree.io.shpapi.shape_new.ShapeFile;
053    import org.deegree.io.shpapi.shape_new.ShapeFileReader;
054    import org.deegree.io.shpapi.shape_new.ShapeGeometryException;
055    import org.deegree.model.feature.FeatureCollection;
056    import org.deegree.model.feature.FeatureException;
057    import org.deegree.model.feature.GMLFeatureAdapter;
058    
059    /**
060     * 
061     * 
062     * @version $Revision: 9346 $
063     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
064     * @author last edited by: $Author: apoth $
065     * 
066     * @version 1.0. $Revision: 9346 $, $Date: 2007-12-27 17:39:07 +0100 (Do, 27 Dez 2007) $
067     * 
068     * @since 1.1
069     */
070    public class Shape2GML_new {
071    
072        private static final ILogger LOG = LoggerFactory.getLogger( Shape2GML_new.class );
073    
074        private String inFile = null;
075    
076        private String outFile = null;
077    
078        /**
079         * Creates a new instance of Shape2GML
080         * 
081         * @param inFile
082         * @param outFile
083         */
084        public Shape2GML_new( String inFile, String outFile ) {
085            if ( inFile.endsWith( ".shp" ) ) {
086                inFile = inFile.substring( 0, inFile.lastIndexOf( "." ) );
087            }
088            this.inFile = inFile;
089            this.outFile = outFile;
090        }
091    
092        private FeatureCollection read()
093                                throws IOException, DBaseException {
094    
095            LOG.logInfo( "Reading " + inFile + " ... " );
096    
097            // open shape file
098            ShapeFileReader reader = new ShapeFileReader( inFile );
099            ShapeFile sf = reader.read();
100            
101            return sf.getFeatureCollection();
102        }
103    
104        private void write( FeatureCollection fc )
105                                throws IOException, FeatureException {
106            LOG.logInfo( "Writing " + outFile + " ... " );
107            FileOutputStream fos = new FileOutputStream( outFile );
108            new GMLFeatureAdapter().export( fc, fos );
109            fos.close();
110        }
111    
112        /**
113         * @param args
114         *            the command line arguments
115         */
116        public static void main( String[] args ) {
117    
118            try {            
119                if ( args == null || args.length < 2 ) {
120                    System.out.println( "Usage: java -cp ... ...Shape2GML_new <inputfile basename> <outputfile>" );
121                    System.exit( 1 );
122                }
123    
124                Shape2GML_new s2g = new Shape2GML_new( args[0], args[1] );
125                s2g.write( s2g.read() );
126                LOG.logInfo( "Done." );
127            } catch ( IOException e ) {
128                e.printStackTrace();
129                LOG.logError( "An IO error occured.", e );
130            } catch ( FeatureException e ) {
131                e.printStackTrace();
132                LOG.logError( "Features could not be created.", e );
133            } catch ( ShapeGeometryException e ) {
134                e.printStackTrace();
135                LOG.logError( "Some error occured while converting Geometries.", e );
136            } catch ( DBaseException e ) {
137                e.printStackTrace();
138                LOG.logError( "The .dbf could not be read.", e );
139            }
140        }
141    
142    }