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