001: //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/trunk/src/org/deegree/tools/shape/ShapefileMerger.java $
002: /*---------------- FILE HEADER ------------------------------------------
003:
004: This file is part of deegree.
005: Copyright (C) 2001-2008 by:
006: 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: Aennchenstraße 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: /*---------------- FILE HEADER ------------------------------------------
044:
045: This file is part of deegree.
046: Copyright (C) 2001-2008 by:
047: EXSE, Department of Geography, University of Bonn
048: http://www.giub.uni-bonn.de/deegree/
049: lat/lon GmbH
050: http://www.lat-lon.de
051:
052: This library is free software; you can redistribute it and/or
053: modify it under the terms of the GNU Lesser General Public
054: License as published by the Free Software Foundation; either
055: version 2.1 of the License, or (at your option) any later version.
056:
057: This library is distributed in the hope that it will be useful,
058: but WITHOUT ANY WARRANTY; without even the implied warranty of
059: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
060: Lesser General Public License for more details.
061:
062: You should have received a copy of the GNU Lesser General Public
063: License along with this library; if not, write to the Free Software
064: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
065:
066: Contact:
067:
068: Andreas Poth
069: lat/lon GmbH
070: Aennchenstr. 19
071: 53115 Bonn
072: Germany
073: E-Mail: poth@lat-lon.de
074:
075: Klaus Greve
076: Department of Geography
077: University of Bonn
078: Meckenheimer Allee 166
079: 53115 Bonn
080: Germany
081: E-Mail: greve@giub.uni-bonn.de
082:
083:
084: ---------------------------------------------------------------------------*/
085: package org.deegree.tools.shape;
086:
087: import java.io.File;
088: import java.io.IOException;
089:
090: import org.deegree.io.dbaseapi.DBaseException;
091: import org.deegree.io.shpapi.HasNoDBaseFileException;
092: import org.deegree.io.shpapi.ShapeFile;
093: import org.deegree.model.feature.FeatureCollection;
094: import org.deegree.model.feature.FeatureFactory;
095:
096: /**
097: * ...
098: *
099: * @author <a href="mailto:taddei@lat-lon.de">Ugo Taddei</a>
100: * @author last edited by: $Author: apoth $
101: *
102: * @version 2.0, $Revision: 9346 $, $Date: 2007-12-27 08:39:07 -0800 (Thu, 27 Dec 2007) $
103: *
104: * @since 2.0
105: */
106:
107: public class ShapefileMerger {
108:
109: private FeatureCollection mergedFeatures;
110:
111: private File outputFile;
112:
113: /**
114: *
115: * @param args
116: * @throws IOException
117: * @throws HasNoDBaseFileException
118: * @throws DBaseException
119: */
120: public ShapefileMerger(String[] args) throws IOException,
121: HasNoDBaseFileException, DBaseException {
122:
123: if (this .mergedFeatures == null) {
124: this .mergedFeatures = FeatureFactory
125: .createFeatureCollection("dummy", 1000);
126: }
127:
128: for (int i = 1; i < args.length; i++) {
129:
130: String s = new File(args[i]).getAbsolutePath();
131: ShapeFile shp = new ShapeFile(s);
132: System.out.println("Opened: " + s);
133:
134: for (int j = 0; j < shp.getRecordNum(); j++) {
135: mergedFeatures.add(shp.getFeatureByRecNo(j + 1));
136: }
137: shp.close();
138: }
139:
140: this .outputFile = new File(args[0]);
141:
142: }
143:
144: /**
145: *
146: * @return merged featurecollection
147: */
148: public FeatureCollection getMergedFeatures() {
149: return this .mergedFeatures;
150: }
151:
152: /**
153: * @param args
154: */
155: public static void main(String[] args) {
156:
157: if (args.length < 3) {
158: System.out
159: .println("Usage: java -classpath .;libs/deegree2.jar;libs/jaxen-1.1-beta-8.jar;libs/jts-1.8.jar;libs/log4j-1.2.9.jar org.deegree.tools.shape.ShapefileMerger <out_shapefile> <in_shape_1> <in_shape2> ... <in_shape_n>");
160: System.exit(0);
161: }
162:
163: ShapefileMerger shpMerger = null;
164: try {
165: shpMerger = new ShapefileMerger(args);
166: shpMerger.save();
167:
168: } catch (Exception e) {
169: e.printStackTrace();
170: System.exit(1);
171: }
172:
173: }
174:
175: private void save() throws IOException {
176:
177: if (this .mergedFeatures != null) {
178: String s = this .outputFile.getAbsolutePath();
179: ShapeFile shp = new ShapeFile(s, "rw");
180: try {
181: shp.writeShape(this .mergedFeatures);
182: System.out.println("Saved: " + s);
183: } catch (Exception e) {
184: throw new IOException(
185: "Could not save merged FeatureCollection: "
186: + e.getLocalizedMessage());
187: }
188: shp.close();
189: }
190: }
191: }
|