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