001    // $HeadURL: svn+ssh://jwilden@svn.wald.intevation.org/deegree/base/branches/2.5_testing/src/org/deegree/tools/shape/Shape2GML.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    
040    import org.deegree.framework.log.ILogger;
041    import org.deegree.framework.log.LoggerFactory;
042    import org.deegree.io.shpapi.ShapeFile;
043    import org.deegree.model.feature.Feature;
044    import org.deegree.model.feature.FeatureCollection;
045    import org.deegree.model.feature.FeatureFactory;
046    import org.deegree.model.feature.GMLFeatureAdapter;
047    
048    /**
049     *
050     *
051     * @version $Revision: 18195 $
052     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
053     * @author last edited by: $Author: mschneider $
054     *
055     * @version 1.0. $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18 Jun 2009) $
056     *
057     * @since 1.1
058     */
059    public class Shape2GML {
060    
061        private static final ILogger LOG = LoggerFactory.getLogger( Shape2GML.class );
062    
063        private String inFile = null;
064    
065        private String outFile = null;
066    
067        private FeatureCollection fc = null;
068    
069        /** Creates a new instance of Shape2GML */
070        public Shape2GML( String inFile, String outFile ) {
071            this.inFile = inFile;
072            this.outFile = outFile;
073        }
074    
075        private FeatureCollection read()
076                                throws Exception {
077    
078            LOG.logInfo( "reading " + inFile + " ... " );
079    
080            // open shape file
081            ShapeFile sf = new ShapeFile( inFile );
082    
083            int count = sf.getRecordNum();
084    
085            // create (empty feature collection)
086            fc = FeatureFactory.createFeatureCollection( inFile, count );
087    
088            // load each feature from the shape file create a deegree feature
089            // and add it to the feature collection
090            for ( int i = 0; i < count; i++ ) {
091                if ( i % 10 == 0 ) {
092                    System.out.print( "." );
093                }
094                Feature feat = sf.getFeatureByRecNo( i + 1 );
095                // add to feature collection
096                fc.add( feat );
097            }
098            System.out.println();
099    
100            sf.close();
101    
102            return fc;
103        }
104    
105        private void write()
106                                throws Exception {
107            LOG.logInfo( "writing " + outFile + " ... " );
108            FileOutputStream fos = new FileOutputStream( outFile );
109            new GMLFeatureAdapter().export( fc, fos );
110            fos.close();
111        }
112    
113        /**
114         * @param args
115         *            the command line arguments
116         */
117        public static void main( String[] args )
118                                throws Exception {
119    
120            if ( args == null || args.length < 2 ) {
121                System.out.println( "Two arguments - input file and output file - are required!" );
122                System.exit( 1 );
123            }
124    
125            Shape2GML s2g = new Shape2GML( args[0], args[1] );
126            s2g.read();
127            s2g.write();
128    
129        }
130    
131    }