001    // $HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/branches/2.2_testing/src/org/deegree/framework/xml/MinMaxExtractor.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     Aennchenstraße 19
030     53177 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    package org.deegree.framework.xml;
044    
045    import org.deegree.model.spatialschema.Envelope;
046    import org.deegree.model.spatialschema.GMLGeometryAdapter;
047    import org.deegree.model.spatialschema.Geometry;
048    import org.deegree.model.spatialschema.GeometryException;
049    import org.deegree.model.spatialschema.Point;
050    import org.deegree.model.spatialschema.Position;
051    import org.w3c.dom.Element;
052    import org.w3c.dom.Node;
053    
054    /**
055     * Provides methods to XSLT-Sheets that determine the min/max coordinates of a gml geometry.
056     * <p>
057     * The submitted node parameter must be set to the root node of the geometry.
058     * 
059     * @version $Revision: 9339 $
060     * @author <a href="mailto:schneider@lat-lon.de">Markus Schneider</a>
061     * @author last edited by: $Author: apoth $
062     * 
063     * @version 1.0. $Revision: 9339 $, $Date: 2007-12-27 13:31:52 +0100 (Do, 27 Dez 2007) $
064     * 
065     * @since 2.0
066     */
067    public class MinMaxExtractor {
068    
069        /**
070         * 
071         * @param node
072         * @return maximum x coordinate
073         * @throws GeometryException
074         */
075        public static String getXMax( Node node )
076                                throws GeometryException {
077            if ( node == null ) {
078                return "";
079            }
080            Geometry geometry = GMLGeometryAdapter.wrap( (Element) node, null );
081            Envelope envelope = geometry.getEnvelope();
082            return Double.toString( envelope.getMax().getX() );
083        }
084    
085        /**
086         * 
087         * @param node
088         * @return minimum x coordinate
089         * @throws GeometryException
090         */
091        public static String getXMin( Node node )
092                                throws GeometryException {
093            if ( node == null ) {
094                return "";
095            }
096            Geometry geometry = GMLGeometryAdapter.wrap( (Element) node, null );
097            Envelope envelope = geometry.getEnvelope();
098            return Double.toString( envelope.getMin().getX() );
099        }
100    
101        /**
102         * 
103         * @param node
104         * @return maximum y coordinate
105         * @throws GeometryException
106         */
107        public static String getYMax( Node node )
108                                throws GeometryException {
109            if ( node == null ) {
110                return "";
111            }
112            Geometry geometry = GMLGeometryAdapter.wrap( (Element) node, null );
113            Envelope envelope = geometry.getEnvelope();
114            return Double.toString( envelope.getMax().getY() );
115        }
116    
117        /**
118         * 
119         * @param node
120         * @return minimum y coordinate
121         * @throws GeometryException
122         */
123        public static String getYMin( Node node )
124                                throws GeometryException {
125            if ( node == null ) {
126                return "";
127            }
128            Geometry geometry = GMLGeometryAdapter.wrap( (Element) node, null );
129            Envelope envelope = geometry.getEnvelope();
130            return Double.toString( envelope.getMin().getY() );
131        }
132    
133        /**
134         * 
135         * @param node
136         * @return maximum z coordinate
137         * @throws GeometryException
138         */
139        public static String getZMin( Node node )
140                                throws GeometryException {
141            if ( node == null ) {
142                return "";
143            }
144            Geometry geometry = GMLGeometryAdapter.wrap( (Element) node, null );
145            Envelope envelope = geometry.getEnvelope();
146            if ( geometry.getCoordinateDimension() > 2 ) {
147                return "";
148            }
149            return Double.toString( envelope.getMin().getZ() );
150        }
151    
152        /**
153         * 
154         * @param node
155         * @return minimum z coordinate
156         * @throws GeometryException
157         */
158        public static String getZMax( Node node )
159                                throws GeometryException {
160            if ( node == null ) {
161                return "";
162            }
163            Geometry geometry = GMLGeometryAdapter.wrap( (Element) node, null );
164            Envelope envelope = geometry.getEnvelope();
165            if ( geometry.getCoordinateDimension() > 2 ) {
166                return "";
167            }
168            return Double.toString( envelope.getMax().getZ() );
169    
170        }
171    
172        /**
173         * Extracts the x value of a gml:Point element described by <code>pointNode</code>
174         * 
175         * @param pointNode
176         *            the point node from which the x value will be extracted. For example, node is
177         *            &lt;gml:Point srsName="EPSG:31466"&gt; &lt;gml:coordinates cs="," decimal="." ts="
178         *            "&gt;0.0,0.0&lt;/gml:coordinates&gt; &lt;/gml:Point&gt;
179         * @return the String representation of the x value
180         * @throws GeometryException
181         */
182        public static String getPointX( Node pointNode )
183                                throws GeometryException {
184            return getPointXorY( pointNode, 0 );
185        }
186    
187        /**
188         * Extracts the y value of a gml:Point element described by <code>pointNode</code>
189         * 
190         * @param pointNode
191         *            the point node from which the y value will be extracted. For example, node is
192         *            &lt;gml:Point srsName="EPSG:31466"&gt; &lt;gml:coordinates cs="," decimal="." ts="
193         *            "&gt;0.0,0.0&lt;/gml:coordinates&gt; &lt;/gml:Point&gt;
194         * @return the String representation of the y value
195         * @throws GeometryException
196         */
197        public static String getPointY( Node pointNode )
198                                throws GeometryException {
199            return getPointXorY( pointNode, 1 );
200        }
201    
202        /**
203         * 
204         * @param pointNode
205         *            the point node from which the x or y value will be extracted. For example, node is
206         *            &lt;gml:Point srsName="EPSG:31466"&gt; &lt;gml:coordinates cs="," decimal="." ts="
207         *            "&gt;0.0,0.0&lt;/gml:coordinates&gt; &lt;/gml:Point&gt;
208         * @param coordIndex
209         *            the coordenate index indicated whether to extract from x (index = 0) otherwise
210         *            from y
211         * @return the String representation of the x or y value
212         * @throws GeometryException
213         */
214        private static String getPointXorY( Node pointNode, int coordIndex )
215                                throws GeometryException {
216            String value = "";
217    
218            if ( pointNode != null ) {
219    
220                Geometry geometry = GMLGeometryAdapter.wrap( (Element) pointNode, null );
221                if ( geometry instanceof Point ) {
222                    Point p = (Point) geometry;
223                    double d = coordIndex == 0 ? p.getX() : p.getY();
224                    value = Double.toString( d );
225                }
226            }
227    
228            return value;
229    
230        }
231    
232        /**
233         * returns the minimum coordinate of the envelope of the geometry encoded by the passed node as
234         * a double array
235         * 
236         * @param node
237         * @return the minimum coordinate of the envelope of the geometry encoded by the passed node as
238         *         a double array
239         * @throws GeometryException
240         */
241        public static String getMinAsArray( Node node )
242                                throws GeometryException {
243            if ( node == null ) {
244                return "";
245            }
246            Geometry geometry = GMLGeometryAdapter.wrap( (Element) node, null );
247            if ( geometry instanceof Point ) {
248                return "";
249            }
250            Envelope env = geometry.getEnvelope();
251            StringBuffer sb = new StringBuffer( 100 );
252    
253            Position pos = env.getMin();
254            int dim = pos.getCoordinateDimension();
255            double[] d = pos.getAsArray();
256            for ( int i = 0; i < dim - 1; i++ ) {
257                sb.append( Double.toString( d[i] ) ).append( ' ' );
258            }
259            sb.append( Double.toString( d[dim - 1] ) );
260    
261            return sb.toString();
262        }
263    
264        /**
265         * returns the minimum coordinate of the envelope of the geometry encoded by the passed node as
266         * a double array
267         * 
268         * @param node
269         * @return the minimum coordinate of the envelope of the geometry encoded by the passed node as
270         *         a double array
271         * @throws GeometryException
272         */
273        public static String getMaxAsArray( Node node )
274                                throws GeometryException {
275            if ( node == null ) {
276                return "";
277            }
278            Geometry geometry = GMLGeometryAdapter.wrap( (Element) node, null );
279            if ( geometry instanceof Point ) {
280                return "";
281            }
282            Envelope env = geometry.getEnvelope();
283            StringBuffer sb = new StringBuffer( 100 );
284    
285            Position pos = env.getMax();
286            int dim = pos.getCoordinateDimension();
287            double[] d = pos.getAsArray();
288            for ( int i = 0; i < dim - 1; i++ ) {
289                sb.append( Double.toString( d[i] ) ).append( ' ' );
290            }
291            sb.append( Double.toString( d[dim - 1] ) );
292    
293            return sb.toString();
294        }
295    
296        /**
297         * returns the the name of the SRS of the geometry encoded by the passed node as a double array
298         * 
299         * @param node
300         * @return the the name of the SRS of the geometry encoded by the passed node as a double array
301         * @throws GeometryException
302         */
303        public static String getSRSName( Node node ) {
304            if ( node == null ) {
305                return "";
306            }
307            return ( (Element) node ).getAttribute( "srsName" );
308        }
309    
310    }