001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2002-2006, Geotools Project Managment Committee (PMC)
005: * (C) 2002, Centre for Computational Geography
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: */
017: package org.geotools.data.shapefile.shp;
018:
019: import java.nio.ByteBuffer;
020:
021: import com.vividsolutions.jts.geom.Coordinate;
022: import com.vividsolutions.jts.geom.GeometryFactory;
023: import com.vividsolutions.jts.geom.Point;
024:
025: /**
026: * Wrapper for a Shapefile point.
027: *
028: * @author aaime
029: * @author Ian Schneider
030: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/plugin/shapefile/src/main/java/org/geotools/data/shapefile/shp/PointHandler.java $
031: *
032: */
033: public class PointHandler implements ShapeHandler {
034:
035: final ShapeType shapeType;
036: GeometryFactory geometryFactory = new GeometryFactory();
037:
038: public PointHandler(ShapeType type) throws ShapefileException {
039: if ((type != ShapeType.POINT) && (type != ShapeType.POINTM)
040: && (type != ShapeType.POINTZ)) { // 2d, 2d+m, 3d+m
041: throw new ShapefileException(
042: "PointHandler constructor: expected a type of 1, 11 or 21");
043: }
044:
045: shapeType = type;
046: }
047:
048: public PointHandler() {
049: shapeType = ShapeType.POINT; //2d
050: }
051:
052: /**
053: * Returns the shapefile shape type value for a point
054: * @return int Shapefile.POINT
055: */
056: public ShapeType getShapeType() {
057: return shapeType;
058: }
059:
060: public int getLength(Object geometry) {
061: int length;
062: if (shapeType == ShapeType.POINT) {
063: length = 20;
064: } else if (shapeType == ShapeType.POINTM) {
065: length = 28;
066: } else if (shapeType == ShapeType.POINTZ) {
067: length = 36;
068: } else {
069: throw new IllegalStateException(
070: "Expected ShapeType of Point, got" + shapeType);
071: }
072: return length;
073: }
074:
075: public Object read(ByteBuffer buffer, ShapeType type) {
076: if (type == ShapeType.NULL) {
077: return createNull();
078: }
079:
080: double x = buffer.getDouble();
081: double y = buffer.getDouble();
082: double z = Double.NaN;
083:
084: if (shapeType == ShapeType.POINTM) {
085: buffer.getDouble();
086: }
087:
088: if (shapeType == ShapeType.POINTZ) {
089: z = buffer.getDouble();
090: }
091:
092: return geometryFactory.createPoint(new Coordinate(x, y, z));
093: }
094:
095: private Object createNull() {
096: return geometryFactory.createPoint(new Coordinate(Double.NaN,
097: Double.NaN, Double.NaN));
098: }
099:
100: public void write(ByteBuffer buffer, Object geometry) {
101: Coordinate c = ((Point) geometry).getCoordinate();
102:
103: buffer.putDouble(c.x);
104: buffer.putDouble(c.y);
105:
106: if (shapeType == ShapeType.POINTZ) {
107: if (Double.isNaN(c.z)) { // nan means not defined
108: buffer.putDouble(0.0);
109: } else {
110: buffer.putDouble(c.z);
111: }
112: }
113:
114: if ((shapeType == ShapeType.POINTZ)
115: || (shapeType == ShapeType.POINTM)) {
116: buffer.putDouble(-10E40); //M
117: }
118: }
119:
120: }
|