001    //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.3_testing/src/org/deegree/io/shpapi/shape_new/ShapeEnvelope.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.io.shpapi.shape_new;
037    
038    import org.deegree.model.spatialschema.ByteUtils;
039    import org.deegree.model.spatialschema.Envelope;
040    import org.deegree.model.spatialschema.Geometry;
041    
042    /**
043     * <code>ShapeEnvelope</code> encapsulates a shapefile envelope.
044     *
045     * @author <a href="mailto:schmitz@lat-lon.de">Andreas Schmitz</a>
046     * @author last edited by: $Author: mschneider $
047     *
048     * @version $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18. Jun 2009) $
049     */
050    public class ShapeEnvelope implements Shape {
051    
052        /**
053         * The minimum x value.
054         */
055        public double xmin;
056    
057        /**
058         * The maximum x value.
059         */
060        public double xmax;
061    
062        /**
063         * The minimum y value.
064         */
065        public double ymin;
066    
067        /**
068         * The maximum y value.
069         */
070        public double ymax;
071    
072        /**
073         * The minimum z value.
074         */
075        public double zmin;
076    
077        /**
078         * The maximum z value.
079         */
080        public double zmax;
081    
082        /**
083         * The minimum m value.
084         */
085        public double mmin;
086    
087        /**
088         * The maximum m value.
089         */
090        public double mmax;
091    
092        private boolean isZ, isM;
093    
094        /**
095         * Copy constructor. Better to do with clone()?
096         *
097         * @param s
098         */
099        public ShapeEnvelope( ShapeEnvelope s ) {
100            xmin = s.xmin;
101            xmax = s.xmax;
102            ymin = s.ymin;
103            ymax = s.ymax;
104            zmin = s.zmin;
105            zmax = s.zmax;
106            mmin = s.mmin;
107            mmax = s.mmax;
108            isZ = s.isZ;
109            isM = s.isM;
110        }
111    
112        /**
113         * Creates a new envelope, with/out z and m dimensions as specified.
114         *
115         * @param z
116         * @param m
117         */
118        public ShapeEnvelope( boolean z, boolean m ) {
119            isZ = z;
120            isM = m;
121        }
122    
123        /**
124         * Construct one from deegree Envelope.
125         *
126         * @param env
127         */
128        public ShapeEnvelope( Envelope env ) {
129            xmin = env.getMin().getX();
130            ymin = env.getMin().getY();
131            zmin = env.getMin().getZ();
132            xmax = env.getMax().getX();
133            ymax = env.getMax().getY();
134            zmax = env.getMax().getZ();
135            if ( env.getMin().getCoordinateDimension() == 3 ) {
136                isZ = true;
137            }
138        }
139    
140        /**
141         * Extends this envelope to z and m direction.
142         *
143         * @param z_min
144         * @param z_max
145         * @param m_min
146         * @param m_max
147         */
148        public void extend( double z_min, double z_max, double m_min, double m_max ) {
149            this.zmin = z_min;
150            this.zmax = z_max;
151            this.mmin = m_min;
152            this.mmax = m_max;
153    
154            isZ = true;
155        }
156    
157        /**
158         * Extends this envelope to m direction.
159         *
160         * @param m_min
161         * @param m_max
162         */
163        public void extend( double m_min, double m_max ) {
164            this.mmin = m_min;
165            this.mmax = m_max;
166    
167            isM = true;
168        }
169    
170        /**
171         * Extends the envelope so the given point fits in.
172         *
173         * @param x
174         * @param y
175         */
176        public void fit( double x, double y ) {
177            xmin = Math.min( x, xmin );
178            xmax = Math.max( x, xmax );
179            ymin = Math.min( y, ymin );
180            ymax = Math.max( y, ymax );
181        }
182    
183        /**
184         * Extends the envelope so the given point fits in.
185         *
186         * @param x
187         * @param y
188         * @param z
189         */
190        public void fit( double x, double y, double z ) {
191            fit( x, y );
192            zmin = Math.min( z, zmin );
193            zmax = Math.max( z, zmax );
194        }
195    
196        /**
197         * Extends the envelope so the given envelope fits in.
198         *
199         * @param s
200         */
201        public void fit( ShapeEnvelope s ) {
202            if ( s.isZ ) {
203                fit( s.xmin, s.ymin, s.zmin );
204                fit( s.xmax, s.ymax, s.zmax );
205            } else {
206                fit( s.xmin, s.ymin );
207                fit( s.xmax, s.ymax );
208            }
209        }
210    
211        /*
212         * (non-Javadoc)
213         *
214         * @see org.deegree.io.shpapi.Shape#getByteLength()
215         */
216        public int getByteLength() {
217            int len = 32;
218            if ( isZ ) {
219                len += 32;
220            }
221            if ( isM ) {
222                len += 16;
223            }
224            return len;
225        }
226    
227        /**
228         * Reads only x and y values.
229         *
230         * @see org.deegree.io.shpapi.shape_new.Shape#read(byte[], int)
231         */
232        public int read( byte[] bytes, int offset ) {
233            int off = offset;
234    
235            xmin = ByteUtils.readLEDouble( bytes, off );
236            off += 8;
237    
238            ymin = ByteUtils.readLEDouble( bytes, off );
239            off += 8;
240    
241            xmax = ByteUtils.readLEDouble( bytes, off );
242            off += 8;
243    
244            ymax = ByteUtils.readLEDouble( bytes, off );
245            off += 8;
246    
247            return off;
248        }
249    
250        /**
251         * Writes only x and y values.
252         *
253         * @see org.deegree.io.shpapi.shape_new.Shape#write(byte[], int)
254         */
255        public int write( byte[] bytes, int offset ) {
256            int off = offset;
257    
258            ByteUtils.writeLEDouble( bytes, off, xmin );
259            off += 8;
260    
261            ByteUtils.writeLEDouble( bytes, off, ymin );
262            off += 8;
263    
264            ByteUtils.writeLEDouble( bytes, off, xmax );
265            off += 8;
266    
267            ByteUtils.writeLEDouble( bytes, off, ymax );
268            off += 8;
269    
270            return off;
271        }
272    
273        /**
274         * @return zero, because the envelope does not have a type
275         * @see org.deegree.io.shpapi.shape_new.Shape#getType()
276         */
277        public int getType() {
278            return 0;
279        }
280    
281        /**
282         * @return itself, of course
283         * @see org.deegree.io.shpapi.shape_new.Shape#getEnvelope()
284         */
285        public ShapeEnvelope getEnvelope() {
286            return this;
287        }
288    
289        @Override
290        public String toString() {
291            StringBuffer sb = new StringBuffer( 200 );
292            sb.append( "x: " ).append( xmin ).append( "/" ).append( xmax );
293            sb.append( ", y: " ).append( ymin ).append( "/" ).append( ymax );
294            if ( isZ ) {
295                sb.append( ", z: " ).append( zmin ).append( "/" ).append( zmax );
296            }
297            if ( isM || isZ ) {
298                sb.append( ", m: " ).append( mmin ).append( "/" ).append( mmax );
299            }
300            return sb.toString();
301        }
302    
303        /**
304         * @return null, because an envelope is not a geometry
305         * @see org.deegree.io.shpapi.shape_new.Shape#getGeometry()
306         */
307        public Geometry getGeometry() {
308            return null;
309        }
310    
311    }