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