001 // $HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.3_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: 18195 $ 064 * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a> 065 * @author last edited by: $Author: mschneider $ 066 * 067 * @version 1.0. $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18. Jun 2009) $ 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 114 * implementation doesn't use CRS for envelopes the srsName attribute of the passed element is 115 * ignored. 116 * 117 * @param element 118 * @return created <tt>Envelope</tt> 119 * @throws InvalidGMLException 120 * @throws UnknownCRSException 121 */ 122 public static Envelope parseEnvelope( Element element ) 123 throws InvalidGMLException, UnknownCRSException { 124 125 String srs = XMLTools.getAttrValue( element, null, "srsName", null ); 126 CoordinateSystem crs = null; 127 if ( srs != null ) { 128 crs = CRSFactory.create( srs ); 129 } 130 131 ElementList el = XMLTools.getChildElements( "pos", GMLNS, element ); 132 if ( el == null || el.getLength() != 2 ) { 133 throw new InvalidGMLException( "A lonLatEnvelope must contain two gml:pos elements" ); 134 } 135 Point min = parsePos( el.item( 0 ) ); 136 Point max = parsePos( el.item( 1 ) ); 137 138 return GeometryFactory.createEnvelope( min.getPosition(), max.getPosition(), crs ); 139 } 140 141 /** 142 * creates a <tt>TimePosition</tt> object from the passed element. 143 * 144 * @param element 145 * @return created <tt>TimePosition</tt> 146 * @throws XMLParsingException 147 * @throws InvalidGMLException 148 */ 149 public static TimePosition parseTimePosition( Element element ) 150 throws XMLParsingException, InvalidGMLException { 151 try { 152 String calendarEraName = XMLTools.getRequiredAttrValue( "calendarEraName", null, element ); 153 URI frame = new URI( XMLTools.getRequiredAttrValue( "frame", null, element ) ); 154 String indeterminatePosition = XMLTools.getRequiredAttrValue( "indeterminatePosition", null, element ); 155 TimeIndeterminateValue tiv = new TimeIndeterminateValue( indeterminatePosition ); 156 String tmp = XMLTools.getStringValue( element ); 157 Calendar cal = null; 158 159 if ( !frame.toString().equals( "#ISO-8601" ) ) { 160 throw new InvalidGMLException( "just #ISO-8601 is supported as " + "frame for TimePosition." ); 161 } 162 163 cal = TimeTools.createCalendar( tmp ); 164 165 return new TimePosition( tiv, calendarEraName, frame, cal ); 166 } catch ( URISyntaxException e ) { 167 throw new XMLParsingException( "couldn't parse timePosition frame\n" + StringTools.stackTraceToString( e ) ); 168 } 169 } 170 171 /** 172 * creates a <tt>Grid</tt> instance from the passed <tt>Element</tt> 173 * 174 * @param element 175 * @return instance of <tt>Grid</tt> 176 * @throws InvalidGMLException 177 */ 178 public static Grid parseGrid( Element element ) 179 throws InvalidGMLException { 180 Grid grid = null; 181 try { 182 String path = "gml:limits/gml:GridEnvelope/gml:low"; 183 String lo = XMLTools.getRequiredNodeAsString( element, path, nsContext ); 184 double[] low = StringTools.toArrayDouble( lo, " ,;" ); 185 path = "gml:limits/gml:GridEnvelope/gml:high"; 186 String hi = XMLTools.getRequiredNodeAsString( element, path, nsContext ); 187 double[] high = StringTools.toArrayDouble( hi, " ,;" ); 188 Position posLo = GeometryFactory.createPosition( low ); 189 Position posHi = GeometryFactory.createPosition( high ); 190 Envelope env = GeometryFactory.createEnvelope( posLo, posHi, null ); 191 String[] axis = XMLTools.getNodesAsStrings( element, "axisName/text()", nsContext ); 192 grid = new Grid( env, axis ); 193 } catch ( Exception e ) { 194 throw new InvalidGMLException( e.getMessage() ); 195 } 196 return grid; 197 } 198 199 }