Java实现经纬度坐标转换---CGCS2000坐标、gps84 、GCJ-02、 BD-09

Java实现经纬度坐标转换

一、坐标系统简介

坐标是描述位置的一组数值,按坐标的维度一般分为一维坐标(公路里程碑)和二维坐标(笛卡尔平面直角坐标、高斯平面直角坐标)、三维坐标(大地坐标、空间直角坐标)

为了描述或确定位置,必须建立坐标系统,坐标只有存在于某个坐标系统才有实际的意义与具体的位置。

地球是一个球体,球面上的位置,是以经纬度来表示,它称为“球面坐标系统”或“地理坐标系统”。

1.经纬度坐标系

经纬度坐标系是一种地理坐标系统,用于描述地球表面上任意位置的坐标。它是基于地球的自转和赤道的划分而建立的。

  • 经度(Longitude):表示地球表面上一个点相对于本初子午线的东西方向的位置。经度的度量单位是度(°),范围从0°到180°,以东经为正值,西经为负值。本初子午线位于英国伦敦的皇家格林尼治天文台,它被定义为经度0°。

  • 纬度(Latitude)表示地球表面上一个点相对于赤道的北南方向的位置。纬度的度量单位也是度(°),范围从0°到90°,以北纬为正值,南纬为负值。赤道位于纬度0°。

经纬度坐标系统是全球通用的 地理坐标系统。

经纬度坐标系统使用经度和纬度的组合来确定地球表面上的特定位置。一个点的经纬度坐标表示为两个数值的组合,例如:40°N,120°E 表示北纬40度,东经120度的位置。

2.坐标系统

(1)WGS84(World Geodetic System 1984,GPS标准)
在这里插入图片描述
(2)GCJ-02(国测局坐标系,也被称为火星坐标系)
在这里插入图片描述
(3)BD-09(Baidu Coordinate System)
在这里插入图片描述
(4)CGCS2000(中国2000国家大地坐标系)
在这里插入图片描述

3.坐标转换简介

在地图应用中,不同的地图服务商通常使用不同的坐标系,坐标转换就是将一个地图服务商的坐标系转换为另一个地图服务商的坐标系,以便在不同的地图上显示相同的位置信息。
在这里插入图片描述

二、地图经纬度转换工具类(CGCS2000坐标 与 WGS84坐标 互转)

1.对于 CGCS2000 需要引入 proj4j依赖:

<dependency>
    <groupId>org.locationtech.proj4j</groupId>
    <artifactId>proj4j</artifactId>
    <version>1.3.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.locationtech.proj4j/proj4j-epsg -->
<dependency>
    <groupId>org.locationtech.proj4j</groupId>
    <artifactId>proj4j-epsg</artifactId>
    <version>1.3.0</version>
</dependency>

2.坐标转换工具类

package com.example.demo.util;

import lombok.extern.slf4j.Slf4j;
import org.locationtech.proj4j.*;


/**
 * 坐标转换工具类  CGCS2000\WGS84 互转
 * @author qzz
 * @date 2024/7/15
 */
@Slf4j
public class CoordinateTransformUtil {
   

    // 定义CGCS2000的坐标系  4490
    private final static String CGCS2000 = "EPSG:4490";
    // 定义WGS84的坐标系
    final static String WGS84 = "EPSG:4326";

    /**
     * CGCS2000 转 WGS84
     *
     * @param lng CGCS2000经度
     * @param lat CGCS2000纬度
     * @return
     */
    public static ProjCoordinate cgcs2000ToWgs84(double lng, double lat) {
   

        //创建坐标系工厂
        CRSFactory crsFactory = new CRSFactory();
        // 创建CGCS2000的坐标参考系统
        CoordinateReferenceSystem sourceCRS = crsFactory.createFromName(CGCS2000);
        // 创建WGS84的坐标参考系统
        CoordinateReferenceSystem targetCRS = crsFactory.createFromName(WGS84);

        // 定义坐标转换器
        CoordinateTransformFactory ctFactory = new CoordinateTransformFactory();
        // 创建转换器
        CoordinateTransform transform = ctFactory.createTransform(sourceCRS, targetCRS);
        // 执行坐标转换
        ProjCoordinate srcCoord = new ProjCoordinate(lng, lat);
        ProjCoordinate targetCoord = new ProjCoordinate();
        transform.transform(srcCoord, targetCoord);
        // 4. 输出转换后的正常经纬度坐标
        return targetCoord;
    }

    /**
     * WGS84 转 CGCS2000
     *
     * @param lng WGS84经度
     * @param lat WGS84纬度
     * @return
     */
    public static ProjCoordinate wgs84ToCgcs2000(double lng, double lat) {
   
        CRSFactory crsFactory = new CRSFactory();
        // 定义源和目标投影
        CoordinateReferenceSystem sourceCRS = crsFactory.createFromName(WGS84);
        CoordinateReferenceSystem targetCRS = crsFactory.createFromName(CGCS2000);

        // 定义坐标转换器
        CoordinateTransformFactory ctFactory = new CoordinateTransformFactory();
        // 创建转换器
        CoordinateTransform transform = ctFactory.createTransform(sourceCRS, targetCRS);
        // 执行坐标转换
        ProjCoordinate srcCoord = new ProjCoordinate(lng, lat);
        ProjCoordinate targetCoord = new ProjCoordinate();
        transform.transform(srcCoord, targetCoord);
        // 输出转换后的正常经纬度坐标
        return targetCoord;
    }

    public static void main(String[] args) {
   
        /**
         * CGCS2000
         */
        double CGCS2000Lon4 = 108.887314;
        double CGCS2000Lat4 = 34.230897;
        ProjCoordina
WSG84(World Geodetic System 1984)和CGCS2000(中国大地坐标系2000)是两种常用的地理坐标系。在Java中将WSG84转换CGCS2000可以通过使用坐标转换库来实现。 首先,我们需要确定所使用的坐标转换库。Java中有许多开源的坐标转换库可以使用,例如Geotools和Proj4j。这里以使用Geotools为例进行说明。 在使用Geotools进行坐标转换之前,我们需要先导入相关的库文件。可以在Maven项目中的pom.xml文件中添加以下依赖项: ```xml <dependency> <groupId>org.geotools</groupId> <artifactId>gt-epsg-hsql</artifactId> <version>24.1</version> </dependency> ``` 接下来,我们可以使用以下代码将WSG84坐标转换CGCS2000坐标: ```java import org.geotools.geometry.DirectPosition2D; import org.geotools.referencing.CRS; import org.opengis.referencing.FactoryException; import org.opengis.referencing.NoSuchAuthorityCodeException; import org.opengis.referencing.crs.CoordinateReferenceSystem; public class CoordinateConversion { public static void main(String[] args) { // 输入的坐标点(WSG84) double longitude = 116.3975; double latitude = 39.9085; // 设置源坐标系(WSG84) CoordinateReferenceSystem sourceCRS; try { sourceCRS = CRS.decode("EPSG:4326"); } catch (NoSuchAuthorityCodeException | FactoryException e) { e.printStackTrace(); return; } // 设置目标坐标系CGCS2000) CoordinateReferenceSystem targetCRS; try { targetCRS = CRS.decode("EPSG:4490"); } catch (NoSuchAuthorityCodeException | FactoryException e) { e.printStackTrace(); return; } // 创建源坐标点 DirectPosition2D sourcePosition = new DirectPosition2D(sourceCRS, longitude, latitude); // 坐标转换 try { DirectPosition2D targetPosition = (DirectPosition2D) CRS.transform(sourcePosition, targetCRS); double targetLongitude = targetPosition.x; double targetLatitude = targetPosition.y; System.out.println("转换后的坐标CGCS2000):"); System.out.println("经度:" + targetLongitude); System.out.println("纬度:" + targetLatitude); } catch (Exception e) { e.printStackTrace(); } } } ``` 上述代码中,首先设置源坐标系为WSG84(EPSG:4326)和目标坐标系CGCS2000(EPSG:4490),然后创建源坐标点并利用`CRS.transform()`方法进行坐标转换。 运行上述代码后,将输出转换后的CGCS2000坐标,包括经度和纬度。 需要注意的是,WSG84CGCS2000坐标系定义可能因使用的地理坐标转换库而有所不同,可以根据实际情况进行相应的调整。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值