001    //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/branches/2.2_testing/src/org/deegree/io/shpapi/ShapeUtils.java $
002    
003    /*----------------    FILE HEADER  ------------------------------------------
004    
005    This file is part of deegree.
006    Copyright (C) 2001-2008 by:
007    EXSE, Department of Geography, University of Bonn
008    http://www.giub.uni-bonn.de/deegree/
009    lat/lon GmbH
010    http://www.lat-lon.de
011    
012    This library is free software; you can redistribute it and/or
013    modify it under the terms of the GNU Lesser General Public
014    License as published by the Free Software Foundation; either
015    version 2.1 of the License, or (at your option) any later version.
016    
017    This library is distributed in the hope that it will be useful,
018    but WITHOUT ANY WARRANTY; without even the implied warranty of
019    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
020    Lesser General Public License for more details.
021    
022    You should have received a copy of the GNU Lesser General Public
023    License along with this library; if not, write to the Free Software
024    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
025    
026    Contact:
027    
028    Andreas Poth
029    lat/lon GmbH
030    Aennchenstr. 19
031    53115 Bonn
032    Germany
033    E-Mail: poth@lat-lon.de
034    
035    Prof. Dr. Klaus Greve
036    Department of Geography
037    University of Bonn
038    Meckenheimer Allee 166
039    53115 Bonn
040    Germany
041    E-Mail: greve@giub.uni-bonn.de
042    
043                     
044     ---------------------------------------------------------------------------*/
045    
046    package org.deegree.io.shpapi;
047    
048    import org.deegree.model.spatialschema.ByteUtils;
049    
050    /**
051     * Utilities for reading and writing the components of shape files.
052     *
053     *
054     * <B>Last changes<B>:<BR>
055     * 25.11.1999 ap: memory allocation dynaminized<BR>
056     * 17.1.2000  ap: method SHPPoint readPoint(byte[] b, int off) modified<BR>
057     * 17.1.2000  ap: method SHPEnvelope readBox(byte[] b, int off) modified<BR>
058     * 17.1.2000  ap: method writePoint(..) modified<BR>
059     *
060     * <!---------------------------------------------------------------------------->
061     * @version 25.1.2000
062     * @author Andreas Poth
063     *
064     */
065    
066    public class ShapeUtils {
067    
068      
069       /**
070        * readPoint(byte[] b, int off)<BR>
071        * Reads a point record. A point record is a double representing the
072        * x value and a double representing a y value.
073        *
074        * @param b the raw data buffer
075        * @param off the offset into the buffer where the int resides
076        * @return the point read from the buffer at the offset location
077        */
078        public static SHPPoint readPoint(byte[] b, int off) {
079    
080            SHPPoint point = new SHPPoint();
081    
082            point.x = ByteUtils.readLEDouble(b, off);
083            point.y = ByteUtils.readLEDouble(b, off + 8);
084    
085            return point;
086    
087        }
088    
089       /**
090        * method: readBox(byte[] b, int off)<BR>
091        * Reads a bounding box record.  A bounding box is four double
092        * representing, in order, xmin, ymin, xmax, ymax.
093        *
094        * @param b the raw data buffer
095        * @param off the offset into the buffer where the int resides
096        * @return the point read from the buffer at the offset location
097        */
098        public static SHPEnvelope readBox(byte[] b, int off) {
099    
100            SHPEnvelope bb = new SHPEnvelope();
101    
102            SHPPoint min = readPoint(b, off);
103            SHPPoint max = readPoint(b, off + 16);
104    
105            bb.west = min.x;
106            bb.south = min.y;
107            bb.east = max.x;
108            bb.north = max.y;
109    
110            return bb;
111    
112        }
113    
114    
115       /**
116        * method: writePoint(byte[] b, int off, ESRIPoint point)<BR>
117        * Writes the given point to the given buffer at the given location.
118        * The point is written as a double representing x followed by a
119        * double representing y.
120        *
121        * @param b the data buffer
122        * @param off the offset into the buffer where writing should occur
123        * @param point the point to write
124        * @return the number of bytes written
125        */
126        public static int writePoint(byte[] b, int off, SHPPoint point) {
127    
128            int nBytes = ByteUtils.writeLEDouble(b, off, point.x);
129    
130            nBytes += ByteUtils.writeLEDouble(b, off + nBytes, point.y);
131    
132            return nBytes;
133    
134        }
135    
136       /**
137        * method: writeBox(byte[] b, int off, ESRIBoundingBox box)<BR>
138        * Writes the given bounding box  to the given buffer at the
139        * given location.  The bounding box is written as four doubles
140        * representing, in order, xmin, ymin, xmax, ymax.
141        *
142        * @param b the data buffer
143        * @param off the offset into the buffer where writing should occur
144        * @param box the bounding box to write
145        * @return the number of bytes written
146        */
147        public static int writeBox(byte[] b, int off, SHPEnvelope box) {
148    
149            SHPPoint min = new SHPPoint();
150            min.x = box.west;
151            min.y = box.south;
152            SHPPoint max = new SHPPoint();
153            max.x = box.east;
154            max.y = box.north;
155    
156            int nBytes = writePoint(b, off, min);
157    
158            nBytes += writePoint(b, off + nBytes, max);
159    
160            return nBytes;
161    
162        }
163    
164    }