001: //$HeadURL: $
002: /*---------------- FILE HEADER ------------------------------------------
003: This file is part of deegree.
004: Copyright (C) 2001-2008 by:
005: Department of Geography, University of Bonn
006: http://www.giub.uni-bonn.de/deegree/
007: lat/lon GmbH
008: http://www.lat-lon.de
009:
010: This library is free software; you can redistribute it and/or
011: modify it under the terms of the GNU Lesser General Public
012: License as published by the Free Software Foundation; either
013: version 2.1 of the License, or (at your option) any later version.
014: This library is distributed in the hope that it will be useful,
015: but WITHOUT ANY WARRANTY; without even the implied warranty of
016: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017: Lesser General Public License for more details.
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: Contact:
022:
023: Andreas Poth
024: lat/lon GmbH
025: Aennchenstr. 19
026: 53177 Bonn
027: Germany
028: E-Mail: poth@lat-lon.de
029:
030: Prof. Dr. Klaus Greve
031: Department of Geography
032: University of Bonn
033: Meckenheimer Allee 166
034: 53115 Bonn
035: Germany
036: E-Mail: greve@giub.uni-bonn.de
037: ---------------------------------------------------------------------------*/
038:
039: package org.deegree.crs.components;
040:
041: /**
042: * The <code>Axis</code> class describe the orientation, unit and the name of a crs-axis.
043: *
044: * @author <a href="mailto:bezema@lat-lon.de">Rutger Bezema</a>
045: *
046: * @author last edited by: $Author:$
047: *
048: * @version $Revision:$, $Date:$
049: *
050: */
051:
052: public class Axis {
053:
054: /**
055: * Axis is pointing NORTH ( == -SOUTH) e.g Polar axis positive northwards.
056: */
057: public static final int AO_NORTH = 1;
058:
059: /**
060: * Axis is pointing SOUTH ( == -NORTH )
061: */
062: public static final int AO_SOUTH = -AO_NORTH;
063:
064: /**
065: * Axis is pointing WEST( == -EAST)
066: */
067: public static final int AO_WEST = 2;
068:
069: /**
070: * Axis is pointing EAST( == -WEST) the intersection of the equator with longitude 90°E.
071: */
072: public static final int AO_EAST = -AO_WEST;
073:
074: /**
075: * Axis is pointing UP ( == -DOWN ),
076: */
077: public static final int AO_UP = 3;
078:
079: /**
080: * Axis is pointing DOWN ( == -UP)
081: */
082: public static final int AO_DOWN = -AO_UP;
083:
084: /**
085: * Axis is pointing FRONT( == -BACK), e.g. the Axis through the intersection of the Greenwich meridian and equator.
086: */
087: public static final int AO_FRONT = 4;
088:
089: /**
090: * Axis is pointing BACK ( == -FRONT) e.g. the Axis through the intersection of the opposite of the Greenwich
091: * meridian and equator.
092: */
093: public static final int AO_BACK = -AO_FRONT;
094:
095: /**
096: * Axis is pointing PERPENDICULAR to the earth's surface, which is used for a vertical axis.
097: */
098: public static final int AO_PERPENDICULAR = 5;
099:
100: /**
101: * Axis is pointing in an OTHER direction, which is not specified.
102: */
103: public static final int AO_OTHER = Integer.MAX_VALUE;
104:
105: private Unit units;
106:
107: private String name;
108:
109: private int orientation;
110:
111: /**
112: * @param units
113: * of this axis
114: * @param name
115: * of this axis (e.g. longitude...)
116: * @param orientation
117: * of the positive scale direction, one of Axis.AO*. If an unknown value is supplied AO_OTHER is assumed.
118: */
119: public Axis(Unit units, String name, final int orientation) {
120: this .units = units;
121: this .name = name;
122: this .orientation = orientation;
123: if (orientation != AO_NORTH && orientation != AO_SOUTH
124: && orientation != AO_WEST && orientation != AO_EAST
125: && orientation != AO_UP && orientation != AO_DOWN
126: && orientation != AO_FRONT && orientation != AO_BACK
127: && orientation != AO_PERPENDICULAR) {
128: this .orientation = AO_OTHER;
129: }
130:
131: }
132:
133: /**
134: * Parses the given orientation and creates a valid orientation of it's non-case-sensitive version. If no conversion
135: * was found, {@link #AO_OTHER} will be used.
136: *
137: * @param units
138: * of the axis.
139: * @param name
140: * of the axis.
141: * @param orientation
142: * of the axis as a string for example north
143: */
144: public Axis(Unit units, String name, String orientation) {
145: this .units = units;
146: this .name = name;
147: this .orientation = AO_OTHER;
148: if (orientation != null) {
149: orientation = orientation.trim().toLowerCase();
150: if ("north".equals(orientation)) {
151: this .orientation = AO_NORTH;
152: } else if ("south".equals(orientation)) {
153: this .orientation = AO_SOUTH;
154: } else if ("east".equals(orientation)) {
155: this .orientation = AO_EAST;
156: } else if ("west".equals(orientation)) {
157: this .orientation = AO_WEST;
158: } else if ("front".equals(orientation)) {
159: this .orientation = AO_FRONT;
160: } else if ("back".equals(orientation)) {
161: this .orientation = AO_BACK;
162: } else if ("up".equals(orientation)) {
163: this .orientation = AO_UP;
164: } else if ("down".equals(orientation)) {
165: this .orientation = AO_DOWN;
166: } else if ("perpendicular".equals(orientation)) {
167: this .orientation = AO_PERPENDICULAR;
168: }
169: }
170: }
171:
172: /**
173: * An Axis with unit set to metre.
174: *
175: * @param name
176: * of this axis (e.g. longitude...)
177: * @param orientation
178: * of the positive scale direction, one of Axis.AO*. If an unknown value is supplied AO_OTHER is assumed.
179: */
180: public Axis(String name, final int orientation) {
181: this (Unit.METRE, name, orientation);
182: }
183:
184: /**
185: * Parses the given orientation and creates a valid orientation of it's non-case-sensitive version. If no conversion
186: * was found, {@link #AO_OTHER} will be used. This axis will have metres as it's unit.
187: *
188: * @param name
189: * of the axis
190: * @param orientation
191: * of the axis as a string for example north
192: */
193: public Axis(String name, final String orientation) {
194: this (Unit.METRE, name, orientation);
195: }
196:
197: /**
198: * @return the name.
199: */
200: public final String getName() {
201: return name;
202: }
203:
204: /**
205: * @return the orientation.
206: */
207: public final int getOrientation() {
208: return orientation;
209: }
210:
211: /**
212: * @return the units.
213: */
214: public final Unit getUnits() {
215: return units;
216: }
217:
218: @Override
219: public String toString() {
220: return new StringBuilder("name: ").append(name).append(
221: " orientation: ").append(getOrientationAsString())
222: .append(" units: ").append(units).toString();
223: }
224:
225: @Override
226: public boolean equals(Object otherAxis) {
227: if (otherAxis == null || !(otherAxis instanceof Axis)) {
228: return false;
229: }
230: Axis other = (Axis) otherAxis;
231: return this .units.equals(other.units)
232: && this .orientation == other.orientation;
233: }
234:
235: /**
236: * Implementation as proposed by Joshua Block in Effective Java (Addison-Wesley 2001), which supplies an even
237: * distribution and is relatively fast. It is created from field <b>f</b> as follows:
238: * <ul>
239: * <li>boolean -- code = (f ? 0 : 1)</li>
240: * <li>byte, char, short, int -- code = (int)f </li>
241: * <li>long -- code = (int)(f ^ (f >>>32))</li>
242: * <li>float -- code = Float.floatToIntBits(f);</li>
243: * <li>double -- long l = Double.doubleToLongBits(f); code = (int)(l ^ (l >>> 32))</li>
244: * <li>all Objects, (where equals( ) calls equals( ) for this field) -- code = f.hashCode( )</li>
245: * <li>Array -- Apply above rules to each element</li>
246: * </ul>
247: * <p>
248: * Combining the hash code(s) computed above: result = 37 * result + code;
249: * </p>
250: *
251: * @return (int) ( result >>> 32 ) ^ (int) result;
252: *
253: * @see java.lang.Object#hashCode()
254: */
255: @Override
256: public int hashCode() {
257: // the 2nd millionth prime, :-)
258: long code = 32452843;
259: if (units != null) {
260: code = code * 37 + units.hashCode();
261: }
262: long tmp = Double.doubleToLongBits(orientation);
263: code = code * 37 + (int) (tmp ^ (tmp >>> 32));
264: return (int) (code >>> 32) ^ (int) code;
265: }
266:
267: /**
268: * @return an 'English' representation for the Axis Orientation, or Unknown if the given direction is not known.
269: */
270: public String getOrientationAsString() {
271: switch (orientation) {
272: case AO_NORTH:
273: return "north";
274: case AO_SOUTH:
275: return "south";
276: case AO_EAST:
277: return "east";
278: case AO_WEST:
279: return "west";
280: case AO_FRONT:
281: return "front";
282: case AO_BACK:
283: return "back";
284: case AO_UP:
285: return "up";
286: case AO_DOWN:
287: return "down";
288: case AO_OTHER:
289: return "Other";
290: case AO_PERPENDICULAR:
291: return "perpendicular";
292: }
293: return "Unknown";
294: }
295:
296: }
|