java火星坐标与真实坐标的转换

1.新建工程,导入dtd文件。

package com.qishui;


import java.io.InputStream;
import java.io.ObjectInputStream;

/**
 * 火星地球坐标转化.地图坐标修偏
 * 
 */
public class ModifyOffset {
    private static ModifyOffset modifyOffset;
    static double[] X = new double[660 * 450];
    static double[] Y = new double[660 * 450];


    private ModifyOffset(InputStream inputStream) throws Exception {
        init(inputStream);
    }

    public synchronized static ModifyOffset getInstance(InputStream is) throws Exception {
        if (modifyOffset == null) {
            modifyOffset = new ModifyOffset(is);
        }
        return modifyOffset;
    }

    public void init(InputStream inputStream) throws Exception {
        ObjectInputStream in = new ObjectInputStream(inputStream);
        try {
            int i = 0;
            while (in.available() > 0) {
                if ((i & 1) == 1) {
                    Y[(i - 1) >> 1] = in.readInt() / 100000.0d;
                    ;
                } else {
                    X[i >> 1] = in.readInt() / 100000.0d;
                    ;
                }
                i++;
            }
        } finally {
            if (in != null)
                in.close();
        }
    }

    // standard -> china
    public PointDouble s2c(PointDouble pt) {
        int cnt = 10;
        double x = pt.x, y = pt.y;
        while (cnt-- > 0) {
            if (x < 71.9989d || x > 137.8998d || y < 9.9997d || y > 54.8996d)
                return pt;
            int ix = (int) (10.0d * (x - 72.0d));
            int iy = (int) (10.0d * (y - 10.0d));
            double dx = (x - 72.0d - 0.1d * ix) * 10.0d;
            double dy = (y - 10.0d - 0.1d * iy) * 10.0d;
            x = (x + pt.x + (1.0d - dx) * (1.0d - dy) * X[ix + 660 * iy] + dx
                    * (1.0d - dy) * X[ix + 660 * iy + 1] + dx * dy
                    * X[ix + 660 * iy + 661] + (1.0d - dx) * dy
                    * X[ix + 660 * iy + 660] - x) / 2.0d;
            y = (y + pt.y + (1.0d - dx) * (1.0d - dy) * Y[ix + 660 * iy] + dx
                    * (1.0d - dy) * Y[ix + 660 * iy + 1] + dx * dy
                    * Y[ix + 660 * iy + 661] + (1.0d - dx) * dy
                    * Y[ix + 660 * iy + 660] - y) / 2.0d;
        }
        return new PointDouble(x, y);
    }

    // china -> standard
    public PointDouble c2s(PointDouble pt) {
        int cnt = 10;
        double x = pt.x, y = pt.y;
        while (cnt-- > 0) {
            if (x < 71.9989d || x > 137.8998d || y < 9.9997d || y > 54.8996d)
                return pt;
            int ix = (int) (10.0d * (x - 72.0d));
            int iy = (int) (10.0d * (y - 10.0d));
            double dx = (x - 72.0d - 0.1d * ix) * 10.0d;
            double dy = (y - 10.0d - 0.1d * iy) * 10.0d;
            x = (x + pt.x - (1.0d - dx) * (1.0d - dy) * X[ix + 660 * iy] - dx
                    * (1.0d - dy) * X[ix + 660 * iy + 1] - dx * dy
                    * X[ix + 660 * iy + 661] - (1.0d - dx) * dy
                    * X[ix + 660 * iy + 660] + x) / 2.0d;
            y = (y + pt.y - (1.0d - dx) * (1.0d - dy) * Y[ix + 660 * iy] - dx
                    * (1.0d - dy) * Y[ix + 660 * iy + 1] - dx * dy
                    * Y[ix + 660 * iy + 661] - (1.0d - dx) * dy
                    * Y[ix + 660 * iy + 660] + y) / 2.0d;
        }
        return new PointDouble(x, y);
    }

}

class PointDouble {
    double x, y;

    PointDouble(double x, double y) {
        this.x = x;
        this.y = y;
    }

    public String toString() {
        return "x=" + x + ", y=" + y;
    }
}

2.使用dtd文件,测试位置

package com.qishui;

/**
 * @author Chixi
 * 
 * @version 创建时间 : 2016-7-21 下午6:59:01
 * 
 */
public class Test {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        try {
            ModifyOffset instance = ModifyOffset.getInstance(ModifyOffset.class.getResourceAsStream("../../axisoffset.dat"));
            PointDouble s2c = instance.s2c(new PointDouble(120, 39));
            System.out.println(s2c);

            PointDouble c2s = instance.c2s(s2c);
            System.out.println(c2s);

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}

3.运行结果

这里写图片描述
4.工程结构

这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值