001: //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/trunk/src/org/deegree/io/shpapi/shape_new/ShapePoint.java $
002: /*---------------- FILE HEADER ------------------------------------------
003: This file is part of deegree.
004: Copyright (C) 2001-2008 by:
005: Department of Geography, University of Bonn
006: http://www.giub.uni-bonn.de/deegree/
007: lat/lon GmbH
008: http://www.lat-lon.de
009:
010: This library is free software; you can redistribute it and/or
011: modify it under the terms of the GNU Lesser General Public
012: License as published by the Free Software Foundation; either
013: version 2.1 of the License, or (at your option) any later version.
014:
015: This library is distributed in the hope that it will be useful,
016: but WITHOUT ANY WARRANTY; without even the implied warranty of
017: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
018: Lesser General Public License for more details.
019:
020: You should have received a copy of the GNU Lesser General Public
021: License along with this library; if not, write to the Free Software
022: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
023:
024: Contact:
025:
026: Andreas Poth
027: lat/lon GmbH
028: Aennchenstr. 19
029: 53177 Bonn
030: Germany
031: E-Mail: poth@lat-lon.de
032:
033: Prof. Dr. Klaus Greve
034: Department of Geography
035: University of Bonn
036: Meckenheimer Allee 166
037: 53115 Bonn
038: Germany
039: E-Mail: greve@giub.uni-bonn.de
040:
041: ---------------------------------------------------------------------------*/
042: package org.deegree.io.shpapi.shape_new;
043:
044: import org.deegree.model.crs.CoordinateSystem;
045: import org.deegree.model.spatialschema.ByteUtils;
046: import org.deegree.model.spatialschema.Geometry;
047: import org.deegree.model.spatialschema.GeometryException;
048: import org.deegree.model.spatialschema.GeometryFactory;
049: import org.deegree.model.spatialschema.Point;
050: import org.deegree.model.spatialschema.Position;
051: import org.deegree.model.spatialschema.WKTAdapter;
052:
053: import com.vividsolutions.jts.geom.Coordinate;
054:
055: /**
056: * <code>ShapePoint</code> corresponds to Point, PointZ and PointM in shapefile terminology.
057: *
058: * @author <a href="mailto:schmitz@lat-lon.de">Andreas Schmitz</a>
059: * @author last edited by: $Author: otonnhofer $
060: *
061: * @version $Revision: 9734 $, $Date: 2008-01-24 02:43:21 -0800 (Thu, 24 Jan 2008) $
062: */
063: public class ShapePoint implements Shape {
064:
065: private CoordinateSystem crs; // optional crs for geometry
066:
067: public boolean isZ, isM;
068:
069: /**
070: * The x value.
071: */
072: public double x;
073:
074: /**
075: * The y value.
076: */
077: public double y;
078:
079: /**
080: * The z value.
081: */
082: public double z;
083:
084: /**
085: * The m value.
086: */
087: public double m;
088:
089: private boolean isNull;
090:
091: /**
092: * Constructs one without values, use read to fill it.
093: *
094: * @param z
095: * if PointZ
096: * @param m
097: * if PointM
098: */
099: public ShapePoint(boolean z, boolean m) {
100: isZ = z;
101: isM = m;
102: }
103:
104: /**
105: * Constructs one without values, use read to fill it.
106: *
107: * @param z
108: * if PointZ
109: * @param m
110: * if PointM
111: * @param crs
112: * CoordinateSystem of the shape
113: */
114: public ShapePoint(boolean z, boolean m, CoordinateSystem crs) {
115: isZ = z;
116: isM = m;
117: this .crs = crs;
118: }
119:
120: /**
121: * Creates a new PointZ from deegree Point.
122: *
123: * @param p
124: */
125: public ShapePoint(Point p) {
126: x = p.getX();
127: y = p.getY();
128: z = p.getZ();
129: isZ = true;
130: }
131:
132: /**
133: * Creates a new PointZ from deegree Position.
134: *
135: * @param p
136: */
137: public ShapePoint(Position p) {
138: x = p.getX();
139: y = p.getY();
140: z = p.getZ();
141: isZ = true;
142: }
143:
144: /**
145: * Just reads x and y from the byte array.
146: *
147: * @param bytes
148: * @param offset
149: */
150: public ShapePoint(byte[] bytes, int offset) {
151: x = ByteUtils.readLEDouble(bytes, offset);
152: y = ByteUtils.readLEDouble(bytes, offset + 8);
153: }
154:
155: /**
156: * @return the Point as (x, y, NaN), the PointZ as (x, y, z) Coordinate
157: */
158: public Coordinate export() {
159: if (isZ) {
160: return new Coordinate(x, y, z);
161: }
162: return new Coordinate(x, y);
163: }
164:
165: /**
166: * Extends this point with z and m values, so it is a PointZ
167: *
168: * @param zVal
169: * @param mVal
170: */
171: public void extend(double zVal, double mVal) {
172: z = zVal;
173: m = mVal;
174: isZ = true;
175: }
176:
177: /**
178: * Extends this point with m values, so it is a PointM
179: *
180: * @param mVal
181: */
182: public void extend(double mVal) {
183: m = mVal;
184: isM = true;
185: }
186:
187: /*
188: * (non-Javadoc)
189: *
190: * @see org.deegree.io.shpapi.Shape#getByteLength()
191: */
192: public int getByteLength() {
193: if (isZ) {
194: return 36;
195: }
196: if (isM) {
197: return 28;
198: }
199: return 20;
200: }
201:
202: private int readPoint(byte[] bytes, int offset) {
203: int off = offset;
204: isM = false;
205: isZ = false;
206: x = ByteUtils.readLEDouble(bytes, off);
207:
208: off += 8;
209:
210: y = ByteUtils.readLEDouble(bytes, off);
211:
212: off += 8;
213: return off;
214: }
215:
216: private int readPointZ(byte[] bytes, int offset) {
217: int off = offset;
218: isM = false;
219: isZ = true;
220: x = ByteUtils.readLEDouble(bytes, off);
221:
222: off += 8;
223:
224: y = ByteUtils.readLEDouble(bytes, off);
225:
226: off += 8;
227:
228: z = ByteUtils.readLEDouble(bytes, off);
229:
230: off += 8;
231:
232: m = ByteUtils.readLEDouble(bytes, off);
233:
234: off += 8;
235:
236: return off;
237: }
238:
239: private int readPointM(byte[] bytes, int offset) {
240: int off = offset;
241: isM = true;
242: isZ = false;
243: x = ByteUtils.readLEDouble(bytes, off);
244:
245: off += 8;
246:
247: y = ByteUtils.readLEDouble(bytes, off);
248:
249: off += 8;
250:
251: m = ByteUtils.readLEDouble(bytes, off);
252:
253: off += 8;
254: return off;
255: }
256:
257: /*
258: * (non-Javadoc)
259: *
260: * @see org.deegree.io.shpapi.Shape#read(byte[], int)
261: */
262: public int read(byte[] bytes, int offset) {
263: int off = offset;
264:
265: int type = ByteUtils.readLEInt(bytes, off);
266: off += 4;
267:
268: if (type == ShapeFile.NULL) {
269: isNull = true;
270: return off;
271: }
272:
273: if (type == ShapeFile.POINT) {
274: return readPoint(bytes, off);
275: }
276:
277: if (type == ShapeFile.POINTZ) {
278: return readPointZ(bytes, off);
279: }
280:
281: if (type == ShapeFile.POINTM) {
282: return readPointM(bytes, off);
283: }
284:
285: return -1;
286: }
287:
288: private int writePoint(byte[] bytes, int offset) {
289: int off = offset;
290:
291: ByteUtils.writeLEDouble(bytes, off, x);
292: off += 8;
293:
294: ByteUtils.writeLEDouble(bytes, off, y);
295: off += 8;
296:
297: return off;
298: }
299:
300: private int writePointZ(byte[] bytes, int offset) {
301: int off = writePoint(bytes, offset);
302:
303: ByteUtils.writeLEDouble(bytes, off, z);
304: off += 8;
305:
306: ByteUtils.writeLEDouble(bytes, off, m);
307: off += 8;
308:
309: return off;
310: }
311:
312: private int writePointM(byte[] bytes, int offset) {
313: int off = writePoint(bytes, offset);
314:
315: ByteUtils.writeLEDouble(bytes, off, m);
316: off += 8;
317:
318: return off;
319: }
320:
321: /*
322: * (non-Javadoc)
323: *
324: * @see org.deegree.io.shpapi.Shape#write(byte[], int)
325: */
326: public int write(byte[] bytes, int offset) {
327: if (isZ) {
328: ByteUtils.writeLEInt(bytes, offset, ShapeFile.POINTZ);
329: return writePointZ(bytes, offset + 4);
330: }
331: if (isM) {
332: ByteUtils.writeLEInt(bytes, offset, ShapeFile.POINTM);
333: return writePointM(bytes, offset + 4);
334: }
335:
336: ByteUtils.writeLEInt(bytes, offset, ShapeFile.POINT);
337: return writePoint(bytes, offset + 4);
338: }
339:
340: /*
341: * (non-Javadoc)
342: *
343: * @see org.deegree.io.shpapi.shape_new.Shape#getType()
344: */
345: public int getType() {
346: if (isZ) {
347: return ShapeFile.POINTZ;
348: }
349: if (isM) {
350: return ShapeFile.POINTM;
351: }
352: return ShapeFile.POINT;
353: }
354:
355: /**
356: * @return null, points do not have an envelope
357: * @see org.deegree.io.shpapi.shape_new.Shape#getEnvelope()
358: */
359: public ShapeEnvelope getEnvelope() {
360: return null;
361: }
362:
363: /**
364: * This creates a Point object.
365: *
366: * @see org.deegree.io.shpapi.shape_new.Shape#getGeometry()
367: */
368: public Geometry getGeometry() throws ShapeGeometryException {
369: if (isNull) {
370: return null;
371: }
372: if (isZ) {
373: return GeometryFactory.createPoint(x, y, z, crs);
374: }
375: return GeometryFactory.createPoint(x, y, crs);
376: }
377:
378: @Override
379: public String toString() {
380: try {
381: return WKTAdapter.export(getGeometry()).toString();
382: } catch (GeometryException e) {
383: return "(unknown)";
384: }
385: }
386:
387: }
|