Java转换坐标系,GPS(WGS84)大地200(CGCS200)、百度(BD-09)、高德(GCJ-02)互转,一文搞懂坐标系、坐标转换

1.分不清的坐标系
WG-S84: 地理坐标系统,GPS仪器记录的经纬度信息,Google Earth采用,Google Map中国范围外使用,高德地图中国范围外使用。 GCJ-02: 投影坐标系统,火星坐标系,中国国家测绘局制定的坐标系统,由WGS-84加密后的坐标。Google中国和搜搜地图,arcgis地图,高德地图 BD-09: 投影坐标系统,百度坐标,GCJ-02加密后的坐标系,只适用于百度地图

(在国内是不允许直接用WGS84坐标系标注的,必须经过加密后才能用。必须至少使用GCJ-02坐标系,或者使用在GCJ-02加密后再进行加密的坐标系,如百度坐标系)

其他:搜狗地图:搜狗坐标系,图吧:图吧坐标等,估计也是在GCJ02基础上加密而成的,这里暂不涉及

2.直接上代码

package ft.util.gps;
// 导入Proj4J库

import org.locationtech.proj4j.*;
import org.locationtech.proj4j.io.Proj4FileReader;

import java.io.IOException;

/**
 * 百度坐标(BD09)、国测局坐标(火星坐标,GCJ02)、和WGS84坐标系之间的转换的工具
 * <p>
 * 参考 https://github.com/wandergis/coordtransform 实现的Java版本
 *
 * @author geosmart
 */
public class CoordinateTransformUtil {
    static double x_pi = 3.14159265358979324 * 3000.0 / 180.0;
    // π
    static double pi = 3.1415926535897932384626;
    // 长半轴
    static double a = 6378245.0;
    // 扁率
    static double ee = 0.00669342162296594323;
    /**
     * 百度坐标系(BD-09)转WGS坐标
     *
     * @param lng 百度坐标纬度
     * @param lat 百度坐标经度
     * @return WGS84坐标数组
     */
    public static PointXY bd09towgs84(double lng, double lat) {
        PointXY gcj = bd09togcj02(lng, lat);
        PointXY wgs84 = gcj02towgs84(gcj.getLon(), gcj.getLat());
        return wgs84;
    }
    /**
     * WGS坐标转百度坐标系(BD-09)
     *
     * @param lng WGS84坐标系的经度
     * @param lat WGS84坐标系的纬度
     * @return 百度坐标数组
     */
    public static PointXY wgs84tobd09(double lng, double lat) {
        PointXY gcj = wgs84togcj02(lng, lat);
        PointXY bd09 = gcj02tobd09(gcj.getLon(), gcj.getLat());
        return bd09;
    }
    /**
     * 火星坐标系(GCJ-02)转百度坐标系(BD-09)
     * <p>
     * 谷歌、高德——>百度
     *
     * @param lng 火星坐标经度
     * @param lat 火星坐标纬度
     * @return 百度坐标数组
     */
    public static PointXY gcj02tobd09(double lng, double lat) {
        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 PointXY(bd_lng, bd_lat);
    }

    /**
     * 百度坐标系(BD-09)转火星坐标系(GCJ-02)
     * <p>
     * 百度——>谷歌、高德
     *
     * @param bd_lon 百度坐标纬度
     * @param bd_lat 百度坐标经度
     * @return 火星坐标数组
     */
    public static PointXY bd09togcj02(double bd_lon, double bd_lat) {
        double x = bd_lon - 0.0065;
        double 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);
        double gg_lng = z * Math.cos(theta);
        double gg_lat = z * Math.sin(theta);
        return new PointXY(gg_lng, gg_lat);
    }
    /**
     * WGS84转GCJ02(火星坐标系)
     *
     * @param lng WGS84坐标系的经度
     * @param lat WGS84坐标系的纬度
     * @return 火星坐标数组
     */
    public static PointXY wgs84togcj02(double lng, double lat) {
        if (out_of_china(lng, lat)) {
            return new PointXY(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 PointXY(mglng, mglat);
    }
    /**
     * GCJ02(火星坐标系)转GPS84
     *
     * @param lng 火星坐标系的经度
     * @param lat 火星坐标系纬度
     * @return WGS84坐标数组
     */
    public static PointXY gcj02towgs84(double lng, double lat) {
        if (out_of_china(lng, lat)) {
            return new PointXY(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 PointXY(lng * 2 - mglng, lat * 2 - mglat);
    }
    /**
     * 纬度转换
     *
     * @param lng
     * @param lat
     * @return
     */
    public 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;
    }
    /**
     * 经度转换
     *
     * @param lng
     * @param lat
     * @return
     */
    public 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;
    }
    /**
     * 判断是否在国内,不在国内不做偏移
     *
     * @param lng
     * @param lat
     * @return
     */
    public static boolean out_of_china(double lng, double lat) {
        if (lng < 72.004 || lng > 137.8347) {
            return true;
        } else if (lat < 0.8293 || lat > 55.8271) {
            return true;
        }
        return false;
    }

   

    public static PointXY wgs2cgcg(double lon, double lat) {
        Proj4FileReader proj4FileReader = new Proj4FileReader();
        String[] paramStr = new String[0];
        try {
            paramStr = proj4FileReader.readParametersFromFile("epsg","4528");
        } catch (IOException e) {
            e.printStackTrace();
        }

        CRSFactory targetFactory = new CRSFactory();
        //目标坐标系统
        CoordinateReferenceSystem cgcs = targetFactory.createFromParameters("4528", paramStr);

        //源坐标系统
        CRSFactory crsFactory = new CRSFactory();
        String wgs84_param = "+title=long/lat:WGS84 +proj=longlat +ellps=WGS84 +datum=WGS84 +units=degress";
        CoordinateReferenceSystem wgs84 = crsFactory.createFromParameters("WGS84", wgs84_param);

        CoordinateTransformFactory ctf = new CoordinateTransformFactory();
        CoordinateTransform transform = ctf.createTransform(wgs84, cgcs);

        ProjCoordinate projCoordinate = new ProjCoordinate(lon,lat);
        transform.transform(projCoordinate,projCoordinate);


        return new PointXY(projCoordinate.x,projCoordinate.y);
    }
    public static PointXY cgcg2wgs(double lon, double lat) {
        Proj4FileReader proj4FileReader = new Proj4FileReader();
        String[] paramStr = new String[0];
        try {
            paramStr = proj4FileReader.readParametersFromFile("epsg","4528");
        } catch (IOException e) {
            e.printStackTrace();
        }

        CRSFactory targetFactory = new CRSFactory();
        //目标坐标系统
        CoordinateReferenceSystem cgcs = targetFactory.createFromParameters("4528", paramStr);

        //源坐标系统
        CRSFactory crsFactory = new CRSFactory();
        String wgs84_param = "+title=long/lat:WGS84 +proj=longlat +ellps=WGS84 +datum=WGS84 +units=degress";
        CoordinateReferenceSystem wgs84 = crsFactory.createFromParameters("WGS84", wgs84_param);

        CoordinateTransformFactory ctf = new CoordinateTransformFactory();
        CoordinateTransform transform = ctf.createTransform(cgcs,wgs84);

        ProjCoordinate projCoordinate = new ProjCoordinate(lon,lat);
        transform.transform(projCoordinate,projCoordinate);


        return new PointXY(projCoordinate.x,projCoordinate.y);
    }


    public static void main(String[] args) {
        // 坐标转化api
        // https://tool.lu/coordinate/?ivk_sa=1024320u
        //
        System.out.println(wgs84togcj02(118.840344,31.99335));   // ok
        System.out.println(gcj02towgs84(107.80473041151717, 29.33190938671697));  // ok
        System.out.println(gcj02tobd09(107.80473041151717, 29.33190938671697));   // ok
        System.out.println(wgs84tobd09(107.8003647, 29.3347596));    // ok
        System.out.println(bd09togcj02(107.81129990277971, 29.337686168994296));
        System.out.println(bd09towgs84(107.81129990277971, 29.337686168994296));


        System.out.println(wgs2cgcg(118.70331743312532,32.103750473727125));
//        System.out.println(bd09towgs84(120.24794,29.72979));
//gcj02towgs84
//        System.out.println(gcj02tobd09(120.236701, 29.726141));
//
        PointXY p = wgs2cgcg(118.70331743312532,32.103750473727125);
        System.out.println(p);
        System.out.println(cgcg2wgs(p.getLon(), p.getLat()));



    }
}`在这里插入代码片`

测试结果
lon:118.84543348019479,lat:31.99121178083339
lon:107.80037405167121,lat:29.334768526991336
lon:107.81129968211874,lat:29.337686107444082
lon:107.81129968211874,lat:29.337686107444082
lon:107.80473019794286,lat:29.33191023473475
lon:107.80037383758244,lat:29.33476937412017
lon:4.0377605981347054E7,lat:3554093.2140468676
lon:4.0377605981347054E7,lat:3554093.2140468676
lon:118.70331743312477,lat:32.10375047372694

  • 6
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
Java实现WGS84CGCS2000坐标系的相互投影转换,可以使用Proj4J库来实现。以下是一个简单的示例代码: 首先,需要下载Proj4J库并将其添加到Java项目中。 然后,可以使用以下代码将WGS84坐标系经纬坐标转换CGCS2000坐标系的平面坐标: ``` import org.osgeo.proj4j.CoordinateReferenceSystem; import org.osgeo.proj4j.CRSFactory; import org.osgeo.proj4j.ProjCoordinate; public class WGS84toCGCS2000 { public static void main(String[] args) { // 定义WGS84坐标系 String wgs84Str = "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"; CRSFactory crsFactory = new CRSFactory(); CoordinateReferenceSystem wgs84 = crsFactory.createFromParameters("WGS84", wgs84Str); // 定义CGCS2000坐标系 String cgcs2000Str = "+proj=utm +zone=50 +ellps=GRS80 +units=m +no_defs"; CoordinateReferenceSystem cgcs2000 = crsFactory.createFromParameters("CGCS2000", cgcs2000Str); // 定义WGS84坐标点 ProjCoordinate wgs84Point = new ProjCoordinate(116.3975, 39.9085); // 将WGS84坐标点转换CGCS2000坐标系下的平面坐标 ProjCoordinate cgcs2000Point = new ProjCoordinate(); crsFactory.createFromCoordinateReferenceSystem(wgs84).getCoordinateOperationFactory() .createOperation(wgs84, cgcs2000).getMathTransform().transform(wgs84Point, cgcs2000Point); System.out.println(cgcs2000Point.x + " " + cgcs2000Point.y); } } ``` 同样,可以使用以下代码将CGCS2000坐标系的平面坐标转换WGS84坐标系经纬度坐标: ``` import org.osgeo.proj4j.CoordinateReferenceSystem; import org.osgeo.proj4j.CRSFactory; import org.osgeo.proj4j.ProjCoordinate; public class CGCS2000toWGS84 { public static void main(String[] args) { // 定义WGS84坐标系 String wgs84Str = "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"; CRSFactory crsFactory = new CRSFactory(); CoordinateReferenceSystem wgs84 = crsFactory.createFromParameters("WGS84", wgs84Str); // 定义CGCS2000坐标系 String cgcs2000Str = "+proj=utm +zone=50 +ellps=GRS80 +units=m +no_defs"; CoordinateReferenceSystem cgcs2000 = crsFactory.createFromParameters("CGCS2000", cgcs2000Str); // 定义CGCS2000坐标点 ProjCoordinate cgcs2000Point = new ProjCoordinate(397829.29, 4407529.65); // 将CGCS2000坐标点转换WGS84坐标系下的经纬度坐标 ProjCoordinate wgs84Point = new ProjCoordinate(); crsFactory.createFromCoordinateReferenceSystem(cgcs2000).getCoordinateOperationFactory() .createOperation(cgcs2000, wgs84).getMathTransform().transform(cgcs2000Point, wgs84Point); System.out.println(wgs84Point.x + " " + wgs84Point.y); } } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

addresstool

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值