001    // $HeadURL: svn+ssh://jwilden@svn.wald.intevation.org/deegree/base/branches/2.5_testing/src/org/deegree/ogcbase/GMLDocument.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.ogcbase;
037    
038    import java.net.URI;
039    import java.net.URISyntaxException;
040    import java.util.Calendar;
041    
042    import org.deegree.datatypes.time.TimeIndeterminateValue;
043    import org.deegree.datatypes.time.TimePosition;
044    import org.deegree.framework.util.StringTools;
045    import org.deegree.framework.util.TimeTools;
046    import org.deegree.framework.xml.ElementList;
047    import org.deegree.framework.xml.XMLFragment;
048    import org.deegree.framework.xml.XMLParsingException;
049    import org.deegree.framework.xml.XMLTools;
050    import org.deegree.model.coverage.grid.Grid;
051    import org.deegree.model.crs.CRSFactory;
052    import org.deegree.model.crs.CoordinateSystem;
053    import org.deegree.model.crs.UnknownCRSException;
054    import org.deegree.model.spatialschema.Envelope;
055    import org.deegree.model.spatialschema.GeometryFactory;
056    import org.deegree.model.spatialschema.Point;
057    import org.deegree.model.spatialschema.Position;
058    import org.w3c.dom.Element;
059    
060    /**
061     * 
062     * 
063     * @version $Revision: 24395 $
064     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
065     * @author last edited by: $Author: apoth $
066     * 
067     * @version 1.0. $Revision: 24395 $, $Date: 2010-05-17 09:34:13 +0200 (Mo, 17 Mai 2010) $
068     * 
069     * @since 1.1
070     */
071    public class GMLDocument extends XMLFragment {
072    
073        private static final long serialVersionUID = -4974669697699282588L;
074    
075        private static URI GMLNS = CommonNamespaces.GMLNS;
076    
077        /**
078         * creates a <tt>Point</tt> from the passed <pos> element containing a GML pos.
079         * 
080         * @param element
081         * @return created <tt>Point</tt>
082         * @throws InvalidGMLException
083         */
084        public static Point parsePos( Element element )
085                                throws InvalidGMLException {
086            String tmp = XMLTools.getAttrValue( element, null, "dimension", null );
087            int dim = 0;
088            if ( tmp != null ) {
089                dim = Integer.parseInt( tmp );
090            }
091            tmp = XMLTools.getStringValue( element );
092            double[] vals = StringTools.toArrayDouble( tmp, ", " );
093            if ( dim != 0 ) {
094                if ( vals.length != dim ) {
095                    throw new InvalidGMLException( "dimension must be equal to the number of "
096                                                   + "coordinate values defined in pos element." );
097                }
098            } else {
099                dim = vals.length;
100            }
101    
102            Position pos = null;
103            if ( dim == 3 ) {
104                pos = GeometryFactory.createPosition( vals[0], vals[1], vals[2] );
105            } else {
106                pos = GeometryFactory.createPosition( vals[0], vals[1] );
107            }
108    
109            return GeometryFactory.createPoint( pos, null );
110        }
111    
112        /**
113         * creates a <tt>Envelope</tt> from the passed element. Because deegree geometry implementation doesn't use CRS for
114         * envelopes the srsName attribute of the passed element is ignored.
115         * 
116         * @param element
117         * @return created <tt>Envelope</tt>
118         * @throws InvalidGMLException
119         * @throws UnknownCRSException
120         */
121        public static Envelope parseEnvelope( Element element )
122                                throws InvalidGMLException, UnknownCRSException {
123    
124            String srs = XMLTools.getAttrValue( element, null, "srsName", null );
125            CoordinateSystem crs = null;
126            if ( srs != null ) {
127                crs = CRSFactory.create( srs );
128            }
129    
130            ElementList el = XMLTools.getChildElements( "pos", GMLNS, element );
131            if ( el == null || el.getLength() != 2 ) {
132                throw new InvalidGMLException( "A lonLatEnvelope must contain two gml:pos elements" );
133            }
134            Point min = parsePos( el.item( 0 ) );
135            Point max = parsePos( el.item( 1 ) );
136    
137            return GeometryFactory.createEnvelope( min.getPosition(), max.getPosition(), crs );
138        }
139    
140        /**
141         * creates a <tt>TimePosition</tt> object from the passed element.
142         * 
143         * @param element
144         * @return created <tt>TimePosition</tt>
145         * @throws XMLParsingException
146         * @throws InvalidGMLException
147         */
148        public static TimePosition parseTimePosition( Element element )
149                                throws XMLParsingException, InvalidGMLException {
150            try {
151                String calendarEraName = XMLTools.getAttrValue( element, null, "calendarEraName", null );
152                String s = XMLTools.getAttrValue( element, null, "frame", null );
153                URI frame = null;
154                if ( s != null ) {
155                    frame = new URI( s );
156                }
157                String indeterminatePosition = XMLTools.getAttrValue( element, null, "indeterminatePosition", null );
158                TimeIndeterminateValue tiv = new TimeIndeterminateValue( indeterminatePosition );
159                String tmp = XMLTools.getStringValue( element );
160                Calendar cal = null;
161    
162                if ( frame != null && !frame.toString().equals( "#ISO-8601" ) ) {
163                    throw new InvalidGMLException( "just #ISO-8601 is supported as frame for TimePosition." );
164                }
165    
166                cal = TimeTools.createCalendar( tmp );
167    
168                return new TimePosition( tiv, calendarEraName, frame, cal );
169            } catch ( URISyntaxException e ) {
170                throw new XMLParsingException( "couldn't parse timePosition frame\n" + StringTools.stackTraceToString( e ) );
171            }
172        }
173    
174        /**
175         * creates a <tt>Grid</tt> instance from the passed <tt>Element</tt>
176         * 
177         * @param element
178         * @return instance of <tt>Grid</tt>
179         * @throws InvalidGMLException
180         */
181        public static Grid parseGrid( Element element )
182                                throws InvalidGMLException {
183            Grid grid = null;
184            try {
185                String path = "gml:limits/gml:GridEnvelope/gml:low";
186                String lo = XMLTools.getRequiredNodeAsString( element, path, nsContext );
187                double[] low = StringTools.toArrayDouble( lo, " ,;" );
188                path = "gml:limits/gml:GridEnvelope/gml:high";
189                String hi = XMLTools.getRequiredNodeAsString( element, path, nsContext );
190                double[] high = StringTools.toArrayDouble( hi, " ,;" );
191                Position posLo = GeometryFactory.createPosition( low );
192                Position posHi = GeometryFactory.createPosition( high );
193                Envelope env = GeometryFactory.createEnvelope( posLo, posHi, null );
194                String[] axis = XMLTools.getNodesAsStrings( element, "axisName/text()", nsContext );
195                grid = new Grid( env, axis );
196            } catch ( Exception e ) {
197                throw new InvalidGMLException( e.getMessage() );
198            }
199            return grid;
200        }
201    
202    }