001: //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/trunk/src/org/deegree/tools/srs/TransformShapeFile.java $
002: /*---------------- FILE HEADER ------------------------------------------
003:
004: This file is part of deegree.
005: Copyright (C) 2001-2008 by:
006: EXSE, Department of Geography, University of Bonn
007: http://www.giub.uni-bonn.de/deegree/
008: lat/lon GmbH
009: http://www.lat-lon.de
010:
011: This library is free software; you can redistribute it and/or
012: modify it under the terms of the GNU Lesser General Public
013: License as published by the Free Software Foundation; either
014: version 2.1 of the License, or (at your option) any later version.
015:
016: This library is distributed in the hope that it will be useful,
017: but WITHOUT ANY WARRANTY; without even the implied warranty of
018: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019: Lesser General Public License for more details.
020:
021: You should have received a copy of the GNU Lesser General Public
022: License along with this library; if not, write to the Free Software
023: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
024:
025: Contact:
026:
027: Andreas Poth
028: lat/lon GmbH
029: Aennchenstr. 19
030: 53177 Bonn
031: Germany
032: E-Mail: poth@lat-lon.de
033:
034: Prof. Dr. Klaus Greve
035: Department of Geography
036: University of Bonn
037: Meckenheimer Allee 166
038: 53115 Bonn
039: Germany
040: E-Mail: greve@giub.uni-bonn.de
041:
042: ---------------------------------------------------------------------------*/
043: package org.deegree.tools.srs;
044:
045: import java.util.Iterator;
046: import java.util.Properties;
047:
048: import org.deegree.io.shpapi.shape_new.ShapeFile;
049: import org.deegree.io.shpapi.shape_new.ShapeFileReader;
050: import org.deegree.io.shpapi.shape_new.ShapeFileWriter;
051: import org.deegree.model.crs.CRSFactory;
052: import org.deegree.model.crs.GeoTransformer;
053: import org.deegree.model.feature.Feature;
054: import org.deegree.model.feature.FeatureCollection;
055:
056: /**
057: * Tool to transform shapefiles from one CRS to another.
058: *
059: * @author <a href="mailto:tonnhofer@lat-lon.de">Oliver Tonnhofer</a>
060: * @author last edited by: $Author: apoth $
061: *
062: * @version $Revision: 10056 $, $Date: 2008-02-14 02:41:56 -0800 (Thu, 14 Feb 2008) $
063: */
064: public class TransformShapeFile {
065:
066: private static void transformShapeFile(String inFile, String inCRS,
067: String outFile, String outCRS) throws Exception {
068:
069: ShapeFile shapeFile = new ShapeFileReader(inFile, CRSFactory
070: .create(inCRS)).read();
071: FeatureCollection fc = shapeFile.getFeatureCollection();
072:
073: GeoTransformer gt = new GeoTransformer(outCRS);
074:
075: int i = 0;
076: Iterator<Feature> iter = fc.iterator();
077: while (iter.hasNext()) {
078: Feature feature = iter.next();
079: gt.transform(feature);
080: if (i++ % 10 == 0)
081: System.err.print('.');
082: }
083: System.err.println();
084:
085: ShapeFile result = new ShapeFile(fc, outFile);
086:
087: new ShapeFileWriter(result).write(outFile);
088:
089: }
090:
091: private static void printHelpAndExit() {
092: System.out
093: .println("Usage: java [...] org.deegree.tools.srs.TransformShapeFile ");
094: System.out
095: .println(" -inFile shapeBasename -inCRS crs ");
096: System.out
097: .println(" [-outFile shapeBasename] -outCRS crs");
098: System.exit(1);
099: }
100:
101: /**
102: * @param args
103: */
104: public static void main(String[] args) {
105:
106: if (args.length % 2 != 0)
107: printHelpAndExit();
108:
109: Properties map = new Properties();
110: for (int i = 0; i < args.length; i += 2) {
111: map.put(args[i], args[i + 1]);
112: }
113:
114: String outCRS = (String) map.get("-outCRS");
115: if (outCRS == null)
116: printHelpAndExit();
117:
118: String inCRS = (String) map.get("-inCRS");
119: if (inCRS == null)
120: printHelpAndExit();
121:
122: String inFilename = (String) map.get("-inFile");
123: if (inFilename == null)
124: printHelpAndExit();
125:
126: String outFilename = (String) map.get("-outFile");
127: if (outFilename == null) {
128: outFilename = inFilename + "." + outCRS;
129: }
130:
131: try {
132: transformShapeFile(inFilename, inCRS, outFilename, outCRS);
133: } catch (Exception e) {
134: e.printStackTrace();
135: }
136: }
137:
138: }
|