001    //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/tags/2.1/src/org/deegree/tools/shape/ShapefileMerger.java $
002    /*----------------    FILE HEADER  ------------------------------------------
003    
004     This file is part of deegree.
005     Copyright (C) 2001-2006 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-2006 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     * 
100     * @author <a href="mailto:taddei@lat-lon.de">Ugo Taddei</a>
101     * @author last edited by: $Author: bezema $
102     * 
103     * @version 2.0, $Revision: 6259 $, $Date: 2007-03-20 10:15:15 +0100 (Di, 20 Mär 2007) $
104     * 
105     * @since 2.0
106     */
107    
108    public class ShapefileMerger {
109    
110        private FeatureCollection mergedFeatures;
111        
112        private File outputFile;
113        
114        public ShapefileMerger( String[] args ) throws IOException, HasNoDBaseFileException, DBaseException {
115    
116            if( this.mergedFeatures == null ){
117                this.mergedFeatures = FeatureFactory.createFeatureCollection( "dummy", 1000 );
118            }
119    
120            for ( int i = 1; i < args.length; i++ ) {
121                
122                String s = new File( args[ i ] ).getAbsolutePath();
123                ShapeFile shp = new ShapeFile( s );
124                System.out.println( "Opened: " + s );
125    
126                for ( int j = 0; j < shp.getRecordNum(); j++ ) {
127                    mergedFeatures.add( shp.getFeatureByRecNo( j + 1 ) );
128                }
129                shp.close();
130            }
131            
132            this.outputFile = new File( args[0] );
133            
134        }
135    
136        
137        public FeatureCollection getMergedFeatures(){
138            return this.mergedFeatures;
139        }
140        
141        /**
142         * @param args
143         */
144        public static void main( String[] args ) {
145            
146            if ( args.length < 3 ){
147                System.out.println( "Usage: java -classpath deegree.jar;. org.deegree.tools.shape.ShapefileMerger <out_shapefile> <in_shape_1> <in_shape2> ... <in_shape_n>" );
148                System.exit( 0 );
149            }
150    
151            ShapefileMerger shpMerger = null;
152            try {
153                shpMerger = new ShapefileMerger( args );
154                shpMerger.save();
155                
156            } catch ( Exception e ) {
157                e.printStackTrace();
158                System.exit( 1 );
159            } 
160            
161        }
162    
163        private void save() throws IOException {
164            
165            if( this.mergedFeatures != null ){
166                String s = this.outputFile.getAbsolutePath();
167                ShapeFile shp = new ShapeFile( s, "rw" );
168                try {
169                    shp.writeShape( this.mergedFeatures );
170                    System.out.println( "Saved: " + s);
171                } catch ( Exception e ) {
172                    throw new IOException( "Could not save merged FeatureCollection: " 
173                                           +  e.getLocalizedMessage() );
174                }
175                shp.close();
176            }
177        }
178    }
179    
180    
181    /* ********************************************************************
182    Changes to this class. What the people have been up to:
183    $Log$
184    Revision 1.3  2006/08/24 06:43:54  poth
185    File header corrected
186    
187    Revision 1.2  2006/07/31 12:33:27  poth
188    help text corrected
189    
190    Revision 1.1  2006/05/19 13:19:20  poth
191    improved program for merging shapes - first load up
192    
193    
194    ********************************************************************** */