/*
This file is part of OpenSatNav.
OpenSatNav is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenSatNav is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with OpenSatNav. If not, see <http://www.gnu.org/licenses/>.
*/
// Created by plusminus on 21:28:12 - 25.09.2008
package org.andnav.osm.util;
import org.andnav.osm.util.constants.GeoConstants;
import org.andnav.osm.views.util.constants.MathConstants;
/**
*
* @author Nicolas Gramlich
*
*/
public class GeoPoint implements MathConstants, GeoConstants{
// ===========================================================
// Constants
// ===========================================================
// ===========================================================
// Fields
// ===========================================================
private int mLongitudeE6;
private int mLatitudeE6;
// ===========================================================
// Constructors
// ===========================================================
public GeoPoint(final int aLatitudeE6, final int aLongitudeE6) {
this.mLatitudeE6 = aLatitudeE6;
this.mLongitudeE6 = aLongitudeE6;
}
public static GeoPoint fromDoubleString(final String s, final char spacer) {
final int spacerPos = s.indexOf(spacer);
return new GeoPoint((int) (Double.parseDouble(s.substring(0,
spacerPos)) * 1E6), (int) (Double.parseDouble(s.substring(
spacerPos + 1, s.length())) * 1E6));
}
public static GeoPoint fromIntString(final String s){
final int commaPos = s.indexOf(',');
return new GeoPoint(Integer.parseInt(s.substring(0,commaPos)),
Integer.parseInt(s.substring(commaPos+1,s.length())));
}
// ===========================================================
// Getter & Setter
// ===========================================================
public int getLongitudeE6() {
return this.mLongitudeE6;
}
public int getLatitudeE6() {
return this.mLatitudeE6;
}
public void setLongitudeE6(final int aLongitudeE6) {
this.mLongitudeE6 = aLongitudeE6;
}
public void setLatitudeE6(final int aLatitudeE6) {
this.mLatitudeE6 = aLatitudeE6;
}
public void setCoordsE6(final int aLatitudeE6, final int aLongitudeE6) {
this.mLatitudeE6 = aLatitudeE6;
this.mLongitudeE6 = aLongitudeE6;
}
// ===========================================================
// Methods from SuperClass/Interfaces
// ===========================================================
@Override
public String toString(){
return new StringBuilder().append(this.mLatitudeE6).append(",").append(this.mLongitudeE6).toString();
}
public String toDoubleString() {
return new StringBuilder().append(this.mLatitudeE6 / 1E6).append(",").append(this.mLongitudeE6 / 1E6).toString();
}
@Override
public boolean equals(Object obj) {
if(!(obj instanceof GeoPoint))
return false;
GeoPoint g = (GeoPoint)obj;
return g.mLatitudeE6 == this.mLatitudeE6 && g.mLongitudeE6 == this.mLongitudeE6;
}
// ===========================================================
// Methods
// ===========================================================
/**
* @see Source@ http://www.geocities.com/DrChengalva/GPSDistance.html
* @param gpA
* @param gpB
* @return distance in meters
*/
public int distanceTo(final GeoPoint other) {
final double a1 = DEG2RAD * (this.mLatitudeE6 / 1E6);
final double a2 = DEG2RAD * (this.mLongitudeE6 / 1E6);
final double b1 = DEG2RAD * (other.mLatitudeE6 / 1E6);
final double b2 = DEG2RAD * (other.mLongitudeE6 / 1E6);
final double cosa1 = Math.cos(a1);
final double cosb1 = Math.cos(b1);
final double t1 = cosa1*Math.cos(a2)*cosb1*Math.cos(b2);
final double t2 = cosa1*Math.sin(a2)*cosb1*Math.sin(b2);
final double t3 = Math.sin(a1)*Math.sin(b1);
final double tt = Math.acos( t1 + t2 + t3 );
return (int)(RADIUS_EARTH_METERS*tt);
}
// ===========================================================
// Inner and Anonymous Classes
// ===========================================================
}