/*
 * Created on 2005-8-30
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.telenav.more.tool.coordinate;

/**
 * @author lnie
 * 
 * TODO To change the template for this generated type comment go to Window -
 * Preferences - Java - Code Style - Code Templates
 */
public class EarthPos {
	private static final double DDS_SCALE_FACTOR = 100000D;

	private static final float DDS_FSCALE_FACTOR = 100000F;

	private int longitude;

	private int latitude;

	public EarthPos(double lon, double lat) {
		if (lon > 0) {
			longitude = (int) (lon * DDS_SCALE_FACTOR + 0.5);
		} else {
			longitude = (int) (lon * DDS_SCALE_FACTOR - 0.5);
		}
		if (lat > 0) {
			latitude = (int) (lat * DDS_SCALE_FACTOR + 0.5);
		} else {
			latitude = (int) (lat * DDS_SCALE_FACTOR - 0.5);
		}
	}
	
	public EarthPos(String value){
		String[] arr = value.split(",");
		double lat  = Double.parseDouble(arr[0]);
		double lon  = Double.parseDouble(arr[1]);
		
		if (lon > 0) {
			longitude = (int) (lon * DDS_SCALE_FACTOR + 0.5);
		} else {
			longitude = (int) (lon * DDS_SCALE_FACTOR - 0.5);
		}
		if (lat > 0) {
			latitude = (int) (lat * DDS_SCALE_FACTOR + 0.5);
		} else {
			latitude = (int) (lat * DDS_SCALE_FACTOR - 0.5);
		}
		
	}
	
	public EarthPos(int lon, int lat){
		longitude = lon;
		latitude = lat;
	}

	public int getILon() {
		return longitude;
	}

	public int getILat() {
		return latitude;
	}

	public double getLongitude() {
		return 1.0 * longitude / DDS_SCALE_FACTOR;
	}

	public double getLatitude() {
		return 1.0 * latitude / DDS_SCALE_FACTOR;
	}

	public String getLongitudeString1() {
		return "" + (1.0f * longitude / DDS_FSCALE_FACTOR);
	}

	public String getLatitudeString1() {
		return "" + (1.0f * latitude / DDS_FSCALE_FACTOR);
	}

	public String getLongitudeString() {
		String str = "" + (1.0d * longitude / DDS_FSCALE_FACTOR) + "0000000";
		int pos = str.indexOf(".");
		int endPos = pos + 7;
		return str.substring(0, endPos);
	}

	public String getLatitudeString() {
		String str = "" + (latitude / DDS_FSCALE_FACTOR) + "0000000";
		int pos = str.indexOf(".");
		int endPos = pos + 7;
		return str.substring(0, endPos);
	}

	public GlobalPos convert2GlobalPos() {
		double posX = 1.0 * (1.0 * longitude / DDS_SCALE_FACTOR + 180) / 360;
		double posY = Math.log(Math
				.tan((1.0 * latitude / DDS_SCALE_FACTOR + 90) * Math.PI / 360));
		posY = 0.5 - posY / 2 / Math.PI;
		// System.out.println("lon:" + longitude + " Lat:" + latitude +
		// " [posX:" + posX + " posY:" + posY + "]");
		return new GlobalPos(posX, posY);
	}

	// public String getLonLatString() {
	// return "Lon:" + getLongitudeString() + " Lat:" + getLatitudeString();
	// }
	public String toString() {
		return getLatitude() + "," + getLongitude();
	}

	public boolean equals(EarthPos p){
		if (p==null) {
			return false;
		}
		return p.latitude == latitude && p.longitude == longitude;
	}

	public static void main(String[] args) {
		EarthPos ePos = new EarthPos(-100203509, 43912250);
		System.out.println(ePos.getLatitudeString());
		System.out.println(ePos.getLongitudeString());
	}

}
