001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2004-2006, GeoTools Project Managment Committee (PMC)
005: * (C) 2004, Institut de Recherche pour le Développement
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;
010: * version 2.1 of the License.
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.geometry;
018:
019: // J2SE dependencies
020: import java.io.Serializable;
021:
022: // OpenGIS dependencies
023: import org.opengis.util.Cloneable;
024: import org.opengis.referencing.crs.CoordinateReferenceSystem;
025: import org.opengis.geometry.DirectPosition;
026: import org.opengis.geometry.MismatchedDimensionException;
027:
028: /**
029: * Holds the coordinates for a one-dimensional position within some coordinate reference system.
030: *
031: * @since 2.0
032: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/referencing/src/main/java/org/geotools/geometry/DirectPosition1D.java $
033: * @version $Id: DirectPosition1D.java 26137 2007-07-03 17:59:44Z desruisseaux $
034: * @author Martin Desruisseaux
035: *
036: * @see DirectPosition2D
037: * @see GeneralPosition
038: */
039: public class DirectPosition1D extends AbstractDirectPosition implements
040: Serializable, Cloneable {
041: /**
042: * Serial number for interoperability with different versions.
043: */
044: private static final long serialVersionUID = 3235094562875693710L;
045:
046: /**
047: * The coordinate reference system for this position;
048: */
049: private CoordinateReferenceSystem crs;
050:
051: /**
052: * The ordinate value.
053: */
054: public double ordinate;
055:
056: /**
057: * Constructs a position initialized to (0) with a {@code null}
058: * coordinate reference system.
059: */
060: public DirectPosition1D() {
061: }
062:
063: /**
064: * Constructs a position with the specified coordinate reference system.
065: */
066: public DirectPosition1D(final CoordinateReferenceSystem crs) {
067: setCoordinateReferenceSystem(crs);
068: }
069:
070: /**
071: * Constructs a 1D position from the specified ordinate.
072: */
073: public DirectPosition1D(final double ordinate) {
074: this .ordinate = ordinate;
075: }
076:
077: /**
078: * Constructs a position initialized to the same values than the specified point.
079: */
080: public DirectPosition1D(final DirectPosition point) {
081: setLocation(point);
082: }
083:
084: /**
085: * Returns the coordinate reference system in which the coordinate is given.
086: * May be {@code null} if this particular {@code DirectPosition} is included
087: * in a larger object with such a reference to a {@linkplain CoordinateReferenceSystem
088: * coordinate reference system}.
089: *
090: * @return The coordinate reference system, or {@code null}.
091: */
092: public final CoordinateReferenceSystem getCoordinateReferenceSystem() {
093: return crs;
094: }
095:
096: /**
097: * Set the coordinate reference system in which the coordinate is given.
098: *
099: * @param crs The new coordinate reference system, or {@code null}.
100: */
101: public void setCoordinateReferenceSystem(
102: final CoordinateReferenceSystem crs) {
103: checkCoordinateReferenceSystemDimension(crs, 1);
104: this .crs = crs;
105: }
106:
107: /**
108: * The length of coordinate sequence (the number of entries).
109: * This is always 1 for <code>DirectPosition1D</code> objects.
110: *
111: * @return The dimensionality of this position.
112: */
113: public final int getDimension() {
114: return 1;
115: }
116:
117: /**
118: * Returns a sequence of numbers that hold the coordinate of this position in its
119: * reference system.
120: *
121: * @return The coordinates.
122: */
123: //@Override
124: public double[] getCoordinates() {
125: return new double[] { ordinate };
126: }
127:
128: /**
129: * Returns the ordinate at the specified dimension.
130: *
131: * @param dimension The dimension, which must be 0.
132: * @return The {@linkplain #ordinate}.
133: * @throws IndexOutOfBoundsException if the specified dimension is out of bounds.
134: *
135: * @todo Provides a more detailled error message.
136: */
137: public final double getOrdinate(final int dimension)
138: throws IndexOutOfBoundsException {
139: if (dimension == 0) {
140: return ordinate;
141: } else {
142: throw new IndexOutOfBoundsException(String
143: .valueOf(dimension));
144: }
145: }
146:
147: /**
148: * Sets the ordinate value along the specified dimension.
149: *
150: * @param dimension The dimension, which must be 0.
151: * @param value the ordinate value.
152: * @throws IndexOutOfBoundsException if the specified dimension is out of bounds.
153: *
154: * @todo Provides a more detailled error message.
155: */
156: public final void setOrdinate(int dimension, double value)
157: throws IndexOutOfBoundsException {
158: if (dimension == 0) {
159: ordinate = value;
160: } else {
161: throw new IndexOutOfBoundsException(String
162: .valueOf(dimension));
163: }
164: }
165:
166: /**
167: * Set this coordinate to the specified direct position. If the specified position
168: * contains a {@linkplain CoordinateReferenceSystem coordinate reference system},
169: * then the CRS for this position will be set to the CRS of the specified position.
170: *
171: * @param position The new position for this point.
172: * @throws MismatchedDimensionException if this point doesn't have the expected dimension.
173: */
174: public void setLocation(final DirectPosition position)
175: throws MismatchedDimensionException {
176: AbstractDirectPosition.ensureDimensionMatch("position",
177: position.getDimension(), 1);
178: setCoordinateReferenceSystem(position
179: .getCoordinateReferenceSystem());
180: ordinate = position.getOrdinate(0);
181: }
182:
183: /**
184: * Returns a hash value for this coordinate.
185: */
186: public int hashCode() {
187: final long value = Double.doubleToLongBits(ordinate);
188: int code = 31 + ((int) value ^ (int) (value >>> 32));
189: if (crs != null) {
190: code += crs.hashCode();
191: }
192: assert code == super .hashCode();
193: return code;
194: }
195:
196: /**
197: * Returns a copy of this position.
198: *
199: * @todo Uncomment after we removed the super-class method.
200: */
201: // public Object clone() {
202: // try {
203: // return super.clone();
204: // } catch (CloneNotSupportedException exception) {
205: // // Should not happen, since we are cloneable.
206: // throw new AssertionError(exception);
207: // }
208: // }
209: }
|