001    //$HeadURL: svn+ssh://jwilden@svn.wald.intevation.org/deegree/base/branches/2.5_testing/src/org/deegree/model/spatialschema/LineStringImpl.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.model.spatialschema;
037    
038    import org.deegree.framework.log.ILogger;
039    import org.deegree.framework.log.LoggerFactory;
040    import org.deegree.framework.util.GeometryUtils;
041    import org.deegree.model.crs.CoordinateSystem;
042    
043    /**
044     * default implementation of the LineString interface of package deegree.model.spatialschema.
045     *
046     *
047     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
048     * @author last edited by: $Author: mschneider $
049     *
050     * @version $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18 Jun 2009) $
051     */
052    public class LineStringImpl extends CurveSegmentImpl implements LineString {
053        /** Use serialVersionUID for interoperability. */
054        private final static long serialVersionUID = 8093549521711824076L;
055    
056        private static final ILogger LOG = LoggerFactory.getLogger( LineStringImpl.class );
057    
058        private double length = -1;
059    
060        /**
061         * Creates a new LineStringImpl object.
062         *
063         * @param gmps
064         * @param cs
065         *
066         * @throws GeometryException
067         */
068        protected LineStringImpl( Position[] gmps, CoordinateSystem cs ) throws GeometryException {
069            super( gmps, cs );
070        }
071    
072        @Override
073        public Object clone() {
074            CurveSegment cs = null;
075    
076            Position[] tmp = new Position[points.length];
077            for ( int i = 0; i < tmp.length; i++ ) {
078                tmp[i] = new PositionImpl( points[i].getX(), points[i].getY(), points[i].getZ() );
079            }
080    
081            try {
082                cs = new LineStringImpl( tmp, getCoordinateSystem() );
083            } catch ( Exception ex ) {
084                LOG.logError( "LineString_Impl.clone: ", ex );
085            }
086    
087            return cs;
088        }
089    
090        private void calcLength() {
091            double d = 0;
092            for ( int i = 0; i < points.length - 1; i++ ) {
093                d += GeometryUtils.distance( points[i], points[i + 1] );
094            }
095            length = d;
096        }
097    
098        /**
099         * @return length of the curve in units of the related spatial reference system
100         */
101        public double getLength() {
102            if ( length < 0 ) {
103                calcLength();
104            }
105            return length;
106        }
107    
108        /**
109         * returns a reference to itself
110         */
111        public LineString getAsLineString()
112                                throws GeometryException {
113            return this;
114        }
115    
116        /**
117         * The Boolean valued operation "intersects" shall return TRUE if this Geometry intersects another Geometry. Within
118         * a Complex, the Primitives do not intersect one another. In general, topologically structured data uses shared
119         * geometric objects to capture intersection information.
120         */
121        public boolean intersects( Geometry gmo ) {
122            boolean inter = false;
123    
124            try {
125                if ( gmo instanceof Point ) {
126                    double tolerance = ( (Point) gmo ).getTolerance();
127                    inter = LinearIntersects.intersects( ( (Point) gmo ).getPosition(), this, tolerance );
128                } else if ( gmo instanceof Curve ) {
129                    CurveSegment[] cs = new CurveSegment[] { this };
130                    inter = LinearIntersects.intersects( (Curve) gmo, new CurveImpl( cs ) );
131                } else if ( gmo instanceof Surface ) {
132                    CurveSegment[] cs = new CurveSegment[] { this };
133                    inter = LinearIntersects.intersects( new CurveImpl( cs ), (Surface) gmo );
134                } else if ( gmo instanceof MultiPrimitive ) {
135                    inter = intersectsMultiPrimitive( (MultiPrimitive) gmo );
136                }
137            } catch ( Exception e ) {
138                LOG.logError( "", e );
139            }
140    
141            return inter;
142        }
143    
144        /**
145         * the operations returns true if the submitted multi primitive intersects with the curve segment
146         */
147        private boolean intersectsMultiPrimitive( MultiPrimitive mprim )
148                                throws Exception {
149            boolean inter = false;
150    
151            int cnt = mprim.getSize();
152    
153            for ( int i = 0; i < cnt; i++ ) {
154                if ( intersects( mprim.getPrimitiveAt( i ) ) ) {
155                    inter = true;
156                    break;
157                }
158            }
159    
160            return inter;
161        }
162    
163        @Override
164        public boolean contains( Geometry gmo ) {
165            return false;
166        }
167    }