    001    //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.4_testing/src/org/deegree/tools/raster/ESRICoverageCatalogue2deegree.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    package org.deegree.tools.raster;
    037
    038    import java.io.File;
    039    import java.io.FileWriter;
    040    import java.net.URI;
    041    import java.util.HashMap;
    042    import java.util.Map;
    043
    044    import javax.media.jai.JAI;
    045    import javax.media.jai.RenderedOp;
    046
    047    import org.deegree.datatypes.QualifiedName;
    048    import org.deegree.framework.xml.NamespaceContext;
    049    import org.deegree.framework.xml.XMLFragment;
    050    import org.deegree.framework.xml.XMLTools;
    051    import org.deegree.io.dbaseapi.DBaseFile;
    052    import org.deegree.io.ecwapi.ECWReader;
    053    import org.deegree.model.crs.CRSFactory;
    054    import org.deegree.model.crs.CoordinateSystem;
    055    import org.deegree.model.crs.GeoTransformer;
    056    import org.deegree.model.feature.Feature;
    057    import org.deegree.model.spatialschema.Envelope;
    058    import org.deegree.model.spatialschema.GeometryFactory;
    059    import org.deegree.ogcbase.CommonNamespaces;
    060    import org.w3c.dom.Element;
    061
    062    import com.sun.media.jai.codec.FileSeekableStream;
    063
    064    /**
    065     * TODO add class documentation here
    066     *
    067     * @author <a href="mailto:name@deegree.org">Andreas Poth</a>
    068     * @author last edited by: $Author: apoth $
    069     *
    070     * @version $Revision: 21401 $, $Date: 2009-12-11 12:02:25 +0100 (Fr, 11. Dez 2009) $
    071     */
    072    public class ESRICoverageCatalogue2deegree {
    073
    074        private static final String TEMPLATE = "esri_imagecatalogue2deegree_template.xml";
    075
    076        private static final NamespaceContext nsContext = CommonNamespaces.getNamespaceContext();
    077
    078        private String inFile, outFile, crs, pathToFiles;
    079
    080        /**
    081         *
    082         * @param inFile
    083         * @param crs
    084         * @param outFile
    085         * @param pathToFiles
    086         */
    087        public ESRICoverageCatalogue2deegree( String inFile, String crs, String outFile, String pathToFiles ) {
    088            if ( inFile.toLowerCase().endsWith( ".dbf" ) ) {
    089                // ensure that input file name has no extension
    090                inFile = inFile.substring( 0, inFile.length() - 4 );
    091            }
    092            this.inFile = inFile;
    093            this.crs = crs;
    094            this.outFile = outFile;
    095            this.pathToFiles = pathToFiles;
    096            if ( !pathToFiles.endsWith( "/" ) && !pathToFiles.endsWith( "\\" ) ) {
    097                this.pathToFiles += "/";
    098            }
    099        }
    100
    101        /**
    102         *
    103         * @throws Exception
    104         */
    105        public void execute()
    106                                throws Exception {
    107            XMLFragment xml = new XMLFragment( ESRICoverageCatalogue2deegree.class.getResource( TEMPLATE ) );
    108            Element parent = (Element) XMLTools.getRequiredNode( xml.getRootElement(), "./deegreewcs:Resolution", nsContext );
    109
    110            DBaseFile dbf = new DBaseFile( inFile );
    111            int count = dbf.getRecordNum();
    112            URI namespace = dbf.getFeatureType().getNameSpace();
    113            QualifiedName image = new QualifiedName( "IMAGE", namespace );
    114            QualifiedName xmin = new QualifiedName( "XMIN", namespace );
    115            QualifiedName ymin = new QualifiedName( "YMIN", namespace );
    116            QualifiedName xmax = new QualifiedName( "XMAX", namespace );
    117            QualifiedName ymax = new QualifiedName( "YMAX", namespace );
    118            double xmin_ = 9E99;
    119            double ymin_ = 9E99;
    120            double xmax_ = -9E99;
    121            double ymax_ = -9E99;
    122            CoordinateSystem srs = CRSFactory.create( crs );
    123            for ( int i = 0; i < count; i++ ) {
    124                Feature feature = dbf.getFRow( i + 1 );
    125                String fileName = (String) feature.getProperties( image )[0].getValue();
    126                File file = new File( fileName );
    127                if ( !file.isAbsolute() ) {
    128                    fileName = pathToFiles + fileName;
    129                }
    130                double minx = ( (Number) feature.getProperties( xmin )[0].getValue() ).doubleValue();
    131                if ( minx < xmin_ ) {
    132                    xmin_ = minx;
    133                }
    134                double miny = ( (Number) feature.getProperties( ymin )[0].getValue() ).doubleValue();
    135                if ( miny < ymin_ ) {
    136                    ymin_ = miny;
    137                }
    138                double maxx = ( (Number) feature.getProperties( xmax )[0].getValue() ).doubleValue();
    139                if ( maxx > xmax_ ) {
    140                    xmax_ = maxx;
    141                }
    142                double maxy = ( (Number) feature.getProperties( ymax )[0].getValue() ).doubleValue();
    143                if ( maxy > ymax_ ) {
    144                    ymax_ = maxy;
    145                }
    146                int width = 0;
    147                int height = 0;
    148                if ( fileName.toLowerCase().endsWith( ".ecw" ) ) {
    149                    ECWReader ecw = new ECWReader( fileName );
    150                    width = ecw.getWidth();
    151                    height = ecw.getHeight();
    152                } else {
    153                    FileSeekableStream fss = new FileSeekableStream( fileName );
    154                    RenderedOp rop = JAI.create( "stream", fss );
    155                    width = ( (Integer) rop.getProperty( "image_width" ) ).intValue();
    156                    height = ( (Integer) rop.getProperty( "image_height" ) ).intValue();
    157                    fss.close();
    158                }
    159                Element elFile = XMLTools.appendElement( parent, CommonNamespaces.DEEGREEWCS, "File" );
    160                elFile.setAttribute( "width", Integer.toString( width ) );
    161                elFile.setAttribute( "height", Integer.toString( height ) );
    162                Element elName = XMLTools.appendElement( elFile, CommonNamespaces.DEEGREEWCS, "Name" );
    163                XMLTools.setNodeValue( elName, fileName );
    164                Element elEnv = XMLTools.appendElement( elFile, CommonNamespaces.GMLNS, "Envelope" );
    165                elEnv.setAttribute( "srsName", "HTTP://WWW.OPENGIS.NET/GML/SRS/EPSG.XML#" + srs.getLocalName() );
    166                Element elCoord = XMLTools.appendElement( elEnv, CommonNamespaces.GMLNS, "coordinates" );
    167                XMLTools.setNodeValue( elCoord, minx + "," + miny + " " + maxx + "," + maxy );
    168            }
    169            System.out.println( "Envelope in " + crs );
    170            System.out.println( xmin_ + "," + ymin_ + "," + xmax_ + "," + ymax_ );
    171            System.out.println();
    172
    173            Envelope env = GeometryFactory.createEnvelope( xmin_, ymin_, xmax_, ymax_, srs );
    174            GeoTransformer gt = new GeoTransformer( "EPSG:4326" );
    175            env = gt.transform( env, srs );
    176
    177            System.out.println( "Envelope in EPSG:4326 (WGS84)" );
    178            System.out.println( env.getMin().getX() + "," + env.getMin().getY() + "," + env.getMax().getX() + ","
    179                                + env.getMax().getY() );
    180
    181            FileWriter fw = new FileWriter( outFile );
    182            fw.write( xml.getAsPrettyString() );
    183            fw.close();
    184        }
    185
    186        /**
    187         * @param args
    188         */
    189        public static void main( String[] args )
    190                                throws Exception {
    191            Map<String, String> param = new HashMap<String, String>();
    192            for ( int i = 0; i < args.length; i += 2 ) {
    193                param.put( args[i], args[i + 1] );
    194            }
    195
    196            String inFile = parameterCheck( param, "-inFile" );
    197            String crs = parameterCheck( param, "-crs" );
    198            String outFile = parameterCheck( param, "-outFile" );
    199            String pathToFiles = param.get( "-pathToFiles" );
    200            ESRICoverageCatalogue2deegree esri = new ESRICoverageCatalogue2deegree( inFile, crs, outFile, pathToFiles );
    201            esri.execute();
    202        }
    203
    204        private static String parameterCheck( Map<String, String> param, String name ) {
    205            String p = param.get( name );
    206            if ( p == null ) {
    207                System.out.println( "parameter " + name + " is missing" );
    208                System.exit( 1 );
    209            }
    210            return p;
    211        }
    212
    213    }