001    //$HeadURL: svn+ssh://developername@svn.wald.intevation.org/deegree/base/trunk/resources/eclipse/files_template.xml $
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.portal.standard.routing.control;
037    
038    import java.text.DecimalFormat;
039    import java.text.NumberFormat;
040    
041    import org.deegree.framework.util.StringTools;
042    import org.deegree.model.crs.CRSFactory;
043    import org.deegree.model.crs.CRSTransformationException;
044    import org.deegree.model.crs.CoordinateSystem;
045    import org.deegree.model.crs.GeoTransformer;
046    import org.deegree.model.spatialschema.GeometryException;
047    import org.deegree.model.spatialschema.GeometryFactory;
048    import org.deegree.model.spatialschema.Point;
049    
050    /**
051     * TODO add class documentation here
052     * 
053     * @author <a href="mailto:name@deegree.org">Andreas Poth</a>
054     * @author last edited by: $Author: admin $
055     * 
056     * @version $Revision: $, $Date: $
057     */
058    public class RouteBean {
059        
060        private static NumberFormat nf = new DecimalFormat( "#.##" );
061    
062        private String coordinates;
063    
064        private String description;
065    
066        private String distance;
067    
068        private double[] bbox = new double[] { 9E99, 9E99, -9E99, -9E99 };
069    
070        private String start;
071    
072        private String end;
073    
074        private int numberOfNodes;
075    
076        /**
077         * Initializes a response bean by assigning according values and transforming route coordinates into the coordinate
078         * reference system of the current map model
079         * 
080         * @param modelCrs
081         * @param coords
082         * @param description
083         * @param distance
084         * @param start
085         *            description/name of the start point
086         * @param end
087         *            description/name of the end point
088         * @throws IllegalArgumentException
089         * @throws CRSTransformationException
090         * @throws GeometryException
091         */
092        public RouteBean( CoordinateSystem modelCrs, String coords, String description, double distance, String start,
093                          String end ) throws IllegalArgumentException, CRSTransformationException, GeometryException {
094            // coordinates returned from YOURS routing service must be transformed into
095            // coordinate reference system of current map model
096            GeoTransformer gt = new GeoTransformer( modelCrs );
097            String[] tmp = StringTools.toArray( coords, " \n\t", false );
098            StringBuilder sb = new StringBuilder( 10000 );
099            sb.append( "LINESTRING(" );
100            for ( int i = 0; i < tmp.length; i++ ) {
101                double[] d = StringTools.toArrayDouble( tmp[i], "," );
102                Point p = GeometryFactory.createPoint( d[0], d[1], CRSFactory.EPSG_4326 );
103                p = (Point) gt.transform( p );
104                if ( p.getX() < bbox[0] ) {
105                    bbox[0] = p.getX();
106                }
107                if ( p.getX() > bbox[2] ) {
108                    bbox[2] = p.getX();
109                }
110                if ( p.getY() < bbox[1] ) {
111                    bbox[1] = p.getY();
112                }
113                if ( p.getY() > bbox[3] ) {
114                    bbox[3] = p.getY();
115                }
116                sb.append( p.getX() ).append( ' ' ).append( p.getY() );
117                if ( i < tmp.length - 1 ) {
118                    sb.append( ',' );
119                }
120            }
121            sb.append( ")" );
122            this.coordinates = sb.toString();
123            
124            this.numberOfNodes = tmp.length;
125            this.description = StringTools.replace( description, "<br>", "\n", true );
126            this.description = StringTools.replace( this.description, "</br>", "\n", true );
127            this.distance = nf.format( distance );
128            this.start = start;
129            this.end = end;
130        }
131    
132        /**
133         * @return the coordinates
134         */
135        public String getCoordinates() {
136            return coordinates;
137        }
138    
139        /**
140         * @return the description
141         */
142        public String getDescription() {
143            return description;
144        }
145    
146        /**
147         * @return the distance
148         */
149        public String getDistance() {
150            return distance;
151        }
152    
153        /**
154         * @return the bbox
155         */
156        public double[] getBbox() {
157            return bbox;
158        }
159    
160        /**
161         * @return the start
162         */
163        public String getStart() {
164            return start;
165        }
166    
167        /**
168         * @return the end
169         */
170        public String getEnd() {
171            return end;
172        }
173        /**
174         * @return the numberOfNodes
175         */
176        public int getNumberOfNodes() {
177            return numberOfNodes;
178        }
179    
180    }