001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2004-2006, GeoTools Project Managment Committee (PMC)
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation;
009: * version 2.1 of the License.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * Created on 31-dic-2004
017: */
018: package org.geotools.geometry.jts.coordinatesequence;
019:
020: import com.vividsolutions.jts.geom.Coordinate;
021: import com.vividsolutions.jts.geom.CoordinateSequence;
022: import com.vividsolutions.jts.geom.CoordinateSequenceFactory;
023: import com.vividsolutions.jts.geom.DefaultCoordinateSequenceFactory;
024:
025: /**
026: * A CSBuilder that generates DefaultCoordinateSequence objects, that is,
027: * coordinate sequences backed by a Coordinate[] array.
028: * @author wolf
029: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/main/src/main/java/org/geotools/geometry/jts/coordinatesequence/DefaultCSBuilder.java $
030: */
031: public class DefaultCSBuilder implements CSBuilder {
032:
033: private Coordinate[] coordinateArray;
034: private CoordinateSequenceFactory factory = DefaultCoordinateSequenceFactory
035: .instance();
036:
037: /**
038: * @see org.geotools.geometry.coordinatesequence.CSBuilder#start(int, int)
039: */
040: public void start(int size, int dimensions) {
041: coordinateArray = new Coordinate[size];
042: for (int i = 0; i < size; i++)
043: coordinateArray[i] = new Coordinate();
044: }
045:
046: /**
047: * @see org.geotools.geometry.coordinatesequence.CSBuilder#getCoordinateSequence()
048: */
049: public CoordinateSequence end() {
050: CoordinateSequence cs = factory.create(coordinateArray);
051: coordinateArray = null;
052: return cs;
053: }
054:
055: /**
056: * @see org.geotools.geometry.coordinatesequence.CSBuilder#setOrdinate(double, int, int)
057: */
058: public void setOrdinate(double value, int ordinateIndex,
059: int coordinateIndex) {
060: Coordinate c = coordinateArray[coordinateIndex];
061: switch (ordinateIndex) {
062: case 0:
063: c.x = value;
064: break;
065: case 1:
066: c.y = value;
067: break;
068: case 2:
069: c.z = value;
070: break;
071: }
072: }
073:
074: /**
075: * @see org.geotools.geometry.coordinatesequence.CSBuilder#getOrdinate(int, int)
076: */
077: public double getOrdinate(int ordinateIndex, int coordinateIndex) {
078: Coordinate c = coordinateArray[coordinateIndex];
079: switch (ordinateIndex) {
080: case 0:
081: return c.x;
082: case 1:
083: return c.y;
084: case 2:
085: return c.z;
086: default:
087: return 0.0;
088: }
089: }
090:
091: /**
092: * @see org.geotools.geometry.coordinatesequence.CSBuilder#getSize()
093: */
094: public int getSize() {
095: if (coordinateArray != null) {
096: return coordinateArray.length;
097: } else {
098: return -1;
099: }
100: }
101:
102: /**
103: * @see org.geotools.geometry.coordinatesequence.CSBuilder#getDimension()
104: */
105: public int getDimension() {
106: if (coordinateArray != null) {
107: return 2;
108: } else {
109: return -1;
110: }
111: }
112:
113: /**
114: * @see org.geotools.geometry.coordinatesequence.CSBuilder#setOrdinate(com.vividsolutions.jts.geom.CoordinateSequence, double, int, int)
115: */
116: public void setOrdinate(CoordinateSequence sequence, double value,
117: int ordinateIndex, int coordinateIndex) {
118: Coordinate c = sequence.getCoordinate(coordinateIndex);
119: switch (ordinateIndex) {
120: case 0:
121: c.x = value;
122: break;
123: case 1:
124: c.y = value;
125: break;
126: case 2:
127: c.z = value;
128: break;
129: }
130:
131: }
132:
133: }
|