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