001: // $HeadURL: https://svn.wald.intevation.org/svn/deegree/base/trunk/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 08:21:56 -0800 (Thu, 27 Dec 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",
094: null);
095: int dim = 0;
096: if (tmp != null) {
097: dim = Integer.parseInt(tmp);
098: }
099: tmp = XMLTools.getStringValue(element);
100: double[] vals = StringTools.toArrayDouble(tmp, ", ");
101: if (dim != 0) {
102: if (vals.length != dim) {
103: throw new InvalidGMLException(
104: "dimension must be equal to the number of "
105: + "coordinate values defined in pos element.");
106: }
107: } else {
108: dim = vals.length;
109: }
110:
111: Position pos = null;
112: if (dim == 3) {
113: pos = GeometryFactory.createPosition(vals[0], vals[1],
114: vals[2]);
115: } else {
116: pos = GeometryFactory.createPosition(vals[0], vals[1]);
117: }
118:
119: return GeometryFactory.createPoint(pos, null);
120: }
121:
122: /**
123: * creates a <tt>Envelope</tt> from the passed element. Because deegree geometry
124: * implementation doesn't use CRS for envelopes the srsName attribute of the passed element is
125: * ignored.
126: *
127: * @param element
128: * @return created <tt>Envelope</tt>
129: * @throws XMLParsingException
130: * @throws InvalidGMLException
131: * @throws UnknownCRSException
132: */
133: public static Envelope parseEnvelope(Element element)
134: throws InvalidGMLException, UnknownCRSException {
135:
136: String srs = XMLTools.getAttrValue(element, null, "srsName",
137: null);
138: CoordinateSystem crs = null;
139: if (srs != null) {
140: crs = CRSFactory.create(srs);
141: }
142:
143: ElementList el = XMLTools.getChildElements("pos", GMLNS,
144: element);
145: if (el == null || el.getLength() != 2) {
146: throw new InvalidGMLException(
147: "A lonLatEnvelope must contain two gml:pos elements");
148: }
149: Point min = parsePos(el.item(0));
150: Point max = parsePos(el.item(1));
151:
152: return GeometryFactory.createEnvelope(min.getPosition(), max
153: .getPosition(), crs);
154: }
155:
156: /**
157: * creates a <tt>TimePosition</tt> object from the passed element.
158: *
159: * @param element
160: * @return created <tt>TimePosition</tt>
161: * @throws XMLParsingException
162: * @throws InvalidGMLException
163: */
164: public static TimePosition parseTimePosition(Element element)
165: throws XMLParsingException, InvalidGMLException {
166: try {
167: String calendarEraName = XMLTools.getRequiredAttrValue(
168: "calendarEraName", null, element);
169: URI frame = new URI(XMLTools.getRequiredAttrValue("frame",
170: null, element));
171: String indeterminatePosition = XMLTools
172: .getRequiredAttrValue("indeterminatePosition",
173: null, element);
174: TimeIndeterminateValue tiv = new TimeIndeterminateValue(
175: indeterminatePosition);
176: String tmp = XMLTools.getStringValue(element);
177: Calendar cal = null;
178:
179: if (!frame.toString().equals("#ISO-8601")) {
180: throw new InvalidGMLException(
181: "just #ISO-8601 is supported as "
182: + "frame for TimePosition.");
183: }
184:
185: cal = TimeTools.createCalendar(tmp);
186:
187: return new TimePosition(tiv, calendarEraName, frame, cal);
188: } catch (URISyntaxException e) {
189: throw new XMLParsingException(
190: "couldn't parse timePosition frame\n"
191: + StringTools.stackTraceToString(e));
192: }
193: }
194:
195: /**
196: * creates a <tt>Grid</tt> instance from the passed <tt>Element</tt>
197: *
198: * @param element
199: * @return instance of <tt>Grid</tt>
200: * @throws XMLParsingException
201: * @throws InvalidGMLException
202: */
203: public static Grid parseGrid(Element element)
204: throws InvalidGMLException {
205: Grid grid = null;
206: try {
207: String path = "gml:limits/gml:GridEnvelope/gml:low";
208: String lo = XMLTools.getRequiredNodeAsString(element, path,
209: nsContext);
210: double[] low = StringTools.toArrayDouble(lo, " ,;");
211: path = "gml:limits/gml:GridEnvelope/gml:high";
212: String hi = XMLTools.getRequiredNodeAsString(element, path,
213: nsContext);
214: double[] high = StringTools.toArrayDouble(hi, " ,;");
215: Position posLo = GeometryFactory.createPosition(low);
216: Position posHi = GeometryFactory.createPosition(high);
217: Envelope env = GeometryFactory.createEnvelope(posLo, posHi,
218: null);
219: String[] axis = XMLTools.getNodesAsStrings(element,
220: "axisName/text()", nsContext);
221: grid = new Grid(env, axis);
222: } catch (Exception e) {
223: throw new InvalidGMLException(e.getMessage());
224: }
225: return grid;
226: }
227:
228: }
|