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