001    //$HeadURL: svn+ssh://jwilden@svn.wald.intevation.org/deegree/base/branches/2.5_testing/src/org/deegree/model/spatialschema/PointImpl.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 java.io.Serializable;
039    
040    import org.deegree.model.crs.CoordinateSystem;
041    
042    /**
043     * default implementation of the Point interface.
044     *
045     * <p>
046     * ------------------------------------------------------------
047     * </p>
048     *
049     * @version 5.6.2001
050     * @author Andreas Poth
051     *         <p>
052     */
053    
054    public class PointImpl extends PrimitiveImpl implements Point, Serializable {
055        /** Use serialVersionUID for interoperability. */
056        private final static long serialVersionUID = 6106017748940535740L;
057    
058        private Position position = null;
059    
060        /**
061         * constructor. initializes a point to the coordinate 0/0
062         *
063         * @param crs
064         *            spatial reference system of the point
065         */
066        protected PointImpl( CoordinateSystem crs ) {
067            super( crs );
068            position = new PositionImpl();
069            empty = true;
070            centroid = this;
071        }
072    
073        /**
074         * constructor for initializing a point within a two-dimensional coordinate system
075         *
076         * @param x
077         *            x-value of the point
078         * @param y
079         *            y-value of the point
080         * @param crs
081         *            spatial reference system of the point
082         */
083        protected PointImpl( double x, double y, CoordinateSystem crs ) {
084            super( crs );
085            position = new PositionImpl( x, y );
086            empty = false;
087            centroid = this;
088        }
089    
090        /**
091         * constructor for initializing a point within a three-dimensional coordinate system
092         *
093         * @param x
094         *            x-value of the point
095         * @param y
096         *            y-value of the point
097         * @param z
098         *            z-value of the point
099         * @param crs
100         *            spatial reference system of the point
101         */
102        protected PointImpl( double x, double y, double z, CoordinateSystem crs ) {
103            super( crs );
104            position = new PositionImpl( x, y, z );
105            empty = false;
106            centroid = this;
107        }
108    
109        /**
110         * constructor
111         *
112         * @param otherPoint
113         *            existing GM_Point
114         */
115        protected PointImpl( Point otherPoint ) {
116            super( otherPoint.getCoordinateSystem() );
117            position = new PositionImpl( otherPoint.getAsArray() );
118            empty = false;
119            centroid = this;
120        }
121    
122        /**
123         * constructor
124         *
125         * @param position
126         *            existing position
127         * @param crs
128         *            spatial reference system of the point
129         */
130        protected PointImpl( Position position, CoordinateSystem crs ) {
131            super( crs );
132            this.position = position;
133            empty = false;
134            centroid = this;
135        }
136    
137        /**
138         * checks if this point is completely equal to the submitted geometry
139         */
140        @Override
141        public boolean equals( Object other ) {
142            if ( super.equals( other ) && ( other instanceof Point ) ) {
143                Point p = (Point) other;
144                boolean flagEq = Math.abs( getX() - p.getX() ) < mute && Math.abs( getY() - p.getY() ) < mute;
145                if ( getCoordinateDimension() == 3 ) {
146                    flagEq = flagEq && Math.abs( getZ() - p.getZ() ) < mute;
147                }
148                return flagEq;
149            }
150    
151            return false;
152        }
153    
154        /**
155         * @return 0
156         */
157        public int getDimension() {
158            return 0;
159        }
160    
161        /**
162         * @return the number of ordinates contained in this point.
163         */
164        public int getCoordinateDimension() {
165            return getPosition().getCoordinateDimension();
166        }
167    
168        @Override
169        public Object clone() {
170            return new PointImpl( position.getX(), position.getY(), position.getZ(), getCoordinateSystem() );
171        }
172    
173        public double getX() {
174            return position.getX();
175        }
176    
177        public double getY() {
178            return position.getY();
179        }
180    
181        public double getZ() {
182            return position.getZ();
183        }
184    
185        /**
186         * @return the x- and y-value of the point as a two dimensional array the first field contains the x- the second
187         *         field the y-value.
188         */
189        public double[] getAsArray() {
190            return position.getAsArray();
191        }
192    
193        /**
194         * translate the point by the submitted values. the <code>dz</code>- value will be ignored.
195         */
196        @Override
197        public void translate( double[] d ) {
198            setValid( false );
199            position.translate( d );
200        }
201    
202        /**
203         * @return position
204         *
205         */
206        public Position getPosition() {
207            return position;
208        }
209    
210        /**
211         * The Boolean valued operation "intersects" shall return TRUE if this Geometry intersects another Geometry. Within
212         * a Complex, the Primitives do not intersect one another. In general, topologically structured data uses shared
213         * geometric objects to capture intersection information.
214         * <p>
215         * </p>
216         * dummy implementation
217         */
218        @Override
219        public boolean intersects( Geometry gmo ) {
220            boolean inter = false;
221    
222            try {
223                if ( gmo instanceof Point ) {
224                    inter = LinearIntersects.intersects( (Point) gmo, this );
225                } else if ( gmo instanceof Curve ) {
226                    inter = LinearIntersects.intersects( this, (Curve) gmo );
227                } else if ( gmo instanceof Surface ) {
228                    inter = LinearIntersects.intersects( this, (Surface) gmo );
229                } else if ( gmo instanceof Aggregate ) {
230                    inter = intersectsAggregate( (Aggregate) gmo );
231                }
232            } catch ( Exception e ) {
233                // ignore
234            }
235    
236            return inter;
237        }
238    
239        /**
240         * the operations returns true if the submitted multi primitive intersects with the curve segment
241         */
242        private boolean intersectsAggregate( Aggregate mprim )
243                                throws Exception {
244            boolean inter = false;
245    
246            int cnt = mprim.getSize();
247    
248            for ( int i = 0; i < cnt; i++ ) {
249                if ( intersects( mprim.getObjectAt( i ) ) ) {
250                    inter = true;
251                    break;
252                }
253            }
254    
255            return inter;
256        }
257    
258        /**
259         * The Boolean valued operation "contains" shall return TRUE if this Geometry contains another Geometry.
260         * <p>
261         * </p>
262         */
263        @Override
264        public boolean contains( Geometry gmo ) {
265            throw new UnsupportedOperationException( "the contains operation for points "
266                                                     + "isn't supported at the moment." );
267        }
268    
269        /**
270         * recalculates internal parameters
271         */
272        @Override
273        protected void calculateParam() {
274            setValid( true );
275        }
276    
277        @Override
278        public String toString() {
279            StringBuffer ret = new StringBuffer( 30 );
280            ret.append( this.getClass().getName() ).append( ": " );
281    
282            for ( int i = 0; i < getAsArray().length; i++ ) {
283                ret.append( getAsArray()[i] ).append( ' ' );
284            }
285    
286            return ret.toString();
287        }
288    
289    }