001    //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/branches/2.2_testing/src/org/deegree/tools/raster/WorldFiles2GML.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.raster;
044    
045    import java.io.File;
046    import java.io.FileOutputStream;
047    import java.io.IOException;
048    import java.net.URI;
049    import java.net.URISyntaxException;
050    import java.util.List;
051    import java.util.Properties;
052    
053    import org.deegree.datatypes.QualifiedName;
054    import org.deegree.datatypes.Types;
055    import org.deegree.framework.util.ConvenienceFileFilter;
056    import org.deegree.framework.util.StringTools;
057    import org.deegree.model.coverage.grid.WorldFile;
058    import org.deegree.model.feature.FeatureCollection;
059    import org.deegree.model.feature.FeatureFactory;
060    import org.deegree.model.feature.FeatureProperty;
061    import org.deegree.model.feature.GMLFeatureAdapter;
062    import org.deegree.model.feature.schema.FeatureType;
063    import org.deegree.model.feature.schema.PropertyType;
064    import org.deegree.model.spatialschema.Geometry;
065    import org.deegree.model.spatialschema.GeometryException;
066    import org.deegree.model.spatialschema.GeometryFactory;
067    import org.deegree.tools.shape.GML2Shape_new;
068    
069    /**
070     * 
071     * 
072     * 
073     * @version $Revision: 9346 $
074     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
075     * @author last edited by: $Author: apoth $
076     * 
077     * @version 1.0. $Revision: 9346 $, $Date: 2007-12-27 17:39:07 +0100 (Do, 27 Dez 2007) $
078     * 
079     * @since 2.0
080     */
081    public class WorldFiles2GML {
082    
083        private File[] files;
084    
085        private WorldFile.TYPE wft;
086    
087        public WorldFiles2GML( File[] files, WorldFile.TYPE wft ) {
088            this.files = files;
089            this.wft = wft;
090        }
091    
092        private FeatureType createFeatureType()
093                                throws URISyntaxException {
094    
095            PropertyType[] ftps = new PropertyType[2];
096    
097            QualifiedName qn = new QualifiedName( "app", "name", new URI( "http://www.deegree.org/app" ) );
098            ftps[0] = FeatureFactory.createSimplePropertyType( qn, Types.VARCHAR, true );
099    
100            qn = new QualifiedName( "app", "geom", new URI( "http://www.deegree.org/app" ) );
101            ftps[1] = FeatureFactory.createSimplePropertyType( qn, Types.GEOMETRY, true );
102    
103            qn = new QualifiedName( "app", "WorldFiles", new URI( "http://www.deegree.org/app" ) );
104            return FeatureFactory.createFeatureType( qn, false, ftps );
105        }
106    
107        public FeatureCollection perform()
108                                throws IOException, GeometryException, URISyntaxException {
109            FeatureType ft = createFeatureType();
110            FeatureCollection fc = FeatureFactory.createFeatureCollection( "FC", 200 );
111            for ( int i = 0; i < files.length; i++ ) {
112                WorldFile wf = WorldFile.readWorldFile( files[i].getAbsolutePath(), wft );
113                Geometry geom = GeometryFactory.createSurface( wf.getEnvelope(), null );
114                FeatureProperty[] fps = new FeatureProperty[2];
115    
116                QualifiedName qn = new QualifiedName( "app", "name", new URI( "http://www.deegree.org/app" ) );
117                fps[0] = FeatureFactory.createFeatureProperty( qn, files[i].getAbsoluteFile() );
118                qn = new QualifiedName( "app", "geom", new URI( "http://www.deegree.org/app" ) );
119                fps[1] = FeatureFactory.createFeatureProperty( qn, geom );
120                fc.add( FeatureFactory.createFeature( "id" + i, ft, fps ) );
121            }
122            return fc;
123        }
124    
125        /**
126         * @param args
127         */
128        public static void main( String[] args )
129                                throws Exception {
130    
131            Properties props = new Properties();
132            for ( int i = 0; i < args.length; i += 2 ) {
133                props.put( args[i], args[i + 1] );
134            }
135    
136            WorldFile.TYPE wft = WorldFile.TYPE.CENTER;
137            if ( "outer".equals( props.get( "-type" ) ) ) {
138                wft = WorldFile.TYPE.OUTER;
139            }
140            System.out.println( wft );
141            List<String> ext = StringTools.toList( props.getProperty( "-ext" ), ",", true );
142            ConvenienceFileFilter ff = new ConvenienceFileFilter( ext, true );
143            File dir = new File( props.getProperty( "-rootDir" ) );
144            File[] files = dir.listFiles( ff );
145            WorldFiles2GML wf2g = new WorldFiles2GML( files, wft );
146            FeatureCollection fc = wf2g.perform();
147    
148            GMLFeatureAdapter ada = new GMLFeatureAdapter( true );
149            FileOutputStream fos = new FileOutputStream( dir.getAbsolutePath() + "/gml.xml" );
150            ada.export( fc, fos );
151            fos.close();
152    
153            GML2Shape_new rws = new GML2Shape_new( dir.getAbsolutePath() + "/gml.xml", dir.getAbsolutePath() + "/gml" );
154            rws.read();
155            rws.write();
156    
157            System.out.println( "finished" );
158    
159        }
160    
161    }