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