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