001    //$HeadURL: svn+ssh://jwilden@svn.wald.intevation.org/deegree/base/branches/2.5_testing/src/org/deegree/tools/shape/GML2Shape_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.File;
039    import java.io.IOException;
040    import java.net.MalformedURLException;
041    import java.net.URL;
042    
043    import org.deegree.framework.log.ILogger;
044    import org.deegree.framework.log.LoggerFactory;
045    import org.deegree.framework.xml.XMLParsingException;
046    import org.deegree.io.dbaseapi.DBaseException;
047    import org.deegree.io.shpapi.shape_new.ShapeFile;
048    import org.deegree.io.shpapi.shape_new.ShapeFileWriter;
049    import org.deegree.model.feature.FeatureCollection;
050    import org.deegree.model.feature.GMLFeatureCollectionDocument;
051    import org.deegree.model.spatialschema.GeometryException;
052    import org.xml.sax.SAXException;
053    
054    /**
055     *
056     *
057     * @version $Revision: 18195 $
058     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
059     * @author last edited by: $Author: mschneider $
060     *
061     * @version 1.0. $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18 Jun 2009) $
062     *
063     * @since 1.1
064     */
065    public class GML2Shape_new {
066    
067        private static final ILogger LOG = LoggerFactory.getLogger( GML2Shape_new.class );
068    
069        private FeatureCollection fc = null;
070    
071        private String inName = null;
072    
073        private String outName = null;
074    
075        /**
076         * @param inName
077         * @param outName
078         */
079        public GML2Shape_new( String inName, String outName ) {
080            if ( outName.endsWith( ".shp" ) ) {
081                outName = outName.substring( 0, outName.lastIndexOf( "." ) );
082            }
083            this.inName = inName;
084            this.outName = outName;
085        }
086    
087        /**
088         * @throws IOException
089         * @throws SAXException
090         * @throws XMLParsingException
091         */
092        public void read()
093                                throws IOException, SAXException, XMLParsingException {
094            LOG.logInfo( "Reading " + inName + " ... " );
095            GMLFeatureCollectionDocument doc = new GMLFeatureCollectionDocument();
096            URL url;
097            try {
098                url = new URL( inName );
099            } catch ( MalformedURLException e ) {
100                url = new File( inName ).toURL();
101            }
102            doc.load( url.openStream(), url.toString() );
103            fc = doc.parse();
104        }
105    
106        /**
107         * @throws GeometryException
108         * @throws DBaseException
109         * @throws IOException
110         *
111         */
112        public void write()
113                                throws DBaseException, GeometryException, IOException {
114            LOG.logInfo( "Writing " + outName + " ... " );
115            ShapeFile sf = new ShapeFile( fc, outName );
116            ShapeFileWriter writer = new ShapeFileWriter( sf );
117            writer.write();
118            LOG.logInfo( "Done." );
119        }
120    
121        /**
122         * This is a command line tool.
123         *
124         * @param args
125         */
126        public static void main( String[] args ) {
127            try {
128    
129                if ( args == null || args.length < 2 ) {
130                    System.out.println( "Usage: java -cp ... ...GML2Shape_new <inputfile/URL> <outputfile basename>" );
131                    System.exit( 1 );
132                }
133    
134                GML2Shape_new rws = new GML2Shape_new( args[0], args[1] );
135                rws.read();
136                rws.write();
137    
138            } catch ( IOException e ) {
139                LOG.logError( "IO error occured.", e );
140            } catch ( SAXException e ) {
141                LOG.logError( "The GML file could not be parsed.", e );
142            } catch ( XMLParsingException e ) {
143                LOG.logError( "The GML file could not be parsed.", e );
144            } catch ( DBaseException e ) {
145                LOG.logError( "Could not create .dbf for shapefile.", e );
146            } catch ( GeometryException e ) {
147                LOG.logError( "A geometry was faulty.", e );
148            }
149        }
150    }