001    //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.3_testing/src/org/deegree/model/spatialschema/PolygonImpl.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.framework.log.ILogger;
041    import org.deegree.framework.log.LoggerFactory;
042    import org.deegree.model.crs.CoordinateSystem;
043    
044    /**
045     *
046     *
047     *
048     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
049     * @author last edited by: $Author: mschneider $
050     *
051     * @version $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18. Jun 2009) $
052     */
053    public class PolygonImpl extends SurfacePatchImpl implements Polygon, Serializable {
054        /** Use serialVersionUID for interoperability. */
055        private final static long serialVersionUID = -1293845886457211088L;
056    
057        private static final ILogger LOG = LoggerFactory.getLogger( PolygonImpl.class );
058    
059        private SurfaceBoundary boundary = null;
060    
061        /**
062         *
063         * @param exteriorRing
064         * @param interiorRings
065         * @param crs
066         */
067        protected PolygonImpl( Ring exteriorRing, Ring[] interiorRings, CoordinateSystem crs ) {
068            super( exteriorRing, interiorRings, crs );
069        }
070    
071        /**
072         * Creates a new PolygonImpl object.
073         *
074         * @param interpolation
075         * @param exteriorRing
076         * @param interiorRings
077         * @param crs
078         *
079         * @throws GeometryException
080         */
081        protected PolygonImpl( SurfaceInterpolation interpolation, Position[] exteriorRing, Position[][] interiorRings,
082                               CoordinateSystem crs ) throws GeometryException {
083            super( interpolation, exteriorRing, interiorRings, crs );
084            // TODO
085            // implementation based on segments
086    
087            Ring outer = new RingImpl( exteriorRing, crs );
088            Ring[] inner = null;
089    
090            if ( interiorRings != null ) {
091                inner = new Ring[interiorRings.length];
092    
093                for ( int i = 0; i < inner.length; i++ ) {
094                    inner[i] = new RingImpl( interiorRings[i], crs );
095                }
096            }
097    
098            boundary = new SurfaceBoundaryImpl( outer, inner );
099        }
100    
101        /**
102         * The operation "boundary" shall return the boundary of this SurfacePatch represented as a collection of Curves
103         * organized as a SurfaceBoundary, consisting of Curve instances along the boundary of the aggregate Surface, and
104         * interior to the Surface where SurfacePatches are adjacent.
105         *
106         * @return the boundary of this SurfacePatch
107         *
108         */
109        public SurfaceBoundary getBoundary() {
110            return boundary;
111        }
112    
113        @Override
114        public boolean equals( Object other ) {
115            if ( !super.equals( other ) || !( other instanceof PolygonImpl ) ) {
116                return false;
117            }
118    
119            return true;
120        }
121    
122        @Override
123        public String toString() {
124            String ret = "SurfacePatch: ";
125            ret = "interpolation = " + interpolation + "\n";
126            ret += "exteriorRing = \n";
127            ret += ( exteriorRing + "\n" );
128            ret += ( "interiorRings = " + interiorRings + "\n" );
129            ret += ( "envelope = " + getEnvelope() + "\n" );
130            return ret;
131        }
132    
133        @Override
134        public Object clone() {
135            Polygon p = null;
136    
137            try {
138                p = new PolygonImpl( new SurfaceInterpolationImpl( getInterpolation().getValue() ), getExteriorRing(),
139                                     getInteriorRings(), this.crs );
140            } catch ( Exception e ) {
141                LOG.logError( e.getMessage(), e );
142            }
143    
144            return p;
145        }
146    
147        /**
148         * The Boolean valued operation "intersects" shall return TRUE if this Geometry intersects another Geometry. Within
149         * a Complex, the Primitives do not intersect one another. In general, topologically structured data uses shared
150         * geometric objects to capture intersection information.
151         */
152        public boolean intersects( Geometry gmo ) {
153            boolean inter = false;
154    
155            try {
156                if ( gmo instanceof Point ) {
157                    double tolerance = ( (Point) gmo ).getTolerance();
158                    inter = LinearIntersects.intersects( ( (Point) gmo ).getPosition(), this, tolerance );
159                } else if ( gmo instanceof Curve ) {
160                    inter = LinearIntersects.intersects( (Curve) gmo, new SurfaceImpl( this ) );
161                } else if ( gmo instanceof Surface ) {
162                    inter = LinearIntersects.intersects( (Surface) gmo, new SurfaceImpl( this ) );
163                } else if ( gmo instanceof Aggregate ) {
164                    inter = intersectsMultiObject( (Aggregate) gmo );
165                }
166            } catch ( Exception e ) {
167                LOG.logError( e.getMessage(), e );
168            }
169    
170            return inter;
171        }
172    
173        /**
174         * the operations returns true if the submitted multi primitive intersects with the curve segment
175         */
176        private boolean intersectsMultiObject( Aggregate mprim )
177                                throws Exception {
178            boolean inter = false;
179    
180            int cnt = mprim.getSize();
181    
182            for ( int i = 0; i < cnt; i++ ) {
183                if ( intersects( mprim.getObjectAt( i ) ) ) {
184                    inter = true;
185                    break;
186                }
187            }
188    
189            return inter;
190        }
191    
192        /**
193         * The Boolean valued operation "contains" shall return TRUE if this Geometry contains another Geometry.
194         * <p>
195         * </p>
196         */
197        public boolean contains( Geometry gmo ) {
198            boolean contain = false;
199    
200            try {
201                if ( gmo instanceof Point ) {
202                    contain = LinearContains.contains( this, ( (Point) gmo ).getPosition(), gmo.getTolerance() );
203                } else if ( gmo instanceof Curve ) {
204                    contain = LinearContains.contains( this, ( (Curve) gmo ).getAsLineString(), gmo.getTolerance() );
205                } else if ( gmo instanceof Surface ) {
206                    contain = LinearContains.contains( new SurfaceImpl( this ), (Surface) gmo );
207                } else if ( gmo instanceof Aggregate ) {
208                    contain = containsMultiObject( (Aggregate) gmo );
209                }
210            } catch ( Exception e ) {
211                LOG.logError( e.getMessage(), e );
212            }
213    
214            return contain;
215        }
216    
217        /**
218         *
219         *
220         * @param gmo
221         *
222         * @return true if the polygon contains the given aggregate.
223         */
224        private boolean containsMultiObject( Aggregate gmo ) {
225            try {
226                for ( int i = 0; i < gmo.getSize(); i++ ) {
227                    if ( !contains( gmo.getObjectAt( i ) ) ) {
228                        return false;
229                    }
230                }
231            } catch ( Exception e ) {
232                LOG.logError( e.getMessage(), e );
233            }
234    
235            return true;
236        }
237    }