001: /*
002: * The JTS Topology Suite is a collection of Java classes that
003: * implement the fundamental operations required to validate a given
004: * geo-spatial data set to a known topological specification.
005: *
006: * Copyright (C) 2001 Vivid Solutions
007: *
008: * This library is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU Lesser General Public
010: * License as published by the Free Software Foundation; either
011: * version 2.1 of the License, or (at your option) any later version.
012: *
013: * This library is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016: * Lesser General Public License for more details.
017: *
018: * You should have received a copy of the GNU Lesser General Public
019: * License along with this library; if not, write to the Free Software
020: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
021: *
022: * For more information, contact:
023: *
024: * Vivid Solutions
025: * Suite #1A
026: * 2328 Government Street
027: * Victoria BC V8T 5G5
028: * Canada
029: *
030: * (250)385-6040
031: * www.vividsolutions.com
032: */
033: package com.vividsolutions.jtsexample.geom;
034:
035: import com.vividsolutions.jts.geom.*;
036:
037: /**
038: * Demonstrates how to implement a CoordinateSequence for a new kind of
039: * coordinate (an {@link ExtendedCoordinate} in this example). In this
040: * implementation, Coordinates returned by #toArray and #get are live -- parties
041: * that change them are actually changing the ExtendedCoordinateSequence's
042: * underlying data.
043: *
044: * @version 1.7
045: */
046: public class ExtendedCoordinateSequence implements CoordinateSequence {
047: public static ExtendedCoordinate[] copy(Coordinate[] coordinates) {
048: ExtendedCoordinate[] copy = new ExtendedCoordinate[coordinates.length];
049: for (int i = 0; i < coordinates.length; i++) {
050: copy[i] = new ExtendedCoordinate(coordinates[i]);
051: }
052: return copy;
053: }
054:
055: public static ExtendedCoordinate[] copy(CoordinateSequence coordSeq) {
056: ExtendedCoordinate[] copy = new ExtendedCoordinate[coordSeq
057: .size()];
058: for (int i = 0; i < coordSeq.size(); i++) {
059: copy[i] = new ExtendedCoordinate(coordSeq.getCoordinate(i));
060: }
061: return copy;
062: }
063:
064: private ExtendedCoordinate[] coordinates;
065:
066: /**
067: * Copy constructor -- simply aliases the input array, for better performance.
068: */
069: public ExtendedCoordinateSequence(ExtendedCoordinate[] coordinates) {
070: this .coordinates = coordinates;
071: }
072:
073: /**
074: * Constructor that makes a copy of an array of Coordinates.
075: * Always makes a copy of the input array, since the actual class
076: * of the Coordinates in the input array may be different from ExtendedCoordinate.
077: */
078: public ExtendedCoordinateSequence(Coordinate[] copyCoords) {
079: coordinates = copy(copyCoords);
080: }
081:
082: /**
083: * Constructor that makes a copy of a CoordinateSequence.
084: */
085: public ExtendedCoordinateSequence(CoordinateSequence coordSeq) {
086: coordinates = copy(coordSeq);
087: }
088:
089: /**
090: * Constructs a sequence of a given size, populated
091: * with new {@link ExtendedCoordinate}s.
092: *
093: * @param size the size of the sequence to create
094: */
095: public ExtendedCoordinateSequence(int size) {
096: coordinates = new ExtendedCoordinate[size];
097: for (int i = 0; i < size; i++) {
098: coordinates[i] = new ExtendedCoordinate();
099: }
100: }
101:
102: /**
103: * @see com.vividsolutions.jts.geom.CoordinateSequence#getDimension()
104: */
105: public int getDimension() {
106: return 4;
107: }
108:
109: public Coordinate getCoordinate(int i) {
110: return coordinates[i];
111: }
112:
113: /**
114: * @see com.vividsolutions.jts.geom.CoordinateSequence#getX(int)
115: */
116: public Coordinate getCoordinateCopy(int index) {
117: return new Coordinate(coordinates[index]);
118: }
119:
120: /**
121: * @see com.vividsolutions.jts.geom.CoordinateSequence#getX(int)
122: */
123: public void getCoordinate(int index, Coordinate coord) {
124: coord.x = coordinates[index].x;
125: coord.y = coordinates[index].y;
126: }
127:
128: /**
129: * @see com.vividsolutions.jts.geom.CoordinateSequence#getX(int)
130: */
131: public double getX(int index) {
132: return coordinates[index].x;
133: }
134:
135: /**
136: * @see com.vividsolutions.jts.geom.CoordinateSequence#getY(int)
137: */
138: public double getY(int index) {
139: return coordinates[index].y;
140: }
141:
142: /**
143: * @see com.vividsolutions.jts.geom.CoordinateSequence#getOrdinate(int, int)
144: */
145: public double getOrdinate(int index, int ordinateIndex) {
146: switch (ordinateIndex) {
147: case CoordinateSequence.X:
148: return coordinates[index].x;
149: case CoordinateSequence.Y:
150: return coordinates[index].y;
151: case CoordinateSequence.Z:
152: return coordinates[index].z;
153: case 4:
154: return coordinates[index].getM();
155: }
156: return Double.NaN;
157: }
158:
159: /**
160: * @see com.vividsolutions.jts.geom.CoordinateSequence#setOrdinate(int, int, double)
161: */
162: public void setOrdinate(int index, int ordinateIndex, double value) {
163: switch (ordinateIndex) {
164: case CoordinateSequence.X:
165: coordinates[index].x = value;
166: case CoordinateSequence.Y:
167: coordinates[index].y = value;
168: case CoordinateSequence.Z:
169: coordinates[index].z = value;
170: case 4:
171: coordinates[index].setM(value);
172: }
173: }
174:
175: public Object clone() {
176: ExtendedCoordinate[] cloneCoordinates = new ExtendedCoordinate[size()];
177: for (int i = 0; i < coordinates.length; i++) {
178: cloneCoordinates[i] = (ExtendedCoordinate) coordinates[i]
179: .clone();
180: }
181:
182: return new ExtendedCoordinateSequence(cloneCoordinates);
183: }
184:
185: public int size() {
186: return coordinates.length;
187: }
188:
189: public Coordinate[] toCoordinateArray() {
190: return coordinates;
191: }
192:
193: public Envelope expandEnvelope(Envelope env) {
194: for (int i = 0; i < coordinates.length; i++) {
195: env.expandToInclude(coordinates[i]);
196: }
197: return env;
198: }
199:
200: public String toString() {
201: StringBuffer strBuf = new StringBuffer();
202: strBuf.append("ExtendedCoordinateSequence [");
203: for (int i = 0; i < coordinates.length; i++) {
204: if (i > 0)
205: strBuf.append(", ");
206: strBuf.append(coordinates[i]);
207: }
208: strBuf.append("]");
209: return strBuf.toString();
210: }
211: }
|