001    /*----------------    FILE HEADER  ------------------------------------------
002    
003     This file is part of LGV 3D 
004    
005     Contact:
006    
007     Andreas Poth
008     lat/lon GmbH
009     Aennchenstr. 19
010     53177 Bonn
011     Germany
012     E-Mail: poth@lat-lon.de
013    
014     ---------------------------------------------------------------------------*/
015    package org.deegree.tools.app3d;
016    
017    import java.io.File;
018    import java.io.IOException;
019    import java.net.MalformedURLException;
020    import java.net.URL;
021    import java.util.HashMap;
022    
023    import javax.xml.transform.TransformerException;
024    
025    import org.deegree.framework.log.ILogger;
026    import org.deegree.framework.log.LoggerFactory;
027    import org.deegree.framework.xml.XMLFragment;
028    import org.deegree.framework.xml.XMLParsingException;
029    import org.deegree.framework.xml.XSLTDocument;
030    import org.deegree.io.dbaseapi.DBaseException;
031    import org.deegree.io.shpapi.shape_new.ShapeFile;
032    import org.deegree.io.shpapi.shape_new.ShapeFileWriter;
033    import org.deegree.model.feature.FeatureCollection;
034    import org.deegree.model.feature.GMLFeatureCollectionDocument;
035    import org.deegree.model.spatialschema.GeometryException;
036    import org.xml.sax.SAXException;
037    
038    /**
039     * 
040     * 
041     * @version $Revision: 6259 $
042     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
043     * @author last edited by: $Author: bezema $
044     * 
045     * @version 1.0. $Revision: 6259 $, $Date: 2007-03-20 10:15:15 +0100 (Di, 20 Mrz 2007) $
046     * 
047     * @since 1.1
048     */
049    public class CityGML2Shape {
050    
051        private static final ILogger LOG = LoggerFactory.getLogger( CityGML2Shape.class );
052        
053        private static URL xsltfile = CityGML2Shape.class.getResource( "toShape.xsl" );
054        
055        private FeatureCollection fc = null;
056    
057        private String inName = null;
058    
059        private String outName = null;
060    
061        /**
062         * @param inName
063         * @param outName
064         * 
065         */
066        public CityGML2Shape( String inName, String outName ) {
067            if ( outName.endsWith( ".shp" ) ) {
068                outName = outName.substring( 0, outName.lastIndexOf( "." ) );
069            }
070            this.inName = inName;
071            this.outName = outName;
072        }
073    
074        /**
075         * @throws IOException
076         * @throws SAXException
077         * @throws XMLParsingException
078         * 
079         */
080        public void read()
081                                throws IOException, SAXException, XMLParsingException {
082            LOG.logInfo( "Reading " + inName + " ... " );
083            
084            HashMap params = new HashMap();
085    
086            XSLTDocument outXSLSheet = new XSLTDocument();
087            outXSLSheet.load( xsltfile );
088    
089            GMLFeatureCollectionDocument doc = new GMLFeatureCollectionDocument();
090            URL url;
091            try {
092                url = new URL( inName );
093            } catch ( MalformedURLException e ) {
094                url = new File( inName ).toURL();
095            }
096            doc.load( url.openStream(), url.toString() );
097    
098            try {
099                XMLFragment resultDocument = outXSLSheet.transform( doc, XMLFragment.DEFAULT_URL, null,
100                                                                    params );            
101                doc.setRootElement( resultDocument.getRootElement() );
102            } catch ( TransformerException e ) {
103                e.printStackTrace();
104            }
105            
106            fc = doc.parse();
107    
108        }
109    
110        /**
111         * @throws GeometryException
112         * @throws DBaseException
113         * @throws IOException
114         * 
115         */
116        public void write()
117                                throws DBaseException, GeometryException, IOException {
118            LOG.logInfo( "Writing " + outName + " ... " );
119            ShapeFile sf = new ShapeFile( fc, outName );
120            ShapeFileWriter writer = new ShapeFileWriter( sf );
121            writer.write( outName );
122            LOG.logInfo( "Done." );
123        }
124    
125        /**
126         * This is a command line tool.
127         * 
128         * @param args
129         */
130        public static void main( String[] args ) {
131            try {
132    
133                if ( args == null || args.length < 2 ) {
134                    System.out.println( "Usage: java -classpath .;deegree2.jar;$additional libs$ " +
135                            "de.latlon.lgv3d.ShapeExporter <inputfile/URL> <outputfile basename>" );
136                    System.exit( 1 );
137                }
138    
139                CityGML2Shape rws = new CityGML2Shape( args[0], args[1] );
140                rws.read();
141                rws.write();
142                
143            } catch ( IOException e ) {
144                LOG.logError( "IO error occured.", e );
145            } catch ( SAXException e ) {
146                LOG.logError( "The GML file could not be parsed.", e );
147            } catch ( XMLParsingException e ) {
148                LOG.logError( "The GML file could not be parsed.", e );
149            } catch ( DBaseException e ) {
150                LOG.logError( "Could not create .dbf for shapefile.", e );
151            } catch ( GeometryException e ) {
152                LOG.logError( "A geometry was faulty.", e );
153            }
154        }
155    }