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