001 //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.3_testing/src/org/deegree/tools/srs/TransformShapeFile.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.srs;
037
038 import java.util.Iterator;
039 import java.util.Properties;
040
041 import org.deegree.io.shpapi.shape_new.ShapeFile;
042 import org.deegree.io.shpapi.shape_new.ShapeFileReader;
043 import org.deegree.io.shpapi.shape_new.ShapeFileWriter;
044 import org.deegree.model.crs.CRSFactory;
045 import org.deegree.model.crs.GeoTransformer;
046 import org.deegree.model.feature.Feature;
047 import org.deegree.model.feature.FeatureCollection;
048
049 /**
050 * Tool to transform shapefiles from one CRS to another.
051 *
052 * @author <a href="mailto:tonnhofer@lat-lon.de">Oliver Tonnhofer</a>
053 * @author last edited by: $Author: mschneider $
054 *
055 * @version $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18. Jun 2009) $
056 */
057 public class TransformShapeFile {
058
059 private static void transformShapeFile( String inFile, String inCRS, String outFile, String outCRS )
060 throws Exception {
061
062 ShapeFile shapeFile = new ShapeFileReader( inFile, CRSFactory.create( inCRS ) ).read();
063 FeatureCollection fc = shapeFile.getFeatureCollection();
064
065 GeoTransformer gt = new GeoTransformer( outCRS );
066
067 int i = 0;
068 System.out.println( "Transforming: " );
069 Iterator<Feature> iter = fc.iterator();
070 int step = (int) Math.floor( 5 * ( fc.size() * 0.01 ) );
071 step = Math.max( 1, step );
072 int percentage = 0;
073 while ( iter.hasNext() ) {
074 Feature feature = iter.next();
075 gt.transform( feature );
076 if ( i++ % step == 0 ) {
077 System.out.print( "\r" + ( percentage ) + ( ( ( percentage ) < 10 ) ? " " : " " ) + "% transformed" );
078 percentage += 5;
079 }
080 }
081 System.out.println();
082
083 ShapeFile result = new ShapeFile( fc, outFile );
084
085 new ShapeFileWriter( result ).write();
086
087 }
088
089 private static void printHelpAndExit() {
090 System.out.println( "Usage: java [...] org.deegree.tools.srs.TransformShapeFile " );
091 System.out.println( " -inFile shapeBasename -inCRS crs " );
092 System.out.println( " [-outFile shapeBasename] -outCRS crs" );
093 System.exit( 1 );
094 }
095
096 /**
097 * @param args
098 */
099 public static void main( String[] args ) {
100
101 if ( args.length % 2 != 0 )
102 printHelpAndExit();
103
104 Properties map = new Properties();
105 for ( int i = 0; i < args.length; i += 2 ) {
106 map.put( args[i], args[i + 1] );
107 }
108
109 String outCRS = (String) map.get( "-outCRS" );
110 if ( outCRS == null )
111 printHelpAndExit();
112
113 String inCRS = (String) map.get( "-inCRS" );
114 if ( inCRS == null )
115 printHelpAndExit();
116
117 String inFilename = (String) map.get( "-inFile" );
118 if ( inFilename == null )
119 printHelpAndExit();
120
121 String outFilename = (String) map.get( "-outFile" );
122 if ( outFilename == null ) {
123 outFilename = inFilename + "." + outCRS;
124 }
125
126 try {
127 transformShapeFile( inFilename, inCRS, outFilename, outCRS );
128 } catch ( Exception e ) {
129 e.printStackTrace();
130 System.out.println( e.getLocalizedMessage() );
131 System.exit( 2 );
132 }
133 }
134
135 }