KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jscience > geography > coordinates > Coordinates


1 /*
2  * JScience - Java(TM) Tools and Libraries for the Advancement of Sciences.
3  * Copyright (C) 2006 - JScience (http://jscience.org/)
4  * All rights reserved.
5  *
6  * Permission to use, copy, modify, and distribute this software is
7  * freely granted, provided that this notice is preserved.
8  */

9 package org.jscience.geography.coordinates;
10
11 import javolution.lang.Immutable;
12 import javolution.text.TextBuilder;
13
14 import org.jscience.geography.coordinates.crs.CoordinateReferenceSystem;
15 import org.opengis.referencing.cs.CoordinateSystem;
16 import org.opengis.spatialschema.geometry.DirectPosition;
17
18 /**
19  * This class designates the position that a point occupies in a given
20  * n-dimensional reference frame or system.
21  * This implementation is compatible with OpenGIS® DirectPosition.
22  *
23  * @author <a HREF="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
24  * @version 3.0, February 13, 2006
25  * @see <a HREF="http://www.opengeospatial.org">Open Geospatial Consortium, Inc.</a>
26  */

27 public abstract class Coordinates<R extends CoordinateReferenceSystem>
28         implements DirectPosition, Immutable {
29
30     /**
31      * Default constructor.
32      */

33     protected Coordinates() {
34     }
35
36     /**
37      * Returns the reference system for this coordinates.
38      *
39      * @return the associated coordinate reference system.
40      */

41     public abstract R getCoordinateReferenceSystem();
42
43     /////////////
44
// OpenGIS //
45
/////////////
46

47     /**
48      * OpenGIS&reg; - The length of coordinate sequence (the number of entries).
49      * This is determined by the {@linkplain #getCoordinateReferenceSystem()
50      * coordinate reference system}.
51      *
52      * @return the dimensionality of this position.
53      */

54     public abstract int getDimension();
55
56     /**
57      * OpenGIS&reg; - Returns the ordinate at the specified dimension.
58      *
59      * @param dimension The dimension in the range 0 to
60      * {@linkplain #getDimension dimension}-1.
61      * @return The coordinate at the specified dimension.
62      * @throws IndexOutOfBoundsException if the specified dimension is out
63      * of bounds.
64      */

65     public abstract double getOrdinate(int dimension)
66             throws IndexOutOfBoundsException JavaDoc;
67
68     /**
69      * OpenGIS&reg; - Throws <code>UnsupportedOperationException</code> as
70      * <b>J</b>Science coordinates are immutable.
71      */

72     public final void setOrdinate(int dimension, double value)
73             throws IndexOutOfBoundsException JavaDoc {
74         throw new UnsupportedOperationException JavaDoc("Immutable coordinates");
75     }
76
77     /**
78      * OpenGIS&reg; - Returns the sequence of numbers that hold the coordinate
79      * of this position in its reference system.
80      *
81      * @return a copy of the coordinates. Changes in the returned array will
82      * not be reflected back in this {@code DirectPosition} object.
83      */

84     public final double[] getCoordinates() {
85         double[] coordinates = new double[getDimension()];
86         for (int i = 0; i < coordinates.length; i++) {
87             coordinates[i] = getOrdinate(i);
88         }
89         return coordinates;
90     }
91
92     /**
93      * OpenGIS&reg; - Returns the direct position for this position.
94      *
95      * @return <code>this</code>
96      */

97     public final DirectPosition getPosition() {
98         return this;
99     }
100
101     /**
102      * OpenGIS&reg; - Makes an exact copy of this coordinate.
103      *
104      * @return the copy.
105      */

106     public final Coordinates<R> clone() {
107         return this.clone();
108     }
109
110     /**
111      * Returns the string representation of this coordinates.
112      *
113      * @return the coordinates values/units.
114      */

115     public String JavaDoc toString() {
116         double[] coordinates = getCoordinates();
117         CoordinateSystem cs = this.getCoordinateReferenceSystem().getCoordinateSystem();
118         TextBuilder tb = TextBuilder.newInstance();
119         tb.append('[');
120         for (int i=0; i < coordinates.length; i++) {
121             if (i != 0) {
122                 tb.append(", ");
123             }
124             tb.append(getOrdinate(i));
125             tb.append(' ');
126             tb.append(cs.getAxis(i).getUnit());
127         }
128         tb.append(']');
129         return tb.toString();
130     }
131 }
Popular Tags