Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (c) OSGi Alliance (2002, 2010). All Rights Reserved.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 package org.osgi.util.position;
 
Position represents a geographic location, based on the WGS84 System (World Geodetic System 1984).

The org.osgi.util.measurement.Measurement class is used to represent the values that make up a position.

A given position object may lack any of it's components, i.e. the altitude may not be known. Such missing values will be represented by null.

Position does not override the implementation of either equals() or hashCode() because it is not clear how missing values should be handled. It is up to the user of a position to determine how best to compare two position objects. A Position object is immutable.

Version:
$Id: 85f546ae0d82caa22d370a8dff0aeea2a595efa5 $
Immutable:
 
 public class Position {
 	private final Measurement	altitude;
 	private final Measurement	longitude;
 	private final Measurement	latitude;
 	private final Measurement	speed;
 	private final Measurement	track;

Constructs a Position object with the given values.

Parameters:
lat a Measurement object specifying the latitude in radians, or null
lon a Measurement object specifying the longitude in radians, or null
alt a Measurement object specifying the altitude in meters, or null
speed a Measurement object specifying the speed in meters per second, or null
track a Measurement object specifying the track in radians, or null
 
 	public Position(Measurement latMeasurement lonMeasurement alt,
 			Measurement speedMeasurement track) {
 		if (lat != null) {
 			if (!..equals(lat.getUnit())) {
 				throw new IllegalArgumentException("Invalid Latitude");
 			}
 		}
 		if (lon != null) {
 			if (!..equals(lon.getUnit())) {
 				throw new IllegalArgumentException("Invalid Longitude");
 			}
 		}
 		if (alt != null) {
 			if (!..equals(alt.getUnit())) {
 				throw new IllegalArgumentException("Invalid Altitude");
 			}
 		}
 		if (speed != null) {
 			if (!..equals(speed.getUnit())) {
 				throw new IllegalArgumentException("Invalid Speed");
 			}
 		}
 		if (track != null) {
 			if (!..equals(track.getUnit())) {
 				throw new IllegalArgumentException("Invalid Track");
 			}
 		}
 
 		/*
 		 * Verify the longitude and latitude parameters so they fit the normal
 		 * coordinate system. A latitude is between -90 (south) and +90 (north).
 		 * A longitude is between -180 (Western hemisphere) and +180 (eastern
 		 * hemisphere). This method first normalizes the latitude and longitude
 		 * between +/- 180. If the |latitude| > 90, then the longitude is added
 		 * 180 and the latitude is normalized to fit +/-90. (Example are with
 		 * degrees though radians are used) <br> No normalization takes place
 		 * when either lon or lat is null.
 		 */
 		normalizeLatLon: {
			if (lat == null || lon == null) {
				break normalizeLatLon;
			}
			double dlat = lat.getValue();
			double dlon = lon.getValue();
			if (dlon >= - && dlon <  && dlat >= -
					&& dlat <= ) {
				break normalizeLatLon;
			}
			dlon = normalize(dlon);
			dlat = normalize(dlat * 2.0D); // First over 180 degree
			// Check if we have to move to other side of the earth
			if (dlat >  || dlat < -) {
				dlon = normalize(dlon - );
				dlat = normalize(( * 2.0D) - dlat);
			}
			lon = new Measurement(dlonlon.getError(), lon.getUnit(), lon
			lat = new Measurement(dlatlat.getError(), lat.getUnit(), lat
		}
		/*
		 * Normalize track to be a value such that: 0 <= value < +2PI. This
		 * corresponds to 0 deg to +360 deg. 0 is North, 0.5PI is East, PI is
		 * South, 1.5PI is West
		 */
		normalizeTrack: {
			if (track == null) {
				break normalizeTrack;
			}
			double dtrack = track.getValue();
			if ((0.0D <= dtrack) && (dtrack < )) {
				break normalizeTrack; /* value is already normalized */
			}
			dtrack %= ;
			if (dtrack < 0.0D) {
				dtrack += ;
			}
			track = new Measurement(dtracktrack.getError(), track.getUnit(),
					track.getTime());
		}
		this. = lat;
		this. = lon;
		this. = alt;
		this. = speed;
		this. = track;
	}

Returns the altitude of this position in meters.

Returns:
a Measurement object in Unit.m representing the altitude in meters above the ellipsoid null if the altitude is not known.
		return ;
	}

Returns the longitude of this position in radians.

Returns:
a Measurement object in Unit.rad representing the longitude, or null if the longitude is not known.
		return ;
	}

Returns the latitude of this position in radians.

Returns:
a Measurement object in Unit.rad representing the latitude, or null if the latitude is not known..
		return ;
	}

Returns the ground speed of this position in meters per second.

Returns:
a Measurement object in Unit.m_s representing the speed, or null if the speed is not known..
	public Measurement getSpeed() {
		return ;
	}

Returns the track of this position in radians as a compass heading. The track is the extrapolation of previous previously measured positions to a future position.

Returns:
a Measurement object in Unit.rad representing the track, or null if the track is not known..
	public Measurement getTrack() {
		return ;
	}
	private static final double	LON_RANGE.;
	private static final double	LAT_RANGE. / 2.0D;

This function normalizes the a value according to a range. This is not simple modulo (as I thought when I started), but requires some special handling. For positive numbers we subtract 2*range from the number so that end up between -/+ range. For negative numbers we add this value. For example, if the value is 270 and the range is +/- 180. Then sign=1 so the (int) factor becomes 270+180/360 = 1. This means that 270-360=-90 is the result. (degrees are only used to make it easier to understand, this function is agnostic for radians/degrees). The result will be in [range,range> The algorithm is not very fast, but it handling the [> ranges made it very messy using integer arithmetic, and this is very readable. Note that it is highly unlikely that this method is called in normal situations. Normally input values to position are already normalized because they come from a GPS. And this is much more readable.

Parameters:
value The value that needs adjusting
range -range = < value < range
	private static double normalize(double valuedouble range) {
		double twiceRange = 2.0D * range;
		while (value >= range) {
			value -= twiceRange;
		}
		while (value < -range) {
			value += twiceRange;
		}
		return value;
	}
	private static final double	TRACK_RANGE. * 2.0D;
New to GrepCode? Check out our FAQ X