001    //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/branches/2.2_testing/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 17:39:07 +0100 (Do, 27 Dez 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, HasNoDBaseFileException, DBaseException {
121    
122            if ( this.mergedFeatures == null ) {
123                this.mergedFeatures = FeatureFactory.createFeatureCollection( "dummy", 1000 );
124            }
125    
126            for ( int i = 1; i < args.length; i++ ) {
127    
128                String s = new File( args[i] ).getAbsolutePath();
129                ShapeFile shp = new ShapeFile( s );
130                System.out.println( "Opened: " + s );
131    
132                for ( int j = 0; j < shp.getRecordNum(); j++ ) {
133                    mergedFeatures.add( shp.getFeatureByRecNo( j + 1 ) );
134                }
135                shp.close();
136            }
137    
138            this.outputFile = new File( args[0] );
139    
140        }
141    
142        /**
143         * 
144         * @return merged featurecollection
145         */
146        public FeatureCollection getMergedFeatures() {
147            return this.mergedFeatures;
148        }
149    
150        /**
151         * @param args
152         */
153        public static void main( String[] args ) {
154    
155            if ( args.length < 3 ) {
156                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>" );
157                System.exit( 0 );
158            }
159    
160            ShapefileMerger shpMerger = null;
161            try {
162                shpMerger = new ShapefileMerger( args );
163                shpMerger.save();
164    
165            } catch ( Exception e ) {
166                e.printStackTrace();
167                System.exit( 1 );
168            }
169    
170        }
171    
172        private void save()
173                                throws IOException {
174    
175            if ( this.mergedFeatures != null ) {
176                String s = this.outputFile.getAbsolutePath();
177                ShapeFile shp = new ShapeFile( s, "rw" );
178                try {
179                    shp.writeShape( this.mergedFeatures );
180                    System.out.println( "Saved: " + s );
181                } catch ( Exception e ) {
182                    throw new IOException( "Could not save merged FeatureCollection: " + e.getLocalizedMessage() );
183                }
184                shp.close();
185            }
186        }
187    }