001    //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/branches/2.2_testing/src/org/deegree/io/shpapi/shape_new/ShapeEnvelope.java $
002    /*----------------    FILE HEADER  ------------------------------------------
003     This file is part of deegree.
004     Copyright (C) 2001-2008 by:
005     Department of Geography, University of Bonn
006     http://www.giub.uni-bonn.de/deegree/
007     lat/lon GmbH
008     http://www.lat-lon.de
009    
010     This library is free software; you can redistribute it and/or
011     modify it under the terms of the GNU Lesser General Public
012     License as published by the Free Software Foundation; either
013     version 2.1 of the License, or (at your option) any later version.
014    
015     This library is distributed in the hope that it will be useful,
016     but WITHOUT ANY WARRANTY; without even the implied warranty of
017     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
018     Lesser General Public License for more details.
019    
020     You should have received a copy of the GNU Lesser General Public
021     License along with this library; if not, write to the Free Software
022     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
023    
024     Contact:
025    
026     Andreas Poth
027     lat/lon GmbH
028     Aennchenstr. 19
029     53177 Bonn
030     Germany
031     E-Mail: poth@lat-lon.de
032    
033     Prof. Dr. Klaus Greve
034     Department of Geography
035     University of Bonn
036     Meckenheimer Allee 166
037     53115 Bonn
038     Germany
039     E-Mail: greve@giub.uni-bonn.de
040    
041     ---------------------------------------------------------------------------*/
042    package org.deegree.io.shpapi.shape_new;
043    
044    import org.deegree.model.spatialschema.ByteUtils;
045    import org.deegree.model.spatialschema.Envelope;
046    import org.deegree.model.spatialschema.Geometry;
047    
048    /**
049     * <code>ShapeEnvelope</code> encapsulates a shapefile envelope.
050     * 
051     * @author <a href="mailto:schmitz@lat-lon.de">Andreas Schmitz</a>
052     * @author last edited by: $Author: apoth $
053     * 
054     * @version $Revision: 9342 $, $Date: 2007-12-27 13:32:57 +0100 (Do, 27 Dez 2007) $
055     */
056    public class ShapeEnvelope implements Shape {
057    
058        /**
059         * The minimum x value.
060         */
061        public double xmin;
062    
063        /**
064         * The maximum x value.
065         */
066        public double xmax;
067    
068        /**
069         * The minimum y value.
070         */
071        public double ymin;
072    
073        /**
074         * The maximum y value.
075         */
076        public double ymax;
077    
078        /**
079         * The minimum z value.
080         */
081        public double zmin;
082    
083        /**
084         * The maximum z value.
085         */
086        public double zmax;
087    
088        /**
089         * The minimum m value.
090         */
091        public double mmin;
092    
093        /**
094         * The maximum m value.
095         */
096        public double mmax;
097    
098        private boolean isZ, isM;
099    
100        /**
101         * Copy constructor. Better to do with clone()?
102         * 
103         * @param s
104         */
105        public ShapeEnvelope( ShapeEnvelope s ) {
106            xmin = s.xmin;
107            xmax = s.xmax;
108            ymin = s.ymin;
109            ymax = s.ymax;
110            zmin = s.zmin;
111            zmax = s.zmax;
112            mmin = s.mmin;
113            mmax = s.mmax;
114            isZ = s.isZ;
115            isM = s.isM;
116        }
117    
118        /**
119         * Creates a new envelope, with/out z and m dimensions as specified.
120         * 
121         * @param z
122         * @param m
123         */
124        public ShapeEnvelope( boolean z, boolean m ) {
125            isZ = z;
126            isM = m;
127        }
128    
129        /**
130         * Construct one from deegree Envelope.
131         * 
132         * @param env
133         */
134        public ShapeEnvelope( Envelope env ) {
135            xmin = env.getMin().getX();
136            ymin = env.getMin().getY();
137            zmin = env.getMin().getZ();
138            xmax = env.getMax().getX();
139            ymax = env.getMax().getY();
140            zmax = env.getMax().getZ();
141            isZ = true;
142        }
143    
144        /**
145         * Extends this envelope to z and m direction.
146         * 
147         * @param z_min
148         * @param z_max
149         * @param m_min
150         * @param m_max
151         */
152        public void extend( double z_min, double z_max, double m_min, double m_max ) {
153            this.zmin = z_min;
154            this.zmax = z_max;
155            this.mmin = m_min;
156            this.mmax = m_max;
157    
158            isZ = true;
159        }
160    
161        /**
162         * Extends this envelope to m direction.
163         * 
164         * @param m_min
165         * @param m_max
166         */
167        public void extend( double m_min, double m_max ) {
168            this.mmin = m_min;
169            this.mmax = m_max;
170    
171            isM = true;
172        }
173    
174        /**
175         * Extends the envelope so the given point fits in.
176         * 
177         * @param x
178         * @param y
179         */
180        public void fit( double x, double y ) {
181            xmin = Math.min( x, xmin );
182            xmax = Math.max( x, xmax );
183            ymin = Math.min( y, ymin );
184            ymax = Math.max( y, ymax );
185        }
186    
187        /**
188         * Extends the envelope so the given point fits in.
189         * 
190         * @param x
191         * @param y
192         * @param z
193         */
194        public void fit( double x, double y, double z ) {
195            fit( x, y );
196            zmin = Math.min( z, zmin );
197            zmax = Math.max( z, zmax );
198        }
199    
200        /**
201         * Extends the envelope so the given envelope fits in.
202         * 
203         * @param s
204         */
205        public void fit( ShapeEnvelope s ) {
206            if ( s.isZ ) {
207                fit( s.xmin, s.ymin, s.zmin );
208                fit( s.xmax, s.ymax, s.zmax );
209            } else {
210                fit( s.xmin, s.ymin );
211                fit( s.xmax, s.ymax );
212            }
213        }
214    
215        /*
216         * (non-Javadoc)
217         * 
218         * @see org.deegree.io.shpapi.Shape#getByteLength()
219         */
220        public int getByteLength() {
221            int len = 32;
222            if ( isZ ) {
223                len += 32;
224            }
225            if ( isM ) {
226                len += 16;
227            }
228            return len;
229        }
230    
231        /**
232         * Reads only x and y values.
233         * 
234         * @see org.deegree.io.shpapi.shape_new.Shape#read(byte[], int)
235         */
236        public int read( byte[] bytes, int offset ) {
237            int off = offset;
238    
239            xmin = ByteUtils.readLEDouble( bytes, off );
240            off += 8;
241    
242            ymin = ByteUtils.readLEDouble( bytes, off );
243            off += 8;
244    
245            xmax = ByteUtils.readLEDouble( bytes, off );
246            off += 8;
247    
248            ymax = ByteUtils.readLEDouble( bytes, off );
249            off += 8;
250    
251            return off;
252        }
253    
254        /**
255         * Writes only x and y values.
256         * 
257         * @see org.deegree.io.shpapi.shape_new.Shape#write(byte[], int)
258         */
259        public int write( byte[] bytes, int offset ) {
260            int off = offset;
261    
262            ByteUtils.writeLEDouble( bytes, off, xmin );
263            off += 8;
264    
265            ByteUtils.writeLEDouble( bytes, off, ymin );
266            off += 8;
267    
268            ByteUtils.writeLEDouble( bytes, off, xmax );
269            off += 8;
270    
271            ByteUtils.writeLEDouble( bytes, off, ymax );
272            off += 8;
273    
274            return off;
275        }
276    
277        /**
278         * @return zero, because the envelope does not have a type
279         * @see org.deegree.io.shpapi.shape_new.Shape#getType()
280         */
281        public int getType() {
282            return 0;
283        }
284    
285        /**
286         * @return itself, of course
287         * @see org.deegree.io.shpapi.shape_new.Shape#getEnvelope()
288         */
289        public ShapeEnvelope getEnvelope() {
290            return this;
291        }
292    
293        @Override
294        public String toString() {
295            StringBuffer sb = new StringBuffer( 200 );
296            sb.append( "x: " ).append( xmin ).append( "/" ).append( xmax );
297            sb.append( ", y: " ).append( ymin ).append( "/" ).append( ymax );
298            if ( isZ ) {
299                sb.append( ", z: " ).append( zmin ).append( "/" ).append( zmax );
300            }
301            if ( isM || isZ ) {
302                sb.append( ", m: " ).append( mmin ).append( "/" ).append( mmax );
303            }
304            return sb.toString();
305        }
306    
307        /**
308         * @return null, because an envelope is not a geometry
309         * @see org.deegree.io.shpapi.shape_new.Shape#getGeometry()
310         */
311        public Geometry getGeometry() {
312            return null;
313        }
314    
315    }