001    /*----------------    FILE HEADER  ------------------------------------------
002    
003     This file is part of deegree.
004     Copyright (C) 2001-2008 by:
005     EXSE, Department of Geography, University of Bonn
006     http://www.giub.uni-bonn.de/deegree/
007     lat/lon GmbH
008     http://www.lat-lon.de
009    
010     This library is free software; you can redistribute it and/or
011     modify it under the terms of the GNU Lesser General Public
012     License as published by the Free Software Foundation; either
013     version 2.1 of the License, or (at your option) any later version.
014    
015     This library is distributed in the hope that it will be useful,
016     but WITHOUT ANY WARRANTY; without even the implied warranty of
017     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
018     Lesser General Public License for more details.
019    
020     You should have received a copy of the GNU Lesser General Public
021     License along with this library; if not, write to the Free Software
022     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
023    
024     Contact:
025    
026     Andreas Poth
027     lat/lon GmbH
028     Aennchenstr. 19
029     53115 Bonn
030     Germany
031     E-Mail: poth@lat-lon.de
032    
033     Prof. Dr. Klaus Greve
034     Department of Geography
035     University of Bonn
036     Meckenheimer Allee 166
037     53115 Bonn
038     Germany
039     E-Mail: greve@giub.uni-bonn.de
040    
041     
042     ---------------------------------------------------------------------------*/
043    package org.deegree.tools.app3d;
044    
045    import java.io.File;
046    import java.io.IOException;
047    import java.net.MalformedURLException;
048    import java.net.URL;
049    import java.util.HashMap;
050    
051    import javax.xml.transform.TransformerException;
052    
053    import org.deegree.framework.log.ILogger;
054    import org.deegree.framework.log.LoggerFactory;
055    import org.deegree.framework.xml.XMLFragment;
056    import org.deegree.framework.xml.XMLParsingException;
057    import org.deegree.framework.xml.XSLTDocument;
058    import org.deegree.io.dbaseapi.DBaseException;
059    import org.deegree.io.shpapi.shape_new.ShapeFile;
060    import org.deegree.io.shpapi.shape_new.ShapeFileWriter;
061    import org.deegree.model.feature.FeatureCollection;
062    import org.deegree.model.feature.GMLFeatureCollectionDocument;
063    import org.deegree.model.spatialschema.GeometryException;
064    import org.xml.sax.SAXException;
065    
066    /**
067     * 
068     * 
069     * @version $Revision: 6259 $
070     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
071     * @author last edited by: $Author: bezema $
072     * 
073     * @version 1.0. $Revision: 6259 $, $Date: 2007-03-20 10:15:15 +0100 (Di, 20 Mrz 2007) $
074     * 
075     * @since 1.1
076     */
077    public class CityGML2Shape {
078    
079        private static final ILogger LOG = LoggerFactory.getLogger( CityGML2Shape.class );
080        
081        private static URL xsltfile = CityGML2Shape.class.getResource( "toShape.xsl" );
082        
083        private FeatureCollection fc = null;
084    
085        private String inName = null;
086    
087        private String outName = null;
088    
089        /**
090         * @param inName
091         * @param outName
092         * 
093         */
094        public CityGML2Shape( String inName, String outName ) {
095            if ( outName.endsWith( ".shp" ) ) {
096                outName = outName.substring( 0, outName.lastIndexOf( "." ) );
097            }
098            this.inName = inName;
099            this.outName = outName;
100        }
101    
102        /**
103         * @throws IOException
104         * @throws SAXException
105         * @throws XMLParsingException
106         * 
107         */
108        public void read()
109                                throws IOException, SAXException, XMLParsingException {
110            LOG.logInfo( "Reading " + inName + " ... " );
111            
112            HashMap params = new HashMap();
113    
114            XSLTDocument outXSLSheet = new XSLTDocument();
115            outXSLSheet.load( xsltfile );
116    
117            GMLFeatureCollectionDocument doc = new GMLFeatureCollectionDocument();
118            URL url;
119            try {
120                url = new URL( inName );
121            } catch ( MalformedURLException e ) {
122                url = new File( inName ).toURL();
123            }
124            doc.load( url.openStream(), url.toString() );
125    
126            try {
127                XMLFragment resultDocument = outXSLSheet.transform( doc, XMLFragment.DEFAULT_URL, null,
128                                                                    params );            
129                doc.setRootElement( resultDocument.getRootElement() );
130            } catch ( TransformerException e ) {
131                e.printStackTrace();
132            }
133            
134            fc = doc.parse();
135    
136        }
137    
138        /**
139         * @throws GeometryException
140         * @throws DBaseException
141         * @throws IOException
142         * 
143         */
144        public void write()
145                                throws DBaseException, GeometryException, IOException {
146            LOG.logInfo( "Writing " + outName + " ... " );
147            ShapeFile sf = new ShapeFile( fc, outName );
148            ShapeFileWriter writer = new ShapeFileWriter( sf );
149            writer.write( outName );
150            LOG.logInfo( "Done." );
151        }
152    
153        /**
154         * This is a command line tool.
155         * 
156         * @param args
157         */
158        public static void main( String[] args ) {
159            try {
160    
161                if ( args == null || args.length < 2 ) {
162                    System.out.println( "Usage: java -classpath .;deegree2.jar;$additional libs$ " +
163                            "de.latlon.lgv3d.ShapeExporter <inputfile/URL> <outputfile basename>" );
164                    System.exit( 1 );
165                }
166    
167                CityGML2Shape rws = new CityGML2Shape( args[0], args[1] );
168                rws.read();
169                rws.write();
170                
171            } catch ( IOException e ) {
172                LOG.logError( "IO error occured.", e );
173            } catch ( SAXException e ) {
174                LOG.logError( "The GML file could not be parsed.", e );
175            } catch ( XMLParsingException e ) {
176                LOG.logError( "The GML file could not be parsed.", e );
177            } catch ( DBaseException e ) {
178                LOG.logError( "Could not create .dbf for shapefile.", e );
179            } catch ( GeometryException e ) {
180                LOG.logError( "A geometry was faulty.", e );
181            }
182        }
183    }