001: //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/trunk/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: mschneider $
057: *
058: * @version $Revision: 10547 $, $Date: 2008-03-11 01:40:28 -0700 (Tue, 11 Mar 2008) $
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)
106: .getPosition());
107: if (points[i].x > xmax) {
108: xmax = points[i].x;
109: } else if (points[i].x < xmin) {
110: xmin = points[i].x;
111: }
112: if (points[i].y > ymax) {
113: ymax = points[i].y;
114: } else if (points[i].y < ymin) {
115: ymin = points[i].y;
116: }
117: }
118: } catch (Exception e) {
119: e.printStackTrace();
120: }
121:
122: envelope = new SHPEnvelope(xmin, xmax, ymax, ymin);
123:
124: }
125:
126: /**
127: * loops through the point array and writes each point to the bytearray<BR>
128: *
129: * @param bytearray
130: * @param start
131: * @return SHPMultiPoint as byte arry
132: */
133: public byte[] writeSHPMultiPoint(byte[] bytearray, int start) {
134:
135: int offset = start;
136:
137: double xmin = points[0].x;
138: double xmax = points[0].x;
139: double ymin = points[0].y;
140: double ymax = points[0].y;
141:
142: // write shape type identifier ( 8 = multipoint )
143: ByteUtils.writeLEInt(bytearray, offset, 8);
144:
145: offset += 4;
146: // save offset of the bounding box
147: int tmp = offset;
148:
149: // increment offset with size of the bounding box
150: offset += (4 * 8);
151:
152: // write number of points
153: ByteUtils.writeLEInt(bytearray, offset, points.length);
154:
155: offset += 4;
156:
157: for (int i = 0; i < points.length; i++) {
158:
159: // calculate bounding box
160: if (points[i].x > xmax) {
161: xmax = points[i].x;
162: } else if (points[i].x < xmin) {
163: xmin = points[i].x;
164: }
165:
166: if (points[i].y > ymax) {
167: ymax = points[i].y;
168: } else if (points[i].y < ymin) {
169: ymin = points[i].y;
170: }
171:
172: // write x-coordinate
173: ByteUtils.writeLEDouble(bytearray, offset, points[i].x);
174:
175: offset += 8;
176:
177: // write y-coordinate
178: ByteUtils.writeLEDouble(bytearray, offset, points[i].y);
179:
180: offset += 8;
181:
182: }
183:
184: // jump back to the offset of the bounding box
185: offset = tmp;
186:
187: // write bounding box to the byte array
188: ByteUtils.writeLEDouble(bytearray, offset, xmin);
189: offset += 8;
190: ByteUtils.writeLEDouble(bytearray, offset, ymin);
191: offset += 8;
192: ByteUtils.writeLEDouble(bytearray, offset, xmax);
193: offset += 8;
194: ByteUtils.writeLEDouble(bytearray, offset, ymax);
195:
196: return bytearray;
197: }
198:
199: /**
200: * returns the size of the multipoint shape in bytes<BR>
201: *
202: * @return size of the byte arry representation
203: */
204: public int size() {
205: return 40 + points.length * 16;
206: }
207:
208: }
|