001    //$HeadURL: svn+ssh://jwilden@svn.wald.intevation.org/deegree/base/branches/2.5_testing/src/org/deegree/io/shpapi/SHPMultiPoint.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    import org.deegree.model.spatialschema.MultiPoint;
042    
043    /**
044     * Class representig a collection of points<BR>
045     *
046     *
047     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
048     * @author last edited by: $Author: mschneider $
049     *
050     * @version $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18 Jun 2009) $
051     */
052    public class SHPMultiPoint extends SHPGeometry {
053    
054        /**
055         *
056         */
057        public SHPPoint[] points = null;
058    
059        /**
060         *
061         */
062        public int numPoints = 0;
063    
064        /**
065         *
066         */
067        public SHPMultiPoint() {
068            // with no points
069        }
070    
071        /**
072         * constructor: recieves a stream
073         *
074         * @param recBuf
075         */
076        public SHPMultiPoint( byte[] recBuf ) {
077    
078            super( recBuf );
079    
080            envelope = ShapeUtils.readBox( recBuf, 4 );
081    
082            numPoints = ByteUtils.readLEInt( recBuffer, 36 );
083    
084            points = new SHPPoint[numPoints];
085    
086            for ( int i = 0; i < numPoints; i++ ) {
087                points[i] = new SHPPoint( recBuffer, 40 + i * 16 );
088            }
089    
090        }
091    
092        /**
093         * constructor: recieves an array of gm_points
094         *
095         * @param multipoint
096         */
097        public SHPMultiPoint( MultiPoint multipoint ) {
098    
099            double xmin = multipoint.getEnvelope().getMin().getX();
100            double xmax = multipoint.getEnvelope().getMax().getX();
101            double ymin = multipoint.getEnvelope().getMin().getY();
102            double ymax = multipoint.getEnvelope().getMax().getY();
103    
104            try {
105                points = new SHPPoint[multipoint.getSize()];
106                for ( int i = 0; i < multipoint.getSize(); i++ ) {
107                    points[i] = new SHPPoint( multipoint.getPointAt( i ).getPosition() );
108                    if ( points[i].x > xmax ) {
109                        xmax = points[i].x;
110                    } else if ( points[i].x < xmin ) {
111                        xmin = points[i].x;
112                    }
113                    if ( points[i].y > ymax ) {
114                        ymax = points[i].y;
115                    } else if ( points[i].y < ymin ) {
116                        ymin = points[i].y;
117                    }
118                }
119            } catch ( Exception e ) {
120                e.printStackTrace();
121            }
122    
123            envelope = new SHPEnvelope( xmin, xmax, ymax, ymin );
124    
125        }
126    
127        /**
128         * loops through the point array and writes each point to the bytearray<BR>
129         *
130         * @param bytearray
131         * @param start
132         * @return SHPMultiPoint as byte arry
133         */
134        public byte[] writeSHPMultiPoint( byte[] bytearray, int start ) {
135    
136            int offset = start;
137    
138            double xmin = points[0].x;
139            double xmax = points[0].x;
140            double ymin = points[0].y;
141            double ymax = points[0].y;
142    
143            // write shape type identifier ( 8 = multipoint )
144            ByteUtils.writeLEInt( bytearray, offset, 8 );
145    
146            offset += 4;
147            // save offset of the bounding box
148            int tmp = offset;
149    
150            // increment offset with size of the bounding box
151            offset += ( 4 * 8 );
152    
153            // write number of points
154            ByteUtils.writeLEInt( bytearray, offset, points.length );
155    
156            offset += 4;
157    
158            for ( int i = 0; i < points.length; i++ ) {
159    
160                // calculate bounding box
161                if ( points[i].x > xmax ) {
162                    xmax = points[i].x;
163                } else if ( points[i].x < xmin ) {
164                    xmin = points[i].x;
165                }
166    
167                if ( points[i].y > ymax ) {
168                    ymax = points[i].y;
169                } else if ( points[i].y < ymin ) {
170                    ymin = points[i].y;
171                }
172    
173                // write x-coordinate
174                ByteUtils.writeLEDouble( bytearray, offset, points[i].x );
175    
176                offset += 8;
177    
178                // write y-coordinate
179                ByteUtils.writeLEDouble( bytearray, offset, points[i].y );
180    
181                offset += 8;
182    
183            }
184    
185            // jump back to the offset of the bounding box
186            offset = tmp;
187    
188            // write bounding box to the byte array
189            ByteUtils.writeLEDouble( bytearray, offset, xmin );
190            offset += 8;
191            ByteUtils.writeLEDouble( bytearray, offset, ymin );
192            offset += 8;
193            ByteUtils.writeLEDouble( bytearray, offset, xmax );
194            offset += 8;
195            ByteUtils.writeLEDouble( bytearray, offset, ymax );
196    
197            return bytearray;
198        }
199    
200        /**
201         * returns the size of the multipoint shape in bytes<BR>
202         *
203         * @return size of the byte arry representation
204         */
205        public int size() {
206            return 40 + points.length * 16;
207        }
208    
209    }