001: //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/trunk/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 04:32:57 -0800 (Thu, 27 Dec 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,
153: double m_max) {
154: this .zmin = z_min;
155: this .zmax = z_max;
156: this .mmin = m_min;
157: this .mmax = m_max;
158:
159: isZ = true;
160: }
161:
162: /**
163: * Extends this envelope to m direction.
164: *
165: * @param m_min
166: * @param m_max
167: */
168: public void extend(double m_min, double m_max) {
169: this .mmin = m_min;
170: this .mmax = m_max;
171:
172: isM = true;
173: }
174:
175: /**
176: * Extends the envelope so the given point fits in.
177: *
178: * @param x
179: * @param y
180: */
181: public void fit(double x, double y) {
182: xmin = Math.min(x, xmin);
183: xmax = Math.max(x, xmax);
184: ymin = Math.min(y, ymin);
185: ymax = Math.max(y, ymax);
186: }
187:
188: /**
189: * Extends the envelope so the given point fits in.
190: *
191: * @param x
192: * @param y
193: * @param z
194: */
195: public void fit(double x, double y, double z) {
196: fit(x, y);
197: zmin = Math.min(z, zmin);
198: zmax = Math.max(z, zmax);
199: }
200:
201: /**
202: * Extends the envelope so the given envelope fits in.
203: *
204: * @param s
205: */
206: public void fit(ShapeEnvelope s) {
207: if (s.isZ) {
208: fit(s.xmin, s.ymin, s.zmin);
209: fit(s.xmax, s.ymax, s.zmax);
210: } else {
211: fit(s.xmin, s.ymin);
212: fit(s.xmax, s.ymax);
213: }
214: }
215:
216: /*
217: * (non-Javadoc)
218: *
219: * @see org.deegree.io.shpapi.Shape#getByteLength()
220: */
221: public int getByteLength() {
222: int len = 32;
223: if (isZ) {
224: len += 32;
225: }
226: if (isM) {
227: len += 16;
228: }
229: return len;
230: }
231:
232: /**
233: * Reads only x and y values.
234: *
235: * @see org.deegree.io.shpapi.shape_new.Shape#read(byte[], int)
236: */
237: public int read(byte[] bytes, int offset) {
238: int off = offset;
239:
240: xmin = ByteUtils.readLEDouble(bytes, off);
241: off += 8;
242:
243: ymin = ByteUtils.readLEDouble(bytes, off);
244: off += 8;
245:
246: xmax = ByteUtils.readLEDouble(bytes, off);
247: off += 8;
248:
249: ymax = ByteUtils.readLEDouble(bytes, off);
250: off += 8;
251:
252: return off;
253: }
254:
255: /**
256: * Writes only x and y values.
257: *
258: * @see org.deegree.io.shpapi.shape_new.Shape#write(byte[], int)
259: */
260: public int write(byte[] bytes, int offset) {
261: int off = offset;
262:
263: ByteUtils.writeLEDouble(bytes, off, xmin);
264: off += 8;
265:
266: ByteUtils.writeLEDouble(bytes, off, ymin);
267: off += 8;
268:
269: ByteUtils.writeLEDouble(bytes, off, xmax);
270: off += 8;
271:
272: ByteUtils.writeLEDouble(bytes, off, ymax);
273: off += 8;
274:
275: return off;
276: }
277:
278: /**
279: * @return zero, because the envelope does not have a type
280: * @see org.deegree.io.shpapi.shape_new.Shape#getType()
281: */
282: public int getType() {
283: return 0;
284: }
285:
286: /**
287: * @return itself, of course
288: * @see org.deegree.io.shpapi.shape_new.Shape#getEnvelope()
289: */
290: public ShapeEnvelope getEnvelope() {
291: return this ;
292: }
293:
294: @Override
295: public String toString() {
296: StringBuffer sb = new StringBuffer(200);
297: sb.append("x: ").append(xmin).append("/").append(xmax);
298: sb.append(", y: ").append(ymin).append("/").append(ymax);
299: if (isZ) {
300: sb.append(", z: ").append(zmin).append("/").append(zmax);
301: }
302: if (isM || isZ) {
303: sb.append(", m: ").append(mmin).append("/").append(mmax);
304: }
305: return sb.toString();
306: }
307:
308: /**
309: * @return null, because an envelope is not a geometry
310: * @see org.deegree.io.shpapi.shape_new.Shape#getGeometry()
311: */
312: public Geometry getGeometry() {
313: return null;
314: }
315:
316: }
|