java版坐标系转换(火星坐标、GPS坐标、百度坐标)

public class Gps {


public double wgLat;
public double wgLon;


public Gps(double wgLat, double wgLon) {
setWgLat(wgLat);
setWgLon(wgLon);
}


public double getWgLat() {
return wgLat;
}


public void setWgLat(double wgLat) {
this.wgLat = wgLat;
}


public double getWgLon() {
return wgLon;
}


public void setWgLon(double wgLon) {
this.wgLon = wgLon;
}


@Override
public String toString() {
return wgLat + "," + wgLon;
}

}

/**
 * 坐标转换工具
 * 
 * @author Administrator
 *
 */
public class CoordinateUtil {


private static double pi = 3.14159265358979324;
private static double a = 6378245.0;
private static double ee = 0.00669342162296594323;
private static double x_pi = 3.14159265358979324 * 3000.0 / 180.0;


private static boolean outOfChina(double lat, double lon) {
if (lon < 72.004 || lon > 137.8347)
return true;
if (lat < 0.8293 || lat > 55.8271)
return true;
return false;
}


private static double transformLat(double x, double y) {
double ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * Math.sqrt(Math.abs(x));
ret += (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0 / 3.0;
ret += (20.0 * Math.sin(y * pi) + 40.0 * Math.sin(y / 3.0 * pi)) * 2.0 / 3.0;
ret += (160.0 * Math.sin(y / 12.0 * pi) + 320 * Math.sin(y * pi / 30.0)) * 2.0 / 3.0;
return ret;
}


private static double transformLon(double x, double y) {
double ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * Math.sqrt(Math.abs(x));
ret += (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0 / 3.0;
ret += (20.0 * Math.sin(x * pi) + 40.0 * Math.sin(x / 3.0 * pi)) * 2.0 / 3.0;
ret += (150.0 * Math.sin(x / 12.0 * pi) + 300.0 * Math.sin(x / 30.0 * pi)) * 2.0 / 3.0;
return ret;
}


/**
* 地球坐标转换为火星坐标 World Geodetic System ==> Mars Geodetic System

* @param wgLat
*            地球坐标
* @param wgLon

*            mglat,mglon 火星坐标
*/
public static Gps transform2Mars(double wgLat, double wgLon) {
double mgLat, mgLon;
if (outOfChina(wgLat, wgLon)) {
mgLat = wgLat;
mgLon = wgLon;
return new Gps(mgLat, mgLon);
}
double dLat = transformLat(wgLon - 105.0, wgLat - 35.0);
double dLon = transformLon(wgLon - 105.0, wgLat - 35.0);
double radLat = wgLat / 180.0 * pi;
double magic = Math.sin(radLat);
magic = 1 - ee * magic * magic;
double sqrtMagic = Math.sqrt(magic);
dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);
dLon = (dLon * 180.0) / (a / sqrtMagic * Math.cos(radLat) * pi);
mgLat = wgLat + dLat;
mgLon = wgLon + dLon;
return new Gps(mgLat, mgLon);
}


/**
* 火星坐标转换为百度坐标

* @param gg_lat,gg_lon
*            火星坐标
* @param bd_lat,bd_lon百度坐标
*/
public static Gps bd_encrypt(double gg_lat, double gg_lon) {
double bd_lat, bd_lon;
double x = gg_lon, y = gg_lat;
double z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * x_pi);
double theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * x_pi);
bd_lon = z * Math.cos(theta) + 0.0065;
bd_lat = z * Math.sin(theta) + 0.006;
return new Gps(bd_lat, bd_lon);
}


/**
* 百度转火星

* @param bd_lat,bd_lon百度坐标
* @param gg_lat,gg_lon
*            火星坐标
*/
public static Gps bd_decrypt(double bd_lat, double bd_lon) {
double gg_lat, gg_lon;
double x = bd_lon - 0.0065, y = bd_lat - 0.006;
double z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi);
double theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi);
gg_lon = z * Math.cos(theta);
gg_lat = z * Math.sin(theta);
return new Gps(gg_lat, gg_lon);
}


public static void main(String[] args) {
double lat = 40.051372861849735;
double lon = 116.30498699312274;
Gps g1 = bd_decrypt(lat, lon);
System.out.println(g1);
}
}

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python中百度坐标系坐标转换可以通过使用第三方库进行实现。一个常用的库是百度地图API的Python SDK,叫做BaiduMapAPI。 要使用BaiduMapAPI进行百度坐标系转换,首先需要安装该库。可以通过在命令行中运行以下指令来安装BaiduMapAPI: ``` pip install BaiduMapAPI ``` 安装完成后,在Python代码中引入BaiduMapAPI库,即可使用其中的转换功能。 下面是一个简单的示例代码,展示了如何使用BaiduMapAPI进行百度坐标系转换: ```python from BaiduMapAPI import convert_coords # 将GPS坐标系的经纬度转换为百度坐标系的经纬度 gps_coords = [(39.1234, 116.5678)] baidu_coords = convert_coords(gps_coords, from_coord='gps', to_coord='bd09') # 将百度坐标系的经纬度转换火星坐标系的经纬度 baidu_coords = [(39.1234, 116.5678)] mars_coords = convert_coords(baidu_coords, from_coord='bd09', to_coord='gcj02') # 将百度坐标系的经纬度转换GPS坐标系的经纬度 baidu_coords = [(39.1234, 116.5678)] gps_coords = convert_coords(baidu_coords, from_coord='bd09', to_coord='gps') ``` 以上示例中,首先通过调用`convert_coords()`函数将GPS坐标系的经纬度转换为百度坐标系的经纬度,参数`from_coord`指定了转换前的坐标系,参数`to_coord`指定了转换后的坐标系。同样的方法可以用于其他坐标系转换。 需要注意的是,在使用BaiduMapAPI进行坐标系转换时,需要提前申请并配置好百度地图API的开发者密钥,以确保API请求正常。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值