Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrated coordTransform to a Java version #32

Open
graphysics opened this issue May 7, 2023 · 0 comments
Open

Migrated coordTransform to a Java version #32

graphysics opened this issue May 7, 2023 · 0 comments

Comments

@graphysics
Copy link

Tried to migrate coordTransform to a Java version, only the wgs84_to_bd09 part.
Please wandergis can merge in your project if wanted.

package com.xxx.xxxworld.util;
Geocoding.zip

public class Geocoding {
static final double x_pi = 3.14159265358979324 * 3000.0 / 180.0;
static final double pi = 3.1415926535897932384626;
static final double a = 6378245.0; // 长半轴
static final double ee = 0.00669342162296594323; // 偏心率平方

static double[] wgs84_to_gcj02(double lng, double lat) {

// WGS84转GCJ02(火星坐标系)
// :param lng:WGS84坐标系的经度
// :param lat:WGS84坐标系的纬度
// :return:
// """
if(out_of_china(lng, lat)) //判断是否在国内
return new double[]{ lng, lat};
double dlat = _transformlat(lng - 105.0, lat - 35.0);
double dlng = _transformlng(lng - 105.0, lat - 35.0);
double radlat = lat / 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);
dlng = (dlng * 180.0) / (a / sqrtmagic * Math.cos(radlat) * pi);
double mglat = lat + dlat;
double mglng = lng + dlng;
return new double[]{mglng, mglat};
}
static double[] gcj02_to_bd09(double lng, double lat) {
// """
// 火星坐标系(GCJ-02)转百度坐标系(BD-09)
// 谷歌、高德——>百度
// :param lng:火星坐标经度
// :param lat:火星坐标纬度
// :return:
// """
double z = Math.sqrt(lng * lng + lat * lat) + 0.00002 * Math.sin(lat * x_pi);
double theta = Math.atan2(lat, lng) + 0.000003 * Math.cos(lng * x_pi);
double bd_lng = z * Math.cos(theta) + 0.0065;
double bd_lat = z * Math.sin(theta) + 0.006;
return new double[]{bd_lng, bd_lat};
}
public static double[] wgs84_to_bd09(double lon, double lat) {
double[] gcj02 = wgs84_to_gcj02(lon, lat);
return gcj02_to_bd09(gcj02[0], gcj02[1]);
}
static boolean out_of_china(double lng, double lat) {
// """
// 判断是否在国内,不在国内不做偏移
// :param lng:
// :param lat:
// :return:
// """
return !(lng > 73.66 && lng < 135.05 && lat > 3.86 && lat < 53.55);
}
static double _transformlat(double lng, double lat) {
double ret = -100.0 + 2.0 * lng + 3.0 * lat + 0.2 * lat * lat +
0.1 * lng * lat + 0.2 * Math.sqrt(Math.abs(lng));
ret += (20.0 * Math.sin(6.0 * lng * pi) + 20.0 *
Math.sin(2.0 * lng * pi)) * 2.0 / 3.0;
ret += (20.0 * Math.sin(lat * pi) + 40.0 *
Math.sin(lat / 3.0 * pi)) * 2.0 / 3.0;
ret += (160.0 * Math.sin(lat / 12.0 * pi) + 320 *
Math.sin(lat * pi / 30.0)) * 2.0 / 3.0;
return ret;
}

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

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant