【转】地球坐标系 (WGS-84) 到火星坐标系 (GCJ-02) 的转换算法 C语言

本文提供了一种用于将全球定位系统(GPS)坐标转换为特定地图坐标系的算法实现。该算法适用于中国范围内的坐标转换,并提供了详细的C++代码示例。通过对经纬度进行一系列数学变换,确保了坐标在中国范围内的准确性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Mark下吧,这是好东西啊。 
http://blog.youkuaiyun.com/coolypf/article/details/8686588


整理成C++代码:
#include


const double pi = 3.14159265358979324;


//
// Krasovsky 1940
//
// a = 6378245.0, 1/f = 298.3
// b = a * (1 - f)
// ee = (a^2 - b^2) / a^2;
const double a = 6378245.0;
const double ee = 0.00669342162296594323;


 static bool 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;
}


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 * sqrt(abs(x));
    ret += (20.0 * sin(6.0 * x * pi) + 20.0 * sin(2.0 * x * pi)) * 2.0 / 3.0;
    ret += (20.0 * sin(y * pi) + 40.0 * sin(y / 3.0 * pi)) * 2.0 / 3.0;
    ret += (160.0 * sin(y / 12.0 * pi) + 320 * sin(y * pi / 30.0)) * 2.0 / 3.0;
    return ret;
}


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 * sqrt(abs(x));
    ret += (20.0 * sin(6.0 * x * pi) + 20.0 * sin(2.0 * x * pi)) * 2.0 / 3.0;
    ret += (20.0 * sin(x * pi) + 40.0 * sin(x / 3.0 * pi)) * 2.0 / 3.0;
    ret += (150.0 * sin(x / 12.0 * pi) + 300.0 * sin(x / 30.0 * pi)) * 2.0 / 3.0;
    return ret;
}


void gps_transform( double wgLat, double wgLon, double& mgLat, double& mgLon)
{
if (outOfChina(wgLat, wgLon))
        {
            mgLat = wgLat;
            mgLon = wgLon;
            return;
        }
        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 = sin(radLat);
        magic = 1 - ee * magic * magic;
        double sqrtMagic = sqrt(magic);
        dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);
        dLon = (dLon * 180.0) / (a / sqrtMagic * cos(radLat) * pi);
        mgLat = wgLat + dLat;
        mgLon = wgLon + dLon;
};

 

### 将火星坐标系转换WGS84坐标的Java代码 为了实现从火星坐标系WGS84坐标的转换,在Java中可以利用地理空间库,比如GeoTools。下面是一个简单的例子来展示如何执行这种转换: ```java import org.geotools.geometry.jts.JTS; import org.geotools.referencing.CRS; import org.locationtech.jts.geom.Geometry; import org.locationtech.jts.geom.Point; import org.opengis.geometry.MismatchedDimensionException; import org.opengis.referencing.FactoryNotFoundException; import org.opengis.referencing.operation.MathTransform; import org.opengis.referencing.operation.TransformException; public class CoordinateTransformation { public static void main(String[] args) { try { // 定义源坐标系 (假设火星坐标系) String sourceCRS = "EPSG:4978"; // 这里只是一个占位符,实际应用需替换为火星坐标系定义 // 定义目标坐标系 (WGS84) String targetCRS = "EPSG:4326"; MathTransform transform = CRS.findMathTransform(CRS.decode(sourceCRS), CRS.decode(targetCRS)); Point point = JTS.toGeometry(new org.locationtech.jts.geom.impl.PrecisionModel(), new org.locationtech.jts.geom.GeometryFactory().createPoint( new org.locationtech.jts.geom.Coordinate(0, 0))); // 替换这里的坐标值为具体的火星坐标 Geometry transformedGeom = JTS.transform(point, transform); System.out.println("Converted coordinates: " + transformedGeom.getCoordinate()); } catch (MismatchedDimensionException | TransformException | FactoryNotFoundException e) { e.printStackTrace(); } } } ``` 需要注意的是,上述代码中的`sourceCRS`变量设置了一个地球中心笛卡尔坐标系作为示例[^1]。对于真正的火星坐标系WGS84的操作,需要找到并指定确切的火星坐标参照系编码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值