001: /*
002: * <copyright>
003: *
004: * Copyright 2001-2004 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026:
027: package org.cougaar.tools.csmart.util;
028:
029: /**
030: * A location (as a <code>LatLonPoint</code>) with a name.
031: * Multiple constructors are available.
032: * Utility functions to find the distance from this location to another
033: *
034: * @see NewNamedLocation
035: * @see LatLonPoint
036: */
037: public class NamedLocationImpl implements NewNamedLocation {
038:
039: private String name = null; // name of this location
040: private LatLonPoint pos = null; // position of this location
041:
042: public NamedLocationImpl() {
043: }
044:
045: /**
046: * Creates a new <code>NamedLocationImpl</code> instance.
047: * This constructor will try to parse the String to find
048: * a name, lat, and long (float degrees). If it cant, it treats the String
049: * as a Name.<br>
050: * The 3 fields are expected to be separated with a vertical bar (|).<br>
051: * If there are errors parsing the lat & long, the default is 0.0 for each.
052: *
053: * @param name a <code>String</code> location name or composite location, lat, long
054: */
055: public NamedLocationImpl(String name) {
056: if (name.indexOf('|') == -1) {
057: //System.err.println("setting name to full thing: " + name);
058: this .setName(name);
059: } else {
060: this .setName(name.substring(0, name.indexOf('|')));
061: //System.err.println("Found composite name, set it to " + this.name);
062: name = name.substring(name.indexOf('|') + 1);
063: String lat = name.substring(0, name.indexOf('|'));
064: String lon = name.substring(name.indexOf('|') + 1);
065: //System.err.println("lat will be: " + lat + ", long: " + lon);
066: float flat = 0.0f;
067: float flon = 0.0f;
068: try {
069: flat = Float.parseFloat(lat);
070: flon = Float.parseFloat(lon);
071: } catch (NumberFormatException nfe) {
072: }
073: this .setPosition(new LatLonPoint(flat, flon));
074: //System.err.println("Got position: " + getPosition());
075: }
076: }
077:
078: /**
079: * Creates a new <code>NamedLocationImpl</code> instance.
080: *
081: * @param pos a <code>LatLonPoint</code> position
082: */
083: public NamedLocationImpl(LatLonPoint pos) {
084: this .setPosition(pos);
085: }
086:
087: /**
088: * Creates a new <code>NamedLocationImpl</code> instance.
089: *
090: * @param name a <code>String</code> name
091: * @param pos a <code>LatLonPoint</code>
092: */
093: public NamedLocationImpl(String name, LatLonPoint pos) {
094: this .setPosition(pos);
095: this .setName(name);
096: }
097:
098: /**
099: * Creates a new <code>NamedLocationImpl</code> instance.
100: *
101: * @param name a <code>String</code> name
102: * @param lat a <code>float</code> latitude in degrees
103: * @param lon a <code>float</code> longitude in degrees
104: */
105: public NamedLocationImpl(String name, float lat, float lon) {
106: this .setPosition(new LatLonPoint(lat, lon));
107: this .setName(name);
108: }
109:
110: /**
111: * Creates a new <code>NamedLocationImpl</code> instance.
112: *
113: * @param name a <code>String</code> location name
114: * @param lat a <code>float</code> latitude in radians
115: * @param lon a <code>float</code> longitdue in radians
116: * @param isRadian a <code>boolean</code> that should be true and is always ignored
117: */
118: public NamedLocationImpl(String name, float lat, float lon,
119: boolean isRadian) {
120: this .setPosition(new LatLonPoint(lat, lon, true));
121: this .setName(name);
122: }
123:
124: /**
125: * Creates a new <code>NamedLocationImpl</code> instance.
126: *
127: * @param pt a <code>NamedLocation</code> to copy
128: */
129: public NamedLocationImpl(NamedLocation pt) {
130: this .setPosition(pt.getPosition());
131: this .setName(pt.getName());
132: }
133:
134: /**
135: * Creates a new <code>NamedLocationImpl</code> instance.
136: *
137: * @param name a <code>String</code> name
138: * @param lat a <code>double</code> latitude in degrees
139: * @param lon a <code>double</code> longitude in degrees
140: */
141: public NamedLocationImpl(String name, double lat, double lon) {
142: this .setPosition(new LatLonPoint(lat, lon));
143: this .setName(name);
144: }
145:
146: /**
147: * Get the name of this location, possibly null
148: *
149: * @return a <code>String</code> location name, possibly null
150: */
151: public String getName() {
152: return name;
153: }
154:
155: /**
156: * Set the name of this location, overriding any previous name
157: *
158: * @param name a <code>String</code> location name
159: */
160: public void setName(String name) {
161: this .name = name;
162: }
163:
164: /**
165: * Get the position of this location
166: *
167: * @return a <code>LatLonPoint</code>
168: */
169: public LatLonPoint getPosition() {
170: return pos;
171: }
172:
173: /**
174: * Set the position of this location
175: *
176: * @param pos a <code>LatLonPoint</code>
177: */
178: public void setPosition(LatLonPoint pos) {
179: this .pos = pos;
180: }
181:
182: /**
183: * Get the distance in kilometers from this point to the given point
184: *
185: * @param other a <code>LatLonPoint</code> to find the distance to
186: * @return a <code>double</code> distance in kilometers
187: */
188: public double distanceFrom(LatLonPoint other) {
189: return EarthConstants.DistanceBetweenLatLonPoints(this
190: .getPosition(), other);
191: }
192:
193: /**
194: * Get the distance in kilometers from this point to the given point
195: *
196: * @param other a <code>NamedLocation</code> value
197: * @return a <code>double</code> distance in kilometers
198: */
199: public double distanceFrom(NamedLocation other) {
200: return EarthConstants.DistanceBetweenLatLonPoints(this
201: .getPosition(), other.getPosition());
202: }
203:
204: /**
205: * Get the distance in kilometers from this point to the given point
206: *
207: * @param lat a <code>double</code> latitude in degrees
208: * @param lon a <code>double</code> longitude in degrees
209: * @return a <code>float</code> distance in kilometers
210: */
211: public float distanceFrom(double lat, double lon) {
212: return EarthConstants.DistanceBetweenPoints(this .getPosition()
213: .getLatitude(), this .getPosition().getLongitude(),
214: (float) lat, (float) lon);
215: }
216:
217: /**
218: * Get the distance in kilometers from this point to the given point
219: *
220: * @param lat a <code>float</code> latitude in degrees
221: * @param lon a <code>float</code> longitude in degrees
222: * @return a <code>float</code> distance in kilometers
223: */
224: public float distanceFrom(float lat, float lon) {
225: return EarthConstants.DistanceBetweenPoints(this .getPosition()
226: .getLatitude(), this .getPosition().getLongitude(), lat,
227: lon);
228: }
229:
230: /**
231: * 2 <code>NamedLocationImpl</code>s are <code>equals</code> if they have the same name
232: * The position is ignored for this purpose.
233: *
234: * @param obj an <code>Object</code> to compare
235: * @return a <code>boolean</code>
236: */
237: public boolean equals(Object obj) {
238: if (obj instanceof NamedLocation) {
239: return getName().equals(((NamedLocation) obj).getName());
240: }
241: return false;
242: }
243:
244: public String toString() {
245: return "NamedLocationImpl[name=" + name + ", pos=" + pos + "]";
246: }
247: } // NamedLocationImpl.java
|